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

use std::any::Any;
use std::cell::Cell;
use std::fmt::{self, Debug};
use std::rc::Rc;

use waft_foundation::{App, Handle};
use waft_gestures::{DragEndDetails, DragStartDetails, DragUpdateDetails};
use waft_rendering::ScrollDirection;

use crate::framework::{BuildContext, Element, Notification, NotificationNode, NotificationTarget};
use crate::widgets::notification_listener::LayoutChangedNotification;
use crate::widgets::scroll_metrics::ScrollMetrics;

/// The fields of Dart's `ViewportNotificationMixin`.
///
/// The counter is a [`Cell`] because a notification bubbles as `&dyn Notification`: the
/// element that increments it never holds the notification exclusively.
#[derive(Debug, Default)]
pub struct ViewportNotificationData {
    depth: Cell<u32>,
}

impl ViewportNotificationData {
    /// A notification that has not bubbled through any viewport yet.
    pub fn new() -> ViewportNotificationData {
        ViewportNotificationData::default()
    }

    /// Dart's `ScrollUpdateNotification(depth:)`, which seeds the counter.
    pub fn with_depth(depth: u32) -> ViewportNotificationData {
        ViewportNotificationData {
            depth: Cell::new(depth),
        }
    }

    /// The counter a viewport element increments, for
    /// [`Notification::viewport_depth`](crate::Notification::viewport_depth).
    pub fn depth_cell(&self) -> &Cell<u32> {
        &self.depth
    }
}

/// Mixin for [`Notification`]s that track how many `RenderAbstractViewport` they
/// have bubbled through.
///
/// This is used by [`ScrollNotification`] and `OverscrollIndicatorNotification`.
pub trait ViewportNotificationMixin: Notification {
    /// Dart's mixin fields, held under the field `viewport_notification`.
    fn viewport_notification_data(&self) -> &ViewportNotificationData;

    /// The number of viewports that this notification has bubbled through.
    ///
    /// Typically listeners only respond to notifications with a
    /// [`depth`](Self::depth) of zero.
    ///
    /// Specifically, this is the number of widgets representing
    /// `RenderAbstractViewport` render objects through which this notification
    /// has bubbled.
    fn depth(&self) -> u32 {
        self.viewport_notification_data().depth.get()
    }

    /// Dart's `debugFillDescription`, which [`Debug`] writes out.
    fn debug_fill_description(&self, description: &mut Vec<String>) {
        let depth = self.depth();
        let locality = if depth == 0 { "local" } else { "remote" };
        description.push(format!("depth: {depth} ({locality})"));
    }
}

/// A mixin that allows [`Element`]s containing `Viewport`-like widgets to correctly
/// modify the notification depth of a [`ViewportNotificationMixin`].
///
/// The two members are the `Element` virtuals Dart reaches through
/// `NotifiableElementMixin`; an element that mixes this in forwards both from its
/// `impl Element`.
///
/// See also:
///   * `Viewport`, which creates a custom `MultiChildRenderObjectElement` that mixes
///     this in.
pub trait ViewportElementMixin: Element {
    /// `NotifiableElementMixin.attachNotificationTree`: a node for this element.
    fn attach_notification_tree(self: Handle<Self>, app: &mut App) {
        let this = self.as_element();
        let parent_tree = this
            .parent(app)
            .and_then(|parent| parent.notification_tree(app));
        this.set_notification_tree(app, Some(Rc::new(NotificationNode::new(parent_tree, this))));
    }

    /// Counts this viewport, then lets the notification keep bubbling.
    fn on_notification(self: Handle<Self>, app: &mut App, notification: &dyn Notification) -> bool {
        let _ = app;
        if let Some(depth) = notification.viewport_depth() {
            depth.set(depth.get() + 1);
        }
        false
    }
}

/// The fields of Dart's `ScrollNotification` base class.
pub struct ScrollNotificationData {
    /// A description of a `Scrollable`'s contents, useful for modeling the state
    /// of its viewport.
    pub metrics: Rc<dyn ScrollMetrics>,

