waft-widgets 0.7.0

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
//! Flutter counterpart: `widgets/undo_history.dart`.

use std::any::TypeId;
use std::cell::Cell;
use std::collections::HashMap;
use std::fmt::{self, Debug};
use std::rc::Rc;
use std::time::Duration;

use waft_embedder::TargetPlatform;
use waft_foundation::{
    App, ChangeNotifier, ChangeNotifierData, Handle, Listener, OptionRetained, Timer,
    ValueListenable, ValueNotifier,
};
use waft_services::{UndoDirection, UndoManager, UndoManagerClient};

use crate::framework::{
    BuildContext, IntoWidget, KeyRef, State, StateData, StatefulWidget, WidgetRef,
};
use crate::widgets::actions::{Actions, AnyAction, CallbackAction};
use crate::widgets::focus_manager::FocusNodeRef;
use crate::widgets::text_editing_intents::{RedoTextIntent, UndoTextIntent};
use waft_foundation::ListenableObject;
use waft_foundation::Ref;

/// Called when an undo or redo causes a state change.
pub type UndoHistoryOnTriggered<T> = Rc<dyn Fn(&mut App, T)>;

/// Called when checking whether a value change should be pushed onto the undo stack.
pub type ShouldChangeUndoStack<T> = Rc<dyn Fn(Option<&T>, &T) -> bool>;

/// Called right before a new entry is pushed to the undo stack.
pub type UndoStackModifier<T> = Rc<dyn Fn(T) -> T>;

/// Provides undo/redo capabilities for a [`ValueNotifier`].
///
/// Listens to [`value`](Self::value) and saves relevant values for undoing/redoing. The
/// cadence at which values are saved is a best approximation of the native
/// behaviors of a number of hardware keyboard on Flutter's desktop
/// platforms, as there are subtle differences between each of the platforms.
///
/// Listens to keyboard undo/redo shortcuts and calls [`on_triggered`](Self::on_triggered) when a
/// shortcut is triggered that would affect the state of the [`value`](Self::value).
///
/// The [`child`](Self::child) must manage focus on the [`focus_node`](Self::focus_node). For
/// example, using a `TextField` or `Focus` widget.
///
/// The [`value`](Self::value) is any kept [`ValueListenable`], which Dart types as a
/// `ValueNotifier<T>`: a `Ref<ValueNotifier<T>>`, or a `TextEditingControllerRef`.
pub struct UndoHistory<
    T: Clone + PartialEq + 'static,
    V: ValueListenable<T> + Clone + PartialEq + 'static = Ref<ValueNotifier<T>>,
> {
    /// See [`Widget::key`](crate::Widget::key).
    pub key: Option<KeyRef>,
    /// Called when checking whether a value change should be pushed onto
    /// the undo stack.
    pub should_change_undo_stack: Option<ShouldChangeUndoStack<T>>,
    /// The value to track over time.
    pub value: V,
    /// Called when an undo or redo causes a state change.
    ///
    /// If the state would still be the same before and after the undo/redo, this
    /// will not be called. For example, receiving a redo when there is nothing
    /// to redo will not call this method.
    ///
    /// Changes to the [`value`](Self::value) while this method is running will not be recorded
    /// on the undo stack. For example, a `TextInputFormatter` may change the value
    /// from what was on the undo stack, but this new value will not be recorded,
    /// as that would wipe out the redo history.
    pub on_triggered: UndoHistoryOnTriggered<T>,
    /// The [`FocusNode`] that will be used to listen for focus to set the initial
    /// undo state for the element.
    pub focus_node: FocusNodeRef,
    /// Called right before a new entry is pushed to the undo stack.
    ///
    /// The value returned from this method will be pushed to the stack instead
    /// of the original value.
    ///
    /// If null then the original value will always be pushed to the stack.
    pub undo_stack_modifier: Option<UndoStackModifier<T>>,
    /// Controls the undo state.
    ///
    /// If null, this widget will create its own [`UndoHistoryController`].
    pub controller: Option<Ref<UndoHistoryController>>,
    /// The child widget of [`UndoHistory`].
    pub child: WidgetRef,
}

