From d586873275cb98e2c3c3c6a967e9fade4d12a8bf Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Mon, 28 Jun 2021 08:41:59 -0700 Subject: [PATCH] Select All via Shortcuts (#85081) Control + A to select all text is now overridable. --- .../flutter/lib/src/rendering/editable.dart | 26 ++-- .../widgets/default_text_editing_actions.dart | 8 ++ .../default_text_editing_shortcuts.dart | 20 ++- .../lib/src/widgets/text_editing_intents.dart | 8 ++ .../test/widgets/editable_text_test.dart | 132 ++++++++++++++++++ 5 files changed, 180 insertions(+), 14 deletions(-) diff --git a/packages/flutter/lib/src/rendering/editable.dart b/packages/flutter/lib/src/rendering/editable.dart index 2cd25f6129..7f9a05c6d2 100644 --- a/packages/flutter/lib/src/rendering/editable.dart +++ b/packages/flutter/lib/src/rendering/editable.dart @@ -644,7 +644,6 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin, }; static final Set _shortcutKeys = { - LogicalKeyboardKey.keyA, LogicalKeyboardKey.keyC, LogicalKeyboardKey.keyV, LogicalKeyboardKey.keyX, @@ -2232,8 +2231,21 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin, _setSelection(nextSelection, cause); } - // Handles shortcut functionality including cut, copy, paste and select all - // using control/command + (X, C, V, A). + /// Set the current [selection] to contain the entire text value. + /// + /// {@macro flutter.rendering.RenderEditable.cause} + void selectAll(SelectionChangedCause cause) { + _setSelection( + selection!.copyWith( + baseOffset: 0, + extentOffset: textSelectionDelegate.textEditingValue.text.length, + ), + cause, + ); + } + + // Handles shortcut functionality including cut, copy, paste using + // using control/command + (X, C, V). Future _handleShortcuts(LogicalKeyboardKey key) async { final TextSelection selection = textSelectionDelegate.textEditingValue.selection; final String text = textSelectionDelegate.textEditingValue.text; @@ -2266,14 +2278,6 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin, ), ); } - } else if (key == LogicalKeyboardKey.keyA) { - value = TextEditingValue( - text: text, - selection: selection.copyWith( - baseOffset: 0, - extentOffset: textSelectionDelegate.textEditingValue.text.length, - ), - ); } if (value != null) { _setTextEditingValue( diff --git a/packages/flutter/lib/src/widgets/default_text_editing_actions.dart b/packages/flutter/lib/src/widgets/default_text_editing_actions.dart index e0e6b01663..d3e501f97f 100644 --- a/packages/flutter/lib/src/widgets/default_text_editing_actions.dart +++ b/packages/flutter/lib/src/widgets/default_text_editing_actions.dart @@ -66,6 +66,7 @@ class DefaultTextEditingActions extends Actions{ MoveSelectionToEndTextIntent: _MoveSelectionToEndTextAction(), MoveSelectionToStartTextIntent: _MoveSelectionToStartTextAction(), MoveSelectionUpTextIntent: _MoveSelectionUpTextAction(), + SelectAllTextIntent: _SelectAllTextAction(), }; } @@ -291,3 +292,10 @@ class _MoveSelectionToStartTextAction extends TextEditingAction { + @override + Object? invoke(SelectAllTextIntent intent, [BuildContext? context]) { + textEditingActionTarget!.renderEditable.selectAll(SelectionChangedCause.keyboard); + } +} 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 e8ce393bcc..3343766069 100644 --- a/packages/flutter/lib/src/widgets/default_text_editing_shortcuts.dart +++ b/packages/flutter/lib/src/widgets/default_text_editing_shortcuts.dart @@ -187,10 +187,12 @@ class DefaultTextEditingShortcuts extends Shortcuts { SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true): ExtendSelectionLeftTextIntent(), SingleActivator(LogicalKeyboardKey.arrowRight, shift: true): ExtendSelectionRightTextIntent(), SingleActivator(LogicalKeyboardKey.arrowUp, shift: true): ExtendSelectionUpTextIntent(), + SingleActivator(LogicalKeyboardKey.keyA, control: true): SelectAllTextIntent(), // The following key combinations have no effect on text editing on this // platform: // * End // * Home + // * Meta + A // * Meta + arrow down // * Meta + arrow left // * Meta + arrow right @@ -232,11 +234,13 @@ class DefaultTextEditingShortcuts extends Shortcuts { SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true): ExtendSelectionLeftTextIntent(), SingleActivator(LogicalKeyboardKey.arrowRight, shift: true): ExtendSelectionRightTextIntent(), SingleActivator(LogicalKeyboardKey.arrowUp, shift: true): ExtendSelectionUpTextIntent(), + SingleActivator(LogicalKeyboardKey.keyA, control: true): SelectAllTextIntent(), // The following key combinations have no effect on text editing on this // platform: - // * Meta + arrow down // * End // * Home + // * Meta + A + // * Meta + arrow down // * Meta + arrow left // * Meta + arrow right // * Meta + arrow up @@ -277,11 +281,13 @@ class DefaultTextEditingShortcuts extends Shortcuts { SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true): ExtendSelectionLeftTextIntent(), SingleActivator(LogicalKeyboardKey.arrowRight, shift: true): ExtendSelectionRightTextIntent(), SingleActivator(LogicalKeyboardKey.arrowUp, shift: true): ExtendSelectionUpTextIntent(), + SingleActivator(LogicalKeyboardKey.keyA, control: true): SelectAllTextIntent(), // The following key combinations have no effect on text editing on this // platform: - // * Meta + arrow down // * End // * Home + // * Meta + A + // * Meta + arrow down // * Meta + arrow left // * Meta + arrow right // * Meta + arrow up @@ -322,11 +328,13 @@ class DefaultTextEditingShortcuts extends Shortcuts { SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true): ExtendSelectionLeftTextIntent(), SingleActivator(LogicalKeyboardKey.arrowRight, shift: true): ExtendSelectionRightTextIntent(), SingleActivator(LogicalKeyboardKey.arrowUp, shift: true): ExtendSelectionUpTextIntent(), + SingleActivator(LogicalKeyboardKey.keyA, control: true): SelectAllTextIntent(), // The following key combinations have no effect on text editing on this // platform: - // * Meta + arrow down // * End // * Home + // * Meta + A + // * Meta + arrow down // * Meta + arrow left // * Meta + arrow right // * Meta + arrow up @@ -371,8 +379,10 @@ class DefaultTextEditingShortcuts extends Shortcuts { SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true): ExtendSelectionLeftTextIntent(), SingleActivator(LogicalKeyboardKey.arrowRight, shift: true): ExtendSelectionRightTextIntent(), SingleActivator(LogicalKeyboardKey.arrowUp, shift: true): ExtendSelectionUpTextIntent(), + SingleActivator(LogicalKeyboardKey.keyA, meta: true): SelectAllTextIntent(), // The following key combinations have no effect on text editing on this // platform: + // * Control + A // * Control + arrow left // * Control + arrow right // * Control + shift + arrow left @@ -416,8 +426,10 @@ class DefaultTextEditingShortcuts extends Shortcuts { SingleActivator(LogicalKeyboardKey.arrowUp, shift: true): ExtendSelectionUpTextIntent(), SingleActivator(LogicalKeyboardKey.end, shift: true): ExpandSelectionRightByLineTextIntent(), SingleActivator(LogicalKeyboardKey.home, shift: true): ExpandSelectionLeftByLineTextIntent(), + SingleActivator(LogicalKeyboardKey.keyA, control: true): SelectAllTextIntent(), // The following key combinations have no effect on text editing on this // platform: + // * Meta + A // * Meta + arrow down // * Meta + arrow left // * Meta + arrow right @@ -474,6 +486,8 @@ class DefaultTextEditingShortcuts extends Shortcuts { SingleActivator(LogicalKeyboardKey.end, shift: true): DoNothingAndStopPropagationTextIntent(), SingleActivator(LogicalKeyboardKey.home, shift: true): DoNothingAndStopPropagationTextIntent(), SingleActivator(LogicalKeyboardKey.space): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.keyA, control: true): DoNothingAndStopPropagationTextIntent(), + SingleActivator(LogicalKeyboardKey.keyA, meta: true): DoNothingAndStopPropagationTextIntent(), }; static Map get _shortcuts { diff --git a/packages/flutter/lib/src/widgets/text_editing_intents.dart b/packages/flutter/lib/src/widgets/text_editing_intents.dart index 97a5459b79..dcadbdf94a 100644 --- a/packages/flutter/lib/src/widgets/text_editing_intents.dart +++ b/packages/flutter/lib/src/widgets/text_editing_intents.dart @@ -266,3 +266,11 @@ class MoveSelectionUpTextIntent extends Intent{ /// Creates an instance of MoveSelectionUpTextIntent. const MoveSelectionUpTextIntent(); } + +/// An [Intent] to select everything in the field. +/// +/// {@macro flutter.widgets.TextEditingIntents.seeAlso} +class SelectAllTextIntent extends Intent{ + /// Creates an instance of SelectAllTextIntent. + const SelectAllTextIntent(); +} diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 6c3cc4be5c..9340a25e82 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -7263,6 +7263,69 @@ void main() { // On web, using keyboard for selection is handled by the browser. }, skip: kIsWeb); + testWidgets('can override select all via Shortcuts', (WidgetTester tester) async { + final TextEditingController controller = TextEditingController(text: testText); + controller.selection = const TextSelection( + baseOffset: 0, + extentOffset: 0, + affinity: TextAffinity.upstream, + ); + await tester.pumpWidget(MaterialApp( + home: Align( + alignment: Alignment.topLeft, + child: SizedBox( + width: 400, + child: Shortcuts( + shortcuts: const { + SingleActivator(LogicalKeyboardKey.keyA, control: true): MoveSelectionRightTextIntent(), + }, + child: EditableText( + maxLines: 10, + controller: controller, + showSelectionHandles: true, + autofocus: true, + focusNode: focusNode, + style: Typography.material2018(platform: TargetPlatform.android).black.subtitle1!, + cursorColor: Colors.blue, + backgroundCursorColor: Colors.grey, + selectionControls: materialTextSelectionControls, + keyboardType: TextInputType.text, + textAlign: TextAlign.right, + ), + ), + ), + ), + )); + + await tester.pump(); // Wait for autofocus to take effect. + expect(controller.selection.isCollapsed, isTrue); + expect(controller.selection.baseOffset, 0); + + // meta + A moves the cursor right instead of doing select all. + final String targetPlatform = defaultTargetPlatform.toString(); + final String platform = targetPlatform.substring(targetPlatform.indexOf('.') + 1).toLowerCase(); + await sendKeys( + tester, + [LogicalKeyboardKey.keyA], + shortcutModifier: true, + platform: platform, + ); + await tester.pump(); + expect(controller.selection.isCollapsed, isTrue); + expect(controller.selection.baseOffset, 1); + await sendKeys( + tester, + [LogicalKeyboardKey.keyA], + shortcutModifier: true, + platform: platform, + ); + await tester.pump(); + expect(controller.selection.isCollapsed, isTrue); + expect(controller.selection.baseOffset, 2); + + // On web, using keyboard for selection is handled by the browser. + }, skip: kIsWeb); + testWidgets('navigating by word', (WidgetTester tester) async { final TextEditingController controller = TextEditingController(text: 'word word word'); // word wo|rd| word @@ -7461,6 +7524,75 @@ void main() { // On web, using keyboard for selection is handled by the browser. }, skip: kIsWeb); + testWidgets('can override select all via Actions', (WidgetTester tester) async { + final TextEditingController controller = TextEditingController(text: testText); + controller.selection = const TextSelection( + baseOffset: 0, + extentOffset: 0, + affinity: TextAffinity.upstream, + ); + late bool myIntentWasCalled; + await tester.pumpWidget(MaterialApp( + home: Align( + alignment: Alignment.topLeft, + child: SizedBox( + width: 400, + child: Actions( + actions: >{ + SelectAllTextIntent: _MyMoveSelectionRightTextAction( + onInvoke: () { + myIntentWasCalled = true; + }, + ), + }, + child: EditableText( + maxLines: 10, + controller: controller, + showSelectionHandles: true, + autofocus: true, + focusNode: focusNode, + style: Typography.material2018(platform: TargetPlatform.android).black.subtitle1!, + cursorColor: Colors.blue, + backgroundCursorColor: Colors.grey, + selectionControls: materialTextSelectionControls, + keyboardType: TextInputType.text, + textAlign: TextAlign.right, + ), + ), + ), + ), + )); + + await tester.pump(); // Wait for autofocus to take effect. + expect(controller.selection.isCollapsed, isTrue); + expect(controller.selection.baseOffset, 0); + + // meta + A moves the cursor right instead of doing select all. + final String targetPlatform = defaultTargetPlatform.toString(); + final String platform = targetPlatform.substring(targetPlatform.indexOf('.') + 1).toLowerCase(); + await sendKeys( + tester, + [LogicalKeyboardKey.keyA], + shortcutModifier: true, + platform: platform, + ); + await tester.pump(); + expect(controller.selection.isCollapsed, isTrue); + expect(controller.selection.baseOffset, 1); + expect(myIntentWasCalled, isTrue); + await sendKeys( + tester, + [LogicalKeyboardKey.keyA], + shortcutModifier: true, + platform: platform, + ); + await tester.pump(); + expect(controller.selection.isCollapsed, isTrue); + expect(controller.selection.baseOffset, 2); + + // On web, using keyboard for selection is handled by the browser. + }, skip: kIsWeb); + testWidgets('ignore key event from web platform', (WidgetTester tester) async { final TextEditingController controller = TextEditingController( text: 'test\ntest',