    /// The build context of the widget that fired this notification.
    ///
    /// This can be used to find the scrollable's render objects to determine the
    /// size of the viewport, for instance.
    pub context: Option<BuildContext>,
}

/// A [`Notification`] related to scrolling.
///
/// `Scrollable` widgets notify their ancestors about scrolling-related changes.
/// The notifications have the following lifecycle:
///
///  * A [`ScrollStartNotification`], which indicates that the widget has started
///    scrolling.
///  * Zero or more [`ScrollUpdateNotification`]s, which indicate that the widget
///    has changed its scroll position, mixed with zero or more
///    [`OverscrollNotification`]s, which indicate that the widget has not changed
///    its scroll position because the change would have caused its scroll
///    position to go outside its scroll bounds.
///  * Interspersed with the [`ScrollUpdateNotification`]s and
///    [`OverscrollNotification`]s are zero or more [`UserScrollNotification`]s,
///    which indicate that the user has changed the direction in which they are
///    scrolling.
///  * A [`ScrollEndNotification`], which indicates that the widget has stopped
///    scrolling.
///  * A [`UserScrollNotification`], with a [`direction`](UserScrollNotification::direction)
///    of [`ScrollDirection::Idle`].
///
/// Notifications bubble up through the tree, which means a given
/// `NotificationListener` will receive notifications for all descendant
/// `Scrollable` widgets. To focus on notifications from the nearest
/// `Scrollable` descendant, check that the [`depth`](ViewportNotificationMixin::depth)
/// of the notification is zero.
///
/// When a scroll notification is received by a `NotificationListener`, the
/// listener will have already completed build and layout, and it is therefore
/// too late for that widget to call `State::set_state`. Any attempt to adjust the
/// build or layout based on a scroll notification would result in a layout that
/// lagged one frame behind, which is a poor user experience. Scroll
/// notifications are therefore primarily useful for paint effects (since paint
/// happens after layout).
///
/// To drive layout based on the scroll position, consider listening to the
/// `ScrollPosition` directly (or indirectly via a `ScrollController`).
pub trait ScrollNotification: LayoutChangedNotification + ViewportNotificationMixin {
    /// Dart's base-class fields, held under the field `scroll_notification`.
    fn scroll_notification_data(&self) -> &ScrollNotificationData;

    /// A description of a `Scrollable`'s contents, useful for modeling the state
    /// of its viewport.
    fn metrics(&self) -> &Rc<dyn ScrollMetrics> {
        &self.scroll_notification_data().metrics
    }

    /// The build context of the widget that fired this notification.
    fn context(&self) -> Option<BuildContext> {
        self.scroll_notification_data().context
    }
}

impl NotificationTarget for dyn ScrollNotification {
    fn cast(notification: &dyn Notification) -> Option<&dyn ScrollNotification> {
        notification.as_scroll_notification()
    }
}

/// Dart's `ScrollNotification.debugFillDescription`: the mixin's depth, then the metrics.
fn fill_scroll_description(notification: &dyn ScrollNotification, description: &mut Vec<String>) {
    ViewportNotificationMixin::debug_fill_description(notification, description);
    description.push(format!("{:?}", notification.metrics()));
}

/// A notification that a `Scrollable` widget has started scrolling.
///
/// See also:
///
///  * [`ScrollEndNotification`], which indicates that scrolling has stopped.
///  * [`ScrollNotification`], which describes the notification lifecycle.
pub struct ScrollStartNotification {
    viewport_notification: ViewportNotificationData,
    scroll_notification: ScrollNotificationData,

    /// If the `Scrollable` started scrolling because of a drag, the details about
    /// that drag start.
    ///
    /// Otherwise, null.
    pub drag_details: Option<DragStartDetails>,
}

