Skip to content

Commit

Permalink
feat: support clicking the selection area to disable floating toolbar (
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 authored Dec 26, 2023
1 parent 92e4260 commit ace40a9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/src/editor/command/text_commands.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ extension TextTransforms on EditorState {
Future<void> toggleAttribute(
String key, {
Selection? selection,
Map? selectionExtraInfo,
}) async {
selection ??= this.selection;
if (selection == null) {
Expand Down Expand Up @@ -222,6 +223,7 @@ extension TextTransforms on EditorState {
{
key: !isHighlight,
},
selectionExtraInfo: selectionExtraInfo,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/editor/editor_component/service/selection/mobile_magnifiter.dart';
import 'package:appflowy_editor/src/flutter/overlay.dart';
Expand All @@ -6,6 +8,12 @@ import 'package:appflowy_editor/src/service/selection/mobile_selection_gesture.d
import 'package:flutter/material.dart' hide Overlay, OverlayEntry;
import 'package:provider/provider.dart';

/// only used in mobile
///
/// this will notify the developers when the selection is not collapsed.
StreamController<int> appFlowyEditorOnTapSelectionArea =
StreamController<int>.broadcast();

enum MobileSelectionDragMode {
none,
leftSelectionHandler,
Expand Down Expand Up @@ -514,6 +522,7 @@ class _MobileSelectionServiceWidgetState
handlerWidth: editorState.editorStyle.mobileDragHandleWidth,
handlerBallWidth:
editorState.editorStyle.mobileDragHandleBallSize.width,
onTapUp: () => appFlowyEditorOnTapSelectionArea.add(0),
),
);
_selectionAreas.add(overlay);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/editor/editor_component/service/selection/mobile_selection_service.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -48,6 +50,8 @@ class _MobileFloatingToolbarState extends State<MobileFloatingToolbar>
// use for skipping the first build for the toolbar when the selection is collapsed.
Selection? prevSelection;

late final StreamSubscription _onTapSelectionAreaSubscription;

@override
void initState() {
super.initState();
Expand All @@ -57,6 +61,10 @@ class _MobileFloatingToolbarState extends State<MobileFloatingToolbar>
widget.editorScrollController.offsetNotifier.addListener(
_onScrollPositionChanged,
);
_onTapSelectionAreaSubscription =
appFlowyEditorOnTapSelectionArea.stream.listen((event) {
_isToolbarVisible ? _clear() : _showAfterDelay();
});
}

@override
Expand All @@ -74,6 +82,7 @@ class _MobileFloatingToolbarState extends State<MobileFloatingToolbar>
widget.editorScrollController.offsetNotifier.removeListener(
_onScrollPositionChanged,
);
_onTapSelectionAreaSubscription.cancel();
WidgetsBinding.instance.removeObserver(this);

_clear();
Expand Down
5 changes: 4 additions & 1 deletion lib/src/render/selection/mobile_selection_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class MobileSelectionWidget extends StatelessWidget {
this.handlerColor = Colors.black,
this.handlerBallWidth = 6.0,
this.handlerWidth = 2.0,
required this.onTapUp,
});

final Color color;
Expand All @@ -23,6 +24,7 @@ class MobileSelectionWidget extends StatelessWidget {
final Color handlerColor;
final double handlerWidth;
final double handlerBallWidth;
final VoidCallback onTapUp;

@override
Widget build(BuildContext context) {
Expand All @@ -46,7 +48,8 @@ class MobileSelectionWidget extends StatelessWidget {
showWhenUnlinked: false,
// Ignore the gestures in selection overlays
// to solve the problem that selection areas cannot overlap.
child: IgnorePointer(
child: GestureDetector(
onTapUp: (details) => onTapUp(),
child: MobileSelectionWithHandler(
color: color,
decoration: decoration,
Expand Down
1 change: 1 addition & 0 deletions test/render/selection/mobile_selection_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void main() {
Stack(
children: [
MobileSelectionWidget(
onTapUp: () {},
showLeftHandler: true,
showRightHandler: true,
layerLink: node.layerLink,
Expand Down

0 comments on commit ace40a9

Please sign in to comment.