waft-widgets 0.8.1

Widget framework and widgets for Waft
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
//! Flutter counterpart: `widgets/text_editing_intents.dart`.
//!
//! The [`Intent`]s a text field's key bindings emit. The actions that consume them live in
//! `editable_text.dart`, which waits; `DefaultTextEditingShortcuts` binds them.

use std::any::Any;

use waft_embedder::TextRange;
use waft_gestures::{PointerDownEvent, PointerUpEvent};
use waft_services::{SelectionChangedCause, TextEditingValue, TextSelection};

use crate::widgets::actions::Intent;
use crate::widgets::focus_manager::FocusNodeRef;

/// An [`Intent`] to send the event straight to the engine.
///
/// See also:
///
///   * `DefaultTextEditingShortcuts`, which triggers this [`Intent`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct DoNothingAndStopPropagationTextIntent;

impl DoNothingAndStopPropagationTextIntent {
    /// Creates an instance of [`DoNothingAndStopPropagationTextIntent`].
    pub const fn new() -> DoNothingAndStopPropagationTextIntent {
        DoNothingAndStopPropagationTextIntent
    }
}

impl Intent for DoNothingAndStopPropagationTextIntent {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// A text editing related [`Intent`] that performs an operation towards a given
/// direction of the current caret location.
pub trait DirectionalTextEditingIntent: Intent {
    /// Whether the input field, if applicable, should perform the text editing
    /// operation from the current caret location towards the end of the document.
    ///
    /// Unless otherwise specified by the recipient of this intent, this parameter
    /// uses the logical order of characters in the string to determine the
    /// direction, and is not affected by the writing direction of the text.
    fn forward(&self) -> bool;
}

/// A [`DirectionalTextEditingIntent`] that moves the caret or the selection to a
/// new location.
pub trait DirectionalCaretMovementIntent: DirectionalTextEditingIntent {
    /// Whether this [`Intent`] should make the selection collapsed (so it becomes a
    /// caret), after the movement.
    ///
    /// When [`collapse_selection`](Self::collapse_selection) is false, the input field
    /// typically only moves the current `TextSelection.extent` to the new location, while
    /// maintains the current `TextSelection.base` location.
    ///
    /// When [`collapse_selection`](Self::collapse_selection) is true, the input field
    /// typically should move both the `TextSelection.base` and the `TextSelection.extent`
    /// to the new location.
    fn collapse_selection(&self) -> bool;

    /// Whether to collapse the selection when it would otherwise reverse order.
    ///
    /// For example, consider when forward is true and the extent is before the
    /// base. If `collapse_at_reversal` is true, then this will cause the selection to
    /// collapse at the base. If it's false, then the extent will be placed at the
    /// linebreak, reversing the order of base and offset.
    ///
    /// Cannot be true when [`collapse_selection`](Self::collapse_selection) is true.
    fn collapse_at_reversal(&self) -> bool;

    /// Whether or not to continue to the next line at a wordwrap.
    ///
    /// If true, when an [`Intent`] to go to the beginning/end of a wordwrapped line
    /// is received and the selection is already at the beginning/end of the line,
    /// then the selection will be moved to the next/previous line. If false, the
    /// selection will remain at the wordwrap.
    fn continues_at_wrap(&self) -> bool;
}

/// Writes the two traits for an intent whose caret movement fields are the four Dart
/// defines, with `collapse_at_reversal` and `continues_at_wrap` fixed to the values the
/// Dart constructor passes to `super`.
macro_rules! caret_movement_intent {
    ($intent:ty, $collapse_at_reversal:expr, $continues_at_wrap:expr) => {
        impl Intent for $intent {
            fn as_any(&self) -> &dyn Any {
                self
            }
        }

        impl DirectionalTextEditingIntent for $intent {
            fn forward(&self) -> bool {
                self.forward
            }
        }

        impl DirectionalCaretMovementIntent for $intent {
            fn collapse_selection(&self) -> bool {
                self.collapse_selection
            }

            fn collapse_at_reversal(&self) -> bool {
                $collapse_at_reversal
            }

            fn continues_at_wrap(&self) -> bool {
                $continues_at_wrap
            }
        }
    };
}

/// Writes [`Intent`] and [`DirectionalTextEditingIntent`] for an intent whose only field is
/// `forward`.
macro_rules! directional_text_editing_intent {
    ($intent:ty) => {
        impl Intent for $intent {
            fn as_any(&self) -> &dyn Any {
                self
            }
        }

        impl DirectionalTextEditingIntent for $intent {
            fn forward(&self) -> bool {
                self.forward
            }
        }
    };
}

/// Deletes the character before or after the caret location, based on whether
/// `forward` is true.
///
/// Typically a text field will not respond to this intent if it has no active
/// caret (`TextSelection::is_valid` is false for the current selection).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DeleteCharacterIntent {
    /// See [`DirectionalTextEditingIntent::forward`].
    pub forward: bool,
}

