Skip to content

Commit

Permalink
[web] Allow shift + left/right keyboard shortcuts to be handled by fr…
Browse files Browse the repository at this point in the history
…amework on web (#117217)

* Remove DoNothing actions for shift + left/right keyboard shorcuts on web and add tests

* Fix spacing

* Add select all left/right cases + tests
  • Loading branch information
htoor3 committed Dec 16, 2022
1 parent 0604a0e commit 98e9032
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -412,16 +412,10 @@ class DefaultTextEditingShortcuts extends StatelessWidget {
SingleActivator(LogicalKeyboardKey.arrowLeft, alt: true): DoNothingAndStopPropagationTextIntent(),
SingleActivator(LogicalKeyboardKey.arrowRight, alt: true): DoNothingAndStopPropagationTextIntent(),
SingleActivator(LogicalKeyboardKey.arrowUp, alt: true): DoNothingAndStopPropagationTextIntent(),
SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true, alt: true): DoNothingAndStopPropagationTextIntent(),
SingleActivator(LogicalKeyboardKey.arrowRight, 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.arrowLeft, shift: true, meta: true): DoNothingAndStopPropagationTextIntent(),
SingleActivator(LogicalKeyboardKey.arrowRight, shift: true, meta: true): DoNothingAndStopPropagationTextIntent(),
SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true): DoNothingAndStopPropagationTextIntent(),
SingleActivator(LogicalKeyboardKey.arrowRight, 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 @@ -452,6 +446,12 @@ class DefaultTextEditingShortcuts extends StatelessWidget {
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(),
const SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true): const DoNothingAndStopPropagationTextIntent(),
const SingleActivator(LogicalKeyboardKey.arrowRight, shift: true): const DoNothingAndStopPropagationTextIntent(),
const SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true, alt: true): const DoNothingAndStopPropagationTextIntent(),
const SingleActivator(LogicalKeyboardKey.arrowRight, shift: true, alt: true): const DoNothingAndStopPropagationTextIntent(),
const SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true, meta: true): const DoNothingAndStopPropagationTextIntent(),
const SingleActivator(LogicalKeyboardKey.arrowRight, shift: true, meta: true): const DoNothingAndStopPropagationTextIntent(),
};

// Hand backspace/delete events that do not depend on text layout (delete
Expand Down
152 changes: 136 additions & 16 deletions packages/flutter/test/widgets/editable_text_shortcuts_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void main() {
bool readOnly = false,
bool obscured = false,
TextStyle style = const TextStyle(fontSize: 10.0),
bool enableInteractiveSelection = true
}) {
return MaterialApp(
home: Align(
Expand All @@ -82,6 +83,7 @@ void main() {
readOnly: readOnly,
textAlign: textAlign,
obscureText: obscured,
enableInteractiveSelection: enableInteractiveSelection,
),
),
),
Expand Down Expand Up @@ -2051,22 +2053,6 @@ void main() {
}
}, variant: TargetPlatformVariant.all());

testWidgets('horizontal movement', (WidgetTester tester) async {
controller.text = testText;
controller.selection = const TextSelection.collapsed(
offset: 0,
);

await tester.pumpWidget(buildEditableText());

for (final SingleActivator activator in allModifierVariants(LogicalKeyboardKey.arrowRight)) {
await sendKeyCombination(tester, activator);
await tester.pump();

expect(controller.selection, const TextSelection.collapsed(offset: 0));
}
}, variant: TargetPlatformVariant.all());

testWidgets('select all non apple', (WidgetTester tester) async {
controller.text = testText;
controller.selection = const TextSelection.collapsed(
Expand Down Expand Up @@ -2285,5 +2271,139 @@ void main() {
reason: selectAllDown.toString(),
);
}, variant: TargetPlatformVariant.desktop());

testWidgets('select left', (WidgetTester tester) async {
const SingleActivator selectLeft =
SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true);
controller.text = 'testing';
controller.selection = const TextSelection.collapsed(
offset: 5,
);

await tester.pumpWidget(buildEditableText());
await sendKeyCombination(tester, selectLeft);
await tester.pump();

expect(controller.text, 'testing');
expect(
controller.selection,
const TextSelection(baseOffset: 5, extentOffset: 4),
reason: selectLeft.toString(),
);
}, variant: TargetPlatformVariant.desktop());

