forked from firka/flutter
Migrate RenderEditable shortcuts from RawKeyboard listeners (#85381)
No more RawKeyboard listeners in RenderEditable, use Shortcuts instead.
This commit is contained in:
@@ -11,7 +11,6 @@ import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/semantics.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import 'package:vector_math/vector_math_64.dart';
|
||||
|
||||
import 'box.dart';
|
||||
@@ -636,77 +635,6 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin,
|
||||
onSelectionChanged?.call(nextSelection, this, cause);
|
||||
}
|
||||
|
||||
static final Set<LogicalKeyboardKey> _movementKeys = <LogicalKeyboardKey>{
|
||||
LogicalKeyboardKey.arrowRight,
|
||||
LogicalKeyboardKey.arrowLeft,
|
||||
LogicalKeyboardKey.arrowUp,
|
||||
LogicalKeyboardKey.arrowDown,
|
||||
};
|
||||
|
||||
static final Set<LogicalKeyboardKey> _shortcutKeys = <LogicalKeyboardKey>{
|
||||
LogicalKeyboardKey.keyC,
|
||||
LogicalKeyboardKey.keyV,
|
||||
LogicalKeyboardKey.keyX,
|
||||
LogicalKeyboardKey.delete,
|
||||
LogicalKeyboardKey.backspace,
|
||||
};
|
||||
|
||||
static final Set<LogicalKeyboardKey> _nonModifierKeys = <LogicalKeyboardKey>{
|
||||
..._shortcutKeys,
|
||||
..._movementKeys,
|
||||
};
|
||||
|
||||
static final Set<LogicalKeyboardKey> _modifierKeys = <LogicalKeyboardKey>{
|
||||
LogicalKeyboardKey.shift,
|
||||
LogicalKeyboardKey.control,
|
||||
LogicalKeyboardKey.alt,
|
||||
};
|
||||
|
||||
static final Set<LogicalKeyboardKey> _macOsModifierKeys = <LogicalKeyboardKey>{
|
||||
LogicalKeyboardKey.shift,
|
||||
LogicalKeyboardKey.meta,
|
||||
LogicalKeyboardKey.alt,
|
||||
};
|
||||
|
||||
static final Set<LogicalKeyboardKey> _interestingKeys = <LogicalKeyboardKey>{
|
||||
..._modifierKeys,
|
||||
..._macOsModifierKeys,
|
||||
..._nonModifierKeys,
|
||||
};
|
||||
|
||||
void _handleKeyEvent(RawKeyEvent keyEvent) {
|
||||
if (kIsWeb) {
|
||||
// On web platform, we should ignore the key because it's processed already.
|
||||
return;
|
||||
}
|
||||
|
||||
if (keyEvent is! RawKeyDownEvent)
|
||||
return;
|
||||
final Set<LogicalKeyboardKey> keysPressed = LogicalKeyboardKey.collapseSynonyms(RawKeyboard.instance.keysPressed);
|
||||
final LogicalKeyboardKey key = keyEvent.logicalKey;
|
||||
|
||||
final bool isMacOS = keyEvent.data is RawKeyEventDataMacOs;
|
||||
if (!_nonModifierKeys.contains(key) ||
|
||||
keysPressed.difference(isMacOS ? _macOsModifierKeys : _modifierKeys).length > 1 ||
|
||||
keysPressed.difference(_interestingKeys).isNotEmpty) {
|
||||
// If the most recently pressed key isn't a non-modifier key, or more than
|
||||
// one non-modifier key is down, or keys other than the ones we're interested in
|
||||
// are pressed, just ignore the keypress.
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO(ianh): It seems to be entirely possible for the selection to be null here, but
|
||||
// all the keyboard handling functions assume it is not.
|
||||
assert(selection != null);
|
||||
|
||||
final bool isShortcutModifierPressed = isMacOS ? keyEvent.isMetaPressed : keyEvent.isControlPressed;
|
||||
if (isShortcutModifierPressed && _shortcutKeys.contains(key)) {
|
||||
// _handleShortcuts depends on being started in the same stack invocation
|
||||
// as the _handleKeyEvent method
|
||||
_handleShortcuts(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the index into the string of the next character boundary after the
|
||||
/// given index.
|
||||
///
|
||||
@@ -2244,45 +2172,62 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin,
|
||||
);
|
||||
}
|
||||
|
||||
// Handles shortcut functionality including cut, copy, paste using
|
||||
// using control/command + (X, C, V).
|
||||
Future<void> _handleShortcuts(LogicalKeyboardKey key) async {
|
||||
/// Copy current [selection] to [Clipboard].
|
||||
void copySelection() {
|
||||
final TextSelection selection = textSelectionDelegate.textEditingValue.selection;
|
||||
final String text = textSelectionDelegate.textEditingValue.text;
|
||||
assert(selection != null);
|
||||
assert(_shortcutKeys.contains(key), 'shortcut key $key not recognized.');
|
||||
if (key == LogicalKeyboardKey.keyC) {
|
||||
if (!selection.isCollapsed) {
|
||||
Clipboard.setData(ClipboardData(text: selection.textInside(text)));
|
||||
}
|
||||
if (!selection.isCollapsed) {
|
||||
Clipboard.setData(ClipboardData(text: selection.textInside(text)));
|
||||
}
|
||||
}
|
||||
|
||||
/// Cut current [selection] to Clipboard.
|
||||
///
|
||||
/// {@macro flutter.rendering.RenderEditable.cause}
|
||||
void cutSelection(SelectionChangedCause cause) {
|
||||
if (_readOnly) {
|
||||
return;
|
||||
}
|
||||
TextEditingValue? value;
|
||||
if (key == LogicalKeyboardKey.keyX && !_readOnly) {
|
||||
if (!selection.isCollapsed) {
|
||||
Clipboard.setData(ClipboardData(text: selection.textInside(text)));
|
||||
value = TextEditingValue(
|
||||
final TextSelection selection = textSelectionDelegate.textEditingValue.selection;
|
||||
final String text = textSelectionDelegate.textEditingValue.text;
|
||||
assert(selection != null);
|
||||
if (!selection.isCollapsed) {
|
||||
Clipboard.setData(ClipboardData(text: selection.textInside(text)));
|
||||
_setTextEditingValue(
|
||||
TextEditingValue(
|
||||
text: selection.textBefore(text) + selection.textAfter(text),
|
||||
selection: TextSelection.collapsed(offset: math.min(selection.start, selection.end)),
|
||||
);
|
||||
}
|
||||
} else if (key == LogicalKeyboardKey.keyV && !_readOnly) {
|
||||
// Snapshot the input before using `await`.
|
||||
// See https://github.com/flutter/flutter/issues/11427
|
||||
final ClipboardData? data = await Clipboard.getData(Clipboard.kTextPlain);
|
||||
if (data != null && selection.isValid) {
|
||||
value = TextEditingValue(
|
||||
text: selection.textBefore(text) + data.text! + selection.textAfter(text),
|
||||
selection: TextSelection.collapsed(
|
||||
offset: math.min(selection.start, selection.end) + data.text!.length,
|
||||
),
|
||||
);
|
||||
}
|
||||
),
|
||||
cause,
|
||||
);
|
||||
}
|
||||
if (value != null) {
|
||||
}
|
||||
|
||||
/// Paste text from [Clipboard].
|
||||
///
|
||||
/// If there is currently a selection, it will be replaced.
|
||||
///
|
||||
/// {@macro flutter.rendering.RenderEditable.cause}
|
||||
Future<void> pasteText(SelectionChangedCause cause) async {
|
||||
if (_readOnly) {
|
||||
return;
|
||||
}
|
||||
final TextSelection selection = textSelectionDelegate.textEditingValue.selection;
|
||||
final String text = textSelectionDelegate.textEditingValue.text;
|
||||
assert(selection != null);
|
||||
// Snapshot the input before using `await`.
|
||||
// See https://github.com/flutter/flutter/issues/11427
|
||||
final ClipboardData? data = await Clipboard.getData(Clipboard.kTextPlain);
|
||||
if (data != null && selection.isValid) {
|
||||
_setTextEditingValue(
|
||||
value,
|
||||
SelectionChangedCause.keyboard,
|
||||
TextEditingValue(
|
||||
text: selection.textBefore(text) + data.text! + selection.textAfter(text),
|
||||
selection: TextSelection.collapsed(
|
||||
offset: math.min(selection.start, selection.end) + data.text!.length,
|
||||
),
|
||||
),
|
||||
cause,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -2441,32 +2386,12 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin,
|
||||
/// Whether the editable is currently focused.
|
||||
bool get hasFocus => _hasFocus;
|
||||
bool _hasFocus = false;
|
||||
bool _listenerAttached = false;
|
||||
set hasFocus(bool value) {
|
||||
assert(value != null);
|
||||
if (_hasFocus == value)
|
||||
return;
|
||||
_hasFocus = value;
|
||||
markNeedsSemanticsUpdate();
|
||||
|
||||
if (!attached) {
|
||||
assert(!_listenerAttached);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_hasFocus) {
|
||||
assert(!_listenerAttached);
|
||||
// TODO(justinmc): This listener should be ported to Actions and removed.
|
||||
// https://github.com/flutter/flutter/issues/75004
|
||||
RawKeyboard.instance.addListener(_handleKeyEvent);
|
||||
_listenerAttached = true;
|
||||
} else {
|
||||
assert(_listenerAttached);
|
||||
// TODO(justinmc): This listener should be ported to Actions and removed.
|
||||
// https://github.com/flutter/flutter/issues/75004
|
||||
RawKeyboard.instance.removeListener(_handleKeyEvent);
|
||||
_listenerAttached = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether this rendering object will take a full line regardless the text width.
|
||||
@@ -3073,11 +2998,6 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin,
|
||||
_offset.addListener(markNeedsPaint);
|
||||
_showHideCursor();
|
||||
_showCursor.addListener(_showHideCursor);
|
||||
assert(!_listenerAttached);
|
||||
if (_hasFocus) {
|
||||
RawKeyboard.instance.addListener(_handleKeyEvent);
|
||||
_listenerAttached = true;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -3086,12 +3006,6 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin,
|
||||
_longPress.dispose();
|
||||
_offset.removeListener(markNeedsPaint);
|
||||
_showCursor.removeListener(_showHideCursor);
|
||||
// TODO(justinmc): This listener should be ported to Actions and removed.
|
||||
// https://github.com/flutter/flutter/issues/75004
|
||||
if (_listenerAttached) {
|
||||
RawKeyboard.instance.removeListener(_handleKeyEvent);
|
||||
_listenerAttached = false;
|
||||
}
|
||||
super.detach();
|
||||
_foregroundRenderObject?.detach();
|
||||
_backgroundRenderObject?.detach();
|
||||
|
||||
@@ -67,6 +67,9 @@ class DefaultTextEditingActions extends Actions{
|
||||
MoveSelectionToStartTextIntent: _MoveSelectionToStartTextAction(),
|
||||
MoveSelectionUpTextIntent: _MoveSelectionUpTextAction(),
|
||||
SelectAllTextIntent: _SelectAllTextAction(),
|
||||
CopySelectionTextIntent: _CopySelectionTextAction(),
|
||||
CutSelectionTextIntent: _CutSelectionTextAction(),
|
||||
PasteTextIntent: _PasteTextAction(),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -293,9 +296,31 @@ class _MoveSelectionToStartTextAction extends TextEditingAction<MoveSelectionToS
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class _SelectAllTextAction extends TextEditingAction<SelectAllTextIntent> {
|
||||
@override
|
||||
Object? invoke(SelectAllTextIntent intent, [BuildContext? context]) {
|
||||
textEditingActionTarget!.renderEditable.selectAll(SelectionChangedCause.keyboard);
|
||||
}
|
||||
}
|
||||
|
||||
class _CopySelectionTextAction extends TextEditingAction<CopySelectionTextIntent> {
|
||||
@override
|
||||
Object? invoke(CopySelectionTextIntent intent, [BuildContext? context]) {
|
||||
textEditingActionTarget!.renderEditable.copySelection();
|
||||
}
|
||||
}
|
||||
|
||||
class _CutSelectionTextAction extends TextEditingAction<CutSelectionTextIntent> {
|
||||
@override
|
||||
Object? invoke(CutSelectionTextIntent intent, [BuildContext? context]) {
|
||||
textEditingActionTarget!.renderEditable.cutSelection(SelectionChangedCause.keyboard);
|
||||
}
|
||||
}
|
||||
|
||||
class _PasteTextAction extends TextEditingAction<PasteTextIntent> {
|
||||
@override
|
||||
Object? invoke(PasteTextIntent intent, [BuildContext? context]) {
|
||||
textEditingActionTarget!.renderEditable.pasteText(SelectionChangedCause.keyboard);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,11 +187,17 @@ class DefaultTextEditingShortcuts extends Shortcuts {
|
||||
SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true): ExtendSelectionLeftTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.arrowRight, shift: true): ExtendSelectionRightTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.arrowUp, shift: true): ExtendSelectionUpTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyX, control: true): CutSelectionTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyC, control: true): CopySelectionTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyV, control: true): PasteTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyA, control: true): SelectAllTextIntent(),
|
||||
// The following key combinations have no effect on text editing on this
|
||||
// platform:
|
||||
// * End
|
||||
// * Home
|
||||
// * Meta + X
|
||||
// * Meta + C
|
||||
// * Meta + V
|
||||
// * Meta + A
|
||||
// * Meta + arrow down
|
||||
// * Meta + arrow left
|
||||
@@ -234,11 +240,17 @@ class DefaultTextEditingShortcuts extends Shortcuts {
|
||||
SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true): ExtendSelectionLeftTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.arrowRight, shift: true): ExtendSelectionRightTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.arrowUp, shift: true): ExtendSelectionUpTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyX, control: true): CutSelectionTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyC, control: true): CopySelectionTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyV, control: true): PasteTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyA, control: true): SelectAllTextIntent(),
|
||||
// The following key combinations have no effect on text editing on this
|
||||
// platform:
|
||||
// * End
|
||||
// * Home
|
||||
// * Meta + X
|
||||
// * Meta + C
|
||||
// * Meta + V
|
||||
// * Meta + A
|
||||
// * Meta + arrow down
|
||||
// * Meta + arrow left
|
||||
@@ -281,11 +293,17 @@ class DefaultTextEditingShortcuts extends Shortcuts {
|
||||
SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true): ExtendSelectionLeftTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.arrowRight, shift: true): ExtendSelectionRightTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.arrowUp, shift: true): ExtendSelectionUpTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyX, control: true): CutSelectionTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyC, control: true): CopySelectionTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyV, control: true): PasteTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyA, control: true): SelectAllTextIntent(),
|
||||
// The following key combinations have no effect on text editing on this
|
||||
// platform:
|
||||
// * End
|
||||
// * Home
|
||||
// * Meta + X
|
||||
// * Meta + C
|
||||
// * Meta + V
|
||||
// * Meta + A
|
||||
// * Meta + arrow down
|
||||
// * Meta + arrow left
|
||||
@@ -328,11 +346,17 @@ class DefaultTextEditingShortcuts extends Shortcuts {
|
||||
SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true): ExtendSelectionLeftTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.arrowRight, shift: true): ExtendSelectionRightTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.arrowUp, shift: true): ExtendSelectionUpTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyX, control: true): CutSelectionTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyC, control: true): CopySelectionTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyV, control: true): PasteTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyA, control: true): SelectAllTextIntent(),
|
||||
// The following key combinations have no effect on text editing on this
|
||||
// platform:
|
||||
// * End
|
||||
// * Home
|
||||
// * Meta + X
|
||||
// * Meta + C
|
||||
// * Meta + V
|
||||
// * Meta + A
|
||||
// * Meta + arrow down
|
||||
// * Meta + arrow left
|
||||
@@ -381,9 +405,15 @@ class DefaultTextEditingShortcuts extends Shortcuts {
|
||||
SingleActivator(LogicalKeyboardKey.arrowUp, shift: true): ExtendSelectionUpTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.end, shift: true): ExpandSelectionToEndTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.home, shift: true): ExpandSelectionToStartTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyX, meta: true): CutSelectionTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyC, meta: true): CopySelectionTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyV, meta: true): PasteTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyA, meta: true): SelectAllTextIntent(),
|
||||
// The following key combinations have no effect on text editing on this
|
||||
// platform:
|
||||
// * Control + X
|
||||
// * Control + C
|
||||
// * Control + V
|
||||
// * Control + A
|
||||
// * Control + arrow left
|
||||
// * Control + arrow right
|
||||
@@ -426,9 +456,15 @@ class DefaultTextEditingShortcuts extends Shortcuts {
|
||||
SingleActivator(LogicalKeyboardKey.arrowUp, shift: true): ExtendSelectionUpTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.end, shift: true): ExpandSelectionRightByLineTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.home, shift: true): ExpandSelectionLeftByLineTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyX, control: true): CutSelectionTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyC, control: true): CopySelectionTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyV, control: true): PasteTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyA, control: true): SelectAllTextIntent(),
|
||||
// The following key combinations have no effect on text editing on this
|
||||
// platform:
|
||||
// * Meta + X
|
||||
// * Meta + C
|
||||
// * Meta + V
|
||||
// * Meta + A
|
||||
// * Meta + arrow down
|
||||
// * Meta + arrow left
|
||||
@@ -486,6 +522,12 @@ class DefaultTextEditingShortcuts extends Shortcuts {
|
||||
SingleActivator(LogicalKeyboardKey.end, shift: true): DoNothingAndStopPropagationTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.home, shift: true): DoNothingAndStopPropagationTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.space): DoNothingAndStopPropagationTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyX, control: true): DoNothingAndStopPropagationTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyX, meta: true): DoNothingAndStopPropagationTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyC, control: true): DoNothingAndStopPropagationTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyC, meta: true): DoNothingAndStopPropagationTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyV, control: true): DoNothingAndStopPropagationTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyV, meta: true): DoNothingAndStopPropagationTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyA, control: true): DoNothingAndStopPropagationTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyA, meta: true): DoNothingAndStopPropagationTextIntent(),
|
||||
};
|
||||
|
||||
@@ -274,3 +274,27 @@ class SelectAllTextIntent extends Intent{
|
||||
/// Creates an instance of SelectAllTextIntent.
|
||||
const SelectAllTextIntent();
|
||||
}
|
||||
|
||||
/// An [Intent] to copy selection in the field.
|
||||
///
|
||||
/// {@macro flutter.widgets.TextEditingIntents.seeAlso}
|
||||
class CopySelectionTextIntent extends Intent{
|
||||
/// Creates an instance of CopyTextIntent.
|
||||
const CopySelectionTextIntent();
|
||||
}
|
||||
|
||||
/// An [Intent] to cut selection in the field.
|
||||
///
|
||||
/// {@macro flutter.widgets.TextEditingIntents.seeAlso}
|
||||
class CutSelectionTextIntent extends Intent{
|
||||
/// Creates an instance of CutTextIntent.
|
||||
const CutSelectionTextIntent();
|
||||
}
|
||||
|
||||
/// An [Intent] to paste text from [Clipboard] to the field.
|
||||
///
|
||||
/// {@macro flutter.widgets.TextEditingIntents.seeAlso}
|
||||
class PasteTextIntent extends Intent{
|
||||
/// Creates an instance of PasteTextIntent.
|
||||
const PasteTextIntent();
|
||||
}
|
||||
|
||||
@@ -7215,6 +7215,13 @@ void main() {
|
||||
});
|
||||
|
||||
testWidgets('can change behavior by overriding text editing shortcuts', (WidgetTester tester) async {
|
||||
const Map<SingleActivator, Intent> testShortcuts = <SingleActivator, Intent>{
|
||||
SingleActivator(LogicalKeyboardKey.arrowLeft): MoveSelectionRightTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyX, control: true): MoveSelectionRightTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyC, control: true): MoveSelectionRightTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyV, control: true): MoveSelectionRightTextIntent(),
|
||||
SingleActivator(LogicalKeyboardKey.keyA, control: true): MoveSelectionRightTextIntent(),
|
||||
};
|
||||
final TextEditingController controller = TextEditingController(text: testText);
|
||||
controller.selection = const TextSelection(
|
||||
baseOffset: 0,
|
||||
@@ -7227,9 +7234,7 @@ void main() {
|
||||
child: SizedBox(
|
||||
width: 400,
|
||||
child: Shortcuts(
|
||||
shortcuts: const <ShortcutActivator, Intent>{
|
||||
SingleActivator(LogicalKeyboardKey.arrowLeft): MoveSelectionRightTextIntent(),
|
||||
},
|
||||
shortcuts: testShortcuts,
|
||||
child: EditableText(
|
||||
maxLines: 10,
|
||||
controller: controller,
|
||||
@@ -7256,72 +7261,20 @@ void main() {
|
||||
expect(controller.selection.isCollapsed, isTrue);
|
||||
expect(controller.selection.baseOffset, 1);
|
||||
|
||||
// And the left arrow also moves to the right due to the Shortcuts override.
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft);
|
||||
await tester.pump();
|
||||
expect(controller.selection.isCollapsed, isTrue);
|
||||
expect(controller.selection.baseOffset, 2);
|
||||
// And the testShortcuts also moves to the right due to the Shortcuts override.
|
||||
for (final SingleActivator singleActivator in testShortcuts.keys) {
|
||||
controller.selection = const TextSelection.collapsed(offset: 0);
|
||||
await tester.pump();
|
||||
|
||||
// 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 <ShortcutActivator, Intent>{
|
||||
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.
|
||||
await sendKeys(
|
||||
tester,
|
||||
<LogicalKeyboardKey>[LogicalKeyboardKey.keyA],
|
||||
shortcutModifier: true,
|
||||
targetPlatform: defaultTargetPlatform,
|
||||
);
|
||||
await tester.pump();
|
||||
expect(controller.selection.isCollapsed, isTrue);
|
||||
expect(controller.selection.baseOffset, 1);
|
||||
await sendKeys(
|
||||
tester,
|
||||
<LogicalKeyboardKey>[LogicalKeyboardKey.keyA],
|
||||
shortcutModifier: true,
|
||||
targetPlatform: defaultTargetPlatform,
|
||||
);
|
||||
await tester.pump();
|
||||
expect(controller.selection.isCollapsed, isTrue);
|
||||
expect(controller.selection.baseOffset, 2);
|
||||
await sendKeys(
|
||||
tester,
|
||||
<LogicalKeyboardKey>[singleActivator.trigger],
|
||||
shortcutModifier: singleActivator.control,
|
||||
targetPlatform: defaultTargetPlatform,
|
||||
);
|
||||
expect(controller.selection.isCollapsed, isTrue);
|
||||
expect(controller.selection.baseOffset, 1);
|
||||
}
|
||||
|
||||
// On web, using keyboard for selection is handled by the browser.
|
||||
}, skip: kIsWeb);
|
||||
@@ -7555,20 +7508,35 @@ void main() {
|
||||
extentOffset: 0,
|
||||
affinity: TextAffinity.upstream,
|
||||
);
|
||||
late final bool myIntentWasCalled;
|
||||
bool myIntentWasCalled = false;
|
||||
final _MyMoveSelectionRightTextAction myMoveSelectionRightTextAction =
|
||||
_MyMoveSelectionRightTextAction(
|
||||
onInvoke: () {
|
||||
myIntentWasCalled = true;
|
||||
},
|
||||
);
|
||||
const Iterable<SingleActivator> testSingleActivators = <SingleActivator>{
|
||||
SingleActivator(LogicalKeyboardKey.arrowLeft),
|
||||
SingleActivator(LogicalKeyboardKey.keyX, control: true),
|
||||
SingleActivator(LogicalKeyboardKey.keyC, control: true),
|
||||
SingleActivator(LogicalKeyboardKey.keyV, control: true),
|
||||
SingleActivator(LogicalKeyboardKey.keyA, control: true),
|
||||
};
|
||||
|
||||
final Map<Type, Action<Intent>> testActions = <Type, Action<Intent>>{
|
||||
MoveSelectionLeftTextIntent: myMoveSelectionRightTextAction,
|
||||
CutSelectionTextIntent: myMoveSelectionRightTextAction,
|
||||
CopySelectionTextIntent: myMoveSelectionRightTextAction,
|
||||
PasteTextIntent: myMoveSelectionRightTextAction,
|
||||
SelectAllTextIntent: myMoveSelectionRightTextAction,
|
||||
};
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
home: Align(
|
||||
alignment: Alignment.topLeft,
|
||||
child: SizedBox(
|
||||
width: 400,
|
||||
child: Actions(
|
||||
actions: <Type, Action<Intent>>{
|
||||
MoveSelectionLeftTextIntent: _MyMoveSelectionRightTextAction(
|
||||
onInvoke: () {
|
||||
myIntentWasCalled = true;
|
||||
},
|
||||
),
|
||||
},
|
||||
actions: testActions,
|
||||
child: EditableText(
|
||||
maxLines: 10,
|
||||
controller: controller,
|
||||
@@ -7594,80 +7562,24 @@ void main() {
|
||||
await tester.pump();
|
||||
expect(controller.selection.isCollapsed, isTrue);
|
||||
expect(controller.selection.baseOffset, 1);
|
||||
expect(myIntentWasCalled, isFalse);
|
||||
|
||||
// And the left arrow also moves to the right due to the Actions override.
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft);
|
||||
await tester.pump();
|
||||
expect(controller.selection.isCollapsed, isTrue);
|
||||
expect(controller.selection.baseOffset, 2);
|
||||
expect(myIntentWasCalled, isTrue);
|
||||
// And the testSingleActivators also moves to the right due to the Shortcuts override.
|
||||
for (final SingleActivator singleActivator in testSingleActivators) {
|
||||
myIntentWasCalled = false;
|
||||
controller.selection = const TextSelection.collapsed(offset: 0);
|
||||
await tester.pump();
|
||||
|
||||
// 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: <Type, Action<Intent>>{
|
||||
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.
|
||||
await sendKeys(
|
||||
tester,
|
||||
<LogicalKeyboardKey>[LogicalKeyboardKey.keyA],
|
||||
shortcutModifier: true,
|
||||
targetPlatform: defaultTargetPlatform,
|
||||
);
|
||||
await tester.pump();
|
||||
expect(controller.selection.isCollapsed, isTrue);
|
||||
expect(controller.selection.baseOffset, 1);
|
||||
expect(myIntentWasCalled, isTrue);
|
||||
await sendKeys(
|
||||
tester,
|
||||
<LogicalKeyboardKey>[LogicalKeyboardKey.keyA],
|
||||
shortcutModifier: true,
|
||||
targetPlatform: defaultTargetPlatform,
|
||||
);
|
||||
await tester.pump();
|
||||
expect(controller.selection.isCollapsed, isTrue);
|
||||
expect(controller.selection.baseOffset, 2);
|
||||
await sendKeys(
|
||||
tester,
|
||||
<LogicalKeyboardKey>[singleActivator.trigger],
|
||||
shortcutModifier: singleActivator.control,
|
||||
targetPlatform: defaultTargetPlatform,
|
||||
);
|
||||
expect(controller.selection.isCollapsed, isTrue);
|
||||
expect(controller.selection.baseOffset, 1);
|
||||
expect(myIntentWasCalled, isTrue);
|
||||
}
|
||||
|
||||
// On web, using keyboard for selection is handled by the browser.
|
||||
}, skip: kIsWeb);
|
||||
|
||||
Reference in New Issue
Block a user