Skip to main content

waft_widgets/widgets/
default_text_editing_shortcuts.rs

1//! Flutter counterpart: `widgets/default_text_editing_shortcuts.dart`.
2
3use std::rc::Rc;
4
5use waft_embedder::TargetPlatform;
6use waft_foundation::{App, K_IS_WEB};
7use waft_painting::AxisDirection;
8use waft_services::SelectionChangedCause;
9
10use crate::framework::{BuildContext, IntoWidget, KeyRef, StatelessWidget, WidgetRef};
11use crate::widgets::actions::{DismissIntent, Intent, IntentRef};
12use crate::widgets::focus_traversal::{NextFocusIntent, PreviousFocusIntent};
13use crate::widgets::scrollable_helpers::{ScrollIncrementType, ScrollIntent};
14use crate::widgets::shortcuts::{
15    LockState, ShortcutActivatorRef, ShortcutMap, Shortcuts, SingleActivator,
16};
17use crate::widgets::text_editing_intents::{
18    CopySelectionTextIntent, DeleteCharacterIntent, DeleteToLineBreakIntent,
19    DeleteToNextWordBoundaryIntent, DoNothingAndStopPropagationTextIntent,
20    ExpandSelectionToDocumentBoundaryIntent, ExpandSelectionToLineBreakIntent,
21    ExtendSelectionByCharacterIntent, ExtendSelectionToDocumentBoundaryIntent,
22    ExtendSelectionToLineBreakIntent, ExtendSelectionToNextParagraphBoundaryIntent,
23    ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent,
24    ExtendSelectionToNextWordBoundaryIntent,
25    ExtendSelectionToNextWordBoundaryOrCaretLocationIntent,
26    ExtendSelectionVerticallyToAdjacentLineIntent, ExtendSelectionVerticallyToAdjacentPageIntent,
27    PasteTextIntent, RedoTextIntent, ScrollToDocumentBoundaryIntent, SelectAllTextIntent,
28    TransposeCharactersIntent, UndoTextIntent,
29};
30
31/// One entry of a shortcut table: Dart's `ShortcutActivator: Intent` pair.
32pub(crate) fn shortcut(
33    activator: SingleActivator,
34    intent: impl Intent,
35) -> (ShortcutActivatorRef, IntentRef) {
36    (Rc::new(activator), Rc::new(intent))
37}
38
39/// A shortcut table in the shape of Dart's map literal: a trigger key with its modifier
40/// setters on the left, the intent it emits on the right.
41macro_rules! shortcut_map {
42    ($($key:ident $(.$modifier:ident($value:expr))* => $intent:expr),* $(,)?) => {
43        vec![$($crate::widgets::default_text_editing_shortcuts::shortcut(
44            $crate::widgets::shortcuts::SingleActivator::new(
45                ::waft_services::LogicalKeyboardKey::$key,
46            )$(.$modifier($value))*,
47            $intent,
48        )),*]
49    };
50}
51
52pub(crate) use shortcut_map;
53
54/// A widget with the shortcuts used for the default text editing behavior.
55///
56/// This default behavior can be overridden by placing a [`Shortcuts`] widget
57/// lower in the widget tree than this. See the `Action` trait for an example
58/// of remapping an [`Intent`] to a custom `Action`.
59///
60/// The [`Shortcuts`] widget usually takes precedence over system keybindings.
61/// Proceed with caution if the shortcut you wish to override is also used by
62/// the system. For example, overriding `LogicalKeyboardKey::BACKSPACE` could
63/// cause CJK input methods to discard more text than they should when the
64/// backspace key is pressed during text composition on iOS.
65///
66/// See also:
67///
68///   * `WidgetsApp`, which creates a [`DefaultTextEditingShortcuts`].
69#[derive(Debug)]
70pub struct DefaultTextEditingShortcuts {
71    /// See [`Widget::key`](crate::Widget::key).
72    pub key: Option<KeyRef>,
73    /// The widget below this widget in the tree.
74    pub child: WidgetRef,
75}
76
77impl DefaultTextEditingShortcuts {
78    /// Creates a [`DefaultTextEditingShortcuts`] widget that provides the default text editing
79    /// shortcuts on the current platform.
80    #[cfg_attr(debug_assertions, track_caller)]
81    pub fn new<K>(child: impl IntoWidget<K>) -> DefaultTextEditingShortcuts {
82        DefaultTextEditingShortcuts {
83            key: None,
84            child: child.into_widget(),
85        }
86    }
87
88    /// Dart `DefaultTextEditingShortcuts(key:)`.
89    pub fn key(mut self, key: KeyRef) -> DefaultTextEditingShortcuts {
90        self.key = Some(key);
91        self
92    }
93
94    /// The shortcuts of the platform the app runs on (Dart's `_shortcuts`).
95    fn shortcuts(app: &App) -> ShortcutMap {
96        match app.platform().target_platform() {
97            TargetPlatform::Android => android_shortcuts(),
98            TargetPlatform::Fuchsia => fuchsia_shortcuts(),
99            TargetPlatform::IOS => ios_shortcuts(),
100            TargetPlatform::Linux => linux_shortcuts(),
101            TargetPlatform::MacOS => mac_shortcuts(),
102            TargetPlatform::Windows => windows_shortcuts(),
103        }
104    }
105
106    /// The shortcuts the platform handles itself, which the framework must hand back to it
107    /// (Dart's `_getDisablingShortcut`): the table for this host, for whether the view's text
108    /// input edits keys itself, and for whether its host provides Edit's commands.
109    fn get_disabling_shortcut(app: &mut App, context: BuildContext) -> Option<ShortcutMap> {
110        let platform = app.platform().target_platform();
111        let view = crate::view::View::maybe_of(app, context);
112        let text_input = view.as_ref().and_then(|view| view.host().text_input());
113        let host_edits = text_input.is_some_and(|input| input.handles_editing_keys());
114        let host_commands = text_input.is_some_and(|input| input.provides_edit_commands());
115        let mut shortcuts = disabling_shortcuts_for(platform, host_edits);
116        if host_commands && !K_IS_WEB {
117            shortcuts
118                .get_or_insert_with(ShortcutMap::new)
119                .append(&mut edit_command_disabling_shortcuts());
120        }
121        shortcuts
122    }
123}
124
125impl StatelessWidget for DefaultTextEditingShortcuts {
126    fn key(&self) -> Option<&KeyRef> {
127        self.key.as_ref()
128    }
129
130    fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
131        let mut result = self.child.clone();
132        if let Some(disabling_shortcut) =
133            DefaultTextEditingShortcuts::get_disabling_shortcut(app, context)
134        {
135            // These shortcuts make sure of the following:
136            //
137            // 1. Shortcuts fired when an EditableText is focused are ignored and
138            //    forwarded to the platform by the EditableText's Actions, because it
139            //    maps DoNothingAndStopPropagationTextIntent to DoNothingAction.
140            // 2. Shortcuts fired when no EditableText is focused will still trigger
141            //    the platform shortcuts assuming DoNothingAndStopPropagationTextIntent is
142            //    unhandled elsewhere.
143            result = Shortcuts::new(disabling_shortcut, result)
144                .debug_label("<Web Disabling Text Editing Shortcuts>")
145                .into_widget();
146        }
147        Shortcuts::new(DefaultTextEditingShortcuts::shortcuts(app), result)
148            .debug_label("<Default Text Editing Shortcuts>")
149            .into_widget()
150    }
151}
152
153/// The shortcuts a platform's own text input handles, which the framework then leaves to
154/// it: on the web, the browser's; elsewhere, only where the host's text input edits keys
155/// itself, as Flutter's macOS and iOS embedders do. Dart steps aside for every macOS and
156/// iOS host, since its embedders for those two are the only ones it has; a host that
157/// reports plain key presses interprets nothing, so stepping aside would leave the keys
158/// to nobody.
159fn disabling_shortcuts_for(platform: TargetPlatform, host_edits: bool) -> Option<ShortcutMap> {
160    if K_IS_WEB {
161        return match platform {
162            TargetPlatform::Linux => {
163                let mut shortcuts = web_disabling_text_shortcuts();
164                for (activator, _) in linux_numpad_shortcuts() {
165                    shortcuts.push((
166                        activator,
167                        Rc::new(DoNothingAndStopPropagationTextIntent::new()) as IntentRef,
168                    ));
169                }
170                Some(shortcuts)
171            }
172            TargetPlatform::Android
173            | TargetPlatform::Fuchsia
174            | TargetPlatform::Windows
175            | TargetPlatform::IOS
176            | TargetPlatform::MacOS => Some(web_disabling_text_shortcuts()),
177        };
178    }
179    if !host_edits {
180        return None;
181    }
182    match platform {
183        TargetPlatform::Android
184        | TargetPlatform::Fuchsia
185        | TargetPlatform::Linux
186        | TargetPlatform::Windows => None,
187        TargetPlatform::IOS => Some(ios_disabling_text_shortcuts()),
188        TargetPlatform::MacOS => Some(mac_disabling_text_shortcuts()),
189    }
190}
191
192/// These shortcuts are shared between all platforms except Apple platforms,
193/// because they use different modifier keys as the line/word modifier.
194fn common_shortcuts() -> ShortcutMap {
195    let mut shortcuts = ShortcutMap::new();
196    // Delete Shortcuts.
197    for press_shift in [true, false] {
198        shortcuts.append(&mut shortcut_map![
199            BACKSPACE.shift(press_shift) => DeleteCharacterIntent::new(false),
200            BACKSPACE.control(true).shift(press_shift) => DeleteToNextWordBoundaryIntent::new(false),
201            BACKSPACE.alt(true).shift(press_shift) => DeleteToLineBreakIntent::new(false),
202            DELETE.control(true).shift(press_shift) => DeleteToNextWordBoundaryIntent::new(true),
203            DELETE.alt(true).shift(press_shift) => DeleteToLineBreakIntent::new(true),
204        ]);
205    }
206    shortcuts.append(&mut shortcut_map![
207        DELETE => DeleteCharacterIntent::new(true),
208
209        // Arrow: Move selection.
210        ARROW_LEFT => ExtendSelectionByCharacterIntent::new(false, true),
211        ARROW_RIGHT => ExtendSelectionByCharacterIntent::new(true, true),
212        ARROW_UP => ExtendSelectionVerticallyToAdjacentLineIntent::new(false, true),
213        ARROW_DOWN => ExtendSelectionVerticallyToAdjacentLineIntent::new(true, true),
214
215        // Shift + Arrow: Extend selection.
216        ARROW_LEFT.shift(true) => ExtendSelectionByCharacterIntent::new(false, false),
217        ARROW_RIGHT.shift(true) => ExtendSelectionByCharacterIntent::new(true, false),
218        ARROW_UP.shift(true) => ExtendSelectionVerticallyToAdjacentLineIntent::new(false, false),
219        ARROW_DOWN.shift(true) => ExtendSelectionVerticallyToAdjacentLineIntent::new(true, false),
220
221        ARROW_LEFT.alt(true) => ExtendSelectionToLineBreakIntent::new(false, true),
222        ARROW_RIGHT.alt(true) => ExtendSelectionToLineBreakIntent::new(true, true),
223        ARROW_UP.alt(true) => ExtendSelectionToDocumentBoundaryIntent::new(false, true),
224        ARROW_DOWN.alt(true) => ExtendSelectionToDocumentBoundaryIntent::new(true, true),
225
226        ARROW_LEFT.shift(true).alt(true) => ExtendSelectionToLineBreakIntent::new(false, false),
227        ARROW_RIGHT.shift(true).alt(true) => ExtendSelectionToLineBreakIntent::new(true, false),
228        ARROW_UP.shift(true).alt(true) => ExtendSelectionToDocumentBoundaryIntent::new(false, false),
229        ARROW_DOWN.shift(true).alt(true) => ExtendSelectionToDocumentBoundaryIntent::new(true, false),
230
231        ARROW_LEFT.control(true) => ExtendSelectionToNextWordBoundaryIntent::new(false, true),
232        ARROW_RIGHT.control(true) => ExtendSelectionToNextWordBoundaryIntent::new(true, true),
233
234        ARROW_LEFT.shift(true).control(true) =>
235            ExtendSelectionToNextWordBoundaryIntent::new(false, false),
236        ARROW_RIGHT.shift(true).control(true) =>
237            ExtendSelectionToNextWordBoundaryIntent::new(true, false),
238
239        ARROW_UP.shift(true).control(true) =>
240            ExtendSelectionToNextParagraphBoundaryIntent::new(false, false),
241        ARROW_DOWN.shift(true).control(true) =>
242            ExtendSelectionToNextParagraphBoundaryIntent::new(true, false),
243
244        // Page Up / Down: Move selection by page.
245        PAGE_UP => ExtendSelectionVerticallyToAdjacentPageIntent::new(false, true),
246        PAGE_DOWN => ExtendSelectionVerticallyToAdjacentPageIntent::new(true, true),
247
248        // Shift + Page Up / Down: Extend selection by page.
249        PAGE_UP.shift(true) => ExtendSelectionVerticallyToAdjacentPageIntent::new(false, false),
250        PAGE_DOWN.shift(true) => ExtendSelectionVerticallyToAdjacentPageIntent::new(true, false),
251    ]);
252    shortcuts
253}
254
255fn clipboard_shortcuts() -> ShortcutMap {
256    shortcut_map![
257        // Xerox/Apple: ^X ^C ^V
258        // -> Standard on Windows
259        // -> Standard on Linux
260        // -> Standard on Mac OS X (with Command as modifier)
261        KEY_X.control(true) => CopySelectionTextIntent::cut(SelectionChangedCause::Keyboard),
262        KEY_C.control(true) => CopySelectionTextIntent::COPY,
263        KEY_V.control(true) => PasteTextIntent::new(SelectionChangedCause::Keyboard),
264
265        // IBM CUA guidelines: Shift-Del Ctrl-Ins Shift-Ins
266        // -> Standard on Windows
267        // -> Standard on Linux (traditionally mapped to the Selection buffer rather than the
268        //                       Clipboard, but the distinction is often no longer present with
269        //                       modern toolkits)
270        // -> Not standard on Mac OS X
271        DELETE.shift(true) => CopySelectionTextIntent::cut(SelectionChangedCause::Keyboard),
272        INSERT.control(true) => CopySelectionTextIntent::COPY,
273        INSERT.shift(true) => PasteTextIntent::new(SelectionChangedCause::Keyboard),
274
275        KEY_A.control(true) => SelectAllTextIntent::new(SelectionChangedCause::Keyboard),
276        KEY_Z.control(true) => UndoTextIntent::new(SelectionChangedCause::Keyboard),
277        KEY_Z.shift(true).control(true) => RedoTextIntent::new(SelectionChangedCause::Keyboard),
278        // These keys should go to the IME when a field is focused, not to other
279        // Shortcuts.
280        SPACE => DoNothingAndStopPropagationTextIntent::new(),
281        ENTER => DoNothingAndStopPropagationTextIntent::new(),
282    ]
283}
284
285/// The following key combinations have no effect on text editing on this
286/// platform:
287///   * Meta + X
288///   * Meta + C
289///   * Meta + V
290///   * Meta + A
291///   * Meta + shift? + Z
292///   * Meta + shift? + arrow down
293///   * Meta + shift? + arrow left
294///   * Meta + shift? + arrow right
295///   * Meta + shift? + arrow up
296///   * Meta + shift? + delete
297///   * Meta + shift? + backspace
298fn android_shortcuts() -> ShortcutMap {
299    let mut shortcuts = common_shortcuts();
300    shortcuts.append(&mut clipboard_shortcuts());
301    shortcuts.append(&mut shortcut_map![
302        HOME => ExtendSelectionToLineBreakIntent::new(false, true).continues_at_wrap(true),
303        END => ExtendSelectionToLineBreakIntent::new(true, true).continues_at_wrap(true),
304        HOME.shift(true) =>
305            ExtendSelectionToLineBreakIntent::new(false, false).continues_at_wrap(true),
306        END.shift(true) =>
307            ExtendSelectionToLineBreakIntent::new(true, false).continues_at_wrap(true),
308        HOME.control(true) => ExtendSelectionToDocumentBoundaryIntent::new(false, true),
309        END.control(true) => ExtendSelectionToDocumentBoundaryIntent::new(true, true),
310        HOME.shift(true).control(true) => ExtendSelectionToDocumentBoundaryIntent::new(false, false),
311        END.shift(true).control(true) => ExtendSelectionToDocumentBoundaryIntent::new(true, false),
312    ]);
313    shortcuts
314}
315
316fn fuchsia_shortcuts() -> ShortcutMap {
317    android_shortcuts()
318}
319
320fn linux_numpad_shortcuts() -> ShortcutMap {
321    shortcut_map![
322        // When numLock is on, numpad keys shortcuts require shift to be pressed too.
323        NUMPAD6.shift(true).num_lock(LockState::Locked) =>
324            ExtendSelectionByCharacterIntent::new(true, false),
325        NUMPAD4.shift(true).num_lock(LockState::Locked) =>
326            ExtendSelectionByCharacterIntent::new(false, false),
327        NUMPAD8.shift(true).num_lock(LockState::Locked) =>
328            ExtendSelectionVerticallyToAdjacentLineIntent::new(false, false),
329        NUMPAD2.shift(true).num_lock(LockState::Locked) =>
330            ExtendSelectionVerticallyToAdjacentLineIntent::new(true, false),
331
332        NUMPAD6.shift(true).control(true).num_lock(LockState::Locked) =>
333            ExtendSelectionToNextWordBoundaryIntent::new(true, false),
334        NUMPAD4.shift(true).control(true).num_lock(LockState::Locked) =>
335            ExtendSelectionToNextWordBoundaryIntent::new(false, false),
336        NUMPAD8.shift(true).control(true).num_lock(LockState::Locked) =>
337            ExtendSelectionToNextParagraphBoundaryIntent::new(false, false),
338        NUMPAD2.shift(true).control(true).num_lock(LockState::Locked) =>
339            ExtendSelectionToNextParagraphBoundaryIntent::new(true, false),
340
341        NUMPAD9.shift(true).num_lock(LockState::Locked) =>
342            ExtendSelectionVerticallyToAdjacentPageIntent::new(false, false),
343        NUMPAD3.shift(true).num_lock(LockState::Locked) =>
344            ExtendSelectionVerticallyToAdjacentPageIntent::new(true, false),
345
346        NUMPAD7.shift(true).num_lock(LockState::Locked) =>
347            ExtendSelectionVerticallyToAdjacentLineIntent::new(false, false),
348        NUMPAD1.shift(true).num_lock(LockState::Locked) =>
349            ExtendSelectionVerticallyToAdjacentLineIntent::new(true, false),
350
351        NUMPAD_DECIMAL.shift(true).num_lock(LockState::Locked) =>
352            DeleteCharacterIntent::new(true),
353        NUMPAD_DECIMAL.shift(true).control(true).num_lock(LockState::Locked) =>
354            DeleteToNextWordBoundaryIntent::new(true),
355
356        // When numLock is off, numpad keys shortcuts require shift not to be pressed.
357        NUMPAD6.num_lock(LockState::Unlocked) =>
358            ExtendSelectionByCharacterIntent::new(true, true),
359        NUMPAD4.num_lock(LockState::Unlocked) =>
360            ExtendSelectionByCharacterIntent::new(false, true),
361        NUMPAD8.num_lock(LockState::Unlocked) =>
362            ExtendSelectionVerticallyToAdjacentLineIntent::new(false, true),
363        NUMPAD2.num_lock(LockState::Unlocked) =>
364            ExtendSelectionVerticallyToAdjacentLineIntent::new(true, true),
365
366        NUMPAD6.control(true).num_lock(LockState::Unlocked) =>
367            ExtendSelectionToNextWordBoundaryIntent::new(true, true),
368        NUMPAD4.control(true).num_lock(LockState::Unlocked) =>
369            ExtendSelectionToNextWordBoundaryIntent::new(false, true),
370        NUMPAD8.control(true).num_lock(LockState::Unlocked) =>
371            ExtendSelectionToNextParagraphBoundaryIntent::new(false, true),
372        NUMPAD2.control(true).num_lock(LockState::Unlocked) =>
373            ExtendSelectionToNextParagraphBoundaryIntent::new(true, true),
374
375        NUMPAD9.num_lock(LockState::Unlocked) =>
376            ExtendSelectionVerticallyToAdjacentPageIntent::new(false, true),
377        NUMPAD3.num_lock(LockState::Unlocked) =>
378            ExtendSelectionVerticallyToAdjacentPageIntent::new(true, true),
379
380        NUMPAD7.num_lock(LockState::Unlocked) =>
381            ExtendSelectionVerticallyToAdjacentLineIntent::new(false, true),
382        NUMPAD1.num_lock(LockState::Unlocked) =>
383            ExtendSelectionVerticallyToAdjacentLineIntent::new(true, true),
384
385        NUMPAD_DECIMAL.num_lock(LockState::Unlocked) => DeleteCharacterIntent::new(true),
386        NUMPAD_DECIMAL.control(true).num_lock(LockState::Unlocked) =>
387            DeleteToNextWordBoundaryIntent::new(true),
388    ]
389}
390
391/// The following key combinations have no effect on text editing on this
392/// platform:
393///   * Control + shift? + end
394///   * Control + shift? + home
395///   * Meta + X
396///   * Meta + C
397///   * Meta + V
398///   * Meta + A
399///   * Meta + shift? + Z
400///   * Meta + shift? + arrow down
401///   * Meta + shift? + arrow left
402///   * Meta + shift? + arrow right
403///   * Meta + shift? + arrow up
404///   * Meta + shift? + delete
405///   * Meta + shift? + backspace
406fn linux_shortcuts() -> ShortcutMap {
407    let mut shortcuts = common_shortcuts();
408    shortcuts.append(&mut clipboard_shortcuts());
409    shortcuts.append(&mut linux_numpad_shortcuts());
410    shortcuts.append(&mut shortcut_map![
411        HOME => ExtendSelectionToLineBreakIntent::new(false, true),
412        END => ExtendSelectionToLineBreakIntent::new(true, true),
413        HOME.shift(true) => ExtendSelectionToLineBreakIntent::new(false, false),
414        END.shift(true) => ExtendSelectionToLineBreakIntent::new(true, false),
415        HOME.control(true) => ExtendSelectionToDocumentBoundaryIntent::new(false, true),
416        END.control(true) => ExtendSelectionToDocumentBoundaryIntent::new(true, true),
417        HOME.shift(true).control(true) => ExtendSelectionToDocumentBoundaryIntent::new(false, false),
418        END.shift(true).control(true) => ExtendSelectionToDocumentBoundaryIntent::new(true, false),
419    ]);
420    shortcuts
421}
422
423/// macOS document shortcuts: <https://support.apple.com/en-us/HT201236>.
424/// The macOS shortcuts uses different word/line modifiers than most other
425/// platforms.
426///
427/// The following key combinations have no effect on text editing on this
428/// platform:
429///   * End
430///   * Home
431///   * Control + shift? + end
432///   * Control + shift? + home
433///   * Control + shift? + Z
434fn mac_shortcuts() -> ShortcutMap {
435    let mut shortcuts = ShortcutMap::new();
436    for press_shift in [true, false] {
437        shortcuts.append(&mut shortcut_map![
438            BACKSPACE.shift(press_shift) => DeleteCharacterIntent::new(false),
439            BACKSPACE.alt(true).shift(press_shift) => DeleteToNextWordBoundaryIntent::new(false),
440            BACKSPACE.meta(true).shift(press_shift) => DeleteToLineBreakIntent::new(false),
441            DELETE.shift(press_shift) => DeleteCharacterIntent::new(true),
442            DELETE.alt(true).shift(press_shift) => DeleteToNextWordBoundaryIntent::new(true),
443            DELETE.meta(true).shift(press_shift) => DeleteToLineBreakIntent::new(true),
444        ]);
445    }
446    shortcuts.append(&mut shortcut_map![
447        ARROW_LEFT => ExtendSelectionByCharacterIntent::new(false, true),
448        ARROW_RIGHT => ExtendSelectionByCharacterIntent::new(true, true),
449        ARROW_UP => ExtendSelectionVerticallyToAdjacentLineIntent::new(false, true),
450        ARROW_DOWN => ExtendSelectionVerticallyToAdjacentLineIntent::new(true, true),
451
452        // Shift + Arrow: Extend selection.
453        ARROW_LEFT.shift(true) => ExtendSelectionByCharacterIntent::new(false, false),
454        ARROW_RIGHT.shift(true) => ExtendSelectionByCharacterIntent::new(true, false),
455        ARROW_UP.shift(true) => ExtendSelectionVerticallyToAdjacentLineIntent::new(false, false),
456        ARROW_DOWN.shift(true) => ExtendSelectionVerticallyToAdjacentLineIntent::new(true, false),
457
458        ARROW_LEFT.alt(true) => ExtendSelectionToNextWordBoundaryIntent::new(false, true),
459        ARROW_RIGHT.alt(true) => ExtendSelectionToNextWordBoundaryIntent::new(true, true),
460        ARROW_UP.alt(true) => ExtendSelectionToLineBreakIntent::new(false, true),
461        ARROW_DOWN.alt(true) => ExtendSelectionToLineBreakIntent::new(true, true),
462
463        ARROW_LEFT.shift(true).alt(true) =>
464            ExtendSelectionToNextWordBoundaryOrCaretLocationIntent::new(false),
465        ARROW_RIGHT.shift(true).alt(true) =>
466            ExtendSelectionToNextWordBoundaryOrCaretLocationIntent::new(true),
467        ARROW_UP.shift(true).alt(true) =>
468            ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent::new(false),
469        ARROW_DOWN.shift(true).alt(true) =>
470            ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent::new(true),
471
472        ARROW_LEFT.meta(true) => ExtendSelectionToLineBreakIntent::new(false, true),
473        ARROW_RIGHT.meta(true) => ExtendSelectionToLineBreakIntent::new(true, true),
474        ARROW_UP.meta(true) => ExtendSelectionToDocumentBoundaryIntent::new(false, true),
475        ARROW_DOWN.meta(true) => ExtendSelectionToDocumentBoundaryIntent::new(true, true),
476
477        ARROW_LEFT.shift(true).meta(true) => ExpandSelectionToLineBreakIntent::new(false),
478        ARROW_RIGHT.shift(true).meta(true) => ExpandSelectionToLineBreakIntent::new(true),
479        ARROW_UP.shift(true).meta(true) => ExpandSelectionToDocumentBoundaryIntent::new(false),
480        ARROW_DOWN.shift(true).meta(true) => ExpandSelectionToDocumentBoundaryIntent::new(true),
481
482        KEY_T.control(true) => TransposeCharactersIntent::new(),
483
484        HOME => ScrollToDocumentBoundaryIntent::new(false),
485        END => ScrollToDocumentBoundaryIntent::new(true),
486        HOME.shift(true) => ExpandSelectionToDocumentBoundaryIntent::new(false),
487        END.shift(true) => ExpandSelectionToDocumentBoundaryIntent::new(true),
488
489        PAGE_UP => ScrollIntent::new(AxisDirection::Up).r#type(ScrollIncrementType::Page),
490        PAGE_DOWN => ScrollIntent::new(AxisDirection::Down).r#type(ScrollIncrementType::Page),
491        PAGE_UP.shift(true) => ExtendSelectionVerticallyToAdjacentPageIntent::new(false, false),
492        PAGE_DOWN.shift(true) => ExtendSelectionVerticallyToAdjacentPageIntent::new(true, false),
493
494        KEY_X.meta(true) => CopySelectionTextIntent::cut(SelectionChangedCause::Keyboard),
495        KEY_C.meta(true) => CopySelectionTextIntent::COPY,
496        KEY_V.meta(true) => PasteTextIntent::new(SelectionChangedCause::Keyboard),
497        KEY_A.meta(true) => SelectAllTextIntent::new(SelectionChangedCause::Keyboard),
498        KEY_Z.meta(true) => UndoTextIntent::new(SelectionChangedCause::Keyboard),
499        KEY_Z.shift(true).meta(true) => RedoTextIntent::new(SelectionChangedCause::Keyboard),
500        KEY_E.control(true) => ExtendSelectionToLineBreakIntent::new(true, true),
501        KEY_A.control(true) => ExtendSelectionToLineBreakIntent::new(false, true),
502        KEY_F.control(true) => ExtendSelectionByCharacterIntent::new(true, true),
503        KEY_B.control(true) => ExtendSelectionByCharacterIntent::new(false, true),
504        KEY_N.control(true) => ExtendSelectionVerticallyToAdjacentLineIntent::new(true, true),
505        KEY_P.control(true) => ExtendSelectionVerticallyToAdjacentLineIntent::new(false, true),
506        // These keys should go to the IME when a field is focused, not to other
507        // Shortcuts.
508        SPACE => DoNothingAndStopPropagationTextIntent::new(),
509        ENTER => DoNothingAndStopPropagationTextIntent::new(),
510    ]);
511    shortcuts
512}
513
514/// There is no complete documentation of iOS shortcuts: use macOS ones.
515fn ios_shortcuts() -> ShortcutMap {
516    mac_shortcuts()
517}
518
519/// The following key combinations have no effect on text editing on this
520/// platform:
521///   * Meta + X
522///   * Meta + C
523///   * Meta + V
524///   * Meta + A
525///   * Meta + shift? + arrow down
526///   * Meta + shift? + arrow left
527///   * Meta + shift? + arrow right
528///   * Meta + shift? + arrow up
529///   * Meta + delete
530///   * Meta + backspace
531fn windows_shortcuts() -> ShortcutMap {
532    let mut shortcuts = common_shortcuts();
533    shortcuts.append(&mut clipboard_shortcuts());
534    shortcuts.append(&mut shortcut_map![
535        PAGE_UP => ExtendSelectionVerticallyToAdjacentPageIntent::new(false, true),
536        PAGE_DOWN => ExtendSelectionVerticallyToAdjacentPageIntent::new(true, true),
537        HOME => ExtendSelectionToLineBreakIntent::new(false, true).continues_at_wrap(true),
538        END => ExtendSelectionToLineBreakIntent::new(true, true).continues_at_wrap(true),
539        HOME.shift(true) =>
540            ExtendSelectionToLineBreakIntent::new(false, false).continues_at_wrap(true),
541        END.shift(true) =>
542            ExtendSelectionToLineBreakIntent::new(true, false).continues_at_wrap(true),
543        HOME.control(true) => ExtendSelectionToDocumentBoundaryIntent::new(false, true),
544        END.control(true) => ExtendSelectionToDocumentBoundaryIntent::new(true, true),
545        HOME.shift(true).control(true) => ExtendSelectionToDocumentBoundaryIntent::new(false, false),
546        END.shift(true).control(true) => ExtendSelectionToDocumentBoundaryIntent::new(true, false),
547    ]);
548    shortcuts
549}
550
551/// Web handles its text selection natively and doesn't use any of these
552/// shortcuts in Flutter.
553fn web_disabling_text_shortcuts() -> ShortcutMap {
554    let mut shortcuts = ShortcutMap::new();
555    for press_shift in [true, false] {
556        shortcuts.append(&mut shortcut_map![
557            BACKSPACE.shift(press_shift) => DoNothingAndStopPropagationTextIntent::new(),
558            DELETE.shift(press_shift) => DoNothingAndStopPropagationTextIntent::new(),
559            BACKSPACE.alt(true).shift(press_shift) => DoNothingAndStopPropagationTextIntent::new(),
560            DELETE.alt(true).shift(press_shift) => DoNothingAndStopPropagationTextIntent::new(),
561            BACKSPACE.control(true).shift(press_shift) =>
562                DoNothingAndStopPropagationTextIntent::new(),
563            DELETE.control(true).shift(press_shift) => DoNothingAndStopPropagationTextIntent::new(),
564            BACKSPACE.meta(true).shift(press_shift) => DoNothingAndStopPropagationTextIntent::new(),
565            DELETE.meta(true).shift(press_shift) => DoNothingAndStopPropagationTextIntent::new(),
566        ]);
567    }
568    shortcuts.append(&mut common_disabling_text_shortcuts());
569    for (activator, _) in clipboard_shortcuts() {
570        shortcuts.push((
571            activator,
572            Rc::new(DoNothingAndStopPropagationTextIntent::new()) as IntentRef,
573        ));
574    }
575    shortcuts.append(&mut shortcut_map![
576        KEY_X.meta(true) => DoNothingAndStopPropagationTextIntent::new(),
577        KEY_C.meta(true) => DoNothingAndStopPropagationTextIntent::new(),
578        KEY_V.meta(true) => DoNothingAndStopPropagationTextIntent::new(),
579        KEY_A.control(true) => DoNothingAndStopPropagationTextIntent::new(),
580        KEY_A.meta(true) => DoNothingAndStopPropagationTextIntent::new(),
581    ]);
582    shortcuts
583}
584
585/// The keys of Edit's cut, copy and paste, which a host that provides those commands receives
586/// once the field leaves them, and sends back as a `clipboard_event`: the clipboard part of
587/// [`web_disabling_text_shortcuts`], select all aside, which the field keeps.
588fn edit_command_disabling_shortcuts() -> ShortcutMap {
589    let mut shortcuts = ShortcutMap::new();
590    for (activator, intent) in clipboard_shortcuts() {
591        if !intent.as_any().is::<SelectAllTextIntent>() {
592            shortcuts.push((
593                activator,
594                Rc::new(DoNothingAndStopPropagationTextIntent::new()) as IntentRef,
595            ));
596        }
597    }
598    shortcuts.append(&mut shortcut_map![
599        KEY_X.meta(true) => DoNothingAndStopPropagationTextIntent::new(),
600        KEY_C.meta(true) => DoNothingAndStopPropagationTextIntent::new(),
601        KEY_V.meta(true) => DoNothingAndStopPropagationTextIntent::new(),
602    ]);
603    shortcuts
604}
605
606fn common_disabling_text_shortcuts() -> ShortcutMap {
607    shortcut_map![
608        ARROW_DOWN.alt(true) => DoNothingAndStopPropagationTextIntent::new(),
609        ARROW_LEFT.alt(true) => DoNothingAndStopPropagationTextIntent::new(),
610        ARROW_RIGHT.alt(true) => DoNothingAndStopPropagationTextIntent::new(),
611        ARROW_UP.alt(true) => DoNothingAndStopPropagationTextIntent::new(),
612        ARROW_DOWN.meta(true) => DoNothingAndStopPropagationTextIntent::new(),
613        ARROW_LEFT.meta(true) => DoNothingAndStopPropagationTextIntent::new(),
614        ARROW_RIGHT.meta(true) => DoNothingAndStopPropagationTextIntent::new(),
615        ARROW_UP.meta(true) => DoNothingAndStopPropagationTextIntent::new(),
616        ARROW_DOWN => DoNothingAndStopPropagationTextIntent::new(),
617        ARROW_LEFT => DoNothingAndStopPropagationTextIntent::new(),
618        ARROW_RIGHT => DoNothingAndStopPropagationTextIntent::new(),
619        ARROW_UP => DoNothingAndStopPropagationTextIntent::new(),
620        ARROW_LEFT.control(true) => DoNothingAndStopPropagationTextIntent::new(),
621        ARROW_RIGHT.control(true) => DoNothingAndStopPropagationTextIntent::new(),
622        ARROW_LEFT.shift(true).control(true) => DoNothingAndStopPropagationTextIntent::new(),
623        ARROW_RIGHT.shift(true).control(true) => DoNothingAndStopPropagationTextIntent::new(),
624        SPACE => DoNothingAndStopPropagationTextIntent::new(),
625        ENTER => DoNothingAndStopPropagationTextIntent::new(),
626    ]
627}
628
629fn mac_disabling_text_shortcuts() -> ShortcutMap {
630    let mut shortcuts = common_disabling_text_shortcuts();
631    shortcuts.append(&mut ios_disabling_text_shortcuts());
632    shortcuts.append(&mut shortcut_map![
633        ESCAPE => DoNothingAndStopPropagationTextIntent::new(),
634        TAB => DoNothingAndStopPropagationTextIntent::new(),
635        TAB.shift(true) => DoNothingAndStopPropagationTextIntent::new(),
636        ARROW_DOWN.shift(true).alt(true) => DoNothingAndStopPropagationTextIntent::new(),
637        ARROW_UP.shift(true).alt(true) => DoNothingAndStopPropagationTextIntent::new(),
638        ARROW_LEFT.shift(true) => DoNothingAndStopPropagationTextIntent::new(),
639        ARROW_RIGHT.shift(true) => DoNothingAndStopPropagationTextIntent::new(),
640        ARROW_LEFT.shift(true).alt(true) => DoNothingAndStopPropagationTextIntent::new(),
641        ARROW_RIGHT.shift(true).alt(true) => DoNothingAndStopPropagationTextIntent::new(),
642        ARROW_LEFT.shift(true).meta(true) => DoNothingAndStopPropagationTextIntent::new(),
643        ARROW_RIGHT.shift(true).meta(true) => DoNothingAndStopPropagationTextIntent::new(),
644        PAGE_UP => DoNothingAndStopPropagationTextIntent::new(),
645        PAGE_DOWN => DoNothingAndStopPropagationTextIntent::new(),
646        END => DoNothingAndStopPropagationTextIntent::new(),
647        HOME => DoNothingAndStopPropagationTextIntent::new(),
648        PAGE_UP.shift(true) => DoNothingAndStopPropagationTextIntent::new(),
649        PAGE_DOWN.shift(true) => DoNothingAndStopPropagationTextIntent::new(),
650        END.shift(true) => DoNothingAndStopPropagationTextIntent::new(),
651        HOME.shift(true) => DoNothingAndStopPropagationTextIntent::new(),
652        END.control(true) => DoNothingAndStopPropagationTextIntent::new(),
653        HOME.control(true) => DoNothingAndStopPropagationTextIntent::new(),
654    ]);
655    shortcuts
656}
657
658/// Hand backspace/delete events that do not depend on text layout (delete
659/// character and delete to the next word) back to the IME to allow it to
660/// update composing text properly.
661fn ios_disabling_text_shortcuts() -> ShortcutMap {
662    shortcut_map![
663        BACKSPACE => DoNothingAndStopPropagationTextIntent::new(),
664        BACKSPACE.shift(true) => DoNothingAndStopPropagationTextIntent::new(),
665        DELETE => DoNothingAndStopPropagationTextIntent::new(),
666        DELETE.shift(true) => DoNothingAndStopPropagationTextIntent::new(),
667        BACKSPACE.alt(true).shift(true) => DoNothingAndStopPropagationTextIntent::new(),
668        BACKSPACE.alt(true) => DoNothingAndStopPropagationTextIntent::new(),
669        DELETE.alt(true).shift(true) => DoNothingAndStopPropagationTextIntent::new(),
670        DELETE.alt(true) => DoNothingAndStopPropagationTextIntent::new(),
671    ]
672}
673
674/// Maps the selector from `NSStandardKeyBindingResponding` to the [`Intent`] if the
675/// selector is recognized.
676pub fn intent_for_macos_selector(selector_name: &str) -> Option<IntentRef> {
677    let intent: IntentRef = match selector_name {
678        "deleteBackward:" => Rc::new(DeleteCharacterIntent::new(false)),
679        "deleteWordBackward:" => Rc::new(DeleteToNextWordBoundaryIntent::new(false)),
680        "deleteToBeginningOfLine:" => Rc::new(DeleteToLineBreakIntent::new(false)),
681        "deleteForward:" => Rc::new(DeleteCharacterIntent::new(true)),
682        "deleteWordForward:" => Rc::new(DeleteToNextWordBoundaryIntent::new(true)),
683        "deleteToEndOfLine:" => Rc::new(DeleteToLineBreakIntent::new(true)),
684
685        "moveLeft:" => Rc::new(ExtendSelectionByCharacterIntent::new(false, true)),
686        "moveRight:" => Rc::new(ExtendSelectionByCharacterIntent::new(true, true)),
687        "moveForward:" => Rc::new(ExtendSelectionByCharacterIntent::new(true, true)),
688        "moveBackward:" => Rc::new(ExtendSelectionByCharacterIntent::new(false, true)),
689
690        "moveUp:" => Rc::new(ExtendSelectionVerticallyToAdjacentLineIntent::new(
691            false, true,
692        )),
693        "moveDown:" => Rc::new(ExtendSelectionVerticallyToAdjacentLineIntent::new(
694            true, true,
695        )),
696
697        "moveLeftAndModifySelection:" => {
698            Rc::new(ExtendSelectionByCharacterIntent::new(false, false))
699        }
700        "moveRightAndModifySelection:" => {
701            Rc::new(ExtendSelectionByCharacterIntent::new(true, false))
702        }
703        "moveUpAndModifySelection:" => Rc::new(ExtendSelectionVerticallyToAdjacentLineIntent::new(
704            false, false,
705        )),
706        "moveDownAndModifySelection:" => Rc::new(
707            ExtendSelectionVerticallyToAdjacentLineIntent::new(true, false),
708        ),
709
710        "moveWordLeft:" => Rc::new(ExtendSelectionToNextWordBoundaryIntent::new(false, true)),
711        "moveWordRight:" => Rc::new(ExtendSelectionToNextWordBoundaryIntent::new(true, true)),
712        "moveToBeginningOfParagraph:" => {
713            Rc::new(ExtendSelectionToLineBreakIntent::new(false, true))
714        }
715        "moveToEndOfParagraph:" => Rc::new(ExtendSelectionToLineBreakIntent::new(true, true)),
716
717        "moveWordLeftAndModifySelection:" => Rc::new(
718            ExtendSelectionToNextWordBoundaryOrCaretLocationIntent::new(false),
719        ),
720        "moveWordRightAndModifySelection:" => Rc::new(
721            ExtendSelectionToNextWordBoundaryOrCaretLocationIntent::new(true),
722        ),
723        "moveParagraphBackwardAndModifySelection:" => {
724            Rc::new(ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent::new(false))
725        }
726        "moveParagraphForwardAndModifySelection:" => {
727            Rc::new(ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent::new(true))
728        }
729
730        "moveToLeftEndOfLine:" => Rc::new(ExtendSelectionToLineBreakIntent::new(false, true)),
731        "moveToRightEndOfLine:" => Rc::new(ExtendSelectionToLineBreakIntent::new(true, true)),
732        "moveToBeginningOfDocument:" => {
733            Rc::new(ExtendSelectionToDocumentBoundaryIntent::new(false, true))
734        }
735        "moveToEndOfDocument:" => Rc::new(ExtendSelectionToDocumentBoundaryIntent::new(true, true)),
736
737        "moveToLeftEndOfLineAndModifySelection:" => {
738            Rc::new(ExpandSelectionToLineBreakIntent::new(false))
739        }
740        "moveToRightEndOfLineAndModifySelection:" => {
741            Rc::new(ExpandSelectionToLineBreakIntent::new(true))
742        }
743        "moveToBeginningOfDocumentAndModifySelection:" => {
744            Rc::new(ExpandSelectionToDocumentBoundaryIntent::new(false))
745        }
746        "moveToEndOfDocumentAndModifySelection:" => {
747            Rc::new(ExpandSelectionToDocumentBoundaryIntent::new(true))
748        }
749
750        "transpose:" => Rc::new(TransposeCharactersIntent::new()),
751
752        "scrollToBeginningOfDocument:" => Rc::new(ScrollToDocumentBoundaryIntent::new(false)),
753        "scrollToEndOfDocument:" => Rc::new(ScrollToDocumentBoundaryIntent::new(true)),
754
755        "scrollPageUp:" => {
756            Rc::new(ScrollIntent::new(AxisDirection::Up).r#type(ScrollIncrementType::Page))
757        }
758        "scrollPageDown:" => {
759            Rc::new(ScrollIntent::new(AxisDirection::Down).r#type(ScrollIncrementType::Page))
760        }
761        "pageUpAndModifySelection:" => Rc::new(ExtendSelectionVerticallyToAdjacentPageIntent::new(
762            false, false,
763        )),
764        "pageDownAndModifySelection:" => Rc::new(
765            ExtendSelectionVerticallyToAdjacentPageIntent::new(true, false),
766        ),
767
768        // Escape key when there's no IME selection popup.
769        "cancelOperation:" => Rc::new(DismissIntent::new()),
770        // Tab when there's no IME selection.
771        "insertTab:" => Rc::new(NextFocusIntent::new()),
772        "insertBacktab:" => Rc::new(PreviousFocusIntent::new()),
773
774        _ => return None,
775    };
776    Some(intent)
777}
778
779#[cfg(test)]
780mod tests {
781
782    use super::*;
783    use crate::binding::WidgetsBinding;
784    use crate::framework::{AnyElement, downcast_widget};
785    use crate::widgets::basic::SizedBox;
786    use crate::widgets::focus_manager::tests::{
787        tester_whose_host_edits, tester_whose_host_provides_edit_commands,
788    };
789
790    /// How the copy shortcut of a platform's table is described.
791    fn copy_shortcut(shortcuts: &ShortcutMap) -> String {
792        let (activator, _) = shortcuts
793            .iter()
794            .find(|(_, intent)| {
795                intent
796                    .as_any()
797                    .downcast_ref::<CopySelectionTextIntent>()
798                    .is_some_and(|copy| !copy.collapse_selection())
799            })
800            .expect("a copy shortcut");
801        activator.debug_describe_keys()
802    }
803
804    /// The debug label and map of every [`Shortcuts`] widget of the mounted tree, outermost
805    /// first.
806    fn mounted_shortcuts(app: &mut App) -> Vec<(String, ShortcutMap)> {
807        let root = WidgetsBinding::instance(app)
808            .root_element(app)
809            .expect("a mounted tree");
810        let mut all: Vec<AnyElement> = vec![root];
811        let mut visited = 0;
812        while visited < all.len() {
813            let element = all[visited];
814            element.visit_children(app, &mut |child| all.push(child));
815            visited += 1;
816        }
817        let mut found = Vec::new();
818        for element in all {
819            let app: &App = app;
820            if let Some(widget) = downcast_widget::<Shortcuts>(&**element.widget(app)) {
821                let label = widget.debug_label.clone().unwrap_or_default();
822                found.push((label, widget.get_shortcuts(app)));
823            }
824        }
825        found
826    }
827
828    #[test]
829    fn every_platform_table_binds_its_own_copy_shortcut_to_the_copy_intent() {
830        assert_eq!(copy_shortcut(&mac_shortcuts()), "Meta + Key C");
831        assert_eq!(copy_shortcut(&ios_shortcuts()), "Meta + Key C");
832        assert_eq!(copy_shortcut(&android_shortcuts()), "Control + Key C");
833        assert_eq!(copy_shortcut(&fuchsia_shortcuts()), "Control + Key C");
834        assert_eq!(copy_shortcut(&linux_shortcuts()), "Control + Key C");
835        assert_eq!(copy_shortcut(&windows_shortcuts()), "Control + Key C");
836    }
837
838    #[test]
839    fn the_cut_intent_collapses_the_selection_and_the_copy_intent_does_not() {
840        let cut = CopySelectionTextIntent::cut(SelectionChangedCause::Keyboard);
841        assert!(cut.collapse_selection());
842        assert_eq!(cut.cause(), SelectionChangedCause::Keyboard);
843        assert!(!CopySelectionTextIntent::COPY.collapse_selection());
844        assert_eq!(
845            CopySelectionTextIntent::COPY.cause(),
846            SelectionChangedCause::Keyboard
847        );
848    }
849
850    #[test]
851    fn an_apple_platform_hands_the_keys_it_handles_itself_back_to_the_ime() {
852        let disabling = disabling_shortcuts_for(TargetPlatform::MacOS, true)
853            .expect("macOS disables the shortcuts the platform handles");
854        assert!(disabling.iter().all(|(_, intent)| {
855            intent
856                .as_any()
857                .is::<DoNothingAndStopPropagationTextIntent>()
858        }));
859        assert!(
860            disabling_shortcuts_for(TargetPlatform::Linux, true).is_none(),
861            "a non-Apple, non-web platform disables nothing"
862        );
863    }
864
865    #[test]
866    fn a_host_that_reports_plain_keys_keeps_every_editing_key() {
867        assert!(
868            disabling_shortcuts_for(TargetPlatform::MacOS, false).is_none(),
869            "nothing interprets those keys, so standing aside would leave them to nobody"
870        );
871    }
872
873    #[test]
874    fn the_widget_wraps_its_child_in_the_platform_table_and_the_disabling_table() {
875        let tester = &mut tester_whose_host_edits();
876        tester.pump_widget(DefaultTextEditingShortcuts::new(SizedBox::shrink()).into_widget());
877        let mut app = tester.cell.borrow_mut();
878        let shortcuts = mounted_shortcuts(&mut app);
879        let labels: Vec<&str> = shortcuts.iter().map(|(label, _)| label.as_str()).collect();
880        assert_eq!(
881            labels,
882            [
883                "<Default Text Editing Shortcuts>",
884                "<Web Disabling Text Editing Shortcuts>",
885            ],
886            "the disabling table is nested inside, so it is found first"
887        );
888        assert_eq!(copy_shortcut(&shortcuts[0].1), "Meta + Key C");
889    }
890
891    #[test]
892    fn a_host_that_provides_edit_commands_gets_the_cut_copy_and_paste_keys_and_no_other() {
893        let tester = &mut tester_whose_host_provides_edit_commands();
894        tester.pump_widget(DefaultTextEditingShortcuts::new(SizedBox::shrink()).into_widget());
895        let mut app = tester.cell.borrow_mut();
896        let shortcuts = mounted_shortcuts(&mut app);
897        let (label, disabling) = &shortcuts[1];
898        assert_eq!(label, "<Web Disabling Text Editing Shortcuts>");
899        let left: Vec<String> = disabling
900            .iter()
901            .map(|(activator, _)| activator.debug_describe_keys())
902            .collect();
903        for keys in [
904            "Meta + Key C",
905            "Meta + Key X",
906            "Meta + Key V",
907            "Control + Key C",
908        ] {
909            assert!(
910                left.iter().any(|left| left == keys),
911                "{keys} left in {left:?}"
912            );
913        }
914        for keys in ["Meta + Key A", "Control + Key A", "Arrow Left"] {
915            assert!(!left.iter().any(|left| left == keys), "{keys} kept");
916        }
917    }
918}