1use std::any::Any;
7
8use waft_embedder::TextRange;
9use waft_gestures::{PointerDownEvent, PointerUpEvent};
10use waft_services::{SelectionChangedCause, TextEditingValue, TextSelection};
11
12use crate::widgets::actions::Intent;
13use crate::widgets::focus_manager::FocusNodeRef;
14
15#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
21pub struct DoNothingAndStopPropagationTextIntent;
22
23impl DoNothingAndStopPropagationTextIntent {
24 pub const fn new() -> DoNothingAndStopPropagationTextIntent {
26 DoNothingAndStopPropagationTextIntent
27 }
28}
29
30impl Intent for DoNothingAndStopPropagationTextIntent {
31 fn as_any(&self) -> &dyn Any {
32 self
33 }
34}
35
36pub trait DirectionalTextEditingIntent: Intent {
39 fn forward(&self) -> bool;
46}
47
48pub trait DirectionalCaretMovementIntent: DirectionalTextEditingIntent {
51 fn collapse_selection(&self) -> bool;
62
63 fn collapse_at_reversal(&self) -> bool;
72
73 fn continues_at_wrap(&self) -> bool;
80}
81
82macro_rules! caret_movement_intent {
86 ($intent:ty, $collapse_at_reversal:expr, $continues_at_wrap:expr) => {
87 impl Intent for $intent {
88 fn as_any(&self) -> &dyn Any {
89 self
90 }
91 }
92
93 impl DirectionalTextEditingIntent for $intent {
94 fn forward(&self) -> bool {
95 self.forward
96 }
97 }
98
99 impl DirectionalCaretMovementIntent for $intent {
100 fn collapse_selection(&self) -> bool {
101 self.collapse_selection
102 }
103
104 fn collapse_at_reversal(&self) -> bool {
105 $collapse_at_reversal
106 }
107
108 fn continues_at_wrap(&self) -> bool {
109 $continues_at_wrap
110 }
111 }
112 };
113}
114
115macro_rules! directional_text_editing_intent {
118 ($intent:ty) => {
119 impl Intent for $intent {
120 fn as_any(&self) -> &dyn Any {
121 self
122 }
123 }
124
125 impl DirectionalTextEditingIntent for $intent {
126 fn forward(&self) -> bool {
127 self.forward
128 }
129 }
130 };
131}
132
133#[derive(Clone, Copy, Debug, PartialEq, Eq)]
139pub struct DeleteCharacterIntent {
140 pub forward: bool,
142}
143
144impl DeleteCharacterIntent {
145 pub const fn new(forward: bool) -> DeleteCharacterIntent {
147 DeleteCharacterIntent { forward }
148 }
149}
150
151directional_text_editing_intent!(DeleteCharacterIntent);
152
153#[derive(Clone, Copy, Debug, PartialEq, Eq)]
156pub struct DeleteToNextWordBoundaryIntent {
157 pub forward: bool,
159}
160
161impl DeleteToNextWordBoundaryIntent {
162 pub const fn new(forward: bool) -> DeleteToNextWordBoundaryIntent {
164 DeleteToNextWordBoundaryIntent { forward }
165 }
166}
167
168directional_text_editing_intent!(DeleteToNextWordBoundaryIntent);
169
170#[derive(Clone, Copy, Debug, PartialEq, Eq)]
173pub struct DeleteToLineBreakIntent {
174 pub forward: bool,
176}
177
178impl DeleteToLineBreakIntent {
179 pub const fn new(forward: bool) -> DeleteToLineBreakIntent {
181 DeleteToLineBreakIntent { forward }
182 }
183}
184
185directional_text_editing_intent!(DeleteToLineBreakIntent);
186
187#[derive(Clone, Copy, Debug, PartialEq, Eq)]
191pub struct ExtendSelectionByCharacterIntent {
192 pub forward: bool,
194 pub collapse_selection: bool,
196}
197
198impl ExtendSelectionByCharacterIntent {
199 pub const fn new(forward: bool, collapse_selection: bool) -> ExtendSelectionByCharacterIntent {
201 ExtendSelectionByCharacterIntent {
202 forward,
203 collapse_selection,
204 }
205 }
206}
207
208caret_movement_intent!(ExtendSelectionByCharacterIntent, false, false);
209
210#[derive(Clone, Copy, Debug, PartialEq, Eq)]
214pub struct ExtendSelectionToNextWordBoundaryIntent {
215 pub forward: bool,
217 pub collapse_selection: bool,
219}
220
221impl ExtendSelectionToNextWordBoundaryIntent {
222 pub const fn new(
224 forward: bool,
225 collapse_selection: bool,
226 ) -> ExtendSelectionToNextWordBoundaryIntent {
227 ExtendSelectionToNextWordBoundaryIntent {
228 forward,
229 collapse_selection,
230 }
231 }
232}
233
234caret_movement_intent!(ExtendSelectionToNextWordBoundaryIntent, false, false);
235
236#[derive(Clone, Copy, Debug, PartialEq, Eq)]
248pub struct ExtendSelectionToNextWordBoundaryOrCaretLocationIntent {
249 pub forward: bool,
251}
252
253impl ExtendSelectionToNextWordBoundaryOrCaretLocationIntent {
254 pub const fn new(forward: bool) -> ExtendSelectionToNextWordBoundaryOrCaretLocationIntent {
256 ExtendSelectionToNextWordBoundaryOrCaretLocationIntent { forward }
257 }
258}
259
260impl Intent for ExtendSelectionToNextWordBoundaryOrCaretLocationIntent {
261 fn as_any(&self) -> &dyn Any {
262 self
263 }
264}
265
266impl DirectionalTextEditingIntent for ExtendSelectionToNextWordBoundaryOrCaretLocationIntent {
267 fn forward(&self) -> bool {
268 self.forward
269 }
270}
271
272impl DirectionalCaretMovementIntent for ExtendSelectionToNextWordBoundaryOrCaretLocationIntent {
273 fn collapse_selection(&self) -> bool {
274 false
275 }
276
277 fn collapse_at_reversal(&self) -> bool {
278 true
279 }
280
281 fn continues_at_wrap(&self) -> bool {
282 false
283 }
284}
285
286#[derive(Clone, Copy, Debug, PartialEq, Eq)]
297pub struct ExpandSelectionToDocumentBoundaryIntent {
298 pub forward: bool,
300}
301
302impl ExpandSelectionToDocumentBoundaryIntent {
303 pub const fn new(forward: bool) -> ExpandSelectionToDocumentBoundaryIntent {
305 ExpandSelectionToDocumentBoundaryIntent { forward }
306 }
307}
308
309impl Intent for ExpandSelectionToDocumentBoundaryIntent {
310 fn as_any(&self) -> &dyn Any {
311 self
312 }
313}
314
315impl DirectionalTextEditingIntent for ExpandSelectionToDocumentBoundaryIntent {
316 fn forward(&self) -> bool {
317 self.forward
318 }
319}
320
321impl DirectionalCaretMovementIntent for ExpandSelectionToDocumentBoundaryIntent {
322 fn collapse_selection(&self) -> bool {
323 false
324 }
325
326 fn collapse_at_reversal(&self) -> bool {
327 false
328 }
329
330 fn continues_at_wrap(&self) -> bool {
331 false
332 }
333}
334
335#[derive(Clone, Copy, Debug, PartialEq, Eq)]
348pub struct ExpandSelectionToLineBreakIntent {
349 pub forward: bool,
351}
352
353impl ExpandSelectionToLineBreakIntent {
354 pub const fn new(forward: bool) -> ExpandSelectionToLineBreakIntent {
356 ExpandSelectionToLineBreakIntent { forward }
357 }
358}
359
360impl Intent for ExpandSelectionToLineBreakIntent {
361 fn as_any(&self) -> &dyn Any {
362 self
363 }
364}
365
366impl DirectionalTextEditingIntent for ExpandSelectionToLineBreakIntent {
367 fn forward(&self) -> bool {
368 self.forward
369 }
370}
371
372impl DirectionalCaretMovementIntent for ExpandSelectionToLineBreakIntent {
373 fn collapse_selection(&self) -> bool {
374 false
375 }
376
377 fn collapse_at_reversal(&self) -> bool {
378 false
379 }
380
381 fn continues_at_wrap(&self) -> bool {
382 false
383 }
384}
385
386#[derive(Clone, Copy, Debug, PartialEq, Eq)]
395pub struct ExtendSelectionToLineBreakIntent {
396 pub forward: bool,
398 pub collapse_selection: bool,
400 pub collapse_at_reversal: bool,
402 pub continues_at_wrap: bool,
404}
405
406impl ExtendSelectionToLineBreakIntent {
407 pub const fn new(forward: bool, collapse_selection: bool) -> ExtendSelectionToLineBreakIntent {
409 ExtendSelectionToLineBreakIntent {
410 forward,
411 collapse_selection,
412 collapse_at_reversal: false,
413 continues_at_wrap: false,
414 }
415 }
416
417 pub const fn collapse_at_reversal(
419 mut self,
420 collapse_at_reversal: bool,
421 ) -> ExtendSelectionToLineBreakIntent {
422 debug_assert!(!(self.collapse_selection && collapse_at_reversal));
423 self.collapse_at_reversal = collapse_at_reversal;
424 self
425 }
426
427 pub const fn continues_at_wrap(
429 mut self,
430 continues_at_wrap: bool,
431 ) -> ExtendSelectionToLineBreakIntent {
432 self.continues_at_wrap = continues_at_wrap;
433 self
434 }
435}
436
437impl Intent for ExtendSelectionToLineBreakIntent {
438 fn as_any(&self) -> &dyn Any {
439 self
440 }
441}
442
443impl DirectionalTextEditingIntent for ExtendSelectionToLineBreakIntent {
444 fn forward(&self) -> bool {
445 self.forward
446 }
447}
448
449impl DirectionalCaretMovementIntent for ExtendSelectionToLineBreakIntent {
450 fn collapse_selection(&self) -> bool {
451 self.collapse_selection
452 }
453
454 fn collapse_at_reversal(&self) -> bool {
455 self.collapse_at_reversal
456 }
457
458 fn continues_at_wrap(&self) -> bool {
459 self.continues_at_wrap
460 }
461}
462
463#[derive(Clone, Copy, Debug, PartialEq, Eq)]
467pub struct ExtendSelectionVerticallyToAdjacentLineIntent {
468 pub forward: bool,
470 pub collapse_selection: bool,
472}
473
474impl ExtendSelectionVerticallyToAdjacentLineIntent {
475 pub const fn new(
477 forward: bool,
478 collapse_selection: bool,
479 ) -> ExtendSelectionVerticallyToAdjacentLineIntent {
480 ExtendSelectionVerticallyToAdjacentLineIntent {
481 forward,
482 collapse_selection,
483 }
484 }
485}
486
487caret_movement_intent!(ExtendSelectionVerticallyToAdjacentLineIntent, false, false);
488
489#[derive(Clone, Copy, Debug, PartialEq, Eq)]
493pub struct ExtendSelectionVerticallyToAdjacentPageIntent {
494 pub forward: bool,
496 pub collapse_selection: bool,
498}
499
500impl ExtendSelectionVerticallyToAdjacentPageIntent {
501 pub const fn new(
503 forward: bool,
504 collapse_selection: bool,
505 ) -> ExtendSelectionVerticallyToAdjacentPageIntent {
506 ExtendSelectionVerticallyToAdjacentPageIntent {
507 forward,
508 collapse_selection,
509 }
510 }
511}
512
513caret_movement_intent!(ExtendSelectionVerticallyToAdjacentPageIntent, false, false);
514
515#[derive(Clone, Copy, Debug, PartialEq, Eq)]
519pub struct ExtendSelectionToNextParagraphBoundaryIntent {
520 pub forward: bool,
522 pub collapse_selection: bool,
524}
525
526impl ExtendSelectionToNextParagraphBoundaryIntent {
527 pub const fn new(
529 forward: bool,
530 collapse_selection: bool,
531 ) -> ExtendSelectionToNextParagraphBoundaryIntent {
532 ExtendSelectionToNextParagraphBoundaryIntent {
533 forward,
534 collapse_selection,
535 }
536 }
537}
538
539caret_movement_intent!(ExtendSelectionToNextParagraphBoundaryIntent, false, false);
540
541#[derive(Clone, Copy, Debug, PartialEq, Eq)]
552pub struct ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent {
553 pub forward: bool,
555}
556
557impl ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent {
558 pub const fn new(forward: bool) -> ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent {
560 ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent { forward }
561 }
562}
563
564impl Intent for ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent {
565 fn as_any(&self) -> &dyn Any {
566 self
567 }
568}
569
570impl DirectionalTextEditingIntent for ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent {
571 fn forward(&self) -> bool {
572 self.forward
573 }
574}
575
576impl DirectionalCaretMovementIntent
577 for ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent
578{
579 fn collapse_selection(&self) -> bool {
580 false
581 }
582
583 fn collapse_at_reversal(&self) -> bool {
584 true
585 }
586
587 fn continues_at_wrap(&self) -> bool {
588 false
589 }
590}
591
592#[derive(Clone, Copy, Debug, PartialEq, Eq)]
600pub struct ExtendSelectionToDocumentBoundaryIntent {
601 pub forward: bool,
603 pub collapse_selection: bool,
605}
606
607impl ExtendSelectionToDocumentBoundaryIntent {
608 pub const fn new(
610 forward: bool,
611 collapse_selection: bool,
612 ) -> ExtendSelectionToDocumentBoundaryIntent {
613 ExtendSelectionToDocumentBoundaryIntent {
614 forward,
615 collapse_selection,
616 }
617 }
618}
619
620caret_movement_intent!(ExtendSelectionToDocumentBoundaryIntent, false, false);
621
622#[derive(Clone, Copy, Debug, PartialEq, Eq)]
625pub struct ScrollToDocumentBoundaryIntent {
626 pub forward: bool,
628}
629
630impl ScrollToDocumentBoundaryIntent {
631 pub const fn new(forward: bool) -> ScrollToDocumentBoundaryIntent {
633 ScrollToDocumentBoundaryIntent { forward }
634 }
635}
636
637directional_text_editing_intent!(ScrollToDocumentBoundaryIntent);
638
639#[derive(Clone, Copy, Debug, PartialEq, Eq)]
641pub struct SelectAllTextIntent {
642 pub cause: SelectionChangedCause,
644}
645
646impl SelectAllTextIntent {
647 pub const fn new(cause: SelectionChangedCause) -> SelectAllTextIntent {
649 SelectAllTextIntent { cause }
650 }
651}
652
653impl Intent for SelectAllTextIntent {
654 fn as_any(&self) -> &dyn Any {
655 self
656 }
657}
658
659#[derive(Clone, Copy, Debug, PartialEq, Eq)]
662pub struct CopySelectionTextIntent {
663 cause: SelectionChangedCause,
664 collapse_selection: bool,
665}
666
667impl CopySelectionTextIntent {
668 pub const COPY: CopySelectionTextIntent = CopySelectionTextIntent {
671 cause: SelectionChangedCause::Keyboard,
672 collapse_selection: false,
673 };
674
675 pub const fn cut(cause: SelectionChangedCause) -> CopySelectionTextIntent {
678 CopySelectionTextIntent {
679 cause,
680 collapse_selection: true,
681 }
682 }
683
684 pub const fn cause(&self) -> SelectionChangedCause {
686 self.cause
687 }
688
689 pub const fn collapse_selection(&self) -> bool {
692 self.collapse_selection
693 }
694}
695
696impl Intent for CopySelectionTextIntent {
697 fn as_any(&self) -> &dyn Any {
698 self
699 }
700}
701
702#[derive(Clone, Copy, Debug, PartialEq, Eq)]
704pub struct PasteTextIntent {
705 pub cause: SelectionChangedCause,
707}
708
709impl PasteTextIntent {
710 pub const fn new(cause: SelectionChangedCause) -> PasteTextIntent {
712 PasteTextIntent { cause }
713 }
714}
715
716impl Intent for PasteTextIntent {
717 fn as_any(&self) -> &dyn Any {
718 self
719 }
720}
721
722#[derive(Clone, Copy, Debug, PartialEq, Eq)]
725pub struct RedoTextIntent {
726 pub cause: SelectionChangedCause,
728}
729
730impl RedoTextIntent {
731 pub const fn new(cause: SelectionChangedCause) -> RedoTextIntent {
733 RedoTextIntent { cause }
734 }
735}
736
737impl Intent for RedoTextIntent {
738 fn as_any(&self) -> &dyn Any {
739 self
740 }
741}
742
743#[derive(Clone, Debug, PartialEq, Eq)]
746pub struct ReplaceTextIntent {
747 pub current_text_editing_value: TextEditingValue,
749 pub replacement_text: String,
751 pub replacement_range: TextRange,
753 pub cause: SelectionChangedCause,
755}
756
757impl ReplaceTextIntent {
758 pub fn new(
760 current_text_editing_value: TextEditingValue,
761 replacement_text: impl Into<String>,
762 replacement_range: TextRange,
763 cause: SelectionChangedCause,
764 ) -> ReplaceTextIntent {
765 ReplaceTextIntent {
766 current_text_editing_value,
767 replacement_text: replacement_text.into(),
768 replacement_range,
769 cause,
770 }
771 }
772}
773
774impl Intent for ReplaceTextIntent {
775 fn as_any(&self) -> &dyn Any {
776 self
777 }
778}
779
780#[derive(Clone, Copy, Debug, PartialEq, Eq)]
783pub struct UndoTextIntent {
784 pub cause: SelectionChangedCause,
786}
787
788impl UndoTextIntent {
789 pub const fn new(cause: SelectionChangedCause) -> UndoTextIntent {
791 UndoTextIntent { cause }
792 }
793}
794
795impl Intent for UndoTextIntent {
796 fn as_any(&self) -> &dyn Any {
797 self
798 }
799}
800
801#[derive(Clone, Debug, PartialEq, Eq)]
804pub struct UpdateSelectionIntent {
805 pub current_text_editing_value: TextEditingValue,
807 pub new_selection: TextSelection,
809 pub cause: SelectionChangedCause,
811}
812
813impl UpdateSelectionIntent {
814 pub fn new(
816 current_text_editing_value: TextEditingValue,
817 new_selection: TextSelection,
818 cause: SelectionChangedCause,
819 ) -> UpdateSelectionIntent {
820 UpdateSelectionIntent {
821 current_text_editing_value,
822 new_selection,
823 cause,
824 }
825 }
826}
827
828impl Intent for UpdateSelectionIntent {
829 fn as_any(&self) -> &dyn Any {
830 self
831 }
832}
833
834#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
837pub struct TransposeCharactersIntent;
838
839impl TransposeCharactersIntent {
840 pub const fn new() -> TransposeCharactersIntent {
842 TransposeCharactersIntent
843 }
844}
845
846impl Intent for TransposeCharactersIntent {
847 fn as_any(&self) -> &dyn Any {
848 self
849 }
850}
851
852#[derive(Clone, Debug)]
865pub struct EditableTextTapOutsideIntent {
866 pub focus_node: FocusNodeRef,
868 pub pointer_down_event: PointerDownEvent,
870}
871
872impl EditableTextTapOutsideIntent {
873 pub fn new(
875 focus_node: FocusNodeRef,
876 pointer_down_event: PointerDownEvent,
877 ) -> EditableTextTapOutsideIntent {
878 EditableTextTapOutsideIntent {
879 focus_node,
880 pointer_down_event,
881 }
882 }
883}
884
885impl Intent for EditableTextTapOutsideIntent {
886 fn as_any(&self) -> &dyn Any {
887 self
888 }
889}
890
891#[derive(Clone, Debug)]
904pub struct EditableTextTapUpOutsideIntent {
905 pub focus_node: FocusNodeRef,
907 pub pointer_up_event: PointerUpEvent,
909}
910
911impl EditableTextTapUpOutsideIntent {
912 pub fn new(
914 focus_node: FocusNodeRef,
915 pointer_up_event: PointerUpEvent,
916 ) -> EditableTextTapUpOutsideIntent {
917 EditableTextTapUpOutsideIntent {
918 focus_node,
919 pointer_up_event,
920 }
921 }
922}
923
924impl Intent for EditableTextTapUpOutsideIntent {
925 fn as_any(&self) -> &dyn Any {
926 self
927 }
928}
929
930#[cfg(test)]
931mod tests {
932 use super::*;
933
934 #[test]
935 fn the_or_caret_location_intents_collapse_at_a_reversal() {
936 let word = ExtendSelectionToNextWordBoundaryOrCaretLocationIntent::new(true);
937 assert!(word.forward());
938 assert!(!word.collapse_selection());
939 assert!(word.collapse_at_reversal());
940 assert!(!word.continues_at_wrap());
941
942 let paragraph = ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent::new(false);
943 assert!(!paragraph.forward());
944 assert!(!paragraph.collapse_selection());
945 assert!(paragraph.collapse_at_reversal());
946 }
947
948 #[test]
949 fn a_line_break_intent_carries_the_two_optional_caret_arguments() {
950 let plain = ExtendSelectionToLineBreakIntent::new(true, true);
951 assert!(!plain.collapse_at_reversal);
952 assert!(!plain.continues_at_wrap);
953
954 let wrapping = ExtendSelectionToLineBreakIntent::new(true, false)
955 .collapse_at_reversal(true)
956 .continues_at_wrap(true);
957 assert!(DirectionalCaretMovementIntent::collapse_at_reversal(
960 &wrapping
961 ));
962 assert!(DirectionalCaretMovementIntent::continues_at_wrap(&wrapping));
963 }
964
965 #[test]
966 fn an_expanding_intent_never_collapses_the_selection() {
967 assert!(!ExpandSelectionToLineBreakIntent::new(true).collapse_selection());
968 assert!(!ExpandSelectionToLineBreakIntent::new(true).collapse_at_reversal());
969 assert!(!ExpandSelectionToDocumentBoundaryIntent::new(false).collapse_selection());
970 assert!(!ExpandSelectionToDocumentBoundaryIntent::new(false).collapse_at_reversal());
971 }
972}