1use 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
31pub(crate) fn shortcut(
33 activator: SingleActivator,
34 intent: impl Intent,
35) -> (ShortcutActivatorRef, IntentRef) {
36 (Rc::new(activator), Rc::new(intent))
37}
38
39macro_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#[derive(Debug)]
70pub struct DefaultTextEditingShortcuts {
71 pub key: Option<KeyRef>,
73 pub child: WidgetRef,
75}
76
77impl DefaultTextEditingShortcuts {
78 #[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 pub fn key(mut self, key: KeyRef) -> DefaultTextEditingShortcuts {
90 self.key = Some(key);
91 self
92 }
93
94 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 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 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
153fn 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
192fn common_shortcuts() -> ShortcutMap {
195 let mut shortcuts = ShortcutMap::new();
196 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_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 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 => ExtendSelectionVerticallyToAdjacentPageIntent::new(false, true),
246 PAGE_DOWN => ExtendSelectionVerticallyToAdjacentPageIntent::new(true, true),
247
248 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 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 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 SPACE => DoNothingAndStopPropagationTextIntent::new(),
281 ENTER => DoNothingAndStopPropagationTextIntent::new(),
282 ]
283}
284
285fn 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 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 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
391fn 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
423fn 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 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 SPACE => DoNothingAndStopPropagationTextIntent::new(),
509 ENTER => DoNothingAndStopPropagationTextIntent::new(),
510 ]);
511 shortcuts
512}
513
514fn ios_shortcuts() -> ShortcutMap {
516 mac_shortcuts()
517}
518
519fn 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
551fn 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
585fn 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
658fn 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
674pub 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 "cancelOperation:" => Rc::new(DismissIntent::new()),
770 "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 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 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}