waft-widgets 0.13.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
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
//! Flutter counterpart: `widgets/viewport.dart`.
//!
//! `_ViewportElement.debugVisitOnstageChildren` waits with the other diagnostics; see
//! `PORTING.md`.

use std::fmt::Debug;

use waft_embedder::Clip;
use waft_foundation::{
    App, DiagnosticPropertiesBuilder, DiagnosticsProperty, DoubleProperty, EnumProperty, Handle,
};
use waft_painting::{AxisDirection, text_direction_to_axis_direction};
use waft_rendering::{
    AnyRenderObject, AnyRenderSliver, CacheExtentStyle, ErasedRenderObject, RenderBox,
    RenderHandle, RenderShrinkWrappingViewport, RenderViewport, RenderViewportBase,
    ScrollCacheExtent, SliverPaintOrder, ViewportOffsetRef,
};

use crate::framework::{
    AnyElement, BuildContext, Element, ElementData, KeyRef, MultiChildRenderObjectElementBase,
    MultiChildRenderObjectElementData, MultiChildRenderObjectWidget, RenderObjectElement,
    RenderObjectElementData, RenderObjectElementWidget, RenderObjectWidget, Slot, WidgetRef,
    downcast_widget, keys_equal,
};
use crate::widgets::basic::Directionality;
use crate::widgets::scroll_notification::ViewportElementMixin;

/// A widget through which a portion of larger content can be viewed, typically in combination
/// with a `Scrollable`.
///
/// [`Viewport`] is the visual workhorse of the scrolling machinery. It displays a subset of its
/// children according to its own dimensions and the given [`offset`](Self::offset). As the offset
/// varies, different children are visible through the viewport.
///
/// [`Viewport`] hosts a bidirectional list of slivers, anchored on a [`center`](Self::center)
/// sliver, which is placed at the zero scroll offset. The center widget is displayed in the
/// viewport according to the [`anchor`](Self::anchor) property.
///
/// Slivers that are earlier in the child list than [`center`](Self::center) are displayed in
/// reverse order in the reverse [`axis_direction`](Self::axis_direction) starting from the
/// [`center`](Self::center). For example, if the axis direction is [`AxisDirection::Down`], the
/// first sliver before the center is placed above the center. The slivers that are later in the
/// child list than the center are placed in order in the axis direction.
///
/// [`Viewport`] cannot contain box children directly. Instead, use a `SliverList`,
/// `SliverFixedExtentList`, or a `SliverToBoxAdapter`, for example.
///
/// See also:
///
///  * `SliverToBoxAdapter`, which allows a box widget to be placed inside a sliver context (the
///    opposite of this widget).
///  * [`ShrinkWrappingViewport`], a variant of [`Viewport`] that shrink-wraps its contents along
///    the main axis.
///  * `ViewportElementMixin`, which should be mixed in to the `Element` type used by
///    viewport-like widgets to correctly handle scroll notifications.
#[derive(Debug)]
pub struct Viewport {
    pub key: Option<KeyRef>,
    /// The direction in which the [`offset`](Self::offset)'s `ViewportOffset::pixels` increases.
    ///
    /// For example, if the axis direction is [`AxisDirection::Down`], a scroll offset of zero is
    /// at the top of the viewport and increases towards the bottom of the viewport.
    pub axis_direction: AxisDirection,
    /// The direction in which child should be laid out in the cross axis.
    ///
    /// If the [`axis_direction`](Self::axis_direction) is [`AxisDirection::Down`] or
    /// [`AxisDirection::Up`], this property defaults to [`AxisDirection::Left`] if the ambient
    /// [`Directionality`] is right-to-left and [`AxisDirection::Right`] if it is left-to-right.
    ///
    /// If the axis direction is [`AxisDirection::Left`] or [`AxisDirection::Right`], this
    /// property defaults to [`AxisDirection::Down`].
    pub cross_axis_direction: Option<AxisDirection>,
    /// The relative position of the zero scroll offset.
    ///
    /// For example, if the anchor is 0.5 and the [`axis_direction`](Self::axis_direction) is
    /// [`AxisDirection::Down`] or [`AxisDirection::Up`], then the zero scroll offset is
    /// vertically centered within the viewport. If the anchor is 1.0, and the axis direction is
    /// [`AxisDirection::Right`], then the zero scroll offset is on the left edge of the viewport.
    pub anchor: f64,
    /// Which part of the content inside the viewport should be visible.
    ///
    /// The `ViewportOffset::pixels` value determines the scroll offset that the viewport uses to
    /// select which part of its content to display. As the user scrolls the viewport, this value
    /// changes, which changes the content that is displayed.
    ///
    /// Typically a `ScrollPosition`.
    pub offset: ViewportOffsetRef,
    /// The first child in the `GrowthDirection::Forward` growth direction.
    ///
    /// Children after the center will be placed in the [`axis_direction`](Self::axis_direction)
    /// relative to the center. Children before it will be placed in the opposite of the axis
    /// direction relative to the center.
    ///
    /// The center must be the key of a child of the viewport.
    pub center: Option<KeyRef>,
    /// The viewport has an area before and after the visible area to cache items that are about
    /// to become visible when the user scrolls.
    ///
    /// See also:
    ///
    ///  * [`cache_extent_style`](Self::cache_extent_style), which controls the units of the cache
    ///    extent.
    #[deprecated(note = "Use scroll_cache_extent instead.")]
    pub cache_extent: Option<f64>,
    /// The unit of measurement of [`cache_extent`](Self::cache_extent).
    #[deprecated(note = "Use scroll_cache_extent instead.")]
    pub cache_extent_style: CacheExtentStyle,
    /// The amount of additional content to display and lay out around the viewport.
    pub scroll_cache_extent: Option<ScrollCacheExtent>,
    /// The order in which to paint the slivers.
    ///
    /// Defaults to [`SliverPaintOrder::FirstIsTop`].
    pub paint_order: SliverPaintOrder,
    /// Defaults to [`Clip::HardEdge`].
    pub clip_behavior: Clip,
    /// The slivers to place inside the viewport.
    pub slivers: Vec<WidgetRef>,
}