impl ScrollStartNotification {
    /// Creates a notification that a `Scrollable` widget has started scrolling.
    pub fn new(
        metrics: Rc<dyn ScrollMetrics>,
        context: Option<BuildContext>,
    ) -> ScrollStartNotification {
        ScrollStartNotification {
            viewport_notification: ViewportNotificationData::new(),
            scroll_notification: ScrollNotificationData { metrics, context },
            drag_details: None,
        }
    }

    /// Dart `ScrollStartNotification(dragDetails:)`.
    pub fn drag_details(mut self, drag_details: DragStartDetails) -> ScrollStartNotification {
        self.drag_details = Some(drag_details);
        self
    }
}

impl Notification for ScrollStartNotification {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn viewport_depth(&self) -> Option<&Cell<u32>> {
        Some(&self.viewport_notification.depth)
    }

    fn as_scroll_notification(&self) -> Option<&dyn ScrollNotification> {
        Some(self)
    }

    fn as_layout_changed_notification(&self) -> Option<&dyn LayoutChangedNotification> {
        Some(self)
    }
}

impl LayoutChangedNotification for ScrollStartNotification {}

impl ViewportNotificationMixin for ScrollStartNotification {
    fn viewport_notification_data(&self) -> &ViewportNotificationData {
        &self.viewport_notification
    }
}

impl ScrollNotification for ScrollStartNotification {
    fn scroll_notification_data(&self) -> &ScrollNotificationData {
        &self.scroll_notification
    }
}

impl Debug for ScrollStartNotification {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut description = Vec::new();
        fill_scroll_description(self, &mut description);
        if let Some(drag_details) = &self.drag_details {
            description.push(format!("{drag_details:?}"));
        }
        write!(f, "ScrollStartNotification({})", description.join(", "))
    }
}

/// A notification that a `Scrollable` widget has changed its scroll position.
///
/// See also:
///
///  * [`OverscrollNotification`], which indicates that a `Scrollable` widget
///    has not changed its scroll position because the change would have caused
///    its scroll position to go outside its scroll bounds.
///  * [`ScrollNotification`], which describes the notification lifecycle.
pub struct ScrollUpdateNotification {
    viewport_notification: ViewportNotificationData,
    scroll_notification: ScrollNotificationData,

    /// If the `Scrollable` changed its scroll position because of a drag, the
    /// details about that drag update.
    ///
    /// Otherwise, null.
    pub drag_details: Option<DragUpdateDetails>,

    /// The distance by which the `Scrollable` was scrolled, in logical pixels.
    pub scroll_delta: Option<f64>,
}

impl ScrollUpdateNotification {
    /// Creates a notification that a `Scrollable` widget has changed its scroll
    /// position.
    pub fn new(metrics: Rc<dyn ScrollMetrics>, context: BuildContext) -> ScrollUpdateNotification {
        ScrollUpdateNotification {
            viewport_notification: ViewportNotificationData::new(),
            scroll_notification: ScrollNotificationData {
                metrics,
                context: Some(context),
            },
            drag_details: None,
            scroll_delta: None,
        }
    }

    /// Dart `ScrollUpdateNotification(dragDetails:)`.
    pub fn drag_details(mut self, drag_details: DragUpdateDetails) -> ScrollUpdateNotification {
        self.drag_details = Some(drag_details);
        self
    }

    /// Dart `ScrollUpdateNotification(scrollDelta:)`.
    pub fn scroll_delta(mut self, scroll_delta: f64) -> ScrollUpdateNotification {
        self.scroll_delta = Some(scroll_delta);
        self
    }

    /// Dart `ScrollUpdateNotification(depth:)`, which seeds the viewport depth. Named
    /// `with_depth` because `depth` is the mixin's getter.
    pub fn with_depth(mut self, depth: u32) -> ScrollUpdateNotification {
        self.viewport_notification = ViewportNotificationData::with_depth(depth);
        self
    }
}

impl Notification for ScrollUpdateNotification {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn viewport_depth(&self) -> Option<&Cell<u32>> {
        Some(&self.viewport_notification.depth)
    }

    fn as_scroll_notification(&self) -> Option<&dyn ScrollNotification> {
        Some(self)
    }