impl<T: Clone + PartialEq + 'static, V: ValueListenable<T> + Clone + PartialEq + 'static>
    UndoHistory<T, V>
{
    /// Creates an instance of [`UndoHistory`].
    pub fn new<K>(
        value: V,
        on_triggered: impl Fn(&mut App, T) + 'static,
        focus_node: FocusNodeRef,
        child: impl IntoWidget<K>,
    ) -> UndoHistory<T, V> {
        UndoHistory {
            key: None,
            should_change_undo_stack: None,
            value,
            on_triggered: Rc::new(on_triggered),
            focus_node,
            undo_stack_modifier: None,
            controller: None,
            child: child.into_widget(),
        }
    }

    /// Dart `UndoHistory(key:)`.
    pub fn key(mut self, key: KeyRef) -> UndoHistory<T, V> {
        self.key = Some(key);
        self
    }

    /// Dart `UndoHistory(shouldChangeUndoStack:)`.
    pub fn should_change_undo_stack(
        mut self,
        should_change_undo_stack: impl Fn(Option<&T>, &T) -> bool + 'static,
    ) -> UndoHistory<T, V> {
        self.should_change_undo_stack = Some(Rc::new(should_change_undo_stack));
        self
    }

    /// Dart `UndoHistory(undoStackModifier:)`.
    pub fn undo_stack_modifier(
        mut self,
        undo_stack_modifier: impl Fn(T) -> T + 'static,
    ) -> UndoHistory<T, V> {
        self.undo_stack_modifier = Some(Rc::new(undo_stack_modifier));
        self
    }

    /// Dart `UndoHistory(controller:)`.
    pub fn controller(mut self, controller: Ref<UndoHistoryController>) -> UndoHistory<T, V> {
        self.controller = Some(controller);
        self
    }
}

impl<T: Clone + PartialEq + 'static, V: ValueListenable<T> + Clone + PartialEq + 'static> Debug
    for UndoHistory<T, V>
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("UndoHistory")
            .field("child", &self.child)
            .finish_non_exhaustive()
    }
}

impl<T: Clone + PartialEq + 'static, V: ValueListenable<T> + Clone + PartialEq + 'static>
    StatefulWidget for UndoHistory<T, V>
{
    type State = UndoHistoryState<T, V>;

    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn create_state(&self) -> UndoHistoryState<T, V> {
        UndoHistoryState {
            state: StateData::new(),
            stack: UndoStack::new(),
            throttle_timer: None,
            throttle_arg: None,
            during_trigger: Rc::new(Cell::new(false)),
            last_value: None,
            controller: None,
        }
    }
}

/// State for a [`UndoHistory`].
///
/// Provides [`undo`](Self::undo), [`redo`](Self::redo), [`can_undo`](Self::can_undo), and
/// [`can_redo`](Self::can_redo) for programmatic access to the undo state for custom undo
/// and redo UI implementations.
pub struct UndoHistoryState<
    T: Clone + PartialEq + 'static,
    V: ValueListenable<T> + Clone + PartialEq + 'static = Ref<ValueNotifier<T>>,
> {
    state: StateData<UndoHistory<T, V>>,
    stack: UndoStack<T>,
    throttle_timer: Option<Timer>,
    throttle_arg: Option<T>,
    during_trigger: Rc<Cell<bool>>,
    // Record the last value to prevent pushing multiple
    // of the same value in a row onto the undo stack. For example, push gets
    // called both in initState and when the EditableText receives focus.
    last_value: Option<T>,
    controller: Option<Ref<UndoHistoryController>>,
}

/// Dart's `try`/`finally` around `_duringTrigger`. Holds the flag by `Rc`, never
/// a borrow of the state, so a panic still clears it.
struct DuringTriggerGuard {
    flag: Rc<Cell<bool>>,
}

impl DuringTriggerGuard {
    fn enter(flag: &Rc<Cell<bool>>) -> DuringTriggerGuard {
        flag.set(true);
        DuringTriggerGuard {
            flag: Rc::clone(flag),
        }
    }
}

impl Drop for DuringTriggerGuard {
    fn drop(&mut self) {
        self.flag.set(false);
    }
}