impl Viewport {
    /// Creates a widget that is bigger on the inside.
    ///
    /// The viewport listens to the [`offset`](Self::offset), which means you do not need to
    /// rebuild this widget when the offset changes.
    #[expect(deprecated, reason = "the deprecated fields have Dart's defaults")]
    pub fn new(offset: ViewportOffsetRef) -> Viewport {
        Viewport {
            key: None,
            axis_direction: AxisDirection::Down,
            cross_axis_direction: None,
            anchor: 0.0,
            offset,
            center: None,
            cache_extent: None,
            cache_extent_style: CacheExtentStyle::Pixel,
            scroll_cache_extent: None,
            paint_order: SliverPaintOrder::FirstIsTop,
            clip_behavior: Clip::HardEdge,
            slivers: Vec::new(),
        }
    }

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

    /// Dart `Viewport(axisDirection:)`.
    pub fn axis_direction(mut self, axis_direction: AxisDirection) -> Viewport {
        self.axis_direction = axis_direction;
        self
    }

    /// Dart `Viewport(crossAxisDirection:)`.
    pub fn cross_axis_direction(mut self, cross_axis_direction: AxisDirection) -> Viewport {
        self.cross_axis_direction = Some(cross_axis_direction);
        self
    }

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

    /// Dart `Viewport(center:)`.
    pub fn center(mut self, center: KeyRef) -> Viewport {
        self.center = Some(center);
        self.debug_assert_center_is_a_sliver();
        self
    }

    /// Dart `Viewport(cacheExtent:)`.
    #[deprecated(note = "Use scroll_cache_extent instead.")]
    pub fn cache_extent(mut self, cache_extent: f64) -> Viewport {
        #[expect(deprecated, reason = "the deprecated field this setter writes")]
        {
            self.cache_extent = Some(cache_extent);
        }
        self
    }

    /// Dart `Viewport(cacheExtentStyle:)`.
    #[deprecated(note = "Use scroll_cache_extent instead.")]
    pub fn cache_extent_style(mut self, cache_extent_style: CacheExtentStyle) -> Viewport {
        #[expect(deprecated, reason = "the deprecated fields this setter guards")]
        {
            self.cache_extent_style = cache_extent_style;
            debug_assert!(
                self.cache_extent_style != CacheExtentStyle::Viewport
                    || self.cache_extent.is_some()
            );
        }
        self
    }

    /// Dart `Viewport(scrollCacheExtent:)`.
    pub fn scroll_cache_extent(mut self, scroll_cache_extent: ScrollCacheExtent) -> Viewport {
        self.scroll_cache_extent = Some(scroll_cache_extent);
        self
    }

    /// Dart `Viewport(paintOrder:)`.
    pub fn paint_order(mut self, paint_order: SliverPaintOrder) -> Viewport {
        self.paint_order = paint_order;
        self
    }

