1use std::rc::Rc;
6use std::time::Duration;
7
8use waft_animation::{
9 Animatable, AnimationController, AnimationStatus, Curve, CurveTween, Curves, Tween,
10};
11use waft_embedder::{Matrix4, Offset, Rect, clamp_double};
12use waft_foundation::{App, AsyncCallback, Handle, HandleId, ListenableObject, Listener};
13use waft_painting::{Axis, AxisDirection, transform_rect};
14use waft_scheduler::{Ticker, TickerCallback, TickerProvider, TickerProviderObject};
15
16use crate::box_::{AnyRenderBox, BoxHitTestResult};
17use crate::object::{
18 AnyRenderObject, RenderHandle, RenderObjectBase, RenderObjectWithChildMixin, resolve,
19};
20use crate::painting_context::PaintingContext;
21use crate::sliver::{
22 RenderSliver, RenderSliverHelpers, SliverGeometry, SliverHitTestResult,
23 apply_growth_direction_to_axis_direction, calculate_cache_offset,
24};
25use crate::viewport_offset::ScrollDirection;
26use waft_animation::AnimationRef;
27use waft_animation::Drive;
28use waft_foundation::OptionRetained;
29use waft_foundation::Ref;
30
31fn trim(original: Option<Rect>, top: f64, right: f64, bottom: f64, left: f64) -> Option<Rect> {
35 original.map(|rect| rect.intersect(Rect::from_ltrb(left, top, right, bottom)))
36}
37
38#[derive(Clone)]
45pub struct TickerProviderRef {
46 id: HandleId,
47 provider: Rc<dyn TickerProvider>,
48}
49
50impl TickerProviderRef {
51 pub fn new<T: TickerProviderObject>(provider: Handle<T>) -> TickerProviderRef {
53 TickerProviderRef {
54 id: provider.id(),
55 provider: Rc::new(provider),
56 }
57 }
58}
59
60impl TickerProvider for TickerProviderRef {
61 fn create_ticker(&self, app: &mut App, on_tick: TickerCallback) -> Ref<Ticker> {
62 self.provider.create_ticker(app, on_tick)
63 }
64}
65
66impl PartialEq for TickerProviderRef {
67 fn eq(&self, other: &TickerProviderRef) -> bool {
68 self.id == other.id
69 }
70}
71
72impl std::fmt::Debug for TickerProviderRef {
73 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74 f.write_str("TickerProviderRef")
75 }
76}
77
78#[derive(Clone)]
80pub struct OverScrollHeaderStretchConfiguration {
81 pub stretch_trigger_offset: f64,
84
85 pub on_stretch_trigger: Option<AsyncCallback>,
88}
89
90impl OverScrollHeaderStretchConfiguration {
91 pub fn new() -> OverScrollHeaderStretchConfiguration {
96 OverScrollHeaderStretchConfiguration {
97 stretch_trigger_offset: 100.0,
98 on_stretch_trigger: None,
99 }
100 }
101
102 pub fn stretch_trigger_offset(mut self, value: f64) -> OverScrollHeaderStretchConfiguration {
104 self.stretch_trigger_offset = value;
105 self
106 }
107
108 pub fn on_stretch_trigger(
110 mut self,
111 value: AsyncCallback,
112 ) -> OverScrollHeaderStretchConfiguration {
113 self.on_stretch_trigger = Some(value);
114 self
115 }
116}
117
118impl Default for OverScrollHeaderStretchConfiguration {
119 fn default() -> OverScrollHeaderStretchConfiguration {
120 OverScrollHeaderStretchConfiguration::new()
121 }
122}
123
124#[derive(Clone, Copy, Debug, PartialEq)]
127pub struct PersistentHeaderShowOnScreenConfiguration {
128 pub min_show_on_screen_extent: f64,
140
141 pub max_show_on_screen_extent: f64,
149}
150
151impl PersistentHeaderShowOnScreenConfiguration {
152 pub const fn new() -> PersistentHeaderShowOnScreenConfiguration {
155 PersistentHeaderShowOnScreenConfiguration {
156 min_show_on_screen_extent: f64::NEG_INFINITY,
157 max_show_on_screen_extent: f64::INFINITY,
158 }
159 }
160
161 pub fn min_show_on_screen_extent(
163 mut self,
164 value: f64,
165 ) -> PersistentHeaderShowOnScreenConfiguration {
166 self.min_show_on_screen_extent = value;
167 debug_assert!(self.min_show_on_screen_extent <= self.max_show_on_screen_extent);
168 self
169 }
170
171 pub fn max_show_on_screen_extent(
173 mut self,
174 value: f64,
175 ) -> PersistentHeaderShowOnScreenConfiguration {
176 self.max_show_on_screen_extent = value;
177 debug_assert!(self.min_show_on_screen_extent <= self.max_show_on_screen_extent);
178 self
179 }
180}
181
182impl Default for PersistentHeaderShowOnScreenConfiguration {
183 fn default() -> PersistentHeaderShowOnScreenConfiguration {
184 PersistentHeaderShowOnScreenConfiguration::new()
185 }
186}
187
188#[derive(Clone)]
190pub struct FloatingHeaderSnapConfiguration {
191 pub curve: Rc<dyn Curve>,
193
194 pub duration: Duration,
196}
197
198impl FloatingHeaderSnapConfiguration {
199 pub fn new() -> FloatingHeaderSnapConfiguration {
204 FloatingHeaderSnapConfiguration {
205 curve: Curves::ease(),
206 duration: Duration::from_millis(300),
207 }
208 }
209
210 pub fn curve(mut self, value: Rc<dyn Curve>) -> FloatingHeaderSnapConfiguration {
212 self.curve = value;
213 self
214 }
215
216 pub fn duration(mut self, value: Duration) -> FloatingHeaderSnapConfiguration {
218 self.duration = value;
219 self
220 }
221}
222
223impl Default for FloatingHeaderSnapConfiguration {
224 fn default() -> FloatingHeaderSnapConfiguration {
225 FloatingHeaderSnapConfiguration::new()
226 }
227}
228
229pub struct RenderSliverPersistentHeaderData {
231 last_stretch_offset: f64,
232 needs_update_child: bool,
233 last_shrink_offset: f64,
234 last_overlaps_content: bool,
235 pub stretch_configuration: Option<OverScrollHeaderStretchConfiguration>,
239}
240
241impl RenderSliverPersistentHeaderData {
242 pub fn new(
244 stretch_configuration: Option<OverScrollHeaderStretchConfiguration>,
245 ) -> RenderSliverPersistentHeaderData {
246 RenderSliverPersistentHeaderData {
247 last_stretch_offset: 0.0,
248 needs_update_child: true,
249 last_shrink_offset: 0.0,
250 last_overlaps_content: false,
251 stretch_configuration,
252 }
253 }
254}
255
256#[macro_export]
258macro_rules! render_sliver_persistent_header_accessors {
259 () => {
260 fn header_data(
261 self: $crate::RenderHandle<Self>,
262 app: &::waft_foundation::App,
263 ) -> &$crate::RenderSliverPersistentHeaderData {
264 &self.get(app).header
265 }
266 fn header_data_mut(
267 self: $crate::RenderHandle<Self>,
268 app: &mut ::waft_foundation::App,
269 ) -> &mut $crate::RenderSliverPersistentHeaderData {
270 &mut self.get_mut(app).header
271 }
272 };
273}
274
275pub trait RenderSliverPersistentHeader:
291 RenderSliver + RenderObjectWithChildMixin<ChildType = AnyRenderBox> + RenderSliverHelpers
292{
293 fn header_data(self: RenderHandle<Self>, app: &App) -> &RenderSliverPersistentHeaderData;
295
296 fn header_data_mut(
298 self: RenderHandle<Self>,
299 app: &mut App,
300 ) -> &mut RenderSliverPersistentHeaderData;
301
302 fn max_extent(self: RenderHandle<Self>, app: &App) -> f64;
306
307 fn min_extent(self: RenderHandle<Self>, app: &App) -> f64;
312
313 fn child_extent(self: RenderHandle<Self>, app: &App) -> f64 {
315 let Some(child) = self.child(app) else {
316 return 0.0;
317 };
318 debug_assert!(child.has_size(app));
319 match self.constraints(app).axis() {
320 Axis::Vertical => child.size(app).height(),
321 Axis::Horizontal => child.size(app).width(),
322 }
323 }
324
325 fn last_shrink_offset(self: RenderHandle<Self>, app: &App) -> f64 {
327 self.header_data(app).last_shrink_offset
328 }
329
330 fn last_overlaps_content(self: RenderHandle<Self>, app: &App) -> bool {
332 self.header_data(app).last_overlaps_content
333 }
334
335 fn stretch_configuration(
337 self: RenderHandle<Self>,
338 app: &App,
339 ) -> Option<OverScrollHeaderStretchConfiguration> {
340 self.header_data(app).stretch_configuration.clone()
341 }
342
343 fn set_stretch_configuration(
345 self: RenderHandle<Self>,
346 app: &mut App,
347 value: Option<OverScrollHeaderStretchConfiguration>,
348 ) {
349 self.header_data_mut(app).stretch_configuration = value;
350 }
351
352 fn update_child(
364 self: RenderHandle<Self>,
365 app: &mut App,
366 shrink_offset: f64,
367 overlaps_content: bool,
368 ) {
369 let _ = (self, app, shrink_offset, overlaps_content);
370 }
371
372 fn mark_needs_layout(self: RenderHandle<Self>, app: &mut App) {
377 self.header_data_mut(app).needs_update_child = true;
378 RenderSliver::mark_needs_layout(self, app);
379 }
380
381 fn layout_child(
389 self: RenderHandle<Self>,
390 app: &mut App,
391 scroll_offset: f64,
392 max_extent: f64,
393 overlaps_content: bool,
394 ) {
395 let shrink_offset = scroll_offset.min(max_extent);
396 let header = self.header_data(app);
397 if header.needs_update_child
398 || header.last_shrink_offset != shrink_offset
399 || header.last_overlaps_content != overlaps_content
400 {
401 self.as_object().invoke_layout_callback(app, |app| {
402 self.update_child(app, shrink_offset, overlaps_content);
403 });
404 let header = self.header_data_mut(app);
405 header.last_shrink_offset = shrink_offset;
406 header.last_overlaps_content = overlaps_content;
407 header.needs_update_child = false;
408 }
409 debug_assert!(
410 self.min_extent(app) <= max_extent,
411 "the max_extent for this persistent header is less than its min_extent"
412 );
413 let constraints = self.constraints(app);
414 let stretch_configuration = self.stretch_configuration(app);
415 let mut stretch_offset = 0.0;
416 if stretch_configuration.is_some() && constraints.scroll_offset == 0.0 {
417 stretch_offset += constraints.overlap.abs();
418 }
419
420 if let Some(child) = self.child(app) {
421 let min_extent = self.min_extent(app);
422 let child_constraints = constraints.as_box_constraints(
423 0.0,
424 min_extent.max(max_extent - shrink_offset) + stretch_offset,
425 None,
426 );
427 child.layout(app, child_constraints, true);
428 }
429
430 if let Some(configuration) = &stretch_configuration
431 && let Some(on_stretch_trigger) = configuration.on_stretch_trigger.clone()
432 && stretch_offset >= configuration.stretch_trigger_offset
433 && self.header_data(app).last_stretch_offset <= configuration.stretch_trigger_offset
434 {
435 drop(on_stretch_trigger(app));
437 }
438 self.header_data_mut(app).last_stretch_offset = stretch_offset;
439 }
440
441 fn hit_test_children(
443 self: RenderHandle<Self>,
444 app: &mut App,
445 result: &mut SliverHitTestResult<'_>,
446 main_axis_position: f64,
447 cross_axis_position: f64,
448 ) -> bool {
449 debug_assert!(self.geometry(app).hit_test_extent > 0.0);
450 let Some(child) = self.child(app) else {
451 return false;
452 };
453 self.hit_test_box_child(
454 app,
455 &mut BoxHitTestResult::wrap(result),
456 child,
457 main_axis_position,
458 cross_axis_position,
459 )
460 }
461
462 fn apply_paint_transform(
464 self: RenderHandle<Self>,
465 app: &App,
466 child: AnyRenderObject,
467 transform: &mut Matrix4,
468 ) {
469 debug_assert_eq!(Some(child), self.child(app).map(AnyRenderBox::as_object));
470 let child = child.as_box().expect("the child is a box");
471 self.apply_paint_transform_for_box_child(app, child, transform);
472 }
473
474 fn paint(
476 self: RenderHandle<Self>,
477 app: &mut App,
478 context: &mut PaintingContext,
479 offset: Offset,
480 ) {
481 let Some(child) = self.child(app) else {
482 return;
483 };
484 if !self.geometry(app).visible {
485 return;
486 }
487 let constraints = self.constraints(app);
488 let paint_extent = self.geometry(app).paint_extent;
489 let child_position = RenderSliver::child_main_axis_position(self, app, child.as_object());
490 let child_extent = self.child_extent(app);
491 let offset = offset
492 + match apply_growth_direction_to_axis_direction(
493 constraints.axis_direction,
494 constraints.growth_direction,
495 ) {
496 AxisDirection::Up => Offset::new(0.0, paint_extent - child_position - child_extent),
497 AxisDirection::Left => {
498 Offset::new(paint_extent - child_position - child_extent, 0.0)
499 }
500 AxisDirection::Right => Offset::new(child_position, 0.0),
501 AxisDirection::Down => Offset::new(0.0, child_position),
502 };
503 context.paint_child(app, child.as_object(), offset);
504 }
505}
506
507#[derive(Debug, Default)]
509pub struct RenderSliverScrollingPersistentHeaderData {
510 child_position: Option<f64>,
513}
514
515impl RenderSliverScrollingPersistentHeaderData {
516 pub const fn new() -> RenderSliverScrollingPersistentHeaderData {
518 RenderSliverScrollingPersistentHeaderData {
519 child_position: None,
520 }
521 }
522}
523
524pub trait RenderSliverScrollingPersistentHeader: RenderSliverPersistentHeader {
529 fn scrolling_data(
531 self: RenderHandle<Self>,
532 app: &App,
533 ) -> &RenderSliverScrollingPersistentHeaderData;
534
535 fn scrolling_data_mut(
537 self: RenderHandle<Self>,
538 app: &mut App,
539 ) -> &mut RenderSliverScrollingPersistentHeaderData;
540
541 fn update_geometry(self: RenderHandle<Self>, app: &mut App) -> f64 {
544 let constraints = self.constraints(app);
545 let mut stretch_offset = 0.0;
546 if self.stretch_configuration(app).is_some() {
547 stretch_offset += constraints.overlap.abs();
548 }
549 let max_extent = self.max_extent(app);
550 let paint_extent = max_extent - constraints.scroll_offset;
551 let cache_extent = calculate_cache_offset(constraints, 0.0, max_extent);
552
553 self.set_geometry(
554 app,
555 SliverGeometry::builder()
556 .scroll_extent(max_extent)
557 .paint_origin(constraints.overlap.min(0.0))
558 .paint_extent(clamp_double(
559 paint_extent,
560 0.0,
561 constraints.remaining_paint_extent,
562 ))
563 .cache_extent(cache_extent)
564 .max_paint_extent(max_extent + stretch_offset)
565 .has_visual_overflow(true)
567 .build(),
568 );
569 if stretch_offset > 0.0 {
570 0.0
571 } else {
572 (paint_extent - self.child_extent(app)).min(0.0)
573 }
574 }
575
576 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
578 let scroll_offset = self.constraints(app).scroll_offset;
579 let max_extent = self.max_extent(app);
580 self.layout_child(app, scroll_offset, max_extent, false);
581 let child_position = self.update_geometry(app);
582 self.scrolling_data_mut(app).child_position = Some(child_position);
583 }
584
585 fn child_main_axis_position(
587 self: RenderHandle<Self>,
588 app: &App,
589 child: AnyRenderObject,
590 ) -> f64 {
591 debug_assert_eq!(Some(child), self.child(app).map(AnyRenderBox::as_object));
592 self.scrolling_data(app)
593 .child_position
594 .expect("the header has been laid out")
595 }
596}
597
598#[derive(Clone, Copy, Debug)]
600pub struct RenderSliverPinnedPersistentHeaderData {
601 pub show_on_screen_configuration: Option<PersistentHeaderShowOnScreenConfiguration>,
606}
607
608impl RenderSliverPinnedPersistentHeaderData {
609 pub const fn new() -> RenderSliverPinnedPersistentHeaderData {
611 RenderSliverPinnedPersistentHeaderData {
612 show_on_screen_configuration: Some(PersistentHeaderShowOnScreenConfiguration::new()),
613 }
614 }
615}
616
617impl Default for RenderSliverPinnedPersistentHeaderData {
618 fn default() -> RenderSliverPinnedPersistentHeaderData {
619 RenderSliverPinnedPersistentHeaderData::new()
620 }
621}
622
623pub trait RenderSliverPinnedPersistentHeader: RenderSliverPersistentHeader {
629 fn pinned_data(self: RenderHandle<Self>, app: &App) -> &RenderSliverPinnedPersistentHeaderData;
631
632 fn pinned_data_mut(
634 self: RenderHandle<Self>,
635 app: &mut App,
636 ) -> &mut RenderSliverPinnedPersistentHeaderData;
637
638 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
640 let constraints = self.constraints(app);
641 let max_extent = self.max_extent(app);
642 let overlaps_content = constraints.overlap > 0.0;
643 self.layout_child(app, constraints.scroll_offset, max_extent, overlaps_content);
644 let effective_remaining_paint_extent =
645 (constraints.remaining_paint_extent - constraints.overlap).max(0.0);
646 let layout_extent = clamp_double(
647 max_extent - constraints.scroll_offset,
648 0.0,
649 effective_remaining_paint_extent,
650 );
651 let stretch_offset = if self.stretch_configuration(app).is_some() {
652 constraints.overlap.abs()
653 } else {
654 0.0
655 };
656 let child_extent = self.child_extent(app);
657 let min_extent = self.min_extent(app);
658 self.set_geometry(
659 app,
660 SliverGeometry::builder()
661 .scroll_extent(max_extent)
662 .paint_origin(constraints.overlap)
663 .paint_extent(child_extent.min(effective_remaining_paint_extent))
664 .layout_extent(layout_extent)
665 .max_paint_extent(max_extent + stretch_offset)
666 .max_scroll_obstruction_extent(min_extent)
667 .cache_extent(if layout_extent > 0.0 {
668 -constraints.cache_origin + layout_extent
669 } else {
670 layout_extent
671 })
672 .has_visual_overflow(true)
674 .build(),
675 );
676 }
677
678 fn child_main_axis_position(
680 self: RenderHandle<Self>,
681 _app: &App,
682 _child: AnyRenderObject,
683 ) -> f64 {
684 let _ = self;
685 0.0
686 }
687
688 fn show_on_screen(
690 self: RenderHandle<Self>,
691 app: &mut App,
692 descendant: Option<AnyRenderObject>,
693 rect: Option<Rect>,
694 duration: Duration,
695 curve: Rc<dyn Curve>,
696 ) {
697 let local_bounds = match descendant {
698 Some(descendant) => Some(transform_rect(
699 &descendant.get_transform_to(app, Some(self.as_object())),
700 rect.unwrap_or_else(|| descendant.paint_bounds(app)),
701 )),
702 None => rect,
703 };
704
705 let constraints = self.constraints(app);
706 let child_extent = self.child_extent(app);
707 let new_rect = match apply_growth_direction_to_axis_direction(
708 constraints.axis_direction,
709 constraints.growth_direction,
710 ) {
711 AxisDirection::Up => trim(
712 local_bounds,
713 f64::NEG_INFINITY,
714 f64::INFINITY,
715 child_extent,
716 f64::NEG_INFINITY,
717 ),
718 AxisDirection::Left => trim(
719 local_bounds,
720 f64::NEG_INFINITY,
721 child_extent,
722 f64::INFINITY,
723 f64::NEG_INFINITY,
724 ),
725 AxisDirection::Right => trim(
726 local_bounds,
727 f64::NEG_INFINITY,
728 f64::INFINITY,
729 f64::INFINITY,
730 0.0,
731 ),
732 AxisDirection::Down => trim(
733 local_bounds,
734 0.0,
735 f64::INFINITY,
736 f64::INFINITY,
737 f64::NEG_INFINITY,
738 ),
739 };
740
741 RenderObjectBase::show_on_screen(
742 self,
743 app,
744 Some(self.as_object()),
745 new_rect,
746 duration,
747 curve,
748 );
749 }
750}
751
752pub struct RenderSliverFloatingPersistentHeaderData {
754 controller: Option<Ref<AnimationController>>,
755 animation: Option<AnimationRef<f64>>,
756 last_actual_scroll_offset: Option<f64>,
757 effective_scroll_offset: Option<f64>,
758 last_started_scroll_direction: Option<ScrollDirection>,
762 child_position: Option<f64>,
765 vsync: Option<TickerProviderRef>,
766 pub snap_configuration: Option<FloatingHeaderSnapConfiguration>,
770 pub show_on_screen_configuration: Option<PersistentHeaderShowOnScreenConfiguration>,
775}
776
777impl RenderSliverFloatingPersistentHeaderData {
778 pub fn new(
780 vsync: Option<TickerProviderRef>,
781 snap_configuration: Option<FloatingHeaderSnapConfiguration>,
782 show_on_screen_configuration: Option<PersistentHeaderShowOnScreenConfiguration>,
783 ) -> RenderSliverFloatingPersistentHeaderData {
784 RenderSliverFloatingPersistentHeaderData {
785 controller: None,
786 animation: None,
787 last_actual_scroll_offset: None,
788 effective_scroll_offset: None,
789 last_started_scroll_direction: None,
790 child_position: None,
791 vsync,
792 snap_configuration,
793 show_on_screen_configuration,
794 }
795 }
796}
797
798pub trait RenderSliverFloatingPersistentHeader: RenderSliverPersistentHeader {
807 fn floating_data(
809 self: RenderHandle<Self>,
810 app: &App,
811 ) -> &RenderSliverFloatingPersistentHeaderData;
812
813 fn floating_data_mut(
815 self: RenderHandle<Self>,
816 app: &mut App,
817 ) -> &mut RenderSliverFloatingPersistentHeaderData;
818
819 fn did_detach(self: RenderHandle<Self>, app: &mut App) {
822 if let Some(controller) = self.floating_data(app).controller.handle() {
823 controller.dispose(app);
824 }
825 self.floating_data_mut(app).controller = None;
826 }
827
828 fn vsync(self: RenderHandle<Self>, app: &App) -> Option<TickerProviderRef> {
830 self.floating_data(app).vsync.clone()
831 }
832
833 fn set_vsync(self: RenderHandle<Self>, app: &mut App, value: Option<TickerProviderRef>) {
835 if value == self.floating_data(app).vsync {
836 return;
837 }
838 self.floating_data_mut(app).vsync = value.clone();
839 match value {
840 None => {
841 if let Some(controller) = self.floating_data(app).controller.handle() {
842 controller.dispose(app);
843 }
844 self.floating_data_mut(app).controller = None;
845 }
846 Some(vsync) => {
847 if let Some(controller) = self.floating_data(app).controller.handle() {
848 controller.resync(app, vsync);
849 }
850 }
851 }
852 }
853
854 fn snap_configuration(
856 self: RenderHandle<Self>,
857 app: &App,
858 ) -> Option<FloatingHeaderSnapConfiguration> {
859 self.floating_data(app).snap_configuration.clone()
860 }
861
862 fn set_snap_configuration(
864 self: RenderHandle<Self>,
865 app: &mut App,
866 value: Option<FloatingHeaderSnapConfiguration>,
867 ) {
868 self.floating_data_mut(app).snap_configuration = value;
869 }
870
871 fn show_on_screen_configuration(
873 self: RenderHandle<Self>,
874 app: &App,
875 ) -> Option<PersistentHeaderShowOnScreenConfiguration> {
876 self.floating_data(app).show_on_screen_configuration
877 }
878
879 fn set_show_on_screen_configuration(
881 self: RenderHandle<Self>,
882 app: &mut App,
883 value: Option<PersistentHeaderShowOnScreenConfiguration>,
884 ) {
885 self.floating_data_mut(app).show_on_screen_configuration = value;
886 }
887
888 fn effective_scroll_offset(self: RenderHandle<Self>, app: &App) -> Option<f64> {
890 self.floating_data(app).effective_scroll_offset
891 }
892
893 fn update_geometry(self: RenderHandle<Self>, app: &mut App) -> f64 {
896 let constraints = self.constraints(app);
897 let mut stretch_offset = 0.0;
898 if self.stretch_configuration(app).is_some() {
899 stretch_offset += constraints.overlap.abs();
900 }
901 let max_extent = self.max_extent(app);
902 let paint_extent = max_extent
903 - self
904 .effective_scroll_offset(app)
905 .expect("perform_layout sets the effective scroll offset");
906 let layout_extent = max_extent - constraints.scroll_offset;
907 self.set_geometry(
908 app,
909 SliverGeometry::builder()
910 .scroll_extent(max_extent)
911 .paint_origin(constraints.overlap.min(0.0))
912 .paint_extent(clamp_double(
913 paint_extent,
914 0.0,
915 constraints.remaining_paint_extent,
916 ))
917 .layout_extent(clamp_double(
918 layout_extent,
919 0.0,
920 constraints.remaining_paint_extent,
921 ))
922 .max_paint_extent(max_extent + stretch_offset)
923 .has_visual_overflow(true)
925 .build(),
926 );
927 if stretch_offset > 0.0 {
928 0.0
929 } else {
930 (paint_extent - self.child_extent(app)).min(0.0)
931 }
932 }
933
934 fn update_animation(
936 self: RenderHandle<Self>,
937 app: &mut App,
938 duration: Duration,
939 end_value: f64,
940 curve: Rc<dyn Curve>,
941 ) {
942 let vsync = self
943 .vsync(app)
944 .expect("vsync must be set if the floating header changes size animatedly");
945 let controller = match self.floating_data(app).controller.clone() {
946 Some(controller) => controller,
947 None => {
948 let controller = AnimationController::new(
949 app,
950 None,
951 Some(duration),
952 None,
953 0.0,
954 1.0,
955 waft_animation::AnimationBehavior::Normal,
956 vsync,
957 );
958 self.floating_data_mut(app).controller = Some(controller.clone());
959 controller.add_listener(
960 app,
961 Listener::handle_method(self.handle(), snap_tick::<Self>),
962 );
963 controller
964 }
965 };
966 let begin = self.effective_scroll_offset(app);
967 let tween = Tween::new(app, begin, Some(end_value));
968 let curve_tween = CurveTween::new(app, curve);
969 let animation = controller.drive(app, tween.chain(curve_tween));
970 self.floating_data_mut(app).animation = Some(animation);
971 }
972
973 fn update_scroll_start_direction(
975 self: RenderHandle<Self>,
976 app: &mut App,
977 direction: ScrollDirection,
978 ) {
979 self.floating_data_mut(app).last_started_scroll_direction = Some(direction);
980 }
981
982 fn maybe_start_snap_animation(
984 self: RenderHandle<Self>,
985 app: &mut App,
986 direction: ScrollDirection,
987 ) {
988 let Some(snap) = self.snap_configuration(app) else {
989 return;
990 };
991 let effective_scroll_offset = self
992 .effective_scroll_offset(app)
993 .expect("the header has been laid out");
994 let max_extent = self.max_extent(app);
995 if direction == ScrollDirection::Forward && effective_scroll_offset <= 0.0 {
996 return;
997 }
998 if direction == ScrollDirection::Reverse && effective_scroll_offset >= max_extent {
999 return;
1000 }
1001
1002 let end_value = if direction == ScrollDirection::Forward {
1003 0.0
1004 } else {
1005 max_extent
1006 };
1007 self.update_animation(app, snap.duration, end_value, Rc::clone(&snap.curve));
1008 if let Some(controller) = self.floating_data(app).controller.handle() {
1009 controller.forward(app, Some(0.0));
1010 }
1011 }
1012
1013 fn maybe_stop_snap_animation(
1015 self: RenderHandle<Self>,
1016 app: &mut App,
1017 _direction: ScrollDirection,
1018 ) {
1019 if let Some(controller) = self.floating_data(app).controller.handle() {
1020 controller.stop(app, false);
1021 }
1022 }
1023
1024 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
1026 let constraints = self.constraints(app);
1027 let max_extent = self.max_extent(app);
1028 let data = self.floating_data(app);
1029 let last_actual_scroll_offset = data.last_actual_scroll_offset;
1030 let effective_scroll_offset = data.effective_scroll_offset;
1031 let last_started_scroll_direction = data.last_started_scroll_direction;
1032 let new_effective_scroll_offset = match last_actual_scroll_offset {
1036 Some(last_actual_scroll_offset)
1037 if constraints.scroll_offset < last_actual_scroll_offset
1038 || effective_scroll_offset.is_some_and(|offset| offset < max_extent) =>
1039 {
1040 let mut delta = last_actual_scroll_offset - constraints.scroll_offset;
1041 let mut effective_scroll_offset =
1042 effective_scroll_offset.expect("laid out at least once");
1043 let allow_floating_expansion = constraints.user_scroll_direction
1044 == ScrollDirection::Forward
1045 || last_started_scroll_direction == Some(ScrollDirection::Forward);
1046 if allow_floating_expansion {
1047 if effective_scroll_offset > max_extent {
1048 effective_scroll_offset = max_extent;
1051 }
1052 } else if delta > 0.0 {
1053 delta = 0.0;
1055 }
1056 clamp_double(
1057 effective_scroll_offset - delta,
1058 0.0,
1059 constraints.scroll_offset,
1060 )
1061 }
1062 _ => constraints.scroll_offset,
1063 };
1064 self.floating_data_mut(app).effective_scroll_offset = Some(new_effective_scroll_offset);
1065 let overlaps_content = new_effective_scroll_offset < constraints.scroll_offset;
1066
1067 self.layout_child(
1068 app,
1069 new_effective_scroll_offset,
1070 max_extent,
1071 overlaps_content,
1072 );
1073 let child_position = self.update_geometry(app);
1074 let data = self.floating_data_mut(app);
1075 data.child_position = Some(child_position);
1076 data.last_actual_scroll_offset = Some(constraints.scroll_offset);
1077 }
1078
1079 fn show_on_screen(
1081 self: RenderHandle<Self>,
1082 app: &mut App,
1083 descendant: Option<AnyRenderObject>,
1084 rect: Option<Rect>,
1085 duration: Duration,
1086 curve: Rc<dyn Curve>,
1087 ) {
1088 let Some(show_on_screen) = self.show_on_screen_configuration(app) else {
1089 return RenderObjectBase::show_on_screen(self, app, descendant, rect, duration, curve);
1090 };
1091
1092 let child = self.child(app);
1093 debug_assert!(child.is_some() || descendant.is_none());
1094 let child_bounds = match descendant {
1100 Some(descendant) => Some(transform_rect(
1101 &descendant.get_transform_to(app, child.map(AnyRenderBox::as_object)),
1102 rect.unwrap_or_else(|| descendant.paint_bounds(app)),
1103 )),
1104 None => rect,
1105 };
1106
1107 let constraints = self.constraints(app);
1108 let child_extent = self.child_extent(app);
1109 let (mut target_extent, target_rect) = match apply_growth_direction_to_axis_direction(
1110 constraints.axis_direction,
1111 constraints.growth_direction,
1112 ) {
1113 AxisDirection::Up => (
1114 child_extent - child_bounds.map_or(0.0, |bounds| bounds.top),
1115 trim(
1116 child_bounds,
1117 f64::NEG_INFINITY,
1118 f64::INFINITY,
1119 child_extent,
1120 f64::NEG_INFINITY,
1121 ),
1122 ),
1123 AxisDirection::Right => (
1124 child_bounds.map_or(child_extent, |bounds| bounds.right),
1125 trim(
1126 child_bounds,
1127 f64::NEG_INFINITY,
1128 f64::INFINITY,
1129 f64::INFINITY,
1130 0.0,
1131 ),
1132 ),
1133 AxisDirection::Down => (
1134 child_bounds.map_or(child_extent, |bounds| bounds.bottom),
1135 trim(
1136 child_bounds,
1137 0.0,
1138 f64::INFINITY,
1139 f64::INFINITY,
1140 f64::NEG_INFINITY,
1141 ),
1142 ),
1143 AxisDirection::Left => (
1144 child_extent - child_bounds.map_or(0.0, |bounds| bounds.left),
1145 trim(
1146 child_bounds,
1147 f64::NEG_INFINITY,
1148 child_extent,
1149 f64::INFINITY,
1150 f64::NEG_INFINITY,
1151 ),
1152 ),
1153 };
1154
1155 let max_extent = self.max_extent(app);
1157 let effective_max_extent = child_extent.max(max_extent);
1158
1159 target_extent = clamp_double(
1160 clamp_double(
1161 target_extent,
1162 show_on_screen.min_show_on_screen_extent,
1163 show_on_screen.max_show_on_screen_extent,
1164 ),
1165 child_extent,
1168 effective_max_extent,
1169 );
1170
1171 let controller_status = self
1173 .floating_data(app)
1174 .controller
1175 .handle()
1176 .map(|controller| controller.status(app));
1177 if target_extent > child_extent && controller_status != Some(AnimationStatus::Forward) {
1178 let target_scroll_offset = max_extent - target_extent;
1179 debug_assert!(
1180 self.vsync(app).is_some(),
1181 "vsync must be set if the floating header changes size animatedly"
1182 );
1183 self.update_animation(app, duration, target_scroll_offset, Rc::clone(&curve));
1184 if let Some(controller) = self.floating_data(app).controller.handle() {
1185 controller.forward(app, Some(0.0));
1186 }
1187 }
1188
1189 let descendant = match descendant {
1190 None => Some(self.as_object()),
1191 Some(_) => child.map(AnyRenderBox::as_object),
1192 };
1193 RenderObjectBase::show_on_screen(self, app, descendant, target_rect, duration, curve);
1194 }
1195
1196 fn child_main_axis_position(
1198 self: RenderHandle<Self>,
1199 app: &App,
1200 child: AnyRenderObject,
1201 ) -> f64 {
1202 debug_assert_eq!(Some(child), self.child(app).map(AnyRenderBox::as_object));
1203 self.floating_data(app).child_position.unwrap_or(0.0)
1204 }
1205
1206 fn as_floating_persistent_header(
1209 self: RenderHandle<Self>,
1210 ) -> AnyRenderSliverFloatingPersistentHeader {
1211 AnyRenderSliverFloatingPersistentHeader {
1212 id: self.id(),
1213 vtable: const { &RenderSliverFloatingPersistentHeaderVTable::of::<Self>() },
1214 }
1215 }
1216}
1217
1218struct RenderSliverFloatingPersistentHeaderVTable {
1220 update_scroll_start_direction: fn(&mut App, HandleId, ScrollDirection),
1221 maybe_start_snap_animation: fn(&mut App, HandleId, ScrollDirection),
1222 maybe_stop_snap_animation: fn(&mut App, HandleId, ScrollDirection),
1223}
1224
1225impl RenderSliverFloatingPersistentHeaderVTable {
1226 const fn of<T: RenderSliverFloatingPersistentHeader>()
1227 -> RenderSliverFloatingPersistentHeaderVTable {
1228 RenderSliverFloatingPersistentHeaderVTable {
1229 update_scroll_start_direction: |app, id, direction| {
1230 T::update_scroll_start_direction(resolve(id), app, direction)
1231 },
1232 maybe_start_snap_animation: |app, id, direction| {
1233 T::maybe_start_snap_animation(resolve(id), app, direction)
1234 },
1235 maybe_stop_snap_animation: |app, id, direction| {
1236 T::maybe_stop_snap_animation(resolve(id), app, direction)
1237 },
1238 }
1239 }
1240}
1241
1242#[derive(Clone, Copy)]
1245pub struct AnyRenderSliverFloatingPersistentHeader {
1246 id: HandleId,
1247 vtable: &'static RenderSliverFloatingPersistentHeaderVTable,
1248}
1249
1250impl AnyRenderSliverFloatingPersistentHeader {
1251 pub fn update_scroll_start_direction(self, app: &mut App, direction: ScrollDirection) {
1253 (self.vtable.update_scroll_start_direction)(app, self.id, direction)
1254 }
1255
1256 pub fn maybe_start_snap_animation(self, app: &mut App, direction: ScrollDirection) {
1258 (self.vtable.maybe_start_snap_animation)(app, self.id, direction)
1259 }
1260
1261 pub fn maybe_stop_snap_animation(self, app: &mut App, direction: ScrollDirection) {
1263 (self.vtable.maybe_stop_snap_animation)(app, self.id, direction)
1264 }
1265}
1266
1267impl PartialEq for AnyRenderSliverFloatingPersistentHeader {
1268 fn eq(&self, other: &AnyRenderSliverFloatingPersistentHeader) -> bool {
1269 self.id == other.id
1270 }
1271}
1272
1273impl Eq for AnyRenderSliverFloatingPersistentHeader {}
1274
1275fn snap_tick<T: RenderSliverFloatingPersistentHeader>(this: Handle<T>, app: &mut App) {
1277 let this = RenderHandle::from_handle(this);
1278 let Some(animation) = this.floating_data(app).animation.handle() else {
1279 return;
1280 };
1281 let value = animation.value(app);
1282 if this.effective_scroll_offset(app) == Some(value) {
1283 return;
1284 }
1285 this.floating_data_mut(app).effective_scroll_offset = Some(value);
1286 RenderSliverPersistentHeader::mark_needs_layout(this, app);
1287}
1288
1289pub trait RenderSliverFloatingPinnedPersistentHeader: RenderSliverFloatingPersistentHeader {
1298 fn update_geometry(self: RenderHandle<Self>, app: &mut App) -> f64 {
1300 let constraints = self.constraints(app);
1301 let min_extent = self.min_extent(app);
1302 let min_allowed_extent = if constraints.remaining_paint_extent > min_extent {
1303 min_extent
1304 } else {
1305 constraints.remaining_paint_extent
1306 };
1307 let max_extent = self.max_extent(app);
1308 let paint_extent = max_extent
1309 - self
1310 .effective_scroll_offset(app)
1311 .expect("perform_layout sets the effective scroll offset");
1312 let clamped_paint_extent = clamp_double(
1313 paint_extent,
1314 min_allowed_extent,
1315 constraints.remaining_paint_extent,
1316 );
1317 let layout_extent = max_extent - constraints.scroll_offset;
1318 let stretch_offset = if self.stretch_configuration(app).is_some() {
1319 constraints.overlap.abs()
1320 } else {
1321 0.0
1322 };
1323 self.set_geometry(
1324 app,
1325 SliverGeometry::builder()
1326 .scroll_extent(max_extent)
1327 .paint_origin(constraints.overlap.min(0.0))
1328 .paint_extent(clamped_paint_extent)
1329 .layout_extent(clamp_double(layout_extent, 0.0, clamped_paint_extent))
1330 .max_paint_extent(max_extent + stretch_offset)
1331 .max_scroll_obstruction_extent(min_extent)
1332 .has_visual_overflow(true)
1334 .build(),
1335 );
1336 0.0
1337 }
1338}
1339
1340#[cfg(test)]
1341mod tests {
1342 use std::cell::Cell;
1343 use waft_foundation::Ref;
1344
1345 use waft_embedder::Size;
1346 use waft_foundation::{AppCell, Task};
1347
1348 use super::*;
1349 use crate::object::{RenderObject, RenderObjectData, RenderObjectWithChildData};
1350 use crate::sliver::{AnyRenderSliver, RenderSliverData};
1351 use crate::sliver_multi_box_adaptor::test_support::hit_testable_box;
1352 use crate::sliver_multi_box_adaptor::viewport_test_support::ScrollHarness;
1353
1354 macro_rules! persistent_header_leaf {
1357 ($leaf:ident, $extra:ident, $perform_layout:path, $child_position:path) => {
1358 struct $leaf {
1359 render_object: RenderObjectData,
1360 render_sliver: RenderSliverData,
1361 child: RenderObjectWithChildData<AnyRenderBox>,
1362 header: RenderSliverPersistentHeaderData,
1363 extra: $extra,
1364 min_extent: f64,
1365 max_extent: f64,
1366 }
1367
1368 impl RenderObjectWithChildMixin for $leaf {
1369 type ChildType = AnyRenderBox;
1370
1371 fn child_data(
1372 self: RenderHandle<Self>,
1373 app: &App,
1374 ) -> &RenderObjectWithChildData<AnyRenderBox> {
1375 &self.get(app).child
1376 }
1377
1378 fn child_data_mut(
1379 self: RenderHandle<Self>,
1380 app: &mut App,
1381 ) -> &mut RenderObjectWithChildData<AnyRenderBox> {
1382 &mut self.get_mut(app).child
1383 }
1384 }
1385
1386 impl RenderSliverHelpers for $leaf {}
1387
1388 impl RenderSliverPersistentHeader for $leaf {
1389 crate::render_sliver_persistent_header_accessors!();
1390
1391 fn max_extent(self: RenderHandle<Self>, app: &App) -> f64 {
1392 self.get(app).max_extent
1393 }
1394
1395 fn min_extent(self: RenderHandle<Self>, app: &App) -> f64 {
1396 self.get(app).min_extent
1397 }
1398 }
1399
1400 impl RenderObject for $leaf {
1401 crate::render_object_accessors!();
1402
1403 fn visit_children(
1404 self: RenderHandle<Self>,
1405 app: &App,
1406 visitor: &mut dyn FnMut(AnyRenderObject),
1407 ) {
1408 if let Some(child) = self.child(app) {
1409 visitor(child.as_object());
1410 }
1411 }
1412
1413 fn apply_paint_transform(
1414 self: RenderHandle<Self>,
1415 app: &App,
1416 child: AnyRenderObject,
1417 transform: &mut Matrix4,
1418 ) {
1419 RenderSliverPersistentHeader::apply_paint_transform(self, app, child, transform)
1420 }
1421
1422 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
1423 $perform_layout(self, app)
1424 }
1425
1426 fn paint(
1427 self: RenderHandle<Self>,
1428 app: &mut App,
1429 context: &mut PaintingContext,
1430 offset: Offset,
1431 ) {
1432 RenderSliverPersistentHeader::paint(self, app, context, offset)
1433 }
1434 }
1435
1436 impl RenderSliver for $leaf {
1437 crate::render_sliver_accessors!();
1438
1439 fn hit_test_children(
1440 self: RenderHandle<Self>,
1441 app: &mut App,
1442 result: &mut SliverHitTestResult<'_>,
1443 main_axis_position: f64,
1444 cross_axis_position: f64,
1445 ) -> bool {
1446 RenderSliverPersistentHeader::hit_test_children(
1447 self,
1448 app,
1449 result,
1450 main_axis_position,
1451 cross_axis_position,
1452 )
1453 }
1454
1455 fn child_main_axis_position(
1456 self: RenderHandle<Self>,
1457 app: &App,
1458 child: AnyRenderObject,
1459 ) -> f64 {
1460 $child_position(self, app, child)
1461 }
1462 }
1463 };
1464 }
1465
1466 type ScrollingExtra = RenderSliverScrollingPersistentHeaderData;
1467 type PinnedExtra = RenderSliverPinnedPersistentHeaderData;
1468
1469 persistent_header_leaf!(
1470 TestScrollingHeader,
1471 ScrollingExtra,
1472 RenderSliverScrollingPersistentHeader::perform_layout,
1473 RenderSliverScrollingPersistentHeader::child_main_axis_position
1474 );
1475
1476 impl RenderSliverScrollingPersistentHeader for TestScrollingHeader {
1477 fn scrolling_data(
1478 self: RenderHandle<Self>,
1479 app: &App,
1480 ) -> &RenderSliverScrollingPersistentHeaderData {
1481 &self.get(app).extra
1482 }
1483
1484 fn scrolling_data_mut(
1485 self: RenderHandle<Self>,
1486 app: &mut App,
1487 ) -> &mut RenderSliverScrollingPersistentHeaderData {
1488 &mut self.get_mut(app).extra
1489 }
1490 }
1491
1492 persistent_header_leaf!(
1493 TestPinnedHeader,
1494 PinnedExtra,
1495 RenderSliverPinnedPersistentHeader::perform_layout,
1496 RenderSliverPinnedPersistentHeader::child_main_axis_position
1497 );
1498
1499 impl RenderSliverPinnedPersistentHeader for TestPinnedHeader {
1500 fn pinned_data(
1501 self: RenderHandle<Self>,
1502 app: &App,
1503 ) -> &RenderSliverPinnedPersistentHeaderData {
1504 &self.get(app).extra
1505 }
1506
1507 fn pinned_data_mut(
1508 self: RenderHandle<Self>,
1509 app: &mut App,
1510 ) -> &mut RenderSliverPinnedPersistentHeaderData {
1511 &mut self.get_mut(app).extra
1512 }
1513 }
1514
1515 fn scrolling_header(
1516 app: &mut App,
1517 min_extent: f64,
1518 max_extent: f64,
1519 ) -> RenderHandle<TestScrollingHeader> {
1520 let child = hit_testable_box(app, max_extent);
1521 let header = RenderHandle::new_sliver(
1522 app,
1523 TestScrollingHeader {
1524 render_object: RenderObjectData::new(),
1525 render_sliver: RenderSliverData::new(),
1526 child: RenderObjectWithChildData::new(),
1527 header: RenderSliverPersistentHeaderData::new(None),
1528 extra: RenderSliverScrollingPersistentHeaderData::new(),
1529 min_extent,
1530 max_extent,
1531 },
1532 );
1533 header.set_child(app, Some(child));
1534 header
1535 }
1536
1537 fn pinned_header(
1538 app: &mut App,
1539 min_extent: f64,
1540 max_extent: f64,
1541 ) -> RenderHandle<TestPinnedHeader> {
1542 let child = hit_testable_box(app, max_extent);
1543 let header = RenderHandle::new_sliver(
1544 app,
1545 TestPinnedHeader {
1546 render_object: RenderObjectData::new(),
1547 render_sliver: RenderSliverData::new(),
1548 child: RenderObjectWithChildData::new(),
1549 header: RenderSliverPersistentHeaderData::new(None),
1550 extra: RenderSliverPinnedPersistentHeaderData::new(),
1551 min_extent,
1552 max_extent,
1553 },
1554 );
1555 header.set_child(app, Some(child));
1556 header
1557 }
1558
1559 fn lay_out(app: &mut App, sliver: AnyRenderSliver, scroll_offset: f64) -> ScrollHarness {
1562 let harness = ScrollHarness::new(app, sliver, Size::new(200.0, 400.0));
1563 harness.scroll_to(app, scroll_offset);
1564 harness
1565 }
1566
1567 #[test]
1570 fn a_scrolling_header_shrinks_as_it_scrolls_off() {
1571 let cell = AppCell::new();
1572 let mut app = cell.borrow_mut();
1573 let header = scrolling_header(&mut app, 40.0, 120.0);
1574 lay_out(&mut app, header.as_sliver(), 50.0);
1575
1576 let geometry = header.geometry(&app);
1577 assert_eq!(geometry.scroll_extent, 120.0);
1578 assert_eq!(geometry.paint_extent, 70.0);
1579 assert_eq!(geometry.max_scroll_obstruction_extent, 0.0);
1580 assert_eq!(header.child_extent(&app), 70.0);
1582 assert_eq!(header.last_shrink_offset(&app), 50.0);
1583 let child = header.child(&app).expect("has a child");
1584 assert_eq!(
1585 RenderSliver::child_main_axis_position(header, &app, child.as_object()),
1586 0.0
1587 );
1588 }
1589
1590 #[test]
1593 fn a_pinned_header_stays_at_the_leading_edge() {
1594 let cell = AppCell::new();
1595 let mut app = cell.borrow_mut();
1596 let header = pinned_header(&mut app, 40.0, 120.0);
1597 lay_out(&mut app, header.as_sliver(), 100.0);
1598
1599 let geometry = header.geometry(&app);
1600 assert_eq!(geometry.scroll_extent, 120.0);
1601 assert_eq!(geometry.max_scroll_obstruction_extent, 40.0);
1602 assert_eq!(header.child_extent(&app), 40.0);
1604 assert_eq!(geometry.paint_extent, 40.0);
1605 assert_eq!(geometry.layout_extent, 20.0);
1606 let child = header.child(&app).expect("has a child");
1607 assert_eq!(
1608 RenderSliver::child_main_axis_position(header, &app, child.as_object()),
1609 0.0
1610 );
1611 }
1612
1613 #[test]
1615 fn an_unscrolled_pinned_header_is_at_its_maximum_extent() {
1616 let cell = AppCell::new();
1617 let mut app = cell.borrow_mut();
1618 let header = pinned_header(&mut app, 40.0, 120.0);
1619 lay_out(&mut app, header.as_sliver(), 0.0);
1620
1621 let geometry = header.geometry(&app);
1622 assert_eq!(geometry.paint_extent, 120.0);
1623 assert_eq!(geometry.layout_extent, 120.0);
1624 assert_eq!(header.last_shrink_offset(&app), 0.0);
1625 assert!(!header.last_overlaps_content(&app));
1626 }
1627
1628 #[test]
1630 fn layout_child_reports_the_shrink_offset() {
1631 let cell = AppCell::new();
1632 let mut app = cell.borrow_mut();
1633 let header = scrolling_header(&mut app, 40.0, 120.0);
1634 lay_out(&mut app, header.as_sliver(), 80.0);
1635
1636 assert_eq!(header.last_shrink_offset(&app), 80.0);
1637 assert_eq!(header.child_extent(&app), 40.0);
1639 }
1640
1641 #[test]
1644 fn the_stretch_trigger_fires_once_per_crossing() {
1645 let cell = AppCell::new();
1646 let mut app = cell.borrow_mut();
1647 let triggered = Rc::new(Cell::new(0));
1648 let counter = Rc::clone(&triggered);
1649 let configuration =
1650 OverScrollHeaderStretchConfiguration::new().on_stretch_trigger(Rc::new(move |_app| {
1651 counter.set(counter.get() + 1);
1652 Task::ready(())
1653 }));
1654 let header = scrolling_header(&mut app, 40.0, 120.0);
1655 header.set_stretch_configuration(&mut app, Some(configuration));
1656 let harness = lay_out(&mut app, header.as_sliver(), 0.0);
1657 assert_eq!(triggered.get(), 0);
1658
1659 harness.scroll_to(&mut app, -120.0);
1660 assert_eq!(triggered.get(), 1);
1661 harness.scroll_to(&mut app, -130.0);
1662 assert_eq!(triggered.get(), 1, "already past the trigger offset");
1663
1664 harness.scroll_to(&mut app, 0.0);
1665 harness.scroll_to(&mut app, -100.0);
1666 assert_eq!(triggered.get(), 2);
1667 }
1668
1669 #[test]
1671 fn the_show_on_screen_configuration_defaults_to_unbounded() {
1672 let configuration = PersistentHeaderShowOnScreenConfiguration::new();
1673 assert_eq!(configuration.min_show_on_screen_extent, f64::NEG_INFINITY);
1674 assert_eq!(configuration.max_show_on_screen_extent, f64::INFINITY);
1675 let snap = FloatingHeaderSnapConfiguration::new();
1676 assert_eq!(snap.duration, Duration::from_millis(300));
1677 }
1678
1679 struct Vsync;
1681
1682 impl TickerProviderObject for Vsync {
1683 fn create_ticker(
1684 self: Handle<Self>,
1685 app: &mut App,
1686 on_tick: TickerCallback,
1687 ) -> Ref<Ticker> {
1688 Ticker::new(app, on_tick, None)
1689 }
1690 }
1691
1692 #[test]
1693 fn two_ticker_provider_refs_are_equal_when_they_erase_the_same_provider() {
1694 let cell = AppCell::new();
1695 let mut app = cell.borrow_mut();
1696 let vsync = app.create(Vsync);
1697 let other_vsync = app.create(Vsync);
1698
1699 assert!(TickerProviderRef::new(vsync.handle()) == TickerProviderRef::new(vsync.handle()));
1700 assert!(
1701 TickerProviderRef::new(vsync.handle()) != TickerProviderRef::new(other_vsync.handle())
1702 );
1703 }
1704}