From 2726f7d48731d45058aa277f401242cbecf36872 Mon Sep 17 00:00:00 2001 From: Alan Mantoux Date: Sun, 12 May 2024 14:36:10 +0200 Subject: [PATCH] Remove selection gestures when enableInteractiveSelection is `false` (#351) * Remove selection gesture detector when enableInteractiveSelection is `false` --- packages/fleather/lib/src/widgets/editor.dart | 8 ++++---- packages/fleather/test/testing.dart | 9 ++++++--- packages/fleather/test/widgets/editor_test.dart | 8 ++++++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/fleather/lib/src/widgets/editor.dart b/packages/fleather/lib/src/widgets/editor.dart index c34cb2c1..0f9f9d27 100644 --- a/packages/fleather/lib/src/widgets/editor.dart +++ b/packages/fleather/lib/src/widgets/editor.dart @@ -491,10 +491,10 @@ class _FleatherEditorState extends State ), ); - return _selectionGestureDetectorBuilder.buildGestureDetector( - behavior: HitTestBehavior.translucent, - child: child, - ); + return widget.enableInteractiveSelection + ? _selectionGestureDetectorBuilder.buildGestureDetector( + behavior: HitTestBehavior.translucent, child: child) + : child; } } diff --git a/packages/fleather/test/testing.dart b/packages/fleather/test/testing.dart index 679de827..6dc1e122 100644 --- a/packages/fleather/test/testing.dart +++ b/packages/fleather/test/testing.dart @@ -15,6 +15,7 @@ class EditorSandBox { ParchmentDocument? document, FleatherThemeData? fleatherTheme, bool autofocus = false, + bool enableSelectionInteraction = true, FakeSpellCheckService? spellCheckService, ClipboardManager clipboardManager = const PlainTextClipboardManager(), FleatherEmbedBuilder embedBuilder = defaultFleatherEmbedBuilder, @@ -27,6 +28,7 @@ class EditorSandBox { controller: controller, focusNode: focusNode, autofocus: autofocus, + enableSelectionInteraction: enableSelectionInteraction, spellCheckService: spellCheckService, embedBuilder: embedBuilder, clipboardManager: clipboardManager, @@ -139,6 +141,7 @@ class _FleatherSandbox extends StatefulWidget { required this.controller, required this.focusNode, this.autofocus = false, + this.enableSelectionInteraction = true, this.spellCheckService, this.embedBuilder = defaultFleatherEmbedBuilder, this.clipboardManager = const PlainTextClipboardManager(), @@ -147,6 +150,7 @@ class _FleatherSandbox extends StatefulWidget { final FleatherController controller; final FocusNode focusNode; final bool autofocus; + final bool enableSelectionInteraction; final FakeSpellCheckService? spellCheckService; final FleatherEmbedBuilder embedBuilder; final ClipboardManager clipboardManager; @@ -167,6 +171,7 @@ class _FleatherSandboxState extends State<_FleatherSandbox> { controller: widget.controller, focusNode: widget.focusNode, readOnly: !_enabled, + enableInteractiveSelection: widget.enableSelectionInteraction, autofocus: widget.autofocus, spellCheckConfiguration: widget.spellCheckService != null ? SpellCheckConfiguration( @@ -178,9 +183,7 @@ class _FleatherSandboxState extends State<_FleatherSandbox> { } void disable() { - setState(() { - _enabled = false; - }); + setState(() => _enabled = false); } } diff --git a/packages/fleather/test/widgets/editor_test.dart b/packages/fleather/test/widgets/editor_test.dart index 64d0b51b..f99d03db 100644 --- a/packages/fleather/test/widgets/editor_test.dart +++ b/packages/fleather/test/widgets/editor_test.dart @@ -392,6 +392,14 @@ void main() { }); group('Text selection', () { + testWidgets('disabled selection interaction disables associated gestures', + (tester) async { + final editor = + EditorSandBox(tester: tester, enableSelectionInteraction: false); + await editor.pump(); + expect(find.byType(TextSelectionGestureDetector), findsNothing); + }); + testWidgets('hides toolbar and selection handles when text changed', (tester) async { const delta = TextEditingDeltaInsertion(