Skip to main content

waft_widgets/widgets/
text_editing_intents.rs

1//! Flutter counterpart: `widgets/text_editing_intents.dart`.
2//!
3//! The [`Intent`]s a text field's key bindings emit. The actions that consume them live in
4//! `editable_text.dart`, which waits; `DefaultTextEditingShortcuts` binds them.
5
6use 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/// An [`Intent`] to send the event straight to the engine.
16///
17/// See also:
18///
19///   * `DefaultTextEditingShortcuts`, which triggers this [`Intent`].
20#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
21pub struct DoNothingAndStopPropagationTextIntent;
22
23impl DoNothingAndStopPropagationTextIntent {
24    /// Creates an instance of [`DoNothingAndStopPropagationTextIntent`].
25    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
36/// A text editing related [`Intent`] that performs an operation towards a given
37/// direction of the current caret location.
38pub trait DirectionalTextEditingIntent: Intent {
39    /// Whether the input field, if applicable, should perform the text editing
40    /// operation from the current caret location towards the end of the document.
41    ///
42    /// Unless otherwise specified by the recipient of this intent, this parameter
43    /// uses the logical order of characters in the string to determine the
44    /// direction, and is not affected by the writing direction of the text.
45    fn forward(&self) -> bool;
46}
47
48/// A [`DirectionalTextEditingIntent`] that moves the caret or the selection to a
49/// new location.
50pub trait DirectionalCaretMovementIntent: DirectionalTextEditingIntent {
51    /// Whether this [`Intent`] should make the selection collapsed (so it becomes a
52    /// caret), after the movement.
53    ///
54    /// When [`collapse_selection`](Self::collapse_selection) is false, the input field
55    /// typically only moves the current `TextSelection.extent` to the new location, while
56    /// maintains the current `TextSelection.base` location.
57    ///
58    /// When [`collapse_selection`](Self::collapse_selection) is true, the input field
59    /// typically should move both the `TextSelection.base` and the `TextSelection.extent`
60    /// to the new location.
61    fn collapse_selection(&self) -> bool;
62
63    /// Whether to collapse the selection when it would otherwise reverse order.
64    ///
65    /// For example, consider when forward is true and the extent is before the
66    /// base. If `collapse_at_reversal` is true, then this will cause the selection to
67    /// collapse at the base. If it's false, then the extent will be placed at the
68    /// linebreak, reversing the order of base and offset.
69    ///
70    /// Cannot be true when [`collapse_selection`](Self::collapse_selection) is true.
71    fn collapse_at_reversal(&self) -> bool;
72
73    /// Whether or not to continue to the next line at a wordwrap.
74    ///
75    /// If true, when an [`Intent`] to go to the beginning/end of a wordwrapped line
76    /// is received and the selection is already at the beginning/end of the line,
77    /// then the selection will be moved to the next/previous line. If false, the
78    /// selection will remain at the wordwrap.
79    fn continues_at_wrap(&self) -> bool;
80}
81
82/// Writes the two traits for an intent whose caret movement fields are the four Dart
83/// defines, with `collapse_at_reversal` and `continues_at_wrap` fixed to the values the
84/// Dart constructor passes to `super`.
85macro_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
115/// Writes [`Intent`] and [`DirectionalTextEditingIntent`] for an intent whose only field is
116/// `forward`.
117macro_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/// Deletes the character before or after the caret location, based on whether
134/// `forward` is true.
135///
136/// Typically a text field will not respond to this intent if it has no active
137/// caret (`TextSelection::is_valid` is false for the current selection).
138#[derive(Clone, Copy, Debug, PartialEq, Eq)]
139pub struct DeleteCharacterIntent {
140    /// See [`DirectionalTextEditingIntent::forward`].
141    pub forward: bool,
142}
143
144impl DeleteCharacterIntent {
145    /// Creates a [`DeleteCharacterIntent`].
146    pub const fn new(forward: bool) -> DeleteCharacterIntent {
147        DeleteCharacterIntent { forward }
148    }
149}
150
151directional_text_editing_intent!(DeleteCharacterIntent);
152
153/// Deletes from the current caret location to the previous or next word
154/// boundary, based on whether `forward` is true.
155#[derive(Clone, Copy, Debug, PartialEq, Eq)]
156pub struct DeleteToNextWordBoundaryIntent {
157    /// See [`DirectionalTextEditingIntent::forward`].
158    pub forward: bool,
159}
160
161impl DeleteToNextWordBoundaryIntent {
162    /// Creates a [`DeleteToNextWordBoundaryIntent`].
163    pub const fn new(forward: bool) -> DeleteToNextWordBoundaryIntent {
164        DeleteToNextWordBoundaryIntent { forward }
165    }
166}
167
168directional_text_editing_intent!(DeleteToNextWordBoundaryIntent);
169
170/// Deletes from the current caret location to the previous or next soft or hard
171/// line break, based on whether `forward` is true.
172#[derive(Clone, Copy, Debug, PartialEq, Eq)]
173pub struct DeleteToLineBreakIntent {
174    /// See [`DirectionalTextEditingIntent::forward`].
175    pub forward: bool,
176}
177
178impl DeleteToLineBreakIntent {
179    /// Creates a [`DeleteToLineBreakIntent`].
180    pub const fn new(forward: bool) -> DeleteToLineBreakIntent {
181        DeleteToLineBreakIntent { forward }
182    }
183}
184
185directional_text_editing_intent!(DeleteToLineBreakIntent);
186
187/// Extends, or moves the current selection from the current
188/// `TextSelection.extent` position to the previous or the next character
189/// boundary.
190#[derive(Clone, Copy, Debug, PartialEq, Eq)]
191pub struct ExtendSelectionByCharacterIntent {
192    /// See [`DirectionalTextEditingIntent::forward`].
193    pub forward: bool,
194    /// See [`DirectionalCaretMovementIntent::collapse_selection`].
195    pub collapse_selection: bool,
196}
197
198impl ExtendSelectionByCharacterIntent {
199    /// Creates an [`ExtendSelectionByCharacterIntent`].
200    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/// Extends, or moves the current selection from the current
211/// `TextSelection.extent` position to the previous or the next word
212/// boundary.
213#[derive(Clone, Copy, Debug, PartialEq, Eq)]
214pub struct ExtendSelectionToNextWordBoundaryIntent {
215    /// See [`DirectionalTextEditingIntent::forward`].
216    pub forward: bool,
217    /// See [`DirectionalCaretMovementIntent::collapse_selection`].
218    pub collapse_selection: bool,
219}
220
221impl ExtendSelectionToNextWordBoundaryIntent {
222    /// Creates an [`ExtendSelectionToNextWordBoundaryIntent`].
223    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/// Extends, or moves the current selection from the current
237/// `TextSelection.extent` position to the previous or the next word
238/// boundary, or the `TextSelection.base` position if it's closer in the move
239/// direction.
240///
241/// This [`Intent`] typically has the same effect as an
242/// [`ExtendSelectionToNextWordBoundaryIntent`], except it collapses the selection
243/// when the order of `TextSelection.base` and `TextSelection.extent` would
244/// reverse.
245///
246/// This is typically only used on MacOS.
247#[derive(Clone, Copy, Debug, PartialEq, Eq)]
248pub struct ExtendSelectionToNextWordBoundaryOrCaretLocationIntent {
249    /// See [`DirectionalTextEditingIntent::forward`].
250    pub forward: bool,
251}
252
253impl ExtendSelectionToNextWordBoundaryOrCaretLocationIntent {
254    /// Creates an [`ExtendSelectionToNextWordBoundaryOrCaretLocationIntent`].
255    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/// Expands the current selection to the document boundary in the direction
287/// given by [`forward`](DirectionalTextEditingIntent::forward).
288///
289/// Unlike [`ExpandSelectionToLineBreakIntent`], the extent will be moved, which
290/// matches the behavior on MacOS.
291///
292/// See also:
293///
294///   [`ExtendSelectionToDocumentBoundaryIntent`], which is similar but always
295///   moves the extent.
296#[derive(Clone, Copy, Debug, PartialEq, Eq)]
297pub struct ExpandSelectionToDocumentBoundaryIntent {
298    /// See [`DirectionalTextEditingIntent::forward`].
299    pub forward: bool,
300}
301
302impl ExpandSelectionToDocumentBoundaryIntent {
303    /// Creates an [`ExpandSelectionToDocumentBoundaryIntent`].
304    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/// Expands the current selection to the closest line break in the direction
336/// given by [`forward`](DirectionalTextEditingIntent::forward).
337///
338/// Either the base or extent can move, whichever is closer to the line break.
339/// The selection will never shrink.
340///
341/// This behavior is common on MacOS.
342///
343/// See also:
344///
345///   [`ExtendSelectionToLineBreakIntent`], which is similar but always moves the
346///   extent.
347#[derive(Clone, Copy, Debug, PartialEq, Eq)]
348pub struct ExpandSelectionToLineBreakIntent {
349    /// See [`DirectionalTextEditingIntent::forward`].
350    pub forward: bool,
351}
352
353impl ExpandSelectionToLineBreakIntent {
354    /// Creates an [`ExpandSelectionToLineBreakIntent`].
355    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/// Extends, or moves the current selection from the current
387/// `TextSelection.extent` position to the closest line break in the direction
388/// given by [`forward`](DirectionalTextEditingIntent::forward).
389///
390/// See also:
391///
392///   [`ExpandSelectionToLineBreakIntent`], which is similar but always increases
393///   the size of the selection.
394#[derive(Clone, Copy, Debug, PartialEq, Eq)]
395pub struct ExtendSelectionToLineBreakIntent {
396    /// See [`DirectionalTextEditingIntent::forward`].
397    pub forward: bool,
398    /// See [`DirectionalCaretMovementIntent::collapse_selection`].
399    pub collapse_selection: bool,
400    /// See [`DirectionalCaretMovementIntent::collapse_at_reversal`].
401    pub collapse_at_reversal: bool,
402    /// See [`DirectionalCaretMovementIntent::continues_at_wrap`].
403    pub continues_at_wrap: bool,
404}
405
406impl ExtendSelectionToLineBreakIntent {
407    /// Creates an [`ExtendSelectionToLineBreakIntent`].
408    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    /// Dart `ExtendSelectionToLineBreakIntent(collapseAtReversal:)`.
418    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    /// Dart `ExtendSelectionToLineBreakIntent(continuesAtWrap:)`.
428    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/// Extends, or moves the current selection from the current
464/// `TextSelection.extent` position to the closest position on the adjacent
465/// line.
466#[derive(Clone, Copy, Debug, PartialEq, Eq)]
467pub struct ExtendSelectionVerticallyToAdjacentLineIntent {
468    /// See [`DirectionalTextEditingIntent::forward`].
469    pub forward: bool,
470    /// See [`DirectionalCaretMovementIntent::collapse_selection`].
471    pub collapse_selection: bool,
472}
473
474impl ExtendSelectionVerticallyToAdjacentLineIntent {
475    /// Creates an [`ExtendSelectionVerticallyToAdjacentLineIntent`].
476    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/// Expands, or moves the current selection from the current
490/// `TextSelection.extent` position to the closest position on the adjacent
491/// page.
492#[derive(Clone, Copy, Debug, PartialEq, Eq)]
493pub struct ExtendSelectionVerticallyToAdjacentPageIntent {
494    /// See [`DirectionalTextEditingIntent::forward`].
495    pub forward: bool,
496    /// See [`DirectionalCaretMovementIntent::collapse_selection`].
497    pub collapse_selection: bool,
498}
499
500impl ExtendSelectionVerticallyToAdjacentPageIntent {
501    /// Creates an [`ExtendSelectionVerticallyToAdjacentPageIntent`].
502    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/// Extends, or moves the current selection from the current
516/// `TextSelection.extent` position to the previous or the next paragraph
517/// boundary.
518#[derive(Clone, Copy, Debug, PartialEq, Eq)]
519pub struct ExtendSelectionToNextParagraphBoundaryIntent {
520    /// See [`DirectionalTextEditingIntent::forward`].
521    pub forward: bool,
522    /// See [`DirectionalCaretMovementIntent::collapse_selection`].
523    pub collapse_selection: bool,
524}
525
526impl ExtendSelectionToNextParagraphBoundaryIntent {
527    /// Creates an [`ExtendSelectionToNextParagraphBoundaryIntent`].
528    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/// Extends, or moves the current selection from the current
542/// `TextSelection.extent` position to the previous or the next paragraph
543/// boundary depending on the [`forward`](DirectionalTextEditingIntent::forward) parameter.
544///
545/// This [`Intent`] typically has the same effect as an
546/// [`ExtendSelectionToNextParagraphBoundaryIntent`], except it collapses the selection
547/// when the order of `TextSelection.base` and `TextSelection.extent` would
548/// reverse.
549///
550/// This is typically only used on MacOS.
551#[derive(Clone, Copy, Debug, PartialEq, Eq)]
552pub struct ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent {
553    /// See [`DirectionalTextEditingIntent::forward`].
554    pub forward: bool,
555}
556
557impl ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent {
558    /// Creates an [`ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent`].
559    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/// Extends, or moves the current selection from the current
593/// `TextSelection.extent` position to the start or the end of the document.
594///
595/// See also:
596///
597///   [`ExpandSelectionToDocumentBoundaryIntent`], which is similar but always
598///   increases the size of the selection.
599#[derive(Clone, Copy, Debug, PartialEq, Eq)]
600pub struct ExtendSelectionToDocumentBoundaryIntent {
601    /// See [`DirectionalTextEditingIntent::forward`].
602    pub forward: bool,
603    /// See [`DirectionalCaretMovementIntent::collapse_selection`].
604    pub collapse_selection: bool,
605}
606
607impl ExtendSelectionToDocumentBoundaryIntent {
608    /// Creates an [`ExtendSelectionToDocumentBoundaryIntent`].
609    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/// Scrolls to the beginning or end of the document depending on the
623/// [`forward`](DirectionalTextEditingIntent::forward) parameter.
624#[derive(Clone, Copy, Debug, PartialEq, Eq)]
625pub struct ScrollToDocumentBoundaryIntent {
626    /// See [`DirectionalTextEditingIntent::forward`].
627    pub forward: bool,
628}
629
630impl ScrollToDocumentBoundaryIntent {
631    /// Creates a [`ScrollToDocumentBoundaryIntent`].
632    pub const fn new(forward: bool) -> ScrollToDocumentBoundaryIntent {
633        ScrollToDocumentBoundaryIntent { forward }
634    }
635}
636
637directional_text_editing_intent!(ScrollToDocumentBoundaryIntent);
638
639/// An [`Intent`] to select everything in the field.
640#[derive(Clone, Copy, Debug, PartialEq, Eq)]
641pub struct SelectAllTextIntent {
642    /// The [`SelectionChangedCause`] that triggered the intent.
643    pub cause: SelectionChangedCause,
644}
645
646impl SelectAllTextIntent {
647    /// Creates an instance of [`SelectAllTextIntent`].
648    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/// An [`Intent`] that represents a user interaction that attempts to copy or cut
660/// the current selection in the field.
661#[derive(Clone, Copy, Debug, PartialEq, Eq)]
662pub struct CopySelectionTextIntent {
663    cause: SelectionChangedCause,
664    collapse_selection: bool,
665}
666
667impl CopySelectionTextIntent {
668    /// An [`Intent`] that represents a user interaction that attempts to copy the
669    /// current selection in the field.
670    pub const COPY: CopySelectionTextIntent = CopySelectionTextIntent {
671        cause: SelectionChangedCause::Keyboard,
672        collapse_selection: false,
673    };
674
675    /// Creates an [`Intent`] that represents a user interaction that attempts to
676    /// cut the current selection in the field.
677    pub const fn cut(cause: SelectionChangedCause) -> CopySelectionTextIntent {
678        CopySelectionTextIntent {
679            cause,
680            collapse_selection: true,
681        }
682    }
683
684    /// The [`SelectionChangedCause`] that triggered the intent.
685    pub const fn cause(&self) -> SelectionChangedCause {
686        self.cause
687    }
688
689    /// Whether the original text needs to be removed from the input field if the
690    /// copy action was successful.
691    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/// An [`Intent`] to paste text from the clipboard to the field.
703#[derive(Clone, Copy, Debug, PartialEq, Eq)]
704pub struct PasteTextIntent {
705    /// The [`SelectionChangedCause`] that triggered the intent.
706    pub cause: SelectionChangedCause,
707}
708
709impl PasteTextIntent {
710    /// Creates an instance of [`PasteTextIntent`].
711    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/// An [`Intent`] that represents a user interaction that attempts to go back to
723/// the previous editing state.
724#[derive(Clone, Copy, Debug, PartialEq, Eq)]
725pub struct RedoTextIntent {
726    /// The [`SelectionChangedCause`] that triggered the intent.
727    pub cause: SelectionChangedCause,
728}
729
730impl RedoTextIntent {
731    /// Creates a [`RedoTextIntent`].
732    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/// An [`Intent`] that represents a user interaction that attempts to modify the
744/// current [`TextEditingValue`] in an input field.
745#[derive(Clone, Debug, PartialEq, Eq)]
746pub struct ReplaceTextIntent {
747    /// The [`TextEditingValue`] that this [`Intent`]'s action should perform on.
748    pub current_text_editing_value: TextEditingValue,
749    /// The text to replace the original text within the [`replacement_range`](Self::replacement_range) with.
750    pub replacement_text: String,
751    /// The range of text in [`current_text_editing_value`](Self::current_text_editing_value) that needs to be replaced.
752    pub replacement_range: TextRange,
753    /// The [`SelectionChangedCause`] that triggered the intent.
754    pub cause: SelectionChangedCause,
755}
756
757impl ReplaceTextIntent {
758    /// Creates a [`ReplaceTextIntent`].
759    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/// An [`Intent`] that represents a user interaction that attempts to go back to
781/// the previous editing state.
782#[derive(Clone, Copy, Debug, PartialEq, Eq)]
783pub struct UndoTextIntent {
784    /// The [`SelectionChangedCause`] that triggered the intent.
785    pub cause: SelectionChangedCause,
786}
787
788impl UndoTextIntent {
789    /// Creates an [`UndoTextIntent`].
790    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/// An [`Intent`] that represents a user interaction that attempts to change the
802/// selection in an input field.
803#[derive(Clone, Debug, PartialEq, Eq)]
804pub struct UpdateSelectionIntent {
805    /// The [`TextEditingValue`] that this [`Intent`]'s action should perform on.
806    pub current_text_editing_value: TextEditingValue,
807    /// The new [`TextSelection`] the input field should adopt.
808    pub new_selection: TextSelection,
809    /// The [`SelectionChangedCause`] that triggered the intent.
810    pub cause: SelectionChangedCause,
811}
812
813impl UpdateSelectionIntent {
814    /// Creates an [`UpdateSelectionIntent`].
815    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/// An [`Intent`] that represents a user interaction that attempts to swap the
835/// characters immediately around the cursor.
836#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
837pub struct TransposeCharactersIntent;
838
839impl TransposeCharactersIntent {
840    /// Creates a [`TransposeCharactersIntent`].
841    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/// An [`Intent`] that represents a tap outside the field.
853///
854/// Invoked when the user taps outside the focused `EditableText` if
855/// `EditableText.onTapOutside` is `None`.
856///
857/// Override this [`Intent`] to modify the default behavior, which is to unfocus
858/// on a touch event on web and do nothing on other platforms.
859///
860/// See also:
861///
862///  * [`AnyAction::overridable`](crate::AnyAction::overridable) for an example on how to make
863///    an `Action` overridable.
864#[derive(Clone, Debug)]
865pub struct EditableTextTapOutsideIntent {
866    /// The `FocusNode` that this [`Intent`]'s action should be performed on.
867    pub focus_node: FocusNodeRef,
868    /// The `PointerDownEvent` that initiated this [`Intent`].
869    pub pointer_down_event: PointerDownEvent,
870}
871
872impl EditableTextTapOutsideIntent {
873    /// Creates an [`EditableTextTapOutsideIntent`].
874    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/// An [`Intent`] that represents a tap outside the field.
892///
893/// Invoked when the user taps up outside the focused `EditableText` if
894/// `EditableText.onTapUpOutside` is `None`.
895///
896/// Override this [`Intent`] to modify the default behavior, which is to unfocus
897/// on a touch event on web and do nothing on other platforms.
898///
899/// See also:
900///
901///  * [`AnyAction::overridable`](crate::AnyAction::overridable) for an example on how to make
902///    an `Action` overridable.
903#[derive(Clone, Debug)]
904pub struct EditableTextTapUpOutsideIntent {
905    /// The `FocusNode` that this [`Intent`]'s action should be performed on.
906    pub focus_node: FocusNodeRef,
907    /// The `PointerUpEvent` that initiated this [`Intent`].
908    pub pointer_up_event: PointerUpEvent,
909}
910
911impl EditableTextTapUpOutsideIntent {
912    /// Creates an [`EditableTextTapUpOutsideIntent`].
913    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        // The setters take the field's name, so the getters of the abstract type are reached
958        // through the trait.
959        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}