impl DeleteCharacterIntent {
    /// Creates a [`DeleteCharacterIntent`].
    pub const fn new(forward: bool) -> DeleteCharacterIntent {
        DeleteCharacterIntent { forward }
    }
}

directional_text_editing_intent!(DeleteCharacterIntent);

/// Deletes from the current caret location to the previous or next word
/// boundary, based on whether `forward` is true.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DeleteToNextWordBoundaryIntent {
    /// See [`DirectionalTextEditingIntent::forward`].
    pub forward: bool,
}

impl DeleteToNextWordBoundaryIntent {
    /// Creates a [`DeleteToNextWordBoundaryIntent`].
    pub const fn new(forward: bool) -> DeleteToNextWordBoundaryIntent {
        DeleteToNextWordBoundaryIntent { forward }
    }
}

directional_text_editing_intent!(DeleteToNextWordBoundaryIntent);

/// Deletes from the current caret location to the previous or next soft or hard
/// line break, based on whether `forward` is true.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DeleteToLineBreakIntent {
    /// See [`DirectionalTextEditingIntent::forward`].
    pub forward: bool,
}

impl DeleteToLineBreakIntent {
    /// Creates a [`DeleteToLineBreakIntent`].
    pub const fn new(forward: bool) -> DeleteToLineBreakIntent {
        DeleteToLineBreakIntent { forward }
    }
}

directional_text_editing_intent!(DeleteToLineBreakIntent);

/// Extends, or moves the current selection from the current
/// `TextSelection.extent` position to the previous or the next character
/// boundary.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ExtendSelectionByCharacterIntent {
    /// See [`DirectionalTextEditingIntent::forward`].
    pub forward: bool,
    /// See [`DirectionalCaretMovementIntent::collapse_selection`].
    pub collapse_selection: bool,
}

impl ExtendSelectionByCharacterIntent {
    /// Creates an [`ExtendSelectionByCharacterIntent`].
    pub const fn new(forward: bool, collapse_selection: bool) -> ExtendSelectionByCharacterIntent {
        ExtendSelectionByCharacterIntent {
            forward,
            collapse_selection,
        }
    }
}

caret_movement_intent!(ExtendSelectionByCharacterIntent, false, false);

/// Extends, or moves the current selection from the current
/// `TextSelection.extent` position to the previous or the next word
/// boundary.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ExtendSelectionToNextWordBoundaryIntent {
    /// See [`DirectionalTextEditingIntent::forward`].
    pub forward: bool,
    /// See [`DirectionalCaretMovementIntent::collapse_selection`].
    pub collapse_selection: bool,
}

impl ExtendSelectionToNextWordBoundaryIntent {
    /// Creates an [`ExtendSelectionToNextWordBoundaryIntent`].
    pub const fn new(
        forward: bool,
        collapse_selection: bool,
    ) -> ExtendSelectionToNextWordBoundaryIntent {
        ExtendSelectionToNextWordBoundaryIntent {
            forward,
            collapse_selection,
        }
    }
}

caret_movement_intent!(ExtendSelectionToNextWordBoundaryIntent, false, false);

/// Extends, or moves the current selection from the current
/// `TextSelection.extent` position to the previous or the next word
/// boundary, or the `TextSelection.base` position if it's closer in the move
/// direction.
///
/// This [`Intent`] typically has the same effect as an
/// [`ExtendSelectionToNextWordBoundaryIntent`], except it collapses the selection
/// when the order of `TextSelection.base` and `TextSelection.extent` would
/// reverse.
///
/// This is typically only used on MacOS.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ExtendSelectionToNextWordBoundaryOrCaretLocationIntent {
    /// See [`DirectionalTextEditingIntent::forward`].
    pub forward: bool,
}

