Skip to content

Commit

Permalink
[web] Changes to web keyboard selection shortcuts for more consistent…
Browse files Browse the repository at this point in the history
… behavior (#114264)
  • Loading branch information
htoor3 committed Nov 2, 2022
1 parent 45c3b02 commit 9f23391
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 7 deletions.
Expand Up @@ -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(),
Expand Down Expand Up @@ -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<ShortcutActivator, Intent> get _shortcuts {
Expand Down
107 changes: 106 additions & 1 deletion packages/flutter/test/widgets/editable_text_shortcuts_test.dart
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand All @@ -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();

Expand Down Expand Up @@ -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.
}

0 comments on commit 9f23391

Please sign in to comment.