1use std::rc::Rc;
4
5use waft_embedder::{Matrix4, Offset};
6use waft_foundation::{App, Handle, PRECISION_ERROR_TOLERANCE};
7
8use crate::box_::AnyRenderBox;
9use crate::object::{
10 AnyRenderObject, ContainerRenderObjectData, ContainerRenderObjectMixin, RenderHandle,
11 RenderObject, RenderObjectData,
12};
13use crate::painting_context::PaintingContext;
14use crate::pipeline_owner::PipelineOwner;
15use crate::sliver::{
16 RenderSliver, RenderSliverData, RenderSliverHelpers, SliverGeometry, SliverHitTestResult,
17};
18use crate::sliver_multi_box_adaptor::{
19 RenderSliverBoxChildManager, RenderSliverMultiBoxAdaptor, RenderSliverMultiBoxAdaptorData,
20 RenderSliverWithKeepAliveMixin, SliverMultiBoxAdaptorParentData,
21};
22
23pub struct RenderSliverList {
33 render_object: RenderObjectData,
34 render_sliver: RenderSliverData,
35 container: ContainerRenderObjectData<AnyRenderBox>,
36 adaptor: RenderSliverMultiBoxAdaptorData,
37}
38
39impl RenderSliverList {
40 pub fn new(
42 app: &mut App,
43 child_manager: Rc<dyn RenderSliverBoxChildManager>,
44 ) -> RenderHandle<RenderSliverList> {
45 RenderHandle::new_sliver(
46 app,
47 RenderSliverList {
48 render_object: RenderObjectData::new(),
49 render_sliver: RenderSliverData::new(),
50 container: ContainerRenderObjectData::new(),
51 adaptor: RenderSliverMultiBoxAdaptorData::new(child_manager),
52 },
53 )
54 }
55}
56
57impl ContainerRenderObjectMixin for RenderSliverList {
58 type ChildType = AnyRenderBox;
59 type ParentDataType = SliverMultiBoxAdaptorParentData;
60
61 fn container_data(
62 self: RenderHandle<Self>,
63 app: &App,
64 ) -> &ContainerRenderObjectData<AnyRenderBox> {
65 &self.get(app).container
66 }
67
68 fn container_data_mut(
69 self: RenderHandle<Self>,
70 app: &mut App,
71 ) -> &mut ContainerRenderObjectData<AnyRenderBox> {
72 &mut self.get_mut(app).container
73 }
74
75 fn insert(
76 self: RenderHandle<Self>,
77 app: &mut App,
78 child: AnyRenderBox,
79 after: Option<AnyRenderBox>,
80 ) {
81 RenderSliverMultiBoxAdaptor::insert(self, app, child, after)
82 }
83
84 fn move_child(
85 self: RenderHandle<Self>,
86 app: &mut App,
87 child: AnyRenderBox,
88 after: Option<AnyRenderBox>,
89 ) {
90 RenderSliverMultiBoxAdaptor::move_child(self, app, child, after)
91 }
92
93 fn remove(self: RenderHandle<Self>, app: &mut App, child: AnyRenderBox) {
94 RenderSliverMultiBoxAdaptor::remove(self, app, child)
95 }
96
97 fn remove_all(self: RenderHandle<Self>, app: &mut App) {
98 RenderSliverMultiBoxAdaptor::remove_all(self, app)
99 }
100}
101
102impl RenderSliverHelpers for RenderSliverList {}
103
104impl RenderSliverWithKeepAliveMixin for RenderSliverList {}
105
106impl RenderSliverMultiBoxAdaptor for RenderSliverList {
107 crate::render_sliver_multi_box_adaptor_accessors!();
108}
109
110impl RenderObject for RenderSliverList {
111 crate::render_object_accessors!();
112
113 fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
114 RenderSliverMultiBoxAdaptor::setup_parent_data(self, app, child);
115 RenderSliverWithKeepAliveMixin::setup_parent_data(self, app, child);
116 }
117
118 fn visit_children(
119 self: RenderHandle<Self>,
120 app: &App,
121 visitor: &mut dyn FnMut(AnyRenderObject),
122 ) {
123 RenderSliverMultiBoxAdaptor::visit_children(self, app, visitor)
124 }
125
126 fn did_attach(self: RenderHandle<Self>, app: &mut App, owner: Handle<PipelineOwner>) {
127 RenderSliverMultiBoxAdaptor::did_attach(self, app, owner)
128 }
129
130 fn did_detach(self: RenderHandle<Self>, app: &mut App) {
131 RenderSliverMultiBoxAdaptor::did_detach(self, app)
132 }
133
134 fn redepth_children(self: RenderHandle<Self>, app: &mut App) {
135 RenderSliverMultiBoxAdaptor::redepth_children(self, app)
136 }
137
138 fn apply_paint_transform(
139 self: RenderHandle<Self>,
140 app: &App,
141 child: AnyRenderObject,
142 transform: &mut Matrix4,
143 ) {
144 RenderSliverMultiBoxAdaptor::apply_paint_transform(self, app, child, transform)
145 }
146
147 fn paints_child(self: RenderHandle<Self>, app: &App, child: AnyRenderObject) -> bool {
148 RenderSliverMultiBoxAdaptor::paints_child(self, app, child)
149 }
150
151 fn paint(
152 self: RenderHandle<Self>,
153 app: &mut App,
154 context: &mut PaintingContext,
155 offset: Offset,
156 ) {
157 RenderSliverMultiBoxAdaptor::paint(self, app, context, offset)
158 }
159
160 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
161 let constraints = self.constraints(app);
162 let child_manager = self.child_manager(app);
163 child_manager.did_start_layout(app);
164 child_manager.set_did_underflow(app, false);
165
166 let scroll_offset = constraints.scroll_offset + constraints.cache_origin;
167 debug_assert!(scroll_offset >= 0.0);
168 let remaining_extent = constraints.remaining_cache_extent;
169 debug_assert!(remaining_extent >= 0.0);
170 let target_end_scroll_offset = scroll_offset + remaining_extent;
171 let child_constraints = constraints.as_box_constraints(0.0, f64::INFINITY, None);
172 let mut leading_garbage = 0;
173 let mut trailing_garbage = 0;
174 let mut reached_end = false;
175
176 if self.first_child(app).is_none() && !self.add_initial_child(app, 0, 0.0) {
192 self.set_geometry(app, SliverGeometry::ZERO);
194 child_manager.did_finish_layout(app);
195 return;
196 }
197
198 let mut leading_child_with_layout: Option<AnyRenderBox> = None;
204 let mut trailing_child_with_layout: Option<AnyRenderBox> = None;
205
206 let mut earliest_useful_child = self.first_child(app);
207
208 let first_child = self.first_child(app).expect("checked above");
213 if RenderSliver::child_scroll_offset(self, app, first_child.as_object()).is_none() {
214 let mut leading_children_without_layout_offset = 0;
215 while let Some(current) = earliest_useful_child {
216 if RenderSliver::child_scroll_offset(self, app, current.as_object()).is_some() {
217 break;
218 }
219 earliest_useful_child = self.child_after(app, current);
220 leading_children_without_layout_offset += 1;
221 }
222 self.collect_garbage(app, leading_children_without_layout_offset, 0);
225 if self.first_child(app).is_none() && !self.add_initial_child(app, 0, 0.0) {
227 self.set_geometry(app, SliverGeometry::ZERO);
229 child_manager.did_finish_layout(app);
230 return;
231 }
232 }
233
234 earliest_useful_child = self.first_child(app);
236 loop {
237 let current = earliest_useful_child.expect("the child list is not empty");
238 let earliest_scroll_offset =
239 RenderSliver::child_scroll_offset(self, app, current.as_object())
240 .expect("a laid-out child has a scroll offset");
241 if earliest_scroll_offset <= scroll_offset {
242 break;
243 }
244 earliest_useful_child =
246 self.insert_and_layout_leading_child(app, child_constraints, true);
247 let Some(inserted) = earliest_useful_child else {
248 let first_child = self.first_child(app).expect("the child list is not empty");
249 first_child
250 .as_object()
251 .parent_data_of_mut::<SliverMultiBoxAdaptorParentData>(app)
252 .set_layout_offset(Some(0.0));
253
254 if scroll_offset == 0.0 {
255 first_child.layout(app, child_constraints, true);
259 leading_child_with_layout = Some(first_child);
260 trailing_child_with_layout.get_or_insert(first_child);
261 break;
262 }
263 self.set_geometry(
267 app,
268 SliverGeometry::builder()
269 .scroll_offset_correction(-scroll_offset)
270 .build(),
271 );
272 return;
273 };
274
275 let first_child = self.first_child(app).expect("the child list is not empty");
276 let first_child_scroll_offset =
277 earliest_scroll_offset - self.paint_extent_of(app, first_child);
278 if first_child_scroll_offset < -PRECISION_ERROR_TOLERANCE {
280 self.set_geometry(
283 app,
284 SliverGeometry::builder()
285 .scroll_offset_correction(-first_child_scroll_offset)
286 .build(),
287 );
288 first_child
289 .as_object()
290 .parent_data_of_mut::<SliverMultiBoxAdaptorParentData>(app)
291 .set_layout_offset(Some(0.0));
292 return;
293 }
294
295 inserted
296 .as_object()
297 .parent_data_of_mut::<SliverMultiBoxAdaptorParentData>(app)
298 .set_layout_offset(Some(first_child_scroll_offset));
299 debug_assert_eq!(Some(inserted), self.first_child(app));
300 leading_child_with_layout = Some(inserted);
301 trailing_child_with_layout.get_or_insert(inserted);
302 }
303
304 debug_assert!(
305 RenderSliver::child_scroll_offset(
306 self,
307 app,
308 self.first_child(app).expect("not empty").as_object()
309 )
310 .expect("laid out")
311 > -PRECISION_ERROR_TOLERANCE
312 );
313
314 if scroll_offset < PRECISION_ERROR_TOLERANCE {
317 loop {
319 let first_child = self.first_child(app).expect("the child list is not empty");
320 if self.index_of(app, first_child) <= 0 {
321 break;
322 }
323 let earliest_scroll_offset =
324 RenderSliver::child_scroll_offset(self, app, first_child.as_object())
325 .expect("laid out");
326 let inserted = self.insert_and_layout_leading_child(app, child_constraints, true);
329 debug_assert!(inserted.is_some());
330 let first_child = self.first_child(app).expect("the child list is not empty");
331 let first_child_scroll_offset =
332 earliest_scroll_offset - self.paint_extent_of(app, first_child);
333 first_child
334 .as_object()
335 .parent_data_of_mut::<SliverMultiBoxAdaptorParentData>(app)
336 .set_layout_offset(Some(0.0));
337 if first_child_scroll_offset < -PRECISION_ERROR_TOLERANCE {
339 self.set_geometry(
340 app,
341 SliverGeometry::builder()
342 .scroll_offset_correction(-first_child_scroll_offset)
343 .build(),
344 );
345 return;
346 }
347 }
348 }
349
350 let earliest_useful_child = self.first_child(app).expect("the child list is not empty");
356
357 if leading_child_with_layout.is_none() {
360 earliest_useful_child.layout(app, child_constraints, true);
361 trailing_child_with_layout = Some(earliest_useful_child);
362 }
363
364 let mut in_layout_range = true;
369 let mut child = Some(earliest_useful_child);
370 let mut index = self.index_of(app, earliest_useful_child);
371 let mut end_scroll_offset =
372 RenderSliver::child_scroll_offset(self, app, earliest_useful_child.as_object())
373 .expect("laid out")
374 + self.paint_extent_of(app, earliest_useful_child);
375
376 let advance = |app: &mut App,
378 child: &mut Option<AnyRenderBox>,
379 index: &mut i32,
380 end_scroll_offset: &mut f64,
381 in_layout_range: &mut bool,
382 trailing_child_with_layout: &mut Option<AnyRenderBox>|
383 -> bool {
384 let current = child.expect("advance is only called with a child");
386 if Some(current) == *trailing_child_with_layout {
387 *in_layout_range = false;
388 }
389 *child = self.child_after(app, current);
390 if child.is_none() {
391 *in_layout_range = false;
392 }
393 *index += 1;
394 if !*in_layout_range {
395 if child.is_none_or(|current| self.index_of(app, current) != *index) {
396 *child = self.insert_and_layout_child(
398 app,
399 child_constraints,
400 *trailing_child_with_layout,
401 true,
402 );
403 if child.is_none() {
404 return false;
406 }
407 } else {
408 child.expect("checked").layout(app, child_constraints, true);
410 }
411 *trailing_child_with_layout = *child;
412 }
413 let current = child.expect("set above");
414 let child_parent_data = current
415 .as_object()
416 .parent_data_of_mut::<SliverMultiBoxAdaptorParentData>(app);
417 child_parent_data.set_layout_offset(Some(*end_scroll_offset));
418 debug_assert_eq!(child_parent_data.index, Some(*index));
419 *end_scroll_offset = RenderSliver::child_scroll_offset(self, app, current.as_object())
420 .expect("just set")
421 + self.paint_extent_of(app, current);
422 true
423 };
424
425 while end_scroll_offset < scroll_offset {
427 leading_garbage += 1;
428 if !advance(
429 app,
430 &mut child,
431 &mut index,
432 &mut end_scroll_offset,
433 &mut in_layout_range,
434 &mut trailing_child_with_layout,
435 ) {
436 debug_assert_eq!(leading_garbage, self.child_count(app));
437 debug_assert!(child.is_none());
438 self.collect_garbage(app, leading_garbage - 1, 0);
441 debug_assert_eq!(self.first_child(app), self.last_child(app));
442 let last_child = self.last_child(app).expect("one child is kept");
443 let extent = RenderSliver::child_scroll_offset(self, app, last_child.as_object())
444 .expect("laid out")
445 + self.paint_extent_of(app, last_child);
446 self.set_geometry(
447 app,
448 SliverGeometry::builder()
449 .scroll_extent(extent)
450 .max_paint_extent(extent)
451 .build(),
452 );
453 return;
454 }
455 }
456
457 while end_scroll_offset < target_end_scroll_offset {
459 if !advance(
460 app,
461 &mut child,
462 &mut index,
463 &mut end_scroll_offset,
464 &mut in_layout_range,
465 &mut trailing_child_with_layout,
466 ) {
467 reached_end = true;
468 break;
469 }
470 }
471
472 if let Some(current) = child {
474 let mut walker = self.child_after(app, current);
475 while let Some(current) = walker {
476 trailing_garbage += 1;
477 walker = self.child_after(app, current);
478 }
479 }
480
481 self.collect_garbage(app, leading_garbage, trailing_garbage);
484
485 debug_assert!(self.debug_assert_child_list_is_non_empty_and_contiguous(app));
486 let first_child = self.first_child(app).expect("not empty");
487 let last_child = self.last_child(app).expect("not empty");
488 let leading_scroll_offset =
489 RenderSliver::child_scroll_offset(self, app, first_child.as_object())
490 .expect("laid out");
491 let estimated_max_scroll_offset = if reached_end {
492 end_scroll_offset
493 } else {
494 let estimated = child_manager.estimate_max_scroll_offset(
495 app,
496 constraints,
497 Some(self.index_of(app, first_child)),
498 Some(self.index_of(app, last_child)),
499 Some(leading_scroll_offset),
500 Some(end_scroll_offset),
501 );
502 debug_assert!(estimated >= end_scroll_offset - leading_scroll_offset);
503 estimated
504 };
505 let paint_extent =
506 self.calculate_paint_offset(app, constraints, leading_scroll_offset, end_scroll_offset);
507 let cache_extent =
508 self.calculate_cache_offset(app, constraints, leading_scroll_offset, end_scroll_offset);
509 let target_end_scroll_offset_for_paint =
510 constraints.scroll_offset + constraints.remaining_paint_extent;
511 self.set_geometry(
512 app,
513 SliverGeometry::builder()
514 .scroll_extent(estimated_max_scroll_offset)
515 .paint_extent(paint_extent)
516 .cache_extent(cache_extent)
517 .max_paint_extent(estimated_max_scroll_offset)
518 .has_visual_overflow(
520 end_scroll_offset > target_end_scroll_offset_for_paint
521 || constraints.scroll_offset > 0.0,
522 )
523 .build(),
524 );
525
526 if estimated_max_scroll_offset == end_scroll_offset {
529 child_manager.set_did_underflow(app, true);
530 }
531 child_manager.did_finish_layout(app);
532 }
533}
534
535impl RenderSliver for RenderSliverList {
536 crate::render_sliver_accessors!();
537
538 fn hit_test_children(
539 self: RenderHandle<Self>,
540 app: &mut App,
541 result: &mut SliverHitTestResult<'_>,
542 main_axis_position: f64,
543 cross_axis_position: f64,
544 ) -> bool {
545 RenderSliverMultiBoxAdaptor::hit_test_children(
546 self,
547 app,
548 result,
549 main_axis_position,
550 cross_axis_position,
551 )
552 }
553
554 fn child_main_axis_position(
555 self: RenderHandle<Self>,
556 app: &App,
557 child: AnyRenderObject,
558 ) -> f64 {
559 RenderSliverMultiBoxAdaptor::child_main_axis_position(self, app, child)
560 }
561
562 fn child_scroll_offset(
563 self: RenderHandle<Self>,
564 app: &App,
565 child: AnyRenderObject,
566 ) -> Option<f64> {
567 RenderSliverMultiBoxAdaptor::child_scroll_offset(self, app, child)
568 }
569}
570
571#[cfg(test)]
572mod tests {
573 use waft_embedder::Size;
574 use waft_foundation::AppCell;
575 use waft_gestures::HitTestResult;
576
577 use super::*;
578 use crate::sliver_multi_box_adaptor::test_support::TestChildManager;
579 use crate::sliver_multi_box_adaptor::viewport_test_support::ScrollHarness;
580
581 const VIEWPORT: Size = Size::new(100.0, 200.0);
582
583 fn sliver_list(
584 app: &mut App,
585 extents: Vec<f64>,
586 ) -> (
587 RenderHandle<RenderSliverList>,
588 Rc<TestChildManager<RenderSliverList>>,
589 ScrollHarness,
590 ) {
591 let manager = TestChildManager::new(extents);
592 let list = RenderSliverList::new(app, manager.clone());
593 manager.attach(list);
594 let harness = ScrollHarness::new(app, list.as_sliver(), VIEWPORT);
595 (list, manager, harness)
596 }
597
598 #[test]
601 fn a_sliver_list_stacks_children_of_differing_extents() {
602 let cell = AppCell::new();
603 let mut app = cell.borrow_mut();
604 let (list, _, _harness) = sliver_list(&mut app, vec![40.0, 60.0, 80.0, 100.0, 120.0]);
605
606 let first = list.first_child(&app).expect("children were created");
607 let second = list.child_after(&app, first).expect("two children");
608 let third = list.child_after(&app, second).expect("three children");
609 assert_eq!(
610 RenderSliver::child_scroll_offset(list, &app, first.as_object()),
611 Some(0.0)
612 );
613 assert_eq!(
614 RenderSliver::child_scroll_offset(list, &app, second.as_object()),
615 Some(40.0)
616 );
617 assert_eq!(
618 RenderSliver::child_scroll_offset(list, &app, third.as_object()),
619 Some(100.0)
620 );
621 assert_eq!(list.geometry(&app).paint_extent, 200.0);
622 assert!(list.geometry(&app).has_visual_overflow);
623 }
624
625 #[test]
627 fn a_short_sliver_list_reports_its_exact_extent() {
628 let cell = AppCell::new();
629 let mut app = cell.borrow_mut();
630 let (list, manager, _harness) = sliver_list(&mut app, vec![40.0, 60.0]);
631
632 let geometry = list.geometry(&app);
633 assert_eq!(geometry.scroll_extent, 100.0);
634 assert_eq!(geometry.paint_extent, 100.0);
635 assert!(!geometry.has_visual_overflow);
636 assert_eq!(list.child_count(&app), 2);
637 assert!(manager.did_underflow.get());
638 }
639
640 #[test]
642 fn scrolling_a_sliver_list_replaces_its_children() {
643 let cell = AppCell::new();
644 let mut app = cell.borrow_mut();
645 let (list, _, harness) = sliver_list(&mut app, vec![100.0; 30]);
646 assert_eq!(
647 list.index_of(&app, list.first_child(&app).expect("built")),
648 0
649 );
650
651 harness.scroll_to(&mut app, 1500.0);
652
653 let first = list.first_child(&app).expect("children were created");
654 assert_eq!(list.index_of(&app, first), 12);
656 assert!(list.geometry(&app).has_visual_overflow);
657 }
658
659 #[test]
661 fn a_sliver_list_hit_tests_its_children() {
662 let cell = AppCell::new();
663 let mut app = cell.borrow_mut();
664 let (list, _, _harness) = sliver_list(&mut app, vec![50.0; 20]);
665
666 let mut result = HitTestResult::new();
667 let hit = list.hit_test(
668 &mut app,
669 &mut SliverHitTestResult::wrap(&mut result),
670 120.0,
671 10.0,
672 );
673
674 assert!(hit);
675 assert_eq!(result.path().len(), 2);
676 }
677
678 #[test]
680 fn a_sliver_list_keeps_a_child_alive() {
681 let cell = AppCell::new();
682 let mut app = cell.borrow_mut();
683 let (list, _, harness) = sliver_list(&mut app, vec![100.0; 30]);
684 let first = list.first_child(&app).expect("children were created");
685 first
686 .as_object()
687 .parent_data_of_mut::<SliverMultiBoxAdaptorParentData>(&mut app)
688 .set_keep_alive(true);
689
690 harness.scroll_to(&mut app, 2000.0);
691
692 assert_eq!(list.keep_alive_children(&app), vec![first]);
693 assert!(first.as_object().attached(&app));
694 }
695}