impl ExtendSelectionToNextWordBoundaryOrCaretLocationIntent {
    /// Creates an [`ExtendSelectionToNextWordBoundaryOrCaretLocationIntent`].
    pub const fn new(forward: bool) -> ExtendSelectionToNextWordBoundaryOrCaretLocationIntent {
        ExtendSelectionToNextWordBoundaryOrCaretLocationIntent { forward }
    }
}

impl Intent for ExtendSelectionToNextWordBoundaryOrCaretLocationIntent {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl DirectionalTextEditingIntent for ExtendSelectionToNextWordBoundaryOrCaretLocationIntent {
    fn forward(&self) -> bool {
        self.forward
    }
}

impl DirectionalCaretMovementIntent for ExtendSelectionToNextWordBoundaryOrCaretLocationIntent {
    fn collapse_selection(&self) -> bool {
        false
    }

    fn collapse_at_reversal(&self) -> bool {
        true
    }

    fn continues_at_wrap(&self) -> bool {
        false
    }
}

/// Expands the current selection to the document boundary in the direction
/// given by [`forward`](DirectionalTextEditingIntent::forward).
///
/// Unlike [`ExpandSelectionToLineBreakIntent`], the extent will be moved, which
/// matches the behavior on MacOS.
///
/// See also:
///
///   [`ExtendSelectionToDocumentBoundaryIntent`], which is similar but always
///   moves the extent.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ExpandSelectionToDocumentBoundaryIntent {
    /// See [`DirectionalTextEditingIntent::forward`].
    pub forward: bool,
}

impl ExpandSelectionToDocumentBoundaryIntent {
    /// Creates an [`ExpandSelectionToDocumentBoundaryIntent`].
    pub const fn new(forward: bool) -> ExpandSelectionToDocumentBoundaryIntent {
        ExpandSelectionToDocumentBoundaryIntent { forward }
    }
}

impl Intent for ExpandSelectionToDocumentBoundaryIntent {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl DirectionalTextEditingIntent for ExpandSelectionToDocumentBoundaryIntent {
    fn forward(&self) -> bool {
        self.forward
    }
}

impl DirectionalCaretMovementIntent for ExpandSelectionToDocumentBoundaryIntent {
    fn collapse_selection(&self) -> bool {
        false
    }

    fn collapse_at_reversal(&self) -> bool {
        false
    }

    fn continues_at_wrap(&self) -> bool {
        false
    }
}

/// Expands the current selection to the closest line break in the direction
/// given by [`forward`](DirectionalTextEditingIntent::forward).
///
/// Either the base or extent can move, whichever is closer to the line break.
/// The selection will never shrink.
///
/// This behavior is common on MacOS.
///
/// See also:
///
///   [`ExtendSelectionToLineBreakIntent`], which is similar but always moves the
///   extent.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ExpandSelectionToLineBreakIntent {
    /// See [`DirectionalTextEditingIntent::forward`].
    pub forward: bool,
}

impl ExpandSelectionToLineBreakIntent {
    /// Creates an [`ExpandSelectionToLineBreakIntent`].
    pub const fn new(forward: bool) -> ExpandSelectionToLineBreakIntent {
        ExpandSelectionToLineBreakIntent { forward }
    }
}

impl Intent for ExpandSelectionToLineBreakIntent {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl DirectionalTextEditingIntent for ExpandSelectionToLineBreakIntent {
    fn forward(&self) -> bool {
        self.forward
    }
}

impl DirectionalCaretMovementIntent for ExpandSelectionToLineBreakIntent {
    fn collapse_selection(&self) -> bool {
        false
    }

    fn collapse_at_reversal(&self) -> bool {
        false
    }

    fn continues_at_wrap(&self) -> bool {
        false
    }
}

/// Extends, or moves the current selection from the current
/// `TextSelection.extent` position to the closest line break in the direction
/// given by [`forward`](DirectionalTextEditingIntent::forward).
///
/// See also:
///
///   [`ExpandSelectionToLineBreakIntent`], which is similar but always increases
///   the size of the selection.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ExtendSelectionToLineBreakIntent {
    /// See [`DirectionalTextEditingIntent::forward`].
    pub forward: bool,
    /// See [`DirectionalCaretMovementIntent::collapse_selection`].
    pub collapse_selection: bool,
    /// See [`DirectionalCaretMovementIntent::collapse_at_reversal`].
    pub collapse_at_reversal: bool,
    /// See [`DirectionalCaretMovementIntent::continues_at_wrap`].
    pub continues_at_wrap: bool,
}