    fn as_layout_changed_notification(&self) -> Option<&dyn LayoutChangedNotification> {
        Some(self)
    }
}

impl LayoutChangedNotification for ScrollUpdateNotification {}

impl ViewportNotificationMixin for ScrollUpdateNotification {
    fn viewport_notification_data(&self) -> &ViewportNotificationData {
        &self.viewport_notification
    }
}

impl ScrollNotification for ScrollUpdateNotification {
    fn scroll_notification_data(&self) -> &ScrollNotificationData {
        &self.scroll_notification
    }
}

impl Debug for ScrollUpdateNotification {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut description = Vec::new();
        fill_scroll_description(self, &mut description);
        description.push(match self.scroll_delta {
            Some(scroll_delta) => format!("scrollDelta: {scroll_delta:?}"),
            None => "scrollDelta: null".to_string(),
        });
        if let Some(drag_details) = &self.drag_details {
            description.push(format!("{drag_details:?}"));
        }
        write!(f, "ScrollUpdateNotification({})", description.join(", "))
    }
}

/// A notification that a `Scrollable` widget has not changed its scroll position
/// because the change would have caused its scroll position to go outside of
/// its scroll bounds.
///
/// See also:
///
///  * [`ScrollUpdateNotification`], which indicates that a `Scrollable` widget
///    has changed its scroll position.
///  * [`ScrollNotification`], which describes the notification lifecycle.
pub struct OverscrollNotification {
    viewport_notification: ViewportNotificationData,
    scroll_notification: ScrollNotificationData,

    /// If the `Scrollable` overscrolled because of a drag, the details about that
    /// drag update.
    ///
    /// Otherwise, null.
    pub drag_details: Option<DragUpdateDetails>,

    /// The number of logical pixels that the `Scrollable` avoided scrolling.
    ///
    /// This will be negative for overscroll on the "start" side and positive for
    /// overscroll on the "end" side.
    pub overscroll: f64,

    /// The velocity at which the `ScrollPosition` was changing when this
    /// overscroll happened.
    ///
    /// This will typically be 0.0 for touch-driven overscrolls, and positive
    /// for overscrolls that happened from a `BallisticScrollActivity` or
    /// `DrivenScrollActivity`.
    pub velocity: f64,
}

impl OverscrollNotification {
    /// Creates a notification that a `Scrollable` widget has changed its scroll
    /// position outside of its scroll bounds.
    pub fn new(
        metrics: Rc<dyn ScrollMetrics>,
        context: BuildContext,
        overscroll: f64,
    ) -> OverscrollNotification {
        debug_assert!(overscroll.is_finite());
        debug_assert!(overscroll != 0.0);
        OverscrollNotification {
            viewport_notification: ViewportNotificationData::new(),
            scroll_notification: ScrollNotificationData {
                metrics,
                context: Some(context),
            },
            drag_details: None,
            overscroll,
            velocity: 0.0,
        }
    }

    /// Dart `OverscrollNotification(dragDetails:)`.
    pub fn drag_details(mut self, drag_details: DragUpdateDetails) -> OverscrollNotification {
        self.drag_details = Some(drag_details);
        self
    }

    /// Dart `OverscrollNotification(velocity:)`.
    pub fn velocity(mut self, velocity: f64) -> OverscrollNotification {
        self.velocity = velocity;
        self
    }
}

impl Notification for OverscrollNotification {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn viewport_depth(&self) -> Option<&Cell<u32>> {
        Some(&self.viewport_notification.depth)
    }

    fn as_scroll_notification(&self) -> Option<&dyn ScrollNotification> {
        Some(self)
    }

    fn as_layout_changed_notification(&self) -> Option<&dyn LayoutChangedNotification> {
        Some(self)
    }
}

impl LayoutChangedNotification for OverscrollNotification {}

impl ViewportNotificationMixin for OverscrollNotification {
    fn viewport_notification_data(&self) -> &ViewportNotificationData {
        &self.viewport_notification
    }
}