impl<T: Clone + PartialEq + 'static, V: ValueListenable<T> + Clone + PartialEq + 'static>
    UndoHistoryState<T, V>
{
    fn effective_controller(self: Handle<Self>, app: &mut App) -> Handle<UndoHistoryController> {
        if let Some(controller) = self.widget(app).controller.handle() {
            return controller;
        }
        if let Some(controller) = app.get(self).controller.handle() {
            return controller;
        }
        let controller = UndoHistoryController::new(app);
        let handle = controller.handle();
        app.get_mut(self).controller = Some(controller);
        handle
    }

    /// Reverts the value on the stack to the previous value.
    pub fn undo(self: Handle<Self>, app: &mut App) {
        if app.get(self).stack.current_value().is_none() {
            // Returns early if there is not a first value registered in the history.
            // This is important because, if an undo is received while the initial
            // value is being pushed (a.k.a when the field gets the focus but the
            // throttling delay is pending), the initial push should not be canceled.
            return;
        }
        if app
            .get(self)
            .throttle_timer
            .is_some_and(|timer| timer.is_active(app))
        {
            if let Some(timer) = app.get(self).throttle_timer {
                timer.cancel(app); // Cancel ongoing push, if any.
            }
            let next = app.get(self).stack.current_value().cloned();
            self.update(app, next);
        } else {
            let next = app.get_mut(self).stack.undo().cloned();
            self.update(app, next);
        }
        self.update_state(app);
    }

    /// Updates the value on the stack to the next value.
    pub fn redo(self: Handle<Self>, app: &mut App) {
        let next = app.get_mut(self).stack.redo().cloned();
        self.update(app, next);
        self.update_state(app);
    }

    /// Will be true if there are past values on the stack.
    pub fn can_undo(self: Handle<Self>, app: &App) -> bool {
        app.get(self).stack.can_undo()
    }

    /// Will be true if there are future values on the stack.
    pub fn can_redo(self: Handle<Self>, app: &App) -> bool {
        app.get(self).stack.can_redo()
    }

    fn update_state(self: Handle<Self>, app: &mut App) {
        let can_undo = self.can_undo(app);
        let can_redo = self.can_redo(app);
        let controller = self.effective_controller(app);
        controller.set_value(
            app,
            UndoHistoryValue::new()
                .can_undo(can_undo)
                .can_redo(can_redo),
        );

        if app.platform().target_platform() != TargetPlatform::IOS {
            return;
        }

        if UndoManager::client(app) == Some(self.as_undo_manager_client()) {
            UndoManager::set_undo_state(app, can_undo, can_redo);
        }
    }

    fn undo_from_intent(self: Handle<Self>, app: &mut App) {
        self.undo(app);
    }

    fn redo_from_intent(self: Handle<Self>, app: &mut App) {
        self.redo(app);
    }

    fn update(self: Handle<Self>, app: &mut App, next_value: Option<T>) {
        let Some(next_value) = next_value else {
            return;
        };
        if app.get(self).last_value.as_ref() == Some(&next_value) {
            return;
        }
        app.get_mut(self).last_value = Some(next_value.clone());
        let flag = Rc::clone(&app.get(self).during_trigger);
        let _guard = DuringTriggerGuard::enter(&flag);
        let on_triggered = Rc::clone(&self.widget(app).on_triggered);
        on_triggered(app, next_value.clone());
        debug_assert!(self.widget(app).value.value(app) == &next_value);
    }

    fn push(self: Handle<Self>, app: &mut App) {
        let current = self.widget(app).value.value(app).clone();
        if app.get(self).last_value.as_ref() == Some(&current) {
            return;
        }

        if app.get(self).during_trigger.get() {
            return;
        }

        if let Some(should_change) = self.widget(app).should_change_undo_stack.clone()
            && !should_change(app.get(self).last_value.as_ref(), &current)
        {
            return;
        }

        let next_value = if let Some(modifier) = self.widget(app).undo_stack_modifier.clone() {
            modifier(current)
        } else {
            current
        };
        if app.get(self).last_value.as_ref() == Some(&next_value) {
            return;
        }

        app.get_mut(self).last_value = Some(next_value.clone());

        let timer = self.throttled_push(app, next_value);
        app.get_mut(self).throttle_timer = Some(timer);
    }

    fn handle_focus(self: Handle<Self>, app: &mut App) {
        let focus_node = self.widget(app).focus_node.handle();
        if !focus_node.has_focus(app) {
            if UndoManager::client(app) == Some(self.as_undo_manager_client()) {
                UndoManager::set_client(app, None);
            }

            return;
        }
        UndoManager::set_client(app, Some(self.as_undo_manager_client()));
        self.update_state(app);
    }

    /// Dart's `_throttledPush`: one pending call, latest arg wins.
    fn throttled_push(self: Handle<Self>, app: &mut App, current_arg: T) -> Timer {
        app.get_mut(self).throttle_arg = Some(current_arg);
        if let Some(timer) = app.get(self).throttle_timer
            && timer.is_active(app)
        {
            return timer;
        }
        Timer::new(
            app,
            K_THROTTLE_DURATION,
            Listener::handle_method(self, Self::throttled_push_fire),
        )
    }

    fn throttled_push_fire(self: Handle<Self>, app: &mut App) {
        let arg = app
            .get_mut(self)
            .throttle_arg
            .take()
            .expect("throttled push stores the arg");
        app.get_mut(self).throttle_timer = None;
        app.get_mut(self).stack.push(arg);
        self.update_state(app);
    }
}

