From 9f2339173320e19557fc3d1ecc567c2cb2a7d33e Mon Sep 17 00:00:00 2001 From: htoor3 <110993981+htoor3@users.noreply.github.com> Date: Wed, 2 Nov 2022 10:41:05 -0500 Subject: [PATCH] [web] Changes to web keyboard selection shortcuts for more consistent behavior (#114264) --- .../default_text_editing_shortcuts.dart | 8 +- .../widgets/editable_text_shortcuts_test.dart | 107 +++++++++++++++++- 2 files changed, 108 insertions(+), 7 deletions(-) diff --git a/packages/flutter/lib/src/widgets/default_text_editing_shortcuts.dart b/packages/flutter/lib/src/widgets/default_text_editing_shortcuts.dart index b8c9cb41f9..f145f8e0bf 100644 --- a/packages/flutter/lib/src/widgets/default_text_editing_shortcuts.dart +++ b/packages/flutter/lib/src/widgets/default_text_editing_shortcuts.dart @@ -402,22 +402,16 @@ class DefaultTextEditingShortcuts extends StatelessWidget { SingleActivator(LogicalKeyboardKey.arrowLeft, alt: true): DoNothingAndStopPropagationTextIntent(), SingleActivator(LogicalKeyboardKey.arrowRight, alt: true): DoNothingAndStopPropagationTextIntent(), SingleActivator(LogicalKeyboardKey.arrowUp, alt: true): DoNothingAndStopPropagationTextIntent(), - SingleActivator(LogicalKeyboardKey.arrowDown, shift: true, alt: true): DoNothingAndStopPropagationTextIntent(), SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true, alt: true): DoNothingAndStopPropagationTextIntent(), SingleActivator(LogicalKeyboardKey.arrowRight, shift: true, alt: true): DoNothingAndStopPropagationTextIntent(), - SingleActivator(LogicalKeyboardKey.arrowUp, shift: true, alt: true): DoNothingAndStopPropagationTextIntent(), SingleActivator(LogicalKeyboardKey.arrowDown, meta: true): DoNothingAndStopPropagationTextIntent(), SingleActivator(LogicalKeyboardKey.arrowLeft, meta: true): DoNothingAndStopPropagationTextIntent(), SingleActivator(LogicalKeyboardKey.arrowRight, meta: true): DoNothingAndStopPropagationTextIntent(), SingleActivator(LogicalKeyboardKey.arrowUp, meta: true): DoNothingAndStopPropagationTextIntent(), - SingleActivator(LogicalKeyboardKey.arrowDown, shift: true, meta: true): DoNothingAndStopPropagationTextIntent(), SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true, meta: true): DoNothingAndStopPropagationTextIntent(), SingleActivator(LogicalKeyboardKey.arrowRight, shift: true, meta: true): DoNothingAndStopPropagationTextIntent(), - SingleActivator(LogicalKeyboardKey.arrowUp, shift: true, meta: true): DoNothingAndStopPropagationTextIntent(), - SingleActivator(LogicalKeyboardKey.arrowDown, shift: true): DoNothingAndStopPropagationTextIntent(), SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true): DoNothingAndStopPropagationTextIntent(), SingleActivator(LogicalKeyboardKey.arrowRight, shift: true): DoNothingAndStopPropagationTextIntent(), - SingleActivator(LogicalKeyboardKey.arrowUp, shift: true): DoNothingAndStopPropagationTextIntent(), SingleActivator(LogicalKeyboardKey.pageUp, shift: true): DoNothingAndStopPropagationTextIntent(), SingleActivator(LogicalKeyboardKey.pageDown, shift: true): DoNothingAndStopPropagationTextIntent(), SingleActivator(LogicalKeyboardKey.end, shift: true): DoNothingAndStopPropagationTextIntent(), @@ -445,6 +439,8 @@ class DefaultTextEditingShortcuts extends StatelessWidget { const SingleActivator(LogicalKeyboardKey.escape): const DoNothingAndStopPropagationTextIntent(), const SingleActivator(LogicalKeyboardKey.tab): const DoNothingAndStopPropagationTextIntent(), const SingleActivator(LogicalKeyboardKey.tab, shift: true): const DoNothingAndStopPropagationTextIntent(), + const SingleActivator(LogicalKeyboardKey.arrowDown, shift: true, alt: true): const DoNothingAndStopPropagationTextIntent(), + const SingleActivator(LogicalKeyboardKey.arrowUp, shift: true, alt: true): const DoNothingAndStopPropagationTextIntent(), }; static Map get _shortcuts { diff --git a/packages/flutter/test/widgets/editable_text_shortcuts_test.dart b/packages/flutter/test/widgets/editable_text_shortcuts_test.dart index 09c3018a45..ce2ab831b0 100644 --- a/packages/flutter/test/widgets/editable_text_shortcuts_test.dart +++ b/packages/flutter/test/widgets/editable_text_shortcuts_test.dart @@ -67,6 +67,8 @@ void main() { '0123456789ABCDEFGHIJ' '0123456789ABCDEFGHIJ' '0123456789ABCDEFGHIJ'; + + const String testVerticalText = '1\n2\n3\n4\n5\n6\n7\n8\n9'; final TextEditingController controller = TextEditingController(text: testText); final FocusNode focusNode = FocusNode(); @@ -2043,7 +2045,8 @@ void main() { }, variant: appleOnly); }); - testWidgets('vertical movement', (WidgetTester tester) async { + testWidgets('vertical movement outside of selection', + (WidgetTester tester) async { controller.text = testText; controller.selection = const TextSelection.collapsed( offset: 0, @@ -2052,6 +2055,10 @@ void main() { await tester.pumpWidget(buildEditableText()); for (final SingleActivator activator in allModifierVariants(LogicalKeyboardKey.arrowDown)) { + // Skip for the shift shortcut since web accepts it. + if (activator.shift) { + continue; + } await sendKeyCombination(tester, activator); await tester.pump(); @@ -2201,4 +2208,102 @@ void main() { }, variant: appleOnly); }, skip: !kIsWeb);// [intended] specific tests target web. + + group('Web does accept', () { + testWidgets('select up', (WidgetTester tester) async { + const SingleActivator selectUp = + SingleActivator(LogicalKeyboardKey.arrowUp, shift: true); + controller.text = testVerticalText; + controller.selection = const TextSelection.collapsed( + offset: 5, + ); + + await tester.pumpWidget(buildEditableText()); + await sendKeyCombination(tester, selectUp); + await tester.pump(); + + expect(controller.text, testVerticalText); + expect( + controller.selection, + const TextSelection( + baseOffset: 5, + extentOffset: 3), // selection extends upwards from 5 + reason: selectUp.toString(), + ); + }, variant: TargetPlatformVariant.desktop()); + + testWidgets('select down', (WidgetTester tester) async { + const SingleActivator selectDown = + SingleActivator(LogicalKeyboardKey.arrowDown, shift: true); + controller.text = testVerticalText; + controller.selection = const TextSelection.collapsed( + offset: 5, + ); + + await tester.pumpWidget(buildEditableText()); + await sendKeyCombination(tester, selectDown); + await tester.pump(); + + expect(controller.text, testVerticalText); + expect( + controller.selection, + const TextSelection( + baseOffset: 5, + extentOffset: 7), // selection extends downwards from 5 + reason: selectDown.toString(), + ); + }, variant: TargetPlatformVariant.desktop()); + + testWidgets('select all up', (WidgetTester tester) async { + final bool isMacOS = defaultTargetPlatform == TargetPlatform.macOS; + final SingleActivator selectAllUp = isMacOS + ? const SingleActivator(LogicalKeyboardKey.arrowUp, + shift: true, meta: true) + : const SingleActivator(LogicalKeyboardKey.arrowUp, + shift: true, alt: true); + controller.text = testVerticalText; + controller.selection = const TextSelection.collapsed( + offset: 5, + ); + + await tester.pumpWidget(buildEditableText()); + await sendKeyCombination(tester, selectAllUp); + await tester.pump(); + + expect(controller.text, testVerticalText); + expect( + controller.selection, + const TextSelection( + baseOffset: 5, + extentOffset: 0), // selection extends all the way up + reason: selectAllUp.toString(), + ); + }, variant: TargetPlatformVariant.desktop()); + + testWidgets('select all down', (WidgetTester tester) async { + final bool isMacOS = defaultTargetPlatform == TargetPlatform.macOS; + final SingleActivator selectAllDown = isMacOS + ? const SingleActivator(LogicalKeyboardKey.arrowDown, + shift: true, meta: true) + : const SingleActivator(LogicalKeyboardKey.arrowDown, + shift: true, alt: true); + controller.text = testVerticalText; + controller.selection = const TextSelection.collapsed( + offset: 5, + ); + + await tester.pumpWidget(buildEditableText()); + await sendKeyCombination(tester, selectAllDown); + await tester.pump(); + + expect(controller.text, testVerticalText); + expect( + controller.selection, + const TextSelection( + baseOffset: 5, + extentOffset: 17), // selection extends all the way down + reason: selectAllDown.toString(), + ); + }, variant: TargetPlatformVariant.desktop()); + }, skip: !kIsWeb); // [intended] specific tests target web. }