impl ExtendSelectionToLineBreakIntent {
    /// Creates an [`ExtendSelectionToLineBreakIntent`].
    pub const fn new(forward: bool, collapse_selection: bool) -> ExtendSelectionToLineBreakIntent {
        ExtendSelectionToLineBreakIntent {
            forward,
            collapse_selection,
            collapse_at_reversal: false,
            continues_at_wrap: false,
        }
    }

    /// Dart `ExtendSelectionToLineBreakIntent(collapseAtReversal:)`.
    pub const fn collapse_at_reversal(
        mut self,
        collapse_at_reversal: bool,
    ) -> ExtendSelectionToLineBreakIntent {
        debug_assert!(!(self.collapse_selection && collapse_at_reversal));
        self.collapse_at_reversal = collapse_at_reversal;
        self
    }

    /// Dart `ExtendSelectionToLineBreakIntent(continuesAtWrap:)`.
    pub const fn continues_at_wrap(
        mut self,
        continues_at_wrap: bool,
    ) -> ExtendSelectionToLineBreakIntent {
        self.continues_at_wrap = continues_at_wrap;
        self
    }
}

impl Intent for ExtendSelectionToLineBreakIntent {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl DirectionalTextEditingIntent for ExtendSelectionToLineBreakIntent {
    fn forward(&self) -> bool {
        self.forward
    }
}

impl DirectionalCaretMovementIntent for ExtendSelectionToLineBreakIntent {
    fn collapse_selection(&self) -> bool {
        self.collapse_selection
    }

    fn collapse_at_reversal(&self) -> bool {
        self.collapse_at_reversal
    }

    fn continues_at_wrap(&self) -> bool {
        self.continues_at_wrap
    }
}

/// Extends, or moves the current selection from the current
/// `TextSelection.extent` position to the closest position on the adjacent
/// line.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ExtendSelectionVerticallyToAdjacentLineIntent {
    /// See [`DirectionalTextEditingIntent::forward`].
    pub forward: bool,
    /// See [`DirectionalCaretMovementIntent::collapse_selection`].
    pub collapse_selection: bool,
}

impl ExtendSelectionVerticallyToAdjacentLineIntent {
    /// Creates an [`ExtendSelectionVerticallyToAdjacentLineIntent`].
    pub const fn new(
        forward: bool,
        collapse_selection: bool,
    ) -> ExtendSelectionVerticallyToAdjacentLineIntent {
        ExtendSelectionVerticallyToAdjacentLineIntent {
            forward,
            collapse_selection,
        }
    }
}

caret_movement_intent!(ExtendSelectionVerticallyToAdjacentLineIntent, false, false);

/// Expands, or moves the current selection from the current
/// `TextSelection.extent` position to the closest position on the adjacent
/// page.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ExtendSelectionVerticallyToAdjacentPageIntent {
    /// See [`DirectionalTextEditingIntent::forward`].
    pub forward: bool,
    /// See [`DirectionalCaretMovementIntent::collapse_selection`].
    pub collapse_selection: bool,
}

impl ExtendSelectionVerticallyToAdjacentPageIntent {
    /// Creates an [`ExtendSelectionVerticallyToAdjacentPageIntent`].
    pub const fn new(
        forward: bool,
        collapse_selection: bool,
    ) -> ExtendSelectionVerticallyToAdjacentPageIntent {
        ExtendSelectionVerticallyToAdjacentPageIntent {
            forward,
            collapse_selection,
        }
    }
}

caret_movement_intent!(ExtendSelectionVerticallyToAdjacentPageIntent, false, false);

/// Extends, or moves the current selection from the current
/// `TextSelection.extent` position to the previous or the next paragraph
/// boundary.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ExtendSelectionToNextParagraphBoundaryIntent {
    /// See [`DirectionalTextEditingIntent::forward`].
    pub forward: bool,
    /// See [`DirectionalCaretMovementIntent::collapse_selection`].
    pub collapse_selection: bool,
}