impl<T: Clone + PartialEq + 'static, V: ValueListenable<T> + Clone + PartialEq + 'static>
    UndoManagerClient for UndoHistoryState<T, V>
{
    fn handle_platform_undo(self: Handle<Self>, app: &mut App, direction: UndoDirection) {
        match direction {
            UndoDirection::Undo => self.undo(app),
            UndoDirection::Redo => self.redo(app),
        }
    }

    fn undo(self: Handle<Self>, app: &mut App) {
        UndoHistoryState::undo(self, app);
    }

    fn redo(self: Handle<Self>, app: &mut App) {
        UndoHistoryState::redo(self, app);
    }

    fn can_undo(self: Handle<Self>, app: &App) -> bool {
        UndoHistoryState::can_undo(self, app)
    }

    fn can_redo(self: Handle<Self>, app: &App) -> bool {
        UndoHistoryState::can_redo(self, app)
    }
}

impl<T: Clone + PartialEq + 'static, V: ValueListenable<T> + Clone + PartialEq + 'static> State
    for UndoHistoryState<T, V>
{
    type Widget = UndoHistory<T, V>;
    crate::state_accessors!();

    fn init_state(self: Handle<Self>, app: &mut App) {
        self.push(app);
        let value = self.widget(app).value.clone();
        value.add_listener(app, Listener::handle_method(self, Self::push));
        self.handle_focus(app);
        let focus_node = self.widget(app).focus_node.handle();
        focus_node.add_listener(app, Listener::handle_method(self, Self::handle_focus));
        let controller = self.effective_controller(app);
        controller
            .on_undo(app)
            .add_listener(app, Listener::handle_method(self, Self::undo));
        let controller = self.effective_controller(app);
        controller
            .on_redo(app)
            .add_listener(app, Listener::handle_method(self, Self::redo));
    }

    fn did_update_widget(self: Handle<Self>, app: &mut App, old_widget: &UndoHistory<T, V>) {
        let value = self.widget(app).value.clone();
        if value != old_widget.value {
            app.get_mut(self).stack.clear();
            old_widget
                .value
                .remove_listener(app, &Listener::handle_method(self, Self::push));
            value.add_listener(app, Listener::handle_method(self, Self::push));
        }
        let focus_node = self.widget(app).focus_node.handle();
        if focus_node != old_widget.focus_node {
            old_widget
                .focus_node
                .remove_listener(app, &Listener::handle_method(self, Self::handle_focus));
            focus_node.add_listener(app, Listener::handle_method(self, Self::handle_focus));
        }
        if self.widget(app).controller != old_widget.controller {
            let controller = self.effective_controller(app);
            controller
                .on_undo(app)
                .remove_listener(app, &Listener::handle_method(self, Self::undo));
            let controller = self.effective_controller(app);
            controller
                .on_redo(app)
                .remove_listener(app, &Listener::handle_method(self, Self::redo));
            if let Some(controller) = app.get(self).controller.handle() {
                controller.dispose(app);
            }
            app.get_mut(self).controller = None;
            let controller = self.effective_controller(app);
            controller
                .on_undo(app)
                .add_listener(app, Listener::handle_method(self, Self::undo));
            let controller = self.effective_controller(app);
            controller
                .on_redo(app)
                .add_listener(app, Listener::handle_method(self, Self::redo));
        }
    }

    fn dispose(self: Handle<Self>, app: &mut App) {
        if UndoManager::client(app) == Some(self.as_undo_manager_client()) {
            UndoManager::set_client(app, None);
        }

        let value = self.widget(app).value.clone();
        value.remove_listener(app, &Listener::handle_method(self, Self::push));
        let focus_node = self.widget(app).focus_node.handle();
        focus_node.remove_listener(app, &Listener::handle_method(self, Self::handle_focus));
        let controller = self.effective_controller(app);
        controller
            .on_undo(app)
            .remove_listener(app, &Listener::handle_method(self, Self::undo));
        let controller = self.effective_controller(app);
        controller
            .on_redo(app)
            .remove_listener(app, &Listener::handle_method(self, Self::redo));
        if let Some(controller) = app.get(self).controller.handle() {
            controller.dispose(app);
        }
        if let Some(timer) = app.get(self).throttle_timer {
            timer.cancel(app);
        }
    }

    fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
        let child = self.widget(app).child.clone();
        let undo_default = CallbackAction::<UndoTextIntent>::new(
            app,
            Rc::new({
                let this = self;
                move |app, _intent| {
                    this.undo_from_intent(app);
                    None
                }
            }),
        );
        let redo_default = CallbackAction::<RedoTextIntent>::new(
            app,
            Rc::new({
                let this = self;
                move |app, _intent| {
                    this.redo_from_intent(app);
                    None
                }
            }),
        );
        let undo = AnyAction::overridable(app, undo_default.into_erased(), context);
        let redo = AnyAction::overridable(app, redo_default.into_erased(), context);
        Actions::new(
            HashMap::from([
                (TypeId::of::<UndoTextIntent>(), undo),
                (TypeId::of::<RedoTextIntent>(), redo),
            ]),
            child,
        )
        .into_widget()
    }
}