impl ScrollNotification for OverscrollNotification {
    fn scroll_notification_data(&self) -> &ScrollNotificationData {
        &self.scroll_notification
    }
}

impl Debug for OverscrollNotification {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut description = Vec::new();
        fill_scroll_description(self, &mut description);
        description.push(format!("overscroll: {:.1}", self.overscroll));
        description.push(format!("velocity: {:.1}", self.velocity));
        if let Some(drag_details) = &self.drag_details {
            description.push(format!("{drag_details:?}"));
        }
        write!(f, "OverscrollNotification({})", description.join(", "))
    }
}

/// A notification that a `Scrollable` widget has stopped scrolling.
///
/// See also:
///
///  * [`ScrollStartNotification`], which indicates that scrolling has started.
///  * [`ScrollNotification`], which describes the notification lifecycle.
pub struct ScrollEndNotification {
    viewport_notification: ViewportNotificationData,
    scroll_notification: ScrollNotificationData,

    /// If the `Scrollable` stopped scrolling because of a drag, the details about
    /// that drag end.
    ///
    /// Otherwise, null.
    ///
    /// If a drag ends with some residual velocity, a typical [`ScrollPhysics`] will
    /// start a ballistic scroll, which delays the [`ScrollEndNotification`] until
    /// the ballistic simulation completes, at which time
    /// [`drag_details`](Self::drag_details) will be null. If the residual velocity is too
    /// small to trigger ballistic scrolling, then the [`ScrollEndNotification`] will be
    /// dispatched immediately and [`drag_details`](Self::drag_details) will be non-null.
    ///
    /// [`ScrollPhysics`]: crate::widgets::scroll_physics::ScrollPhysics
    pub drag_details: Option<DragEndDetails>,
}

impl ScrollEndNotification {
    /// Creates a notification that a `Scrollable` widget has stopped scrolling.
    pub fn new(metrics: Rc<dyn ScrollMetrics>, context: BuildContext) -> ScrollEndNotification {
        ScrollEndNotification {
            viewport_notification: ViewportNotificationData::new(),
            scroll_notification: ScrollNotificationData {
                metrics,
                context: Some(context),
            },
            drag_details: None,
        }
    }

    /// Dart `ScrollEndNotification(dragDetails:)`.
    pub fn drag_details(mut self, drag_details: DragEndDetails) -> ScrollEndNotification {
        self.drag_details = Some(drag_details);
        self
    }
}

impl Notification for ScrollEndNotification {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn viewport_depth(&self) -> Option<&Cell<u32>> {
        Some(&self.viewport_notification.depth)
    }

    fn as_scroll_notification(&self) -> Option<&dyn ScrollNotification> {
        Some(self)
    }

    fn as_layout_changed_notification(&self) -> Option<&dyn LayoutChangedNotification> {
        Some(self)
    }
}

impl LayoutChangedNotification for ScrollEndNotification {}

impl ViewportNotificationMixin for ScrollEndNotification {
    fn viewport_notification_data(&self) -> &ViewportNotificationData {
        &self.viewport_notification
    }
}

impl ScrollNotification for ScrollEndNotification {
    fn scroll_notification_data(&self) -> &ScrollNotificationData {
        &self.scroll_notification
    }
}

impl Debug for ScrollEndNotification {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut description = Vec::new();
        fill_scroll_description(self, &mut description);
        if let Some(drag_details) = &self.drag_details {
            description.push(format!("{drag_details:?}"));
        }
        write!(f, "ScrollEndNotification({})", description.join(", "))
    }
}

/// A notification that the user has changed the [`ScrollDirection`] in which they
/// are scrolling, or have stopped scrolling.
///
/// For the direction that the `ScrollView` is oriented to, and the direction
/// contents are being laid out in, see `AxisDirection` and `GrowthDirection`.
///
/// See also:
///
///  * [`ScrollNotification`], which describes the notification lifecycle.
pub struct UserScrollNotification {
    viewport_notification: ViewportNotificationData,
    scroll_notification: ScrollNotificationData,