    /// Dart `Viewport(clipBehavior:)`.
    pub fn clip_behavior(mut self, clip_behavior: Clip) -> Viewport {
        self.clip_behavior = clip_behavior;
        self
    }

    /// Dart `Viewport(slivers:)`.
    pub fn slivers(mut self, slivers: Vec<WidgetRef>) -> Viewport {
        self.slivers = slivers;
        self.debug_assert_center_is_a_sliver();
        self
    }

    /// Dart's `assert(center == null || slivers.where((child) => child.key == center).length == 1)`.
    fn debug_assert_center_is_a_sliver(&self) {
        if cfg!(debug_assertions)
            && let Some(center) = &self.center
        {
            let matches = self
                .slivers
                .iter()
                .filter(|child| keys_equal(child.key(), Some(center)))
                .count();
            assert!(matches == 1, "the center must be the key of one sliver");
        }
    }

    /// Flutter's `_effectiveScrollCacheExtent`.
    fn effective_scroll_cache_extent(&self) -> Option<ScrollCacheExtent> {
        if self.scroll_cache_extent.is_some() {
            return self.scroll_cache_extent;
        }
        #[expect(deprecated, reason = "the deprecated fields this getter folds in")]
        let (cache_extent, cache_extent_style) = (self.cache_extent, self.cache_extent_style);
        let cache_extent = cache_extent?;
        Some(match cache_extent_style {
            CacheExtentStyle::Pixel => ScrollCacheExtent::Pixels(cache_extent),
            CacheExtentStyle::Viewport => ScrollCacheExtent::Viewport(cache_extent),
        })
    }

    /// Given a [`BuildContext`] and an [`AxisDirection`], determine the correct cross axis
    /// direction.
    ///
    /// This depends on the [`Directionality`] if the `axis_direction` is vertical; otherwise, the
    /// default cross axis direction is downwards.
    pub fn get_default_cross_axis_direction(
        app: &mut App,
        context: BuildContext,
        axis_direction: AxisDirection,
    ) -> AxisDirection {
        match axis_direction {
            AxisDirection::Up | AxisDirection::Down => {
                text_direction_to_axis_direction(Directionality::of(app, context))
            }
            AxisDirection::Right | AxisDirection::Left => AxisDirection::Down,
        }
    }

    fn resolved_cross_axis_direction(&self, app: &mut App, context: BuildContext) -> AxisDirection {
        self.cross_axis_direction.unwrap_or_else(|| {
            Viewport::get_default_cross_axis_direction(app, context, self.axis_direction)
        })
    }
}

impl RenderObjectWidget for Viewport {
    type RenderObject = RenderViewport;

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

    fn debug_fill_properties(&self, app: &App, properties: &mut DiagnosticPropertiesBuilder) {
        properties.add(EnumProperty::new("axisDirection", self.axis_direction));
        properties.add(
            EnumProperty::<AxisDirection>::new("crossAxisDirection", self.cross_axis_direction)
                .default_value(None),
        );
        properties.add(DoubleProperty::new("anchor", self.anchor));
        properties.add(DiagnosticsProperty::<String>::new(
            "offset",
            Some(self.offset.to_string_with_app(app)),
        ));
        if let Some(center) = &self.center {
            properties.add(DiagnosticsProperty::<String>::new(
                "center",
                Some(format!("{center:?}")),
            ));
        } else if let Some(first) = self.slivers.first()
            && let Some(key) = first.key()
        {
            properties.add(
                DiagnosticsProperty::<String>::new("center", Some(format!("{key:?}")))
                    .tooltip("implicit"),
            );
        }
        properties.add(DiagnosticsProperty::<ScrollCacheExtent>::new(
            "scrollCacheExtent",
            self.scroll_cache_extent,
        ));
    }

    fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
        let cross_axis_direction = self.resolved_cross_axis_direction(app, context);
        let viewport =
            RenderViewport::new(app, cross_axis_direction, self.offset.clone(), None, None);
        viewport.set_axis_direction(app, self.axis_direction);
        viewport.set_anchor(app, self.anchor);
        viewport.set_scroll_cache_extent(app, self.effective_scroll_cache_extent());
        viewport.set_paint_order(app, self.paint_order);
        viewport.set_clip_behavior(app, self.clip_behavior);
        viewport.as_object()
    }

    fn update_render_object(
        &self,
        app: &mut App,
        context: BuildContext,
        render_object: RenderHandle<RenderViewport>,
    ) {
        let cross_axis_direction = self.resolved_cross_axis_direction(app, context);
        render_object.set_axis_direction(app, self.axis_direction);
        render_object.set_cross_axis_direction(app, cross_axis_direction);
        render_object.set_anchor(app, self.anchor);
        render_object.set_offset(app, self.offset.clone());
        render_object.set_scroll_cache_extent(app, self.effective_scroll_cache_extent());
        render_object.set_paint_order(app, self.paint_order);
        render_object.set_clip_behavior(app, self.clip_behavior);
    }
}