/// Represents whether the current undo stack can undo or redo.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct UndoHistoryValue {
    /// Whether the current undo stack can perform an undo operation.
    pub can_undo: bool,
    /// Whether the current undo stack can perform a redo operation.
    pub can_redo: bool,
}

impl UndoHistoryValue {
    /// A value corresponding to an undo stack that can neither undo nor redo.
    pub const EMPTY: UndoHistoryValue = UndoHistoryValue {
        can_undo: false,
        can_redo: false,
    };

    /// Creates a value for whether the current undo stack can undo or redo.
    ///
    /// The [`can_undo`](Self::can_undo) and [`can_redo`](Self::can_redo) arguments must have a
    /// value, but default to false.
    pub const fn new() -> UndoHistoryValue {
        UndoHistoryValue::EMPTY
    }

    /// Dart `UndoHistoryValue(canUndo:)`.
    pub const fn can_undo(mut self, can_undo: bool) -> UndoHistoryValue {
        self.can_undo = can_undo;
        self
    }

    /// Dart `UndoHistoryValue(canRedo:)`.
    pub const fn can_redo(mut self, can_redo: bool) -> UndoHistoryValue {
        self.can_redo = can_redo;
        self
    }
}

impl Default for UndoHistoryValue {
    fn default() -> UndoHistoryValue {
        UndoHistoryValue::EMPTY
    }
}

/// A controller for the undo history, for example for an editable text field.
///
/// Whenever a change happens to the underlying value that the `UndoHistory` widget tracks,
/// that widget updates the [`value`](Self::value) and the controller notifies its listeners.
/// Listeners can then read the [`can_undo`](UndoHistoryValue::can_undo) and
/// [`can_redo`](UndoHistoryValue::can_redo) properties of the value to discover whether
/// [`undo`](Self::undo) or [`redo`](Self::redo) are possible.
///
/// The controller also has [`undo`](Self::undo) and [`redo`](Self::redo) methods to modify
/// the undo history.
///
/// See also:
///
///  * `EditableText`, which uses the `UndoHistory` widget and allows control of the
///    underlying history using an [`UndoHistoryController`].
pub struct UndoHistoryController {
    change_notifier: ChangeNotifierData,
    value: UndoHistoryValue,
    on_undo: Ref<ChangeNotifierData>,
    on_redo: Ref<ChangeNotifierData>,
}

impl Debug for UndoHistoryController {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("UndoHistoryController")
            .field("value", &self.value)
            .finish()
    }
}

impl UndoHistoryController {
    /// Creates a controller for an `UndoHistory` widget.
    pub fn new(app: &mut App) -> Ref<UndoHistoryController> {
        Self::from_value(app, UndoHistoryValue::EMPTY)
    }