impl ExtendSelectionToNextParagraphBoundaryIntent {
    /// Creates an [`ExtendSelectionToNextParagraphBoundaryIntent`].
    pub const fn new(
        forward: bool,
        collapse_selection: bool,
    ) -> ExtendSelectionToNextParagraphBoundaryIntent {
        ExtendSelectionToNextParagraphBoundaryIntent {
            forward,
            collapse_selection,
        }
    }
}

caret_movement_intent!(ExtendSelectionToNextParagraphBoundaryIntent, false, false);

/// Extends, or moves the current selection from the current
/// `TextSelection.extent` position to the previous or the next paragraph
/// boundary depending on the [`forward`](DirectionalTextEditingIntent::forward) parameter.
///
/// This [`Intent`] typically has the same effect as an
/// [`ExtendSelectionToNextParagraphBoundaryIntent`], except it collapses the selection
/// when the order of `TextSelection.base` and `TextSelection.extent` would
/// reverse.
///
/// This is typically only used on MacOS.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent {
    /// See [`DirectionalTextEditingIntent::forward`].
    pub forward: bool,
}

impl ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent {
    /// Creates an [`ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent`].
    pub const fn new(forward: bool) -> ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent {
        ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent { forward }
    }
}

impl Intent for ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl DirectionalTextEditingIntent for ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent {
    fn forward(&self) -> bool {
        self.forward
    }
}

impl DirectionalCaretMovementIntent
    for ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent
{
    fn collapse_selection(&self) -> bool {
        false
    }

    fn collapse_at_reversal(&self) -> bool {
        true
    }

    fn continues_at_wrap(&self) -> bool {
        false
    }
}

/// Extends, or moves the current selection from the current
/// `TextSelection.extent` position to the start or the end of the document.
///
/// See also:
///
///   [`ExpandSelectionToDocumentBoundaryIntent`], which is similar but always
///   increases the size of the selection.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ExtendSelectionToDocumentBoundaryIntent {
    /// See [`DirectionalTextEditingIntent::forward`].
    pub forward: bool,
    /// See [`DirectionalCaretMovementIntent::collapse_selection`].
    pub collapse_selection: bool,
}

impl ExtendSelectionToDocumentBoundaryIntent {
    /// Creates an [`ExtendSelectionToDocumentBoundaryIntent`].
    pub const fn new(
        forward: bool,
        collapse_selection: bool,
    ) -> ExtendSelectionToDocumentBoundaryIntent {
        ExtendSelectionToDocumentBoundaryIntent {
            forward,
            collapse_selection,
        }
    }
}

caret_movement_intent!(ExtendSelectionToDocumentBoundaryIntent, false, false);

/// Scrolls to the beginning or end of the document depending on the
/// [`forward`](DirectionalTextEditingIntent::forward) parameter.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ScrollToDocumentBoundaryIntent {
    /// See [`DirectionalTextEditingIntent::forward`].
    pub forward: bool,
}

impl ScrollToDocumentBoundaryIntent {
    /// Creates a [`ScrollToDocumentBoundaryIntent`].
    pub const fn new(forward: bool) -> ScrollToDocumentBoundaryIntent {
        ScrollToDocumentBoundaryIntent { forward }
    }
}

directional_text_editing_intent!(ScrollToDocumentBoundaryIntent);

/// An [`Intent`] to select everything in the field.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SelectAllTextIntent {
    /// The [`SelectionChangedCause`] that triggered the intent.
    pub cause: SelectionChangedCause,
}

impl SelectAllTextIntent {
    /// Creates an instance of [`SelectAllTextIntent`].
    pub const fn new(cause: SelectionChangedCause) -> SelectAllTextIntent {
        SelectAllTextIntent { cause }
    }
}

impl Intent for SelectAllTextIntent {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// An [`Intent`] that represents a user interaction that attempts to copy or cut
/// the current selection in the field.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CopySelectionTextIntent {
    cause: SelectionChangedCause,
    collapse_selection: bool,
}

impl CopySelectionTextIntent {
    /// An [`Intent`] that represents a user interaction that attempts to copy the
    /// current selection in the field.
    pub const COPY: CopySelectionTextIntent = CopySelectionTextIntent {
        cause: SelectionChangedCause::Keyboard,
        collapse_selection: false,
    };