impl MultiChildRenderObjectWidget for Viewport {
    fn children(&self) -> &[WidgetRef] {
        &self.slivers
    }

    fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement {
        ViewportElement::new(app, this).as_element()
    }
}

/// Dart's `_ViewportElement`: the [`Viewport`]'s element, which keeps the render object's
/// `center` in step with the child list.
pub struct ViewportElement {
    element: ElementData,
    render_object_element: RenderObjectElementData,
    multi_child: MultiChildRenderObjectElementData,
    doing_mount_or_update: bool,
    center_slot_index: Option<usize>,
}

impl ViewportElement {
    /// Creates an element that uses the given widget as its configuration.
    pub fn new(app: &mut App, widget: WidgetRef) -> Handle<ViewportElement> {
        debug_assert!(downcast_widget::<Viewport>(&*widget).is_some());
        app.create(ViewportElement {
            element: ElementData::new(widget),
            render_object_element: RenderObjectElementData::default(),
            multi_child: MultiChildRenderObjectElementData::default(),
            doing_mount_or_update: false,
            center_slot_index: None,
        })
        .into_tree_node(app)
    }

    /// The viewport this element manages.
    pub fn render_object(self: Handle<Self>, app: &App) -> RenderHandle<RenderViewport> {
        self.typed_render_object(app)
    }

    /// Flutter's `_updateCenter`.
    fn update_center(self: Handle<Self>, app: &mut App) {
        let center = Self::widget_of(self.as_element().widget(app))
            .center
            .clone();
        let children = MultiChildRenderObjectElementBase::children(self, app);
        let render_object = self.render_object(app);
        match center {
            Some(center) => {
                let mut element_index = 0;
                for child in &children {
                    if keys_equal(child.widget(app).key(), Some(&center)) {
                        let sliver = child.render_object(app).map(AnyRenderSliver::from_object);
                        render_object.set_center(app, sliver);
                        break;
                    }
                    element_index += 1;
                }
                debug_assert!(element_index < children.len());
                app.get_mut(self).center_slot_index = Some(element_index);
            }
            None if !children.is_empty() => {
                let sliver = children[0]
                    .render_object(app)
                    .map(AnyRenderSliver::from_object);
                render_object.set_center(app, sliver);
                app.get_mut(self).center_slot_index = Some(0);
            }
            None => {
                render_object.set_center(app, None);
                app.get_mut(self).center_slot_index = None;
            }
        }
    }
}

impl RenderObjectElementWidget for ViewportElement {
    type Widget = Viewport;

    fn widget_of(widget: &WidgetRef) -> &Viewport {
        downcast_widget::<Viewport>(&**widget).expect("a ViewportElement holds its Viewport")
    }
}

impl RenderObjectElement for ViewportElement {
    fn render_object_element_data(self: Handle<Self>, app: &App) -> &RenderObjectElementData {
        &app.get(self).render_object_element
    }

    fn render_object_element_data_mut(
        self: Handle<Self>,
        app: &mut App,
    ) -> &mut RenderObjectElementData {
        &mut app.get_mut(self).render_object_element
    }
}

impl MultiChildRenderObjectElementBase for ViewportElement {
    fn multi_child_data(self: Handle<Self>, app: &App) -> &MultiChildRenderObjectElementData {
        &app.get(self).multi_child
    }

    fn multi_child_data_mut(
        self: Handle<Self>,
        app: &mut App,
    ) -> &mut MultiChildRenderObjectElementData {
        &mut app.get_mut(self).multi_child
    }
}

impl ViewportElementMixin for ViewportElement {}

impl Element for ViewportElement {
    crate::element_accessors!();

    const IS_RENDER_OBJECT_ELEMENT: bool = true;

    fn render_object(self: Handle<Self>, app: &App) -> Option<AnyRenderObject> {
        self.render_object_element_data(app).render_object()
    }

    fn render_object_attaching_child(self: Handle<Self>, _app: &App) -> Option<AnyElement> {
        None
    }

    fn debug_doing_build(self: Handle<Self>, app: &App) -> bool {
        self.render_object_element_data(app).debug_doing_build()
    }