    /// Dart `UndoHistoryController(value:)`.
    pub fn from_value(app: &mut App, value: UndoHistoryValue) -> Ref<UndoHistoryController> {
        let on_undo = app.create(ChangeNotifierData::new());
        let on_redo = app.create(ChangeNotifierData::new());
        app.create(UndoHistoryController {
            change_notifier: ChangeNotifierData::new(),
            value,
            on_undo,
            on_redo,
        })
    }

    /// The current undo/redo capability.
    pub fn value(self: Handle<Self>, app: &App) -> &UndoHistoryValue {
        &app.get(self).value
    }

    /// Setting this notifies listeners when the value differs.
    pub fn set_value(self: Handle<Self>, app: &mut App, new_value: UndoHistoryValue) {
        if app.get(self).value == new_value {
            return;
        }
        app.get_mut(self).value = new_value;
        self.notify_listeners(app);
    }

    /// Notifies listeners that [`undo`](Self::undo) has been called.
    pub fn on_undo(self: Handle<Self>, app: &App) -> Handle<ChangeNotifierData> {
        app.get(self).on_undo.handle()
    }

    /// Notifies listeners that [`redo`](Self::redo) has been called.
    pub fn on_redo(self: Handle<Self>, app: &App) -> Handle<ChangeNotifierData> {
        app.get(self).on_redo.handle()
    }

    /// Reverts the value on the stack to the previous value.
    pub fn undo(self: Handle<Self>, app: &mut App) {
        if !self.value(app).can_undo {
            return;
        }
        let on_undo = self.on_undo(app);
        on_undo.notify_listeners(app);
    }

    /// Updates the value on the stack to the next value.
    pub fn redo(self: Handle<Self>, app: &mut App) {
        if !self.value(app).can_redo {
            return;
        }
        let on_redo = self.on_redo(app);
        on_redo.notify_listeners(app);
    }

    /// Discards any resources used by the object.
    pub fn dispose(self: Handle<Self>, app: &mut App) {
        let on_undo = self.on_undo(app);
        let on_redo = self.on_redo(app);
        ChangeNotifierData::dispose(app, on_undo);
        ChangeNotifierData::dispose(app, on_redo);
        ChangeNotifierData::dispose(app, self);
    }
}

impl ChangeNotifier for UndoHistoryController {
    fn change_notifier_data(&self) -> &ChangeNotifierData {
        &self.change_notifier
    }

    fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
        &mut self.change_notifier
    }
}

/// A data structure representing a chronological list of states that can be undone and redone.
///
/// Dart's `_UndoStack`.
struct UndoStack<T> {
    list: Vec<T>,
    // The index of the current value, or -1 if the list is empty.
    index: i32,
}

// This duration was chosen as a best fit for the behavior of Mac, Linux,
// and Windows undo/redo state save durations, but it is not perfect for any
// of them.
const K_THROTTLE_DURATION: Duration = Duration::from_millis(500);

impl<T: PartialEq> UndoStack<T> {
    /// Creates an instance of [`UndoStack`].
    fn new() -> UndoStack<T> {
        UndoStack {
            list: Vec::new(),
            index: -1,
        }
    }

    /// Returns the current value of the stack.
    fn current_value(&self) -> Option<&T> {
        if self.list.is_empty() {
            None
        } else {
            Some(&self.list[self.index as usize])
        }
    }

    fn can_undo(&self) -> bool {
        !self.list.is_empty() && self.index > 0
    }

    fn can_redo(&self) -> bool {
        !self.list.is_empty() && (self.index as usize) < self.list.len() - 1
    }

    /// Add a new state change to the stack.
    ///
    /// Pushing identical objects will not create multiple entries.
    fn push(&mut self, value: T) {
        if self.list.is_empty() {
            self.index = 0;
            self.list.push(value);
            return;
        }

        debug_assert!((self.index as usize) < self.list.len() && self.index >= 0);

        if self.current_value() == Some(&value) {
            return;
        }

        // If anything has been undone in this stack, remove those irrelevant states
        // before adding the new one.
        if (self.index as usize) != self.list.len() - 1 {
            self.list.truncate((self.index as usize) + 1);
        }
        self.list.push(value);
        self.index = self.list.len() as i32 - 1;
    }