    /// The direction in which the user is scrolling.
    ///
    /// This does not represent the current `AxisDirection` or `GrowthDirection`
    /// of the `Viewport`, which respectively represent the direction that the
    /// scroll offset is increasing in, and the direction that contents are being
    /// laid out in.
    pub direction: ScrollDirection,
}

impl UserScrollNotification {
    /// Creates a notification that the user has changed the direction in which
    /// they are scrolling.
    pub fn new(
        metrics: Rc<dyn ScrollMetrics>,
        context: BuildContext,
        direction: ScrollDirection,
    ) -> UserScrollNotification {
        UserScrollNotification {
            viewport_notification: ViewportNotificationData::new(),
            scroll_notification: ScrollNotificationData {
                metrics,
                context: Some(context),
            },
            direction,
        }
    }
}

impl Notification for UserScrollNotification {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn viewport_depth(&self) -> Option<&Cell<u32>> {
        Some(&self.viewport_notification.depth)
    }

    fn as_scroll_notification(&self) -> Option<&dyn ScrollNotification> {
        Some(self)
    }

    fn as_layout_changed_notification(&self) -> Option<&dyn LayoutChangedNotification> {
        Some(self)
    }
}

impl LayoutChangedNotification for UserScrollNotification {}

impl ViewportNotificationMixin for UserScrollNotification {
    fn viewport_notification_data(&self) -> &ViewportNotificationData {
        &self.viewport_notification
    }
}

impl ScrollNotification for UserScrollNotification {
    fn scroll_notification_data(&self) -> &ScrollNotificationData {
        &self.scroll_notification
    }
}

impl Debug for UserScrollNotification {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut description = Vec::new();
        fill_scroll_description(self, &mut description);
        description.push(format!("direction: {:?}", self.direction));
        write!(f, "UserScrollNotification({})", description.join(", "))
    }
}

/// A predicate for [`ScrollNotification`], used to customize widgets that
/// listen to notifications from their children.
pub type ScrollNotificationPredicate = Rc<dyn Fn(&dyn ScrollNotification) -> bool>;

/// A [`ScrollNotificationPredicate`] that checks whether `notification.depth() == 0`,
/// which means that the notification did not bubble through any intervening scrolling
/// widgets.
pub fn default_scroll_notification_predicate(notification: &dyn ScrollNotification) -> bool {
    notification.depth() == 0
}

#[cfg(test)]
mod tests {
    use std::cell::RefCell;
    use waft_foundation::AppCell;

    use waft_painting::AxisDirection;

    use super::*;
    use crate::framework::{
        AnyElement, ComponentElement, ComponentElementData, ElementData, IntoWidget, KeyRef,
        ProxyElement, Slot, Widget, WidgetKind, WidgetRef, component_element_overrides,
        downcast_widget,
    };
    use crate::test_harness::Harness;
    use crate::widgets::basic::{Builder, SizedBox};
    use crate::widgets::notification_listener::{
        NotificationListener, PlainLayoutChangedNotification,
    };
    use crate::widgets::scroll_metrics::FixedScrollMetrics;

    /// A stand-in for `Viewport`: a proxy whose element mixes in
    /// [`ViewportElementMixin`], so a notification bubbling past it is one viewport deeper.
    #[derive(Debug)]
    struct TestViewport {
        child: WidgetRef,
    }

    fn viewport<K>(child: impl IntoWidget<K>) -> WidgetRef {
        Rc::new(TestViewport {
            child: child.into_widget(),
        })
    }

    impl Widget for TestViewport {
        fn key(&self) -> Option<&KeyRef> {
            None
        }

        fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement {
            let element = app.create(TestViewportElement {
                element: ElementData::new(this),
                component: ComponentElementData::default(),
            });
            element.as_element()
        }

        fn as_any(&self) -> &dyn Any {
            self
        }

        fn widget_type(&self) -> std::any::TypeId {
            std::any::TypeId::of::<TestViewport>()
        }