testWidgets('select right', (WidgetTester tester) async {
const SingleActivator selectRight =
SingleActivator(LogicalKeyboardKey.arrowRight, shift: true);
controller.text = 'testing';
controller.selection = const TextSelection.collapsed(
offset: 5,
);

await tester.pumpWidget(buildEditableText());
await sendKeyCombination(tester, selectRight);
await tester.pump();

expect(controller.text, 'testing');
expect(
controller.selection,
const TextSelection(baseOffset: 5, extentOffset: 6),
reason: selectRight.toString(),
);
}, variant: TargetPlatformVariant.desktop());

testWidgets(
'select left should not expand selection if selection is disabled',
(WidgetTester tester) async {
const SingleActivator selectLeft =
SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true);
controller.text = 'testing';
controller.selection = const TextSelection.collapsed(
offset: 5,
);

await tester
.pumpWidget(buildEditableText(enableInteractiveSelection: false));
await sendKeyCombination(tester, selectLeft);
await tester.pump();

expect(controller.text, 'testing');
expect(
controller.selection,
const TextSelection.collapsed(offset: 4), // should not expand selection
reason: selectLeft.toString(),
);
}, variant: TargetPlatformVariant.desktop());

testWidgets(
'select right should not expand selection if selection is disabled',
(WidgetTester tester) async {
const SingleActivator selectRight =
SingleActivator(LogicalKeyboardKey.arrowRight, shift: true);
controller.text = 'testing';
controller.selection = const TextSelection.collapsed(offset: 5);

await tester
.pumpWidget(buildEditableText(enableInteractiveSelection: false));
await sendKeyCombination(tester, selectRight);
await tester.pump();

expect(controller.text, 'testing');
expect(
controller.selection,
const TextSelection.collapsed(
offset: 6,
affinity: TextAffinity.upstream), // should not expand selection
reason: selectRight.toString(),
);
}, variant: TargetPlatformVariant.desktop());

testWidgets('select all left', (WidgetTester tester) async {
final bool isMacOS = defaultTargetPlatform == TargetPlatform.macOS;
final SingleActivator selectAllLeft = isMacOS
? const SingleActivator(LogicalKeyboardKey.arrowLeft,
shift: true, meta: true)
: const SingleActivator(LogicalKeyboardKey.arrowLeft,
shift: true, alt: true);
controller.text = 'testing';
controller.selection = const TextSelection.collapsed(
offset: 5,
);

await tester.pumpWidget(buildEditableText());
await sendKeyCombination(tester, selectAllLeft);
await tester.pump();

expect(controller.text, 'testing');
expect(
controller.selection,
const TextSelection(baseOffset: 5, extentOffset: 0),
reason: selectAllLeft.toString(),
);
}, variant: TargetPlatformVariant.desktop());

testWidgets('select all right', (WidgetTester tester) async {
final bool isMacOS = defaultTargetPlatform == TargetPlatform.macOS;
final SingleActivator selectAllRight = isMacOS
? const SingleActivator(LogicalKeyboardKey.arrowRight,
shift: true, meta: true)
: const SingleActivator(LogicalKeyboardKey.arrowRight,
shift: true, alt: true);
controller.text = 'testing';
controller.selection = const TextSelection.collapsed(
offset: 5,
);

await tester.pumpWidget(buildEditableText());
await sendKeyCombination(tester, selectAllRight);
await tester.pump();

expect(controller.text, 'testing');
expect(
controller.selection,
const TextSelection(baseOffset: 5, extentOffset: 7),
reason: selectAllRight.toString(),
);
}, variant: TargetPlatformVariant.desktop());
}, skip: !kIsWeb); // [intended] specific tests target web.
}

0 comments on commit 98e9032

Please sign in to comment.