    /// Returns the current value after an undo operation.
    ///
    /// An undo operation moves the current value to the previously pushed value, if any.
    ///
    /// Iff the stack is completely empty, then returns null.
    fn undo(&mut self) -> Option<&T> {
        if self.list.is_empty() {
            return None;
        }

        debug_assert!((self.index as usize) < self.list.len() && self.index >= 0);

        if self.index != 0 {
            self.index -= 1;
        }

        self.current_value()
    }

    /// Returns the current value after a redo operation.
    ///
    /// A redo operation moves the current value to the value that was last undone, if any.
    ///
    /// Iff the stack is completely empty, then returns null.
    fn redo(&mut self) -> Option<&T> {
        if self.list.is_empty() {
            return None;
        }

        debug_assert!((self.index as usize) < self.list.len() && self.index >= 0);

        if (self.index as usize) < self.list.len() - 1 {
            self.index += 1;
        }

        self.current_value()
    }

    /// Remove everything from the stack.
    fn clear(&mut self) {
        self.list.clear();
        self.index = -1;
    }
}

impl<T: Debug> Debug for UndoStack<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "_UndoStack {:?}", self.list)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::widgets::focus_manager::FocusNode;
    use std::cell::Cell;
    use std::rc::Rc;
    use std::time::Duration;
    use waft_foundation::ListenableObject;
    use waft_foundation::{AppCell, Listener, ValueListenable};

    use crate::framework::GlobalKey;
    use crate::test_harness::Harness;
    use crate::widgets::basic::SizedBox;

    #[test]
    fn controller_starts_empty() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let controller = UndoHistoryController::new(&mut app);
        assert_eq!(controller.value(&app), &UndoHistoryValue::EMPTY);
    }

    #[test]
    fn undo_notifies_on_undo_when_can_undo() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let controller =
            UndoHistoryController::from_value(&mut app, UndoHistoryValue::new().can_undo(true));
        let fired = Rc::new(Cell::new(false));
        let seen = Rc::clone(&fired);
        controller
            .on_undo(&app)
            .add_listener(&mut app, Listener::new(move |_app| seen.set(true)));
        controller.undo(&mut app);
        assert!(fired.get());
    }

    #[test]
    fn undo_is_a_no_op_when_empty() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let controller = UndoHistoryController::new(&mut app);
        let fired = Rc::new(Cell::new(false));
        let seen = Rc::clone(&fired);
        controller
            .on_undo(&app)
            .add_listener(&mut app, Listener::new(move |_app| seen.set(true)));
        controller.undo(&mut app);
        assert!(!fired.get());
    }

    #[test]
    fn stack_push_undo_redo() {
        let mut stack = UndoStack::new();
        stack.push("a");
        stack.push("b");
        stack.push("c");
        assert_eq!(stack.current_value(), Some(&"c"));
        assert!(stack.can_undo());
        assert!(!stack.can_redo());
        assert_eq!(stack.undo(), Some(&"b"));
        assert!(stack.can_redo());
        assert_eq!(stack.redo(), Some(&"c"));
        stack.undo();
        stack.push("d");
        assert_eq!(stack.current_value(), Some(&"d"));
        assert!(!stack.can_redo());
        assert_eq!(stack.undo(), Some(&"b"));
    }

    #[test]
    fn push_two_values_then_undo_restores() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let value = app.create(ValueNotifier::new(0i32));
        let focus_node = FocusNode::new(&mut app);
        let key = GlobalKey::new();
        let notifier = value.handle();
        let widget = UndoHistory::new(
            value.clone(),
            move |app, next| {
                notifier.set_value(app, next);
            },
            focus_node.into_erased(),
            SizedBox::shrink(),
        )
        .key(key.clone().into());
        let harness = Harness::mount(&mut app, widget.into_widget());
        harness.pump(&mut app);
        drop(app);
        cell.elapse(Duration::from_millis(500));

        let mut app = cell.borrow_mut();
        value.set_value(&mut app, 1);
        drop(app);
        cell.elapse(Duration::from_millis(500));

        let mut app = cell.borrow_mut();
        let state = key
            .current_state::<UndoHistoryState<i32>>(&app)
            .expect("the history is mounted");
        assert!(state.can_undo(&app));
        assert!(!state.can_redo(&app));
        state.undo(&mut app);
        assert_eq!(*value.value(&app), 0);
        assert!(!state.can_undo(&app));
        assert!(state.can_redo(&app));
        state.redo(&mut app);
        assert_eq!(*value.value(&app), 1);
    }
}