    /// Creates an [`Intent`] that represents a user interaction that attempts to
    /// cut the current selection in the field.
    pub const fn cut(cause: SelectionChangedCause) -> CopySelectionTextIntent {
        CopySelectionTextIntent {
            cause,
            collapse_selection: true,
        }
    }

    /// The [`SelectionChangedCause`] that triggered the intent.
    pub const fn cause(&self) -> SelectionChangedCause {
        self.cause
    }

    /// Whether the original text needs to be removed from the input field if the
    /// copy action was successful.
    pub const fn collapse_selection(&self) -> bool {
        self.collapse_selection
    }
}

impl Intent for CopySelectionTextIntent {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// An [`Intent`] to paste text from the clipboard to the field.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PasteTextIntent {
    /// The [`SelectionChangedCause`] that triggered the intent.
    pub cause: SelectionChangedCause,
}

impl PasteTextIntent {
    /// Creates an instance of [`PasteTextIntent`].
    pub const fn new(cause: SelectionChangedCause) -> PasteTextIntent {
        PasteTextIntent { cause }
    }
}

impl Intent for PasteTextIntent {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// An [`Intent`] that represents a user interaction that attempts to go back to
/// the previous editing state.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RedoTextIntent {
    /// The [`SelectionChangedCause`] that triggered the intent.
    pub cause: SelectionChangedCause,
}

impl RedoTextIntent {
    /// Creates a [`RedoTextIntent`].
    pub const fn new(cause: SelectionChangedCause) -> RedoTextIntent {
        RedoTextIntent { cause }
    }
}

impl Intent for RedoTextIntent {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// An [`Intent`] that represents a user interaction that attempts to modify the
/// current [`TextEditingValue`] in an input field.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ReplaceTextIntent {
    /// The [`TextEditingValue`] that this [`Intent`]'s action should perform on.
    pub current_text_editing_value: TextEditingValue,
    /// The text to replace the original text within the [`replacement_range`](Self::replacement_range) with.
    pub replacement_text: String,
    /// The range of text in [`current_text_editing_value`](Self::current_text_editing_value) that needs to be replaced.
    pub replacement_range: TextRange,
    /// The [`SelectionChangedCause`] that triggered the intent.
    pub cause: SelectionChangedCause,
}

impl ReplaceTextIntent {
    /// Creates a [`ReplaceTextIntent`].
    pub fn new(
        current_text_editing_value: TextEditingValue,
        replacement_text: impl Into<String>,
        replacement_range: TextRange,
        cause: SelectionChangedCause,
    ) -> ReplaceTextIntent {
        ReplaceTextIntent {
            current_text_editing_value,
            replacement_text: replacement_text.into(),
            replacement_range,
            cause,
        }
    }
}

impl Intent for ReplaceTextIntent {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// An [`Intent`] that represents a user interaction that attempts to go back to
/// the previous editing state.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UndoTextIntent {
    /// The [`SelectionChangedCause`] that triggered the intent.
    pub cause: SelectionChangedCause,
}

impl UndoTextIntent {
    /// Creates an [`UndoTextIntent`].
    pub const fn new(cause: SelectionChangedCause) -> UndoTextIntent {
        UndoTextIntent { cause }
    }
}

impl Intent for UndoTextIntent {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// An [`Intent`] that represents a user interaction that attempts to change the
/// selection in an input field.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UpdateSelectionIntent {
    /// The [`TextEditingValue`] that this [`Intent`]'s action should perform on.
    pub current_text_editing_value: TextEditingValue,
    /// The new [`TextSelection`] the input field should adopt.
    pub new_selection: TextSelection,
    /// The [`SelectionChangedCause`] that triggered the intent.
    pub cause: SelectionChangedCause,
}

impl UpdateSelectionIntent {
    /// Creates an [`UpdateSelectionIntent`].
    pub fn new(
        current_text_editing_value: TextEditingValue,
        new_selection: TextSelection,
        cause: SelectionChangedCause,
    ) -> UpdateSelectionIntent {
        UpdateSelectionIntent {
            current_text_editing_value,
            new_selection,
            cause,
        }
    }
}

