From 1bc787ee710c543ce518f90ca018882138a8299a Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Fri, 15 Jul 2022 19:37:39 +0300 Subject: [PATCH] Fix dragging while the text selection toolbar is up doesn't hide toolbar (#106878) Fixes a fidelity bug on desktop involving the selection toolbar not hiding when it should have. --- .../flutter/lib/src/cupertino/text_field.dart | 18 ++++++- .../flutter/lib/src/material/text_field.dart | 18 ++++++- .../test/cupertino/text_field_test.dart | 49 +++++++++++++++++- .../test/material/text_field_test.dart | 50 +++++++++++++++++++ 4 files changed, 130 insertions(+), 5 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/text_field.dart b/packages/flutter/lib/src/cupertino/text_field.dart index 9a5e69dc7a..aeccd24d9f 100644 --- a/packages/flutter/lib/src/cupertino/text_field.dart +++ b/packages/flutter/lib/src/cupertino/text_field.dart @@ -969,7 +969,7 @@ class _CupertinoTextFieldState extends State with Restoratio || cause == SelectionChangedCause.drag) { _editableText.bringIntoView(selection.extent); } - return; + break; case TargetPlatform.linux: case TargetPlatform.windows: case TargetPlatform.fuchsia: @@ -977,7 +977,21 @@ class _CupertinoTextFieldState extends State with Restoratio if (cause == SelectionChangedCause.drag) { _editableText.bringIntoView(selection.extent); } - return; + break; + } + + switch (defaultTargetPlatform) { + case TargetPlatform.iOS: + case TargetPlatform.fuchsia: + case TargetPlatform.android: + break; + case TargetPlatform.macOS: + case TargetPlatform.linux: + case TargetPlatform.windows: + if (cause == SelectionChangedCause.drag) { + _editableText.hideToolbar(); + } + break; } } diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart index a7abaa8a06..159f4f5cca 100644 --- a/packages/flutter/lib/src/material/text_field.dart +++ b/packages/flutter/lib/src/material/text_field.dart @@ -1063,7 +1063,7 @@ class _TextFieldState extends State with RestorationMixin implements || cause == SelectionChangedCause.drag) { _editableText?.bringIntoView(selection.extent); } - return; + break; case TargetPlatform.linux: case TargetPlatform.windows: case TargetPlatform.fuchsia: @@ -1071,7 +1071,21 @@ class _TextFieldState extends State with RestorationMixin implements if (cause == SelectionChangedCause.drag) { _editableText?.bringIntoView(selection.extent); } - return; + break; + } + + switch (Theme.of(context).platform) { + case TargetPlatform.iOS: + case TargetPlatform.fuchsia: + case TargetPlatform.android: + break; + case TargetPlatform.macOS: + case TargetPlatform.linux: + case TargetPlatform.windows: + if (cause == SelectionChangedCause.drag) { + _editableText?.hideToolbar(); + } + break; } } diff --git a/packages/flutter/test/cupertino/text_field_test.dart b/packages/flutter/test/cupertino/text_field_test.dart index 88cdd5b365..331b848b6d 100644 --- a/packages/flutter/test/cupertino/text_field_test.dart +++ b/packages/flutter/test/cupertino/text_field_test.dart @@ -155,7 +155,6 @@ class PathPointsMatcher extends Matcher { } } - void main() { TestWidgetsFlutterBinding.ensureInitialized(); final MockClipboard mockClipboard = MockClipboard(); @@ -4080,6 +4079,53 @@ void main() { expect(find.byType(CupertinoTextSelectionToolbar), findsOneWidget); expect(tester.takeException(), null); }); + + testWidgets('Drag selection hides the selection menu', (WidgetTester tester) async { + final TextEditingController controller = TextEditingController( + text: 'blah1 blah2', + ); + await tester.pumpWidget( + CupertinoApp( + home: Center( + child: CupertinoTextField( + controller: controller, + ), + ), + ), + ); + + // Initially, the menu is not shown and there is no selection. + expect(controller.selection, const TextSelection(baseOffset: -1, extentOffset: -1)); + final Offset midBlah1 = textOffsetToPosition(tester, 2); + final Offset midBlah2 = textOffsetToPosition(tester, 8); + + // Right click the second word. + final TestGesture gesture = await tester.startGesture( + midBlah2, + kind: PointerDeviceKind.mouse, + buttons: kSecondaryMouseButton, + ); + await tester.pump(); + await gesture.up(); + await tester.pumpAndSettle(); + + // The toolbar is shown. + expect(find.text('Paste'), findsOneWidget); + + // Drag the mouse to the first word. + final TestGesture gesture2 = await tester.startGesture( + midBlah1, + kind: PointerDeviceKind.mouse, + ); + await tester.pump(); + await gesture2.moveTo(midBlah2); + await tester.pump(); + await gesture2.up(); + await tester.pumpAndSettle(); + + // The toolbar is hidden. + expect(find.text('Paste'), findsNothing); + }, variant: TargetPlatformVariant.desktop()); }, skip: isContextMenuProvidedByPlatform); // [intended] only applies to platforms where we supply the context menu. group('textAlignVertical position', () { @@ -4509,6 +4555,7 @@ void main() { }, ); }); + testWidgets("Arrow keys don't move input focus", (WidgetTester tester) async { final TextEditingController controller1 = TextEditingController(); final TextEditingController controller2 = TextEditingController(); diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index a7d5922d55..91469a89d2 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -7658,6 +7658,56 @@ void main() { skip: isContextMenuProvidedByPlatform, // [intended] only applies to platforms where we supply the context menu., ); + testWidgets('Drag selection hides the selection menu', (WidgetTester tester) async { + final TextEditingController controller = TextEditingController( + text: 'blah1 blah2', + ); + await tester.pumpWidget( + MaterialApp( + home: Material( + child: TextField( + controller: controller, + ), + ), + ), + ); + + // Initially, the menu is not shown and there is no selection. + expect(controller.selection, const TextSelection(baseOffset: -1, extentOffset: -1)); + final Offset midBlah1 = textOffsetToPosition(tester, 2); + final Offset midBlah2 = textOffsetToPosition(tester, 8); + + // Right click the second word. + final TestGesture gesture = await tester.startGesture( + midBlah2, + kind: PointerDeviceKind.mouse, + buttons: kSecondaryMouseButton, + ); + await tester.pump(); + await gesture.up(); + await tester.pumpAndSettle(); + + // The toolbar is shown. + expect(find.text('Paste'), findsOneWidget); + + // Drag the mouse to the first word. + final TestGesture gesture2 = await tester.startGesture( + midBlah1, + kind: PointerDeviceKind.mouse, + ); + await tester.pump(); + await gesture2.moveTo(midBlah2); + await tester.pump(); + await gesture2.up(); + await tester.pumpAndSettle(); + + // The toolbar is hidden. + expect(find.text('Paste'), findsNothing); + }, + variant: TargetPlatformVariant.desktop(), + skip: isContextMenuProvidedByPlatform, // [intended] only applies to platforms where we supply the context menu. + ); + testWidgets( 'Long press on an autofocused field shows the selection menu', (WidgetTester tester) async {