Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 99b6bd8

Browse files
Renzo-OlivaresRenzo Olivares
andauthored
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 <[email protected]>
1 parent f94fa7e commit 99b6bd8

File tree

4 files changed

+61
-17
lines changed

4 files changed

+61
-17
lines changed

packages/flutter/lib/src/widgets/default_text_editing_shortcuts.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ class DefaultTextEditingShortcuts extends StatelessWidget {
207207
const SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true, control: true): const ExtendSelectionToNextWordBoundaryIntent(forward: false, collapseSelection: false),
208208
const SingleActivator(LogicalKeyboardKey.arrowRight, shift: true, control: true): const ExtendSelectionToNextWordBoundaryIntent(forward: true, collapseSelection: false),
209209

210+
const SingleActivator(LogicalKeyboardKey.arrowUp, shift: true, control: true): const ExtendSelectionToNextParagraphBoundaryIntent(forward: false, collapseSelection: false),
211+
const SingleActivator(LogicalKeyboardKey.arrowDown, shift: true, control: true): const ExtendSelectionToNextParagraphBoundaryIntent(forward: true, collapseSelection: false),
212+
210213
// Page Up / Down: Move selection by page.
211214
const SingleActivator(LogicalKeyboardKey.pageUp): const ExtendSelectionVerticallyToAdjacentPageIntent(forward: false, collapseSelection: true),
212215
const SingleActivator(LogicalKeyboardKey.pageDown): const ExtendSelectionVerticallyToAdjacentPageIntent(forward: true, collapseSelection: true),

packages/flutter/lib/src/widgets/editable_text.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4424,6 +4424,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
44244424
ExtendSelectionByCharacterIntent: _makeOverridable(_UpdateTextSelectionAction<ExtendSelectionByCharacterIntent>(this, _characterBoundary, _moveBeyondTextBoundary, ignoreNonCollapsedSelection: false)),
44254425
ExtendSelectionByPageIntent: _makeOverridable(CallbackAction<ExtendSelectionByPageIntent>(onInvoke: _extendSelectionByPage)),
44264426
ExtendSelectionToNextWordBoundaryIntent: _makeOverridable(_UpdateTextSelectionAction<ExtendSelectionToNextWordBoundaryIntent>(this, _nextWordBoundary, _moveBeyondTextBoundary, ignoreNonCollapsedSelection: true)),
4427+
ExtendSelectionToNextParagraphBoundaryIntent : _makeOverridable(_UpdateTextSelectionAction<ExtendSelectionToNextParagraphBoundaryIntent>(this, _paragraphBoundary, _moveBeyondTextBoundary, ignoreNonCollapsedSelection: true)),
44274428
ExtendSelectionToLineBreakIntent: _makeOverridable(_UpdateTextSelectionAction<ExtendSelectionToLineBreakIntent>(this, _linebreak, _moveToTextBoundary, ignoreNonCollapsedSelection: true)),
44284429
ExtendSelectionVerticallyToAdjacentLineIntent: _makeOverridable(_verticalSelectionUpdateAction),
44294430
ExtendSelectionVerticallyToAdjacentPageIntent: _makeOverridable(_verticalSelectionUpdateAction),

packages/flutter/lib/src/widgets/text_editing_intents.dart

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,25 @@ class ExtendSelectionVerticallyToAdjacentPageIntent extends DirectionalCaretMove
221221
}) : super(forward, collapseSelection);
222222
}
223223