impl Intent for UpdateSelectionIntent {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// An [`Intent`] that represents a user interaction that attempts to swap the
/// characters immediately around the cursor.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct TransposeCharactersIntent;

impl TransposeCharactersIntent {
    /// Creates a [`TransposeCharactersIntent`].
    pub const fn new() -> TransposeCharactersIntent {
        TransposeCharactersIntent
    }
}

impl Intent for TransposeCharactersIntent {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// An [`Intent`] that represents a tap outside the field.
///
/// Invoked when the user taps outside the focused `EditableText` if
/// `EditableText.onTapOutside` is `None`.
///
/// Override this [`Intent`] to modify the default behavior, which is to unfocus
/// on a touch event on web and do nothing on other platforms.
///
/// See also:
///
///  * [`AnyAction::overridable`](crate::AnyAction::overridable) for an example on how to make
///    an `Action` overridable.
#[derive(Clone, Debug)]
pub struct EditableTextTapOutsideIntent {
    /// The `FocusNode` that this [`Intent`]'s action should be performed on.
    pub focus_node: FocusNodeRef,
    /// The `PointerDownEvent` that initiated this [`Intent`].
    pub pointer_down_event: PointerDownEvent,
}

impl EditableTextTapOutsideIntent {
    /// Creates an [`EditableTextTapOutsideIntent`].
    pub fn new(
        focus_node: FocusNodeRef,
        pointer_down_event: PointerDownEvent,
    ) -> EditableTextTapOutsideIntent {
        EditableTextTapOutsideIntent {
            focus_node,
            pointer_down_event,
        }
    }
}

impl Intent for EditableTextTapOutsideIntent {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// An [`Intent`] that represents a tap outside the field.
///
/// Invoked when the user taps up outside the focused `EditableText` if
/// `EditableText.onTapUpOutside` is `None`.
///
/// Override this [`Intent`] to modify the default behavior, which is to unfocus
/// on a touch event on web and do nothing on other platforms.
///
/// See also:
///
///  * [`AnyAction::overridable`](crate::AnyAction::overridable) for an example on how to make
///    an `Action` overridable.
#[derive(Clone, Debug)]
pub struct EditableTextTapUpOutsideIntent {
    /// The `FocusNode` that this [`Intent`]'s action should be performed on.
    pub focus_node: FocusNodeRef,
    /// The `PointerUpEvent` that initiated this [`Intent`].
    pub pointer_up_event: PointerUpEvent,
}

impl EditableTextTapUpOutsideIntent {
    /// Creates an [`EditableTextTapUpOutsideIntent`].
    pub fn new(
        focus_node: FocusNodeRef,
        pointer_up_event: PointerUpEvent,
    ) -> EditableTextTapUpOutsideIntent {
        EditableTextTapUpOutsideIntent {
            focus_node,
            pointer_up_event,
        }
    }
}

impl Intent for EditableTextTapUpOutsideIntent {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_or_caret_location_intents_collapse_at_a_reversal() {
        let word = ExtendSelectionToNextWordBoundaryOrCaretLocationIntent::new(true);
        assert!(word.forward());
        assert!(!word.collapse_selection());
        assert!(word.collapse_at_reversal());
        assert!(!word.continues_at_wrap());

        let paragraph = ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent::new(false);
        assert!(!paragraph.forward());
        assert!(!paragraph.collapse_selection());
        assert!(paragraph.collapse_at_reversal());
    }

    #[test]
    fn a_line_break_intent_carries_the_two_optional_caret_arguments() {
        let plain = ExtendSelectionToLineBreakIntent::new(true, true);
        assert!(!plain.collapse_at_reversal);
        assert!(!plain.continues_at_wrap);

        let wrapping = ExtendSelectionToLineBreakIntent::new(true, false)
            .collapse_at_reversal(true)
            .continues_at_wrap(true);
        // The setters take the field's name, so the getters of the abstract type are reached
        // through the trait.
        assert!(DirectionalCaretMovementIntent::collapse_at_reversal(
            &wrapping
        ));
        assert!(DirectionalCaretMovementIntent::continues_at_wrap(&wrapping));
    }

    #[test]
    fn an_expanding_intent_never_collapses_the_selection() {
        assert!(!ExpandSelectionToLineBreakIntent::new(true).collapse_selection());
        assert!(!ExpandSelectionToLineBreakIntent::new(true).collapse_at_reversal());
        assert!(!ExpandSelectionToDocumentBoundaryIntent::new(false).collapse_selection());
        assert!(!ExpandSelectionToDocumentBoundaryIntent::new(false).collapse_at_reversal());
    }
}