        fn kind(&self) -> WidgetKind {
            WidgetKind::Other
        }
    }

    struct TestViewportElement {
        element: ElementData,
        component: ComponentElementData,
    }

    impl ComponentElement for TestViewportElement {
        fn component_data(self: Handle<Self>, app: &App) -> &ComponentElementData {
            &app.get(self).component
        }

        fn component_data_mut(self: Handle<Self>, app: &mut App) -> &mut ComponentElementData {
            &mut app.get_mut(self).component
        }

        fn build(self: Handle<Self>, app: &mut App) -> WidgetRef {
            self.proxied_child(app)
        }
    }

    impl ProxyElement for TestViewportElement {
        fn proxied_child(self: Handle<Self>, app: &App) -> WidgetRef {
            downcast_widget::<TestViewport>(&**self.as_element().widget(app))
                .expect("a TestViewportElement holds its TestViewport")
                .child
                .clone()
        }

        fn notify_clients(self: Handle<Self>, _app: &mut App, _old_widget: WidgetRef) {}
    }

    impl ViewportElementMixin for TestViewportElement {}

    impl Element for TestViewportElement {
        crate::element_accessors!();
        component_element_overrides!();

        fn mount(
            self: Handle<Self>,
            app: &mut App,
            parent: Option<AnyElement>,
            new_slot: Option<Slot>,
        ) {
            ComponentElement::mount(self, app, parent, new_slot);
        }

        fn update(self: Handle<Self>, app: &mut App, new_widget: WidgetRef) {
            ProxyElement::update(self, app, new_widget);
        }

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

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

        fn on_notification(
            self: Handle<Self>,
            app: &mut App,
            notification: &dyn Notification,
        ) -> bool {
            ViewportElementMixin::on_notification(self, app, notification)
        }
    }

    fn test_metrics() -> Rc<dyn ScrollMetrics> {
        Rc::new(FixedScrollMetrics::new(
            Some(0.0),
            Some(400.0),
            Some(20.0),
            Some(600.0),
            AxisDirection::Down,
            3.0,
        ))
    }

    type Log = Rc<RefCell<Vec<(&'static str, u32)>>>;

    fn listener(log: &Log, name: &'static str, child: WidgetRef) -> WidgetRef {
        let log = Rc::clone(log);
        NotificationListener::<ScrollUpdateNotification>::new(child)
            .on_notification(move |_app, notification| {
                log.borrow_mut().push((name, notification.depth()));
                false
            })
            .into_widget()
    }

    #[test]
    fn a_family_listener_receives_every_kind_of_scroll_notification() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let seen: Rc<RefCell<Vec<String>>> = Rc::default();
        let dispatcher = Builder::new(|app, context| {
            ScrollStartNotification::new(test_metrics(), Some(context))
                .dispatch(app, Some(context));
            ScrollUpdateNotification::new(test_metrics(), context)
                .scroll_delta(4.0)
                .dispatch(app, Some(context));
            ScrollEndNotification::new(test_metrics(), context).dispatch(app, Some(context));
            SizedBox::shrink().into_widget()
        });
        let tree = NotificationListener::<dyn ScrollNotification>::new(dispatcher)
            .on_notification({
                let seen = Rc::clone(&seen);
                move |_app, notification| {
                    seen.borrow_mut().push(format!("{notification:?}"));
                    false
                }
            })
            .into_widget();
        let harness = Harness::mount(&mut app, tree);
        harness.pump(&mut app);
        let seen = seen.borrow();
        assert_eq!(seen.len(), 3);
        assert!(
            seen[0].starts_with("ScrollStartNotification"),
            "{}",
            seen[0]
        );
        assert!(
            seen[1].starts_with("ScrollUpdateNotification"),
            "{}",
            seen[1]
        );
        assert!(seen[2].starts_with("ScrollEndNotification"), "{}", seen[2]);
    }

