Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support clicking the selection area to disable floating toolbar #632

Merged
merged 1 commit into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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