224+
/// Extends, or moves the current selection from the current
225+
/// [TextSelection.extent] position to the previous or the next paragraph
226+
/// boundary.
227+
class ExtendSelectionToNextParagraphBoundaryIntent extends DirectionalCaretMovementIntent {
228+
/// Creates an [ExtendSelectionToNextParagraphBoundaryIntent].
229+
const ExtendSelectionToNextParagraphBoundaryIntent({
230+
required bool forward,
231+
required bool collapseSelection,
232+
}) : super(forward, collapseSelection);
233+
}
234+
224235
/// Extends, or moves the current selection from the current
225236
/// [TextSelection.extent] position to the previous or the next paragraph
226237
/// boundary depending on the [forward] parameter.
227238
///
228-
/// This [Intent] collapses the selection when the order of [TextSelection.base]
229-
/// and [TextSelection.extent] would reverse.
239+
/// This [Intent] typically has the same effect as an
240+
/// [ExtendSelectionToNextParagraphBoundaryIntent], except it collapses the selection
241+
/// when the order of [TextSelection.base] and [TextSelection.extent] would
242+
/// reverse.
230243
///
231244
/// This is typically only used on MacOS.
232245
class ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent extends DirectionalCaretMovementIntent {

packages/flutter/test/widgets/editable_text_test.dart

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6723,7 +6723,7 @@ void main() {
67236723
);
67246724
expect(controller.text, equals(testText), reason: 'on $platform');
67256725

6726-
final bool platformCanSelectByParagraph = defaultTargetPlatform == TargetPlatform.iOS || defaultTargetPlatform == TargetPlatform.macOS;
6726+
final bool platformIsApple = defaultTargetPlatform == TargetPlatform.iOS || defaultTargetPlatform == TargetPlatform.macOS;
67276727
// Move down one paragraph.
67286728
await sendKeys(
67296729
tester,
@@ -6738,9 +6738,9 @@ void main() {
67386738
expect(
67396739
selection,
67406740
equals(
6741-
TextSelection(
6741+
const TextSelection(
67426742
baseOffset: 10,
6743-
extentOffset: platformCanSelectByParagraph ? 20 : 10,
6743+
extentOffset: 20,
67446744
),
67456745
),
67466746
reason: 'on $platform',
@@ -6760,9 +6760,9 @@ void main() {
67606760
expect(
67616761
selection,
67626762
equals(
6763-
TextSelection(
6763+
const TextSelection(
67646764
baseOffset: 10,
6765-
extentOffset: platformCanSelectByParagraph ? 36 : 10,
6765+
extentOffset: 36,
67666766
),
67676767
),
67686768
reason: 'on $platform',
@@ -6782,9 +6782,9 @@ void main() {
67826782
expect(
67836783
selection,
67846784
equals(
6785-
TextSelection(
6785+
const TextSelection(
67866786
baseOffset: 10,
6787-
extentOffset: platformCanSelectByParagraph ? 55 : 10,
6787+
extentOffset: 55,
67886788
),
67896789
),
67906790
reason: 'on $platform',
@@ -6804,9 +6804,9 @@ void main() {
68046804
expect(
68056805
selection,
68066806
equals(
6807-
TextSelection(
6807+
const TextSelection(
68086808
baseOffset: 10,
6809-
extentOffset: platformCanSelectByParagraph ? 36 : 10,
6809+
extentOffset: 36,
68106810
),
68116811
),
68126812
reason: 'on $platform',
@@ -6826,15 +6826,16 @@ void main() {
68266826
expect(
68276827
selection,
68286828
equals(
6829-
TextSelection(
6829+
const TextSelection(
68306830
baseOffset: 10,
6831-
extentOffset: platformCanSelectByParagraph ? 20 : 10,
6831+
extentOffset: 20,
68326832
),
68336833
),
68346834
reason: 'on $platform',
68356835
);
68366836

6837-
// Move up back to the origin.
6837+
// Move up. This will collapse the selection to the origin on Apple platforms, and
6838+
// extend to the previous paragraph boundary on other platforms.
68386839
await sendKeys(
68396840
tester,
68406841
<LogicalKeyboardKey>[
@@ -6850,13 +6851,15 @@ void main() {
68506851
equals(
68516852
TextSelection(
68526853
baseOffset: 10,
6853-
extentOffset: platformCanSelectByParagraph ? 10 : 10,
6854+
extentOffset: platformIsApple ? 10 : 0,
68546855
),
68556856
),
68566857
reason: 'on $platform',
68576858
);
68586859

6859-
// Move up, extending the selection backwards to the next paragraph.
6860+
// Move up, extending the selection backwards to the previous paragraph on Apple platforms.
6861+
// On other platforms this does nothing since our extent is already at 0 from the previous
6862+
// set of keys sent.
68606863
await sendKeys(
68616864
tester,
68626865
<LogicalKeyboardKey>[
@@ -6867,12 +6870,36 @@ void main() {
68676870
targetPlatform: defaultTargetPlatform,
68686871
);
68696872

6873+
expect(
6874+
selection,
6875+
equals(
6876+
const TextSelection(
6877+
baseOffset: 10,
6878+
extentOffset: 0,
6879+
),
6880+
),
6881+
reason: 'on $platform',
6882+
);
6883+
6884+
// Move down, collapsing the selection to the origin on Apple platforms.
6885+
// On other platforms this moves the selection's extent to the next paragraph boundary.
6886+
await sendKeys(
6887+
tester,
6888+
<LogicalKeyboardKey>[
6889+
LogicalKeyboardKey.arrowDown,
6890+
],
6891+
shift: true,
6892+
wordModifier: true,
6893+
targetPlatform: defaultTargetPlatform,
6894+
);
6895+
68706896
expect(
68716897
selection,
68726898
equals(
68736899
TextSelection(
68746900
baseOffset: 10,
6875-
extentOffset: platformCanSelectByParagraph ? 0 : 10,
6901+
extentOffset: platformIsApple ? 10 : 20,
6902+
affinity: platformIsApple ? TextAffinity.upstream : TextAffinity.downstream,
68766903
),
68776904
),
68786905
reason: 'on $platform',

0 commit comments

Comments
 (0)