    #[test]
    fn a_layout_changed_listener_receives_scroll_notifications_and_plain_layout_changes() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let seen: Rc<RefCell<Vec<String>>> = Rc::default();
        let dispatcher = Builder::new(|app, context| {
            PlainLayoutChangedNotification.dispatch(app, Some(context));
            ScrollUpdateNotification::new(test_metrics(), context)
                .scroll_delta(4.0)
                .dispatch(app, Some(context));
            UserScrollNotification::new(test_metrics(), context, ScrollDirection::Idle)
                .dispatch(app, Some(context));
            SizedBox::shrink().into_widget()
        });
        let tree = NotificationListener::<dyn LayoutChangedNotification>::new(dispatcher)
            .on_notification({
                let seen = Rc::clone(&seen);
                move |_app, notification| {
                    seen.borrow_mut().push(format!("{notification:?}"));
                    false
                }
            })
            .into_widget();
        let harness = Harness::mount(&mut app, tree);
        harness.pump(&mut app);
        let seen = seen.borrow();
        assert_eq!(seen.len(), 3);
        assert_eq!(seen[0], "PlainLayoutChangedNotification");
        assert!(
            seen[1].starts_with("ScrollUpdateNotification"),
            "{}",
            seen[1]
        );
        assert!(seen[2].starts_with("UserScrollNotification"), "{}", seen[2]);
    }

    #[test]
    fn a_scroll_update_notification_bubbles_and_each_viewport_adds_to_its_depth() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let log: Log = Rc::default();
        let dispatcher = Builder::new(|app, context| {
            ScrollUpdateNotification::new(test_metrics(), context)
                .scroll_delta(4.0)
                .dispatch(app, Some(context));
            SizedBox::shrink().into_widget()
        });
        let tree = listener(
            &log,
            "outer",
            viewport(listener(
                &log,
                "middle",
                viewport(listener(&log, "inner", dispatcher.into_widget())),
            )),
        );
        let harness = Harness::mount(&mut app, tree);
        harness.pump(&mut app);

        assert_eq!(
            *log.borrow(),
            vec![("inner", 0), ("middle", 1), ("outer", 2)]
        );
    }

    #[test]
    fn a_notification_reports_its_depth_and_its_metrics() {
        let notification = ScrollUpdateNotification::new(
            test_metrics(),
            // A `BuildContext` is required; the element tree is not walked by `Debug`.
            {
                let cell = AppCell::new();
                let mut app = cell.borrow_mut();
                let harness = Harness::mount(&mut app, SizedBox::shrink().into_widget());
                harness.root.as_element()
            },
        )
        .scroll_delta(4.0)
        .with_depth(2);

        assert_eq!(notification.depth(), 2);
        assert!(default_scroll_notification_predicate(
            &ScrollUpdateNotification::new(
                test_metrics(),
                notification.context().expect("a context was given"),
            )
        ));
        assert!(!default_scroll_notification_predicate(&notification));
        assert_eq!(
            format!("{notification:?}"),
            "ScrollUpdateNotification(depth: 2 (remote), \
             FixedScrollMetrics(20.0..[600.0]..380.0), scrollDelta: 4.0)"
        );
    }

    #[test]
    fn the_overscroll_notification_reports_its_overscroll_and_velocity() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let harness = Harness::mount(&mut app, SizedBox::shrink().into_widget());
        let context = harness.root.as_element();
        let notification =
            OverscrollNotification::new(test_metrics(), context, -12.0).velocity(3.0);
        assert_eq!(notification.overscroll, -12.0);
        assert_eq!(
            format!("{notification:?}"),
            "OverscrollNotification(depth: 0 (local), \
             FixedScrollMetrics(20.0..[600.0]..380.0), overscroll: -12.0, velocity: 3.0)"
        );

        let user_scroll =
            UserScrollNotification::new(test_metrics(), context, ScrollDirection::Reverse);
        assert_eq!(
            format!("{user_scroll:?}"),
            "UserScrollNotification(depth: 0 (local), \
             FixedScrollMetrics(20.0..[600.0]..380.0), direction: Reverse)"
        );
    }
}