    fn visit_children(self: Handle<Self>, app: &App, visitor: &mut dyn FnMut(AnyElement)) {
        MultiChildRenderObjectElementBase::visit_children(self, app, visitor);
    }

    /// Dart's `_ViewportElement.debugVisitOnstageChildren`: the slivers whose geometry is
    /// visible.
    fn debug_visit_onstage_children(
        self: Handle<Self>,
        app: &App,
        visitor: &mut dyn FnMut(AnyElement),
    ) {
        let mut children = Vec::new();
        MultiChildRenderObjectElementBase::visit_children(self, app, &mut |child| {
            children.push(child);
        });
        children
            .into_iter()
            .filter(|child| {
                child
                    .render_object(app)
                    .and_then(|render| render.as_sliver())
                    .is_some_and(|sliver| sliver.geometry(app).visible)
            })
            .for_each(visitor);
    }

    fn forget_child(self: Handle<Self>, app: &mut App, child: AnyElement) {
        MultiChildRenderObjectElementBase::forget_child(self, app, child);
    }

    fn mount(
        self: Handle<Self>,
        app: &mut App,
        parent: Option<AnyElement>,
        new_slot: Option<Slot>,
    ) {
        debug_assert!(!app.get(self).doing_mount_or_update);
        app.get_mut(self).doing_mount_or_update = true;
        MultiChildRenderObjectElementBase::mount(self, app, parent, new_slot);
        self.update_center(app);
        debug_assert!(app.get(self).doing_mount_or_update);
        app.get_mut(self).doing_mount_or_update = false;
    }

    fn update(self: Handle<Self>, app: &mut App, new_widget: WidgetRef) {
        debug_assert!(!app.get(self).doing_mount_or_update);
        app.get_mut(self).doing_mount_or_update = true;
        MultiChildRenderObjectElementBase::update(self, app, new_widget);
        self.update_center(app);
        debug_assert!(app.get(self).doing_mount_or_update);
        app.get_mut(self).doing_mount_or_update = false;
    }

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

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

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

    fn update_parent_data(self: Handle<Self>, app: &mut App, parent_data_element: AnyElement) {
        RenderObjectElement::update_parent_data(self, app, parent_data_element);
    }

    fn update_slot(self: Handle<Self>, app: &mut App, new_slot: Option<Slot>) {
        RenderObjectElement::update_slot(self, app, new_slot);
    }

    fn attach_render_object(self: Handle<Self>, app: &mut App, new_slot: Option<Slot>) {
        RenderObjectElement::attach_render_object(self, app, new_slot);
    }

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

    fn insert_render_object_child(
        self: Handle<Self>,
        app: &mut App,
        child: AnyRenderObject,
        slot: Option<Slot>,
    ) {
        MultiChildRenderObjectElementBase::insert_render_object_child(
            self,
            app,
            child,
            slot.clone(),
        );
        // Once `mount` / `update` are done, the render object's center will be updated in
        // `update_center`.
        let index = Self::indexed_slot(slot.as_ref()).index;
        if !app.get(self).doing_mount_or_update && Some(index) == app.get(self).center_slot_index {
            let center = AnyRenderSliver::from_object(child);
            self.render_object(app).set_center(app, Some(center));
        }
    }

    fn move_render_object_child(
        self: Handle<Self>,
        app: &mut App,
        child: AnyRenderObject,
        old_slot: Option<Slot>,
        new_slot: Option<Slot>,
    ) {
        MultiChildRenderObjectElementBase::move_render_object_child(
            self, app, child, old_slot, new_slot,
        );
        debug_assert!(app.get(self).doing_mount_or_update);
    }

    fn remove_render_object_child(
        self: Handle<Self>,
        app: &mut App,
        child: AnyRenderObject,
        slot: Option<Slot>,
    ) {
        MultiChildRenderObjectElementBase::remove_render_object_child(self, app, child, slot);
        let render_object = self.render_object(app);
        if !app.get(self).doing_mount_or_update
            && render_object.center(app) == Some(AnyRenderSliver::from_object(child))
        {
            render_object.set_center(app, None);
        }
    }

    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 crate::framework::Notification,
    ) -> bool {
        ViewportElementMixin::on_notification(self, app, notification)
    }
}

