From b7a425f1968233490448192c3c4f68fc87ac039e Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Fri, 27 May 2022 09:41:03 -0700 Subject: [PATCH] Right click on selection when unfocused should re-focus (#104666) Bug fix for focusing a field with right click on existing selection (Mac and iOS) --- .../lib/src/widgets/text_selection.dart | 2 +- .../test/cupertino/text_field_test.dart | 186 +++++++++++++---- .../test/material/text_field_test.dart | 189 ++++++++++++++---- .../test/widgets/text_selection_test.dart | 4 + 4 files changed, 293 insertions(+), 88 deletions(-) diff --git a/packages/flutter/lib/src/widgets/text_selection.dart b/packages/flutter/lib/src/widgets/text_selection.dart index 8027b5d785..3075c511ca 100644 --- a/packages/flutter/lib/src/widgets/text_selection.dart +++ b/packages/flutter/lib/src/widgets/text_selection.dart @@ -1622,7 +1622,7 @@ class TextSelectionGestureDetectorBuilder { switch (defaultTargetPlatform) { case TargetPlatform.iOS: case TargetPlatform.macOS: - if (!_lastSecondaryTapWasOnSelection) { + if (!_lastSecondaryTapWasOnSelection || !renderEditable.hasFocus) { renderEditable.selectWord(cause: SelectionChangedCause.tap); } if (shouldShowSelectionToolbar) { diff --git a/packages/flutter/test/cupertino/text_field_test.dart b/packages/flutter/test/cupertino/text_field_test.dart index a2605aadde..7b058980c0 100644 --- a/packages/flutter/test/cupertino/text_field_test.dart +++ b/packages/flutter/test/cupertino/text_field_test.dart @@ -5653,55 +5653,155 @@ void main() { skip: isContextMenuProvidedByPlatform, // [intended] only applies to platforms where we supply the context menu. ); - testWidgets('Can right click to focus multiple times', (WidgetTester tester) async { - // Regression test for https://github.com/flutter/flutter/pull/103228 - final FocusNode focusNode1 = FocusNode(); - final FocusNode focusNode2 = FocusNode(); - final UniqueKey key1 = UniqueKey(); - final UniqueKey key2 = UniqueKey(); - await tester.pumpWidget( - CupertinoApp( - home: Column( - children: [ - CupertinoTextField( - key: key1, - focusNode: focusNode1, - ), - CupertinoTextField( - key: key2, - focusNode: focusNode2, - ), - ], + group('Right click focus', () { + testWidgets('Can right click to focus multiple times', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/pull/103228 + final FocusNode focusNode1 = FocusNode(); + final FocusNode focusNode2 = FocusNode(); + final UniqueKey key1 = UniqueKey(); + final UniqueKey key2 = UniqueKey(); + await tester.pumpWidget( + CupertinoApp( + home: Column( + children: [ + CupertinoTextField( + key: key1, + focusNode: focusNode1, + ), + CupertinoTextField( + key: key2, + focusNode: focusNode2, + ), + ], + ), ), - ), - ); + ); - // Interact with the field to establish the input connection. - await tester.tapAt( - tester.getCenter(find.byKey(key1)), - buttons: kSecondaryMouseButton, - ); - await tester.pump(); + // Interact with the field to establish the input connection. + await tester.tapAt( + tester.getCenter(find.byKey(key1)), + buttons: kSecondaryMouseButton, + ); + await tester.pump(); - expect(focusNode1.hasFocus, isTrue); - expect(focusNode2.hasFocus, isFalse); + expect(focusNode1.hasFocus, isTrue); + expect(focusNode2.hasFocus, isFalse); - await tester.tapAt( - tester.getCenter(find.byKey(key2)), - buttons: kSecondaryMouseButton, - ); - await tester.pump(); + await tester.tapAt( + tester.getCenter(find.byKey(key2)), + buttons: kSecondaryMouseButton, + ); + await tester.pump(); - expect(focusNode1.hasFocus, isFalse); - expect(focusNode2.hasFocus, isTrue); + expect(focusNode1.hasFocus, isFalse); + expect(focusNode2.hasFocus, isTrue); - await tester.tapAt( - tester.getCenter(find.byKey(key1)), - buttons: kSecondaryMouseButton, - ); - await tester.pump(); + await tester.tapAt( + tester.getCenter(find.byKey(key1)), + buttons: kSecondaryMouseButton, + ); + await tester.pump(); - expect(focusNode1.hasFocus, isTrue); - expect(focusNode2.hasFocus, isFalse); + expect(focusNode1.hasFocus, isTrue); + expect(focusNode2.hasFocus, isFalse); + }); + + testWidgets('Can right click to focus on previously selected word on Apple platforms', (WidgetTester tester) async { + final FocusNode focusNode1 = FocusNode(); + final FocusNode focusNode2 = FocusNode(); + final TextEditingController controller = TextEditingController( + text: 'first second', + ); + final UniqueKey key1 = UniqueKey(); + await tester.pumpWidget( + CupertinoApp( + home: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + CupertinoTextField( + key: key1, + controller: controller, + focusNode: focusNode1, + ), + Focus( + focusNode: focusNode2, + child: const Text('focusable'), + ), + ], + ), + ), + ); + + // Interact with the field to establish the input connection. + await tester.tapAt( + tester.getCenter(find.byKey(key1)), + buttons: kSecondaryMouseButton, + ); + await tester.pump(); + + expect(focusNode1.hasFocus, isTrue); + expect(focusNode2.hasFocus, isFalse); + + // Select the second word. + controller.selection = const TextSelection( + baseOffset: 6, + extentOffset: 12, + ); + await tester.pump(); + + expect(focusNode1.hasFocus, isTrue); + expect(focusNode2.hasFocus, isFalse); + expect(controller.selection.isCollapsed, isFalse); + expect(controller.selection.baseOffset, 6); + expect(controller.selection.extentOffset, 12); + + // Unfocus the first field. + focusNode2.requestFocus(); + await tester.pumpAndSettle(); + + expect(focusNode1.hasFocus, isFalse); + expect(focusNode2.hasFocus, isTrue); + + // Right click the second word in the first field, which is still selected + // even though the selection is not visible. + await tester.tapAt( + textOffsetToPosition(tester, 8), + buttons: kSecondaryMouseButton, + ); + await tester.pump(); + + expect(focusNode1.hasFocus, isTrue); + expect(focusNode2.hasFocus, isFalse); + expect(controller.selection.baseOffset, 6); + expect(controller.selection.extentOffset, 12); + + // Select everything. + controller.selection = const TextSelection( + baseOffset: 0, + extentOffset: 12, + ); + await tester.pump(); + + expect(focusNode1.hasFocus, isTrue); + expect(focusNode2.hasFocus, isFalse); + expect(controller.selection.baseOffset, 0); + expect(controller.selection.extentOffset, 12); + + // Unfocus the first field. + focusNode2.requestFocus(); + await tester.pumpAndSettle(); + + // Right click the first word in the first field. + await tester.tapAt( + textOffsetToPosition(tester, 2), + buttons: kSecondaryMouseButton, + ); + await tester.pump(); + + expect(focusNode1.hasFocus, isTrue); + expect(focusNode2.hasFocus, isFalse); + expect(controller.selection.baseOffset, 0); + expect(controller.selection.extentOffset, 5); + }, variant: const TargetPlatformVariant({ TargetPlatform.iOS, TargetPlatform.macOS })); }); } diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 6f241bf985..5aee23098a 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -11378,57 +11378,158 @@ void main() { skip: isContextMenuProvidedByPlatform, // [intended] only applies to platforms where we supply the context menu. ); - testWidgets('Can right click to focus multiple times', (WidgetTester tester) async { - // Regression test for https://github.com/flutter/flutter/pull/103228 - final FocusNode focusNode1 = FocusNode(); - final FocusNode focusNode2 = FocusNode(); - final UniqueKey key1 = UniqueKey(); - final UniqueKey key2 = UniqueKey(); - await tester.pumpWidget( - MaterialApp( - home: Material( - child: Column( - children: [ - TextField( - key: key1, - focusNode: focusNode1, - ), - TextField( - key: key2, - focusNode: focusNode2, - ), - ], + group('Right click focus', () { + testWidgets('Can right click to focus multiple times', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/pull/103228 + final FocusNode focusNode1 = FocusNode(); + final FocusNode focusNode2 = FocusNode(); + final UniqueKey key1 = UniqueKey(); + final UniqueKey key2 = UniqueKey(); + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Column( + children: [ + TextField( + key: key1, + focusNode: focusNode1, + ), + TextField( + key: key2, + focusNode: focusNode2, + ), + ], + ), ), ), - ), - ); + ); - // Interact with the field to establish the input connection. - await tester.tapAt( - tester.getCenter(find.byKey(key1)), - buttons: kSecondaryButton, - ); - await tester.pump(); + // Interact with the field to establish the input connection. + await tester.tapAt( + tester.getCenter(find.byKey(key1)), + buttons: kSecondaryButton, + ); + await tester.pump(); - expect(focusNode1.hasFocus, isTrue); - expect(focusNode2.hasFocus, isFalse); + expect(focusNode1.hasFocus, isTrue); + expect(focusNode2.hasFocus, isFalse); - await tester.tapAt( - tester.getCenter(find.byKey(key2)), - buttons: kSecondaryButton, - ); - await tester.pump(); + await tester.tapAt( + tester.getCenter(find.byKey(key2)), + buttons: kSecondaryButton, + ); + await tester.pump(); - expect(focusNode1.hasFocus, isFalse); - expect(focusNode2.hasFocus, isTrue); + expect(focusNode1.hasFocus, isFalse); + expect(focusNode2.hasFocus, isTrue); - await tester.tapAt( - tester.getCenter(find.byKey(key1)), - buttons: kSecondaryButton, - ); - await tester.pump(); + await tester.tapAt( + tester.getCenter(find.byKey(key1)), + buttons: kSecondaryButton, + ); + await tester.pump(); - expect(focusNode1.hasFocus, isTrue); - expect(focusNode2.hasFocus, isFalse); + expect(focusNode1.hasFocus, isTrue); + expect(focusNode2.hasFocus, isFalse); + }); + + testWidgets('Can right click to focus on previously selected word on Apple platforms', (WidgetTester tester) async { + final FocusNode focusNode1 = FocusNode(); + final FocusNode focusNode2 = FocusNode(); + final TextEditingController controller = TextEditingController( + text: 'first second', + ); + final UniqueKey key1 = UniqueKey(); + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Column( + children: [ + TextField( + key: key1, + controller: controller, + focusNode: focusNode1, + ), + Focus( + focusNode: focusNode2, + child: const Text('focusable'), + ), + ], + ), + ), + ), + ); + + // Interact with the field to establish the input connection. + await tester.tapAt( + tester.getCenter(find.byKey(key1)), + buttons: kSecondaryButton, + ); + await tester.pump(); + + expect(focusNode1.hasFocus, isTrue); + expect(focusNode2.hasFocus, isFalse); + + // Select the second word. + controller.selection = const TextSelection( + baseOffset: 6, + extentOffset: 12, + ); + await tester.pump(); + + expect(focusNode1.hasFocus, isTrue); + expect(focusNode2.hasFocus, isFalse); + expect(controller.selection.isCollapsed, isFalse); + expect(controller.selection.baseOffset, 6); + expect(controller.selection.extentOffset, 12); + + // Unfocus the first field. + focusNode2.requestFocus(); + await tester.pumpAndSettle(); + + expect(focusNode1.hasFocus, isFalse); + expect(focusNode2.hasFocus, isTrue); + + // Right click the second word in the first field, which is still selected + // even though the selection is not visible. + await tester.tapAt( + textOffsetToPosition(tester, 8), + buttons: kSecondaryButton, + ); + await tester.pump(); + + expect(focusNode1.hasFocus, isTrue); + expect(focusNode2.hasFocus, isFalse); + expect(controller.selection.baseOffset, 6); + expect(controller.selection.extentOffset, 12); + + // Select everything. + controller.selection = const TextSelection( + baseOffset: 0, + extentOffset: 12, + ); + await tester.pump(); + + expect(focusNode1.hasFocus, isTrue); + expect(focusNode2.hasFocus, isFalse); + expect(controller.selection.baseOffset, 0); + expect(controller.selection.extentOffset, 12); + + // Unfocus the first field. + focusNode2.requestFocus(); + await tester.pumpAndSettle(); + + // Right click the first word in the first field. + await tester.tapAt( + textOffsetToPosition(tester, 2), + buttons: kSecondaryButton, + ); + await tester.pump(); + + expect(focusNode1.hasFocus, isTrue); + expect(focusNode2.hasFocus, isFalse); + expect(controller.selection.baseOffset, 0); + expect(controller.selection.extentOffset, 5); + }, variant: const TargetPlatformVariant({ TargetPlatform.iOS, TargetPlatform.macOS })); }); } diff --git a/packages/flutter/test/widgets/text_selection_test.dart b/packages/flutter/test/widgets/text_selection_test.dart index b0da65e747..bf04f41bd3 100644 --- a/packages/flutter/test/widgets/text_selection_test.dart +++ b/packages/flutter/test/widgets/text_selection_test.dart @@ -1391,12 +1391,14 @@ class FakeRenderEditable extends RenderEditable { @override void selectWordsInRange({ required Offset from, Offset? to, required SelectionChangedCause cause }) { selectWordsInRangeCalled = true; + hasFocus = true; } bool selectWordEdgeCalled = false; @override void selectWordEdge({ required SelectionChangedCause cause }) { selectWordEdgeCalled = true; + hasFocus = true; } bool selectPositionAtCalled = false; @@ -1407,6 +1409,7 @@ class FakeRenderEditable extends RenderEditable { selectPositionAtCalled = true; selectPositionAtFrom = from; selectPositionAtTo = to; + hasFocus = true; } bool selectPositionCalled = false; @@ -1420,6 +1423,7 @@ class FakeRenderEditable extends RenderEditable { @override void selectWord({ required SelectionChangedCause cause }) { selectWordCalled = true; + hasFocus = true; } @override