From 99b6bd8c05a56680a62b161698c069d39951caf2 Mon Sep 17 00:00:00 2001 From: Renzo Olivares Date: Tue, 7 Feb 2023 18:55:26 -0800 Subject: [PATCH] Add support for extending selection to paragraph on ctrl + shift + arrow up/down on Non-Apple platforms (#120151) * Add support for extending selection to paragraph on ctrl + shift + arrow up/down for common keyboard actions * Add ctrl + shift + arrow up/down common text editing shortcuts/actions * fix analyzer --------- Co-authored-by: Renzo Olivares --- .../default_text_editing_shortcuts.dart | 3 + .../lib/src/widgets/editable_text.dart | 1 + .../lib/src/widgets/text_editing_intents.dart | 17 +++++- .../test/widgets/editable_text_test.dart | 57 ++++++++++++++----- 4 files changed, 61 insertions(+), 17 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 7ccbe9fc77..9ca135bc7e 100644 --- a/packages/flutter/lib/src/widgets/default_text_editing_shortcuts.dart +++ b/packages/flutter/lib/src/widgets/default_text_editing_shortcuts.dart @@ -207,6 +207,9 @@ class DefaultTextEditingShortcuts extends StatelessWidget { const SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true, control: true): const ExtendSelectionToNextWordBoundaryIntent(forward: false, collapseSelection: false), const SingleActivator(LogicalKeyboardKey.arrowRight, shift: true, control: true): const ExtendSelectionToNextWordBoundaryIntent(forward: true, collapseSelection: false), + const SingleActivator(LogicalKeyboardKey.arrowUp, shift: true, control: true): const ExtendSelectionToNextParagraphBoundaryIntent(forward: false, collapseSelection: false), + const SingleActivator(LogicalKeyboardKey.arrowDown, shift: true, control: true): const ExtendSelectionToNextParagraphBoundaryIntent(forward: true, collapseSelection: false), + // Page Up / Down: Move selection by page. const SingleActivator(LogicalKeyboardKey.pageUp): const ExtendSelectionVerticallyToAdjacentPageIntent(forward: false, collapseSelection: true), const SingleActivator(LogicalKeyboardKey.pageDown): const ExtendSelectionVerticallyToAdjacentPageIntent(forward: true, collapseSelection: true), diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 0ce522f2e6..13823752d8 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -4424,6 +4424,7 @@ class EditableTextState extends State with AutomaticKeepAliveClien ExtendSelectionByCharacterIntent: _makeOverridable(_UpdateTextSelectionAction(this, _characterBoundary, _moveBeyondTextBoundary, ignoreNonCollapsedSelection: false)), ExtendSelectionByPageIntent: _makeOverridable(CallbackAction(onInvoke: _extendSelectionByPage)), ExtendSelectionToNextWordBoundaryIntent: _makeOverridable(_UpdateTextSelectionAction(this, _nextWordBoundary, _moveBeyondTextBoundary, ignoreNonCollapsedSelection: true)), + ExtendSelectionToNextParagraphBoundaryIntent : _makeOverridable(_UpdateTextSelectionAction(this, _paragraphBoundary, _moveBeyondTextBoundary, ignoreNonCollapsedSelection: true)), ExtendSelectionToLineBreakIntent: _makeOverridable(_UpdateTextSelectionAction(this, _linebreak, _moveToTextBoundary, ignoreNonCollapsedSelection: true)), ExtendSelectionVerticallyToAdjacentLineIntent: _makeOverridable(_verticalSelectionUpdateAction), ExtendSelectionVerticallyToAdjacentPageIntent: _makeOverridable(_verticalSelectionUpdateAction), diff --git a/packages/flutter/lib/src/widgets/text_editing_intents.dart b/packages/flutter/lib/src/widgets/text_editing_intents.dart index b1a13335f5..ccb39b8834 100644 --- a/packages/flutter/lib/src/widgets/text_editing_intents.dart +++ b/packages/flutter/lib/src/widgets/text_editing_intents.dart @@ -221,12 +221,25 @@ class ExtendSelectionVerticallyToAdjacentPageIntent extends DirectionalCaretMove }) : super(forward, collapseSelection); } +/// Extends, or moves the current selection from the current +/// [TextSelection.extent] position to the previous or the next paragraph +/// boundary. +class ExtendSelectionToNextParagraphBoundaryIntent extends DirectionalCaretMovementIntent { + /// Creates an [ExtendSelectionToNextParagraphBoundaryIntent]. + const ExtendSelectionToNextParagraphBoundaryIntent({ + required bool forward, + required bool collapseSelection, + }) : super(forward, collapseSelection); +} + /// Extends, or moves the current selection from the current /// [TextSelection.extent] position to the previous or the next paragraph /// boundary depending on the [forward] parameter. /// -/// This [Intent] collapses the selection when the order of [TextSelection.base] -/// and [TextSelection.extent] would reverse. +/// This [Intent] typically has the same effect as an +/// [ExtendSelectionToNextParagraphBoundaryIntent], except it collapses the selection +/// when the order of [TextSelection.base] and [TextSelection.extent] would +/// reverse. /// /// This is typically only used on MacOS. class ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent extends DirectionalCaretMovementIntent { diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 9d56c15822..de325135a6 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -6723,7 +6723,7 @@ void main() { ); expect(controller.text, equals(testText), reason: 'on $platform'); - final bool platformCanSelectByParagraph = defaultTargetPlatform == TargetPlatform.iOS || defaultTargetPlatform == TargetPlatform.macOS; + final bool platformIsApple = defaultTargetPlatform == TargetPlatform.iOS || defaultTargetPlatform == TargetPlatform.macOS; // Move down one paragraph. await sendKeys( tester, @@ -6738,9 +6738,9 @@ void main() { expect( selection, equals( - TextSelection( + const TextSelection( baseOffset: 10, - extentOffset: platformCanSelectByParagraph ? 20 : 10, + extentOffset: 20, ), ), reason: 'on $platform', @@ -6760,9 +6760,9 @@ void main() { expect( selection, equals( - TextSelection( + const TextSelection( baseOffset: 10, - extentOffset: platformCanSelectByParagraph ? 36 : 10, + extentOffset: 36, ), ), reason: 'on $platform', @@ -6782,9 +6782,9 @@ void main() { expect( selection, equals( - TextSelection( + const TextSelection( baseOffset: 10, - extentOffset: platformCanSelectByParagraph ? 55 : 10, + extentOffset: 55, ), ), reason: 'on $platform', @@ -6804,9 +6804,9 @@ void main() { expect( selection, equals( - TextSelection( + const TextSelection( baseOffset: 10, - extentOffset: platformCanSelectByParagraph ? 36 : 10, + extentOffset: 36, ), ), reason: 'on $platform', @@ -6826,15 +6826,16 @@ void main() { expect( selection, equals( - TextSelection( + const TextSelection( baseOffset: 10, - extentOffset: platformCanSelectByParagraph ? 20 : 10, + extentOffset: 20, ), ), reason: 'on $platform', ); - // Move up back to the origin. + // Move up. This will collapse the selection to the origin on Apple platforms, and + // extend to the previous paragraph boundary on other platforms. await sendKeys( tester, [ @@ -6850,13 +6851,15 @@ void main() { equals( TextSelection( baseOffset: 10, - extentOffset: platformCanSelectByParagraph ? 10 : 10, + extentOffset: platformIsApple ? 10 : 0, ), ), reason: 'on $platform', ); - // Move up, extending the selection backwards to the next paragraph. + // Move up, extending the selection backwards to the previous paragraph on Apple platforms. + // On other platforms this does nothing since our extent is already at 0 from the previous + // set of keys sent. await sendKeys( tester, [ @@ -6867,12 +6870,36 @@ void main() { targetPlatform: defaultTargetPlatform, ); + expect( + selection, + equals( + const TextSelection( + baseOffset: 10, + extentOffset: 0, + ), + ), + reason: 'on $platform', + ); + + // Move down, collapsing the selection to the origin on Apple platforms. + // On other platforms this moves the selection's extent to the next paragraph boundary. + await sendKeys( + tester, + [ + LogicalKeyboardKey.arrowDown, + ], + shift: true, + wordModifier: true, + targetPlatform: defaultTargetPlatform, + ); + expect( selection, equals( TextSelection( baseOffset: 10, - extentOffset: platformCanSelectByParagraph ? 0 : 10, + extentOffset: platformIsApple ? 10 : 20, + affinity: platformIsApple ? TextAffinity.upstream : TextAffinity.downstream, ), ), reason: 'on $platform',