/// A widget that is bigger on the inside and shrink wraps its children in the main axis.
///
/// [`ShrinkWrappingViewport`] displays a subset of its children according to its own dimensions
/// and the given [`offset`](Self::offset). As the offset varies, different children are visible
/// through the viewport.
///
/// [`ShrinkWrappingViewport`] differs from [`Viewport`] in that [`Viewport`] expands to fill the
/// main axis whereas [`ShrinkWrappingViewport`] sizes itself to match its children in the main
/// axis. This shrink wrapping behavior is expensive because the children, and hence the viewport,
/// could potentially change size whenever the offset changes (e.g., because of a collapsing
/// header).
///
/// [`ShrinkWrappingViewport`] cannot contain box children directly. Instead, use a `SliverList`,
/// `SliverFixedExtentList`, or a `SliverToBoxAdapter`, for example.
///
/// See also:
///
///  * `SliverToBoxAdapter`, which allows a box widget to be placed inside a sliver context (the
///    opposite of this widget).
///  * [`Viewport`], a viewport that does not shrink-wrap its contents.
#[derive(Debug)]
pub struct ShrinkWrappingViewport {
    pub key: Option<KeyRef>,
    /// The direction in which the [`offset`](Self::offset)'s `ViewportOffset::pixels` increases.
    pub axis_direction: AxisDirection,
    /// The direction in which child should be laid out in the cross axis.
    pub cross_axis_direction: Option<AxisDirection>,
    /// Which part of the content inside the viewport should be visible.
    ///
    /// Typically a `ScrollPosition`.
    pub offset: ViewportOffsetRef,
    /// The order in which to paint the slivers.
    ///
    /// Defaults to [`SliverPaintOrder::FirstIsTop`].
    pub paint_order: SliverPaintOrder,
    /// Defaults to [`Clip::HardEdge`].
    pub clip_behavior: Clip,
    /// The viewport has an area before and after the visible area to cache items that are about
    /// to become visible when the user scrolls.
    #[deprecated(note = "Use scroll_cache_extent instead.")]
    pub cache_extent: Option<f64>,
    /// The unit of measurement of [`cache_extent`](Self::cache_extent).
    #[deprecated(note = "Use scroll_cache_extent instead.")]
    pub cache_extent_style: CacheExtentStyle,
    /// The amount of additional content to display and lay out around the viewport.
    pub scroll_cache_extent: Option<ScrollCacheExtent>,
    /// The slivers to place inside the viewport.
    pub slivers: Vec<WidgetRef>,
}

impl ShrinkWrappingViewport {
    /// Creates a widget that is bigger on the inside and shrink wraps its children in the main
    /// axis.
    ///
    /// The viewport listens to the [`offset`](Self::offset), which means you do not need to
    /// rebuild this widget when the offset changes.
    #[expect(deprecated, reason = "the deprecated fields have Dart's defaults")]
    pub fn new(offset: ViewportOffsetRef) -> ShrinkWrappingViewport {
        ShrinkWrappingViewport {
            key: None,
            axis_direction: AxisDirection::Down,
            cross_axis_direction: None,
            offset,
            paint_order: SliverPaintOrder::FirstIsTop,
            clip_behavior: Clip::HardEdge,
            cache_extent: None,
            cache_extent_style: CacheExtentStyle::Pixel,
            scroll_cache_extent: None,
            slivers: Vec::new(),
        }
    }

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

    /// Dart `ShrinkWrappingViewport(axisDirection:)`.
    pub fn axis_direction(mut self, axis_direction: AxisDirection) -> ShrinkWrappingViewport {
        self.axis_direction = axis_direction;
        self
    }

    /// Dart `ShrinkWrappingViewport(crossAxisDirection:)`.
    pub fn cross_axis_direction(
        mut self,
        cross_axis_direction: AxisDirection,
    ) -> ShrinkWrappingViewport {
        self.cross_axis_direction = Some(cross_axis_direction);
        self
    }

    /// Dart `ShrinkWrappingViewport(paintOrder:)`.
    pub fn paint_order(mut self, paint_order: SliverPaintOrder) -> ShrinkWrappingViewport {
        self.paint_order = paint_order;
        self
    }

    /// Dart `ShrinkWrappingViewport(clipBehavior:)`.
    pub fn clip_behavior(mut self, clip_behavior: Clip) -> ShrinkWrappingViewport {
        self.clip_behavior = clip_behavior;
        self
    }

    /// Dart `ShrinkWrappingViewport(cacheExtent:)`.
    #[deprecated(note = "Use scroll_cache_extent instead.")]
    pub fn cache_extent(mut self, cache_extent: f64) -> ShrinkWrappingViewport {
        #[expect(deprecated, reason = "the deprecated field this setter writes")]
        {
            self.cache_extent = Some(cache_extent);
        }
        self
    }

    /// Dart `ShrinkWrappingViewport(cacheExtentStyle:)`.
    #[deprecated(note = "Use scroll_cache_extent instead.")]
    pub fn cache_extent_style(
        mut self,
        cache_extent_style: CacheExtentStyle,
    ) -> ShrinkWrappingViewport {
        #[expect(deprecated, reason = "the deprecated field this setter writes")]
        {
            self.cache_extent_style = cache_extent_style;
        }
        self
    }

    /// Dart `ShrinkWrappingViewport(scrollCacheExtent:)`.
    pub fn scroll_cache_extent(
        mut self,
        scroll_cache_extent: ScrollCacheExtent,
    ) -> ShrinkWrappingViewport {
        self.scroll_cache_extent = Some(scroll_cache_extent);
        self
    }

    /// Dart `ShrinkWrappingViewport(slivers:)`.
    pub fn slivers(mut self, slivers: Vec<WidgetRef>) -> ShrinkWrappingViewport {
        self.slivers = slivers;
        self
    }

    /// Flutter's `_effectiveScrollCacheExtent`.
    fn effective_scroll_cache_extent(&self) -> Option<ScrollCacheExtent> {
        if self.scroll_cache_extent.is_some() {
            return self.scroll_cache_extent;
        }
        #[expect(deprecated, reason = "the deprecated fields this getter folds in")]
        let (cache_extent, cache_extent_style) = (self.cache_extent, self.cache_extent_style);
        let cache_extent = cache_extent?;
        Some(match cache_extent_style {
            CacheExtentStyle::Pixel => ScrollCacheExtent::Pixels(cache_extent),
            CacheExtentStyle::Viewport => ScrollCacheExtent::Viewport(cache_extent),
        })
    }

    fn resolved_cross_axis_direction(&self, app: &mut App, context: BuildContext) -> AxisDirection {
        self.cross_axis_direction.unwrap_or_else(|| {
            Viewport::get_default_cross_axis_direction(app, context, self.axis_direction)
        })
    }
}

impl RenderObjectWidget for ShrinkWrappingViewport {
    type RenderObject = RenderShrinkWrappingViewport;

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

    fn debug_fill_properties(&self, app: &App, properties: &mut DiagnosticPropertiesBuilder) {
        properties.add(EnumProperty::new("axisDirection", self.axis_direction));
        properties.add(
            EnumProperty::<AxisDirection>::new("crossAxisDirection", self.cross_axis_direction)
                .default_value(None),
        );
        properties.add(DiagnosticsProperty::<String>::new(
            "offset",
            Some(self.offset.to_string_with_app(app)),
        ));
        properties.add(
            DiagnosticsProperty::<ScrollCacheExtent>::new(
                "scrollCacheExtent",
                self.scroll_cache_extent,
            )
            .default_value(None),
        );
    }

    fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
        let cross_axis_direction = self.resolved_cross_axis_direction(app, context);
        let viewport =
            RenderShrinkWrappingViewport::new(app, cross_axis_direction, self.offset.clone(), None);
        viewport.set_axis_direction(app, self.axis_direction);
        viewport.set_paint_order(app, self.paint_order);
        viewport.set_clip_behavior(app, self.clip_behavior);
        viewport.set_scroll_cache_extent(app, self.effective_scroll_cache_extent());
        viewport.as_object()
    }

    fn update_render_object(
        &self,
        app: &mut App,
        context: BuildContext,
        render_object: RenderHandle<RenderShrinkWrappingViewport>,
    ) {
        let cross_axis_direction = self.resolved_cross_axis_direction(app, context);
        render_object.set_axis_direction(app, self.axis_direction);
        render_object.set_cross_axis_direction(app, cross_axis_direction);
        render_object.set_offset(app, self.offset.clone());
        render_object.set_paint_order(app, self.paint_order);
        render_object.set_clip_behavior(app, self.clip_behavior);
        render_object.set_scroll_cache_extent(app, self.effective_scroll_cache_extent());
    }
}

impl MultiChildRenderObjectWidget for ShrinkWrappingViewport {
    fn children(&self) -> &[WidgetRef] {
        &self.slivers
    }
}

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

    use waft_embedder::TextDirection;
    use waft_rendering::{
        ContainerRenderObjectMixin, FixedViewportOffset, RenderObjectWithChildMixin,
        RenderPositionedBox, RenderSliverList, SliverMultiBoxAdaptorParentData, ViewportOffset,
    };

    use super::*;
    use crate::framework::IntoWidget;
    use crate::test_harness::Harness;
    use crate::widgets::basic::{Center, Directionality, SizedBox, SliverToBoxAdapter};
    use crate::widgets::sliver::SliverList;

    /// The indices the list's item builder was asked for, in order.
    type Built = Rc<RefCell<Vec<i32>>>;

    fn item_list(built: &Built, item_count: i32) -> WidgetRef {
        let built = Rc::clone(built);
        SliverList::builder(move |_app, _context, index| {
            built.borrow_mut().push(index);
            Some(SizedBox::new().height(40.0).into_widget())
        })
        .item_count(item_count)
        .into_widget()
    }

    fn viewport_tree(offset: &Ref<FixedViewportOffset>, built: &Built) -> WidgetRef {
        Directionality::new(
            TextDirection::Ltr,
            Viewport::new(offset.erase())
                .scroll_cache_extent(ScrollCacheExtent::Pixels(0.0))
                .slivers(vec![
                    SliverToBoxAdapter::new()
                        .child(SizedBox::new().height(50.0))
                        .into_widget(),
                    item_list(built, 20),
                ]),
        )
        .into_widget()
    }

    fn render_viewport(harness: &Harness, app: &App) -> RenderHandle<RenderViewport> {
        harness
            .render_root(app)
            .child(app)
            .expect("a child")
            .as_object()
            .downcast::<RenderViewport>(app)
            .expect("a RenderViewport")
    }

    fn sliver_list(
        viewport: RenderHandle<RenderViewport>,
        app: &App,
    ) -> RenderHandle<RenderSliverList> {
        let adapter = viewport.first_child(app).expect("the box adapter");
        viewport
            .child_after(app, adapter)
            .expect("the list")
            .as_object()
            .downcast::<RenderSliverList>(app)
            .expect("a RenderSliverList")
    }

    /// The indices of the list's reified children, in child order.
    fn child_indices(list: RenderHandle<RenderSliverList>, app: &App) -> Vec<i32> {
        let mut indices = Vec::new();
        let mut child = list.first_child(app);
        while let Some(current) = child {
            indices.push(
                current
                    .as_object()
                    .parent_data_of::<SliverMultiBoxAdaptorParentData>(app)
                    .index
                    .expect("a reified child has an index"),
            );
            child = list.child_after(app, current);
        }
        indices
    }

    #[test]
    fn a_viewport_builds_only_the_visible_children_and_more_when_the_offset_moves() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let offset = FixedViewportOffset::zero(&mut app);
        let built: Built = Rc::default();
        let harness = Harness::mount(&mut app, viewport_tree(&offset, &built));
        harness.pump(&mut app);

        let viewport = render_viewport(&harness, &app);
        let list = sliver_list(viewport, &app);
        // The box adapter takes the first 50 pixels; the 200-pixel viewport fits four more
        // 40-pixel children with no cache extent.
        assert_eq!(child_indices(list, &app), vec![0, 1, 2, 3]);
        assert_eq!(*built.borrow(), vec![0, 1, 2, 3]);

        offset.correct_by(&mut app, 200.0);
        viewport.mark_needs_layout(&mut app);
        harness.pump(&mut app);

        // The window 200..400 covers the list items that start at 50: indices 3 through 8.
        assert_eq!(child_indices(list, &app), vec![3, 4, 5, 6, 7, 8]);
        // The children that scrolled off were destroyed; only the new ones were built.
        assert_eq!(*built.borrow(), vec![0, 1, 2, 3, 4, 5, 6, 7, 8]);
    }

    #[test]
    fn a_shrink_wrapping_viewport_sizes_itself_to_its_content() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let offset = FixedViewportOffset::zero(&mut app);
        let built: Built = Rc::default();
        let tree = Directionality::new(
            TextDirection::Ltr,
            Center::new().child(ShrinkWrappingViewport::new(offset.erase()).slivers(vec![
                    SliverToBoxAdapter::new()
                        .child(SizedBox::new().height(50.0))
                        .into_widget(),
                    item_list(&built, 2),
                ])),
        )
        .into_widget();
        let harness = Harness::mount(&mut app, tree);
        harness.pump(&mut app);

        let center = harness
            .render_root(&app)
            .child(&app)
            .expect("a child")
            .as_object()
            .downcast::<RenderPositionedBox>(&app)
            .expect("a RenderPositionedBox");
        let viewport = center
            .child(&app)
            .expect("the viewport")
            .as_object()
            .downcast::<RenderShrinkWrappingViewport>(&app)
            .expect("a RenderShrinkWrappingViewport");
        // 50 for the box adapter plus two 40-pixel list items.
        assert_eq!(viewport.size(&app).height(), 130.0);
    }
}