1use std::any::{Any, TypeId};
4use std::fmt;
5
6use waft_embedder::{Offset, Size, TextBaseline};
7use waft_foundation::{App, DiagnosticPropertiesBuilder, EnumProperty, Handle};
8use waft_painting::{Axis, AxisDirection, axis_direction_to_axis};
9
10use crate::box_::{
11 AnyRenderBox, BaselineOffset, BoxConstraints, BoxHitTestResult, BoxParentData,
12 ContainerBoxParentData, RenderBox, RenderBoxContainerDefaultsMixin, RenderBoxData,
13};
14use crate::object::{
15 AnyRenderObject, ContainerParentData, ContainerParentDataMixin, ContainerRenderObjectData,
16 ContainerRenderObjectMixin, ParentData, RenderHandle, RenderObject, RenderObjectData,
17};
18use crate::painting_context::PaintingContext;
19use crate::pipeline_owner::PipelineOwner;
20
21#[derive(Debug, Default)]
23pub struct ListBodyParentData {
24 box_parent_data: BoxParentData,
25 container_parent_data: ContainerParentData<AnyRenderBox>,
26}
27
28impl ListBodyParentData {
29 pub const fn new() -> ListBodyParentData {
31 ListBodyParentData {
32 box_parent_data: BoxParentData::new(),
33 container_parent_data: ContainerParentData::new(),
34 }
35 }
36}
37
38impl ParentData for ListBodyParentData {
39 fn detach(&mut self) {
40 ContainerParentDataMixin::detach(self);
41 }
42
43 fn provide(&self, id: TypeId) -> Option<&dyn Any> {
44 if id == TypeId::of::<ListBodyParentData>() {
45 return Some(self);
46 }
47 self.box_parent_data.provide(id)
48 }
49
50 fn provide_mut(&mut self, id: TypeId) -> Option<&mut dyn Any> {
51 if id == TypeId::of::<ListBodyParentData>() {
52 return Some(self);
53 }
54 self.box_parent_data.provide_mut(id)
55 }
56}
57
58impl ContainerParentDataMixin for ListBodyParentData {
59 type ChildType = AnyRenderBox;
60
61 fn container_parent_data(&self) -> &ContainerParentData<AnyRenderBox> {
62 &self.container_parent_data
63 }
64
65 fn container_parent_data_mut(&mut self) -> &mut ContainerParentData<AnyRenderBox> {
66 &mut self.container_parent_data
67 }
68}
69
70impl ContainerBoxParentData for ListBodyParentData {
71 fn box_parent_data(&self) -> &BoxParentData {
72 &self.box_parent_data
73 }
74
75 fn box_parent_data_mut(&mut self) -> &mut BoxParentData {
76 &mut self.box_parent_data
77 }
78}
79
80impl fmt::Display for ListBodyParentData {
81 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82 fmt::Display::fmt(&self.box_parent_data, f)
83 }
84}
85
86pub struct RenderListBodyData {
91 axis_direction: AxisDirection,
92}
93
94impl RenderListBodyData {
95 pub fn new() -> RenderListBodyData {
99 RenderListBodyData {
100 axis_direction: AxisDirection::Down,
101 }
102 }
103
104 pub fn axis_direction(mut self, axis_direction: AxisDirection) -> RenderListBodyData {
106 self.axis_direction = axis_direction;
107 self
108 }
109}
110
111impl Default for RenderListBodyData {
112 fn default() -> RenderListBodyData {
113 RenderListBodyData::new()
114 }
115}
116pub trait RenderListBodyMixin:
125 RenderBox
126 + ContainerRenderObjectMixin<ChildType = AnyRenderBox, ParentDataType = ListBodyParentData>
127 + RenderBoxContainerDefaultsMixin
128{
129 fn list_body_data(self: RenderHandle<Self>, app: &App) -> &RenderListBodyData;
131
132 fn list_body_data_mut(self: RenderHandle<Self>, app: &mut App) -> &mut RenderListBodyData;
134
135 fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
137 if !child.parent_data_is::<ListBodyParentData>(app) {
138 child.set_parent_data(app, ListBodyParentData::new());
139 }
140 }
141
142 fn debug_fill_properties(
145 self: RenderHandle<Self>,
146 app: &App,
147 properties: &mut DiagnosticPropertiesBuilder,
148 ) {
149 properties.add(EnumProperty::<AxisDirection>::new(
150 "axisDirection",
151 self.list_body_data(app).axis_direction,
152 ));
153 }
154
155 fn axis_direction(self: RenderHandle<Self>, app: &App) -> AxisDirection {
160 self.list_body_data(app).axis_direction
161 }
162
163 fn set_axis_direction(self: RenderHandle<Self>, app: &mut App, value: AxisDirection) {
165 if self.list_body_data(app).axis_direction == value {
166 return;
167 }
168 self.list_body_data_mut(app).axis_direction = value;
169 self.mark_needs_layout(app);
170 }
171
172 fn main_axis(self: RenderHandle<Self>, app: &App) -> Axis {
175 axis_direction_to_axis(self.axis_direction(app))
176 }
177
178 fn compute_dry_baseline(
180 self: RenderHandle<Self>,
181 app: &mut App,
182 constraints: BoxConstraints,
183 baseline: TextBaseline,
184 ) -> Option<f64> {
185 debug_assert!(self.debug_check_constraints(app, constraints));
186 let mut child;
187 let next_child: fn(RenderHandle<Self>, &App, AnyRenderBox) -> Option<AnyRenderBox>;
188 match self.axis_direction(app) {
189 AxisDirection::Right | AxisDirection::Left => {
190 let child_constraints =
191 BoxConstraints::tight_for(None, Some(constraints.max_height));
192 let mut baseline_offset = BaselineOffset::NO_BASELINE;
193 child = self.first_child(app);
194 while let Some(current) = child {
195 baseline_offset = baseline_offset.min_of(BaselineOffset(
196 current.get_dry_baseline(app, child_constraints, baseline),
197 ));
198 child = self.child_after(app, current);
199 }
200 return baseline_offset.offset();
201 }
202 AxisDirection::Up => {
203 child = self.last_child(app);
204 next_child = |this, app, child| this.child_before(app, child);
205 }
206 AxisDirection::Down => {
207 child = self.first_child(app);
208 next_child = |this, app, child| this.child_after(app, child);
209 }
210 }
211 let child_constraints = BoxConstraints::tight_for(Some(constraints.max_width), None);
212 let mut main_axis_extent = 0.0;
213 while let Some(current) = child {
214 let child_baseline = current.get_dry_baseline(app, child_constraints, baseline);
215 if let Some(child_baseline) = child_baseline {
216 return Some(child_baseline + main_axis_extent);
217 }
218 main_axis_extent += current.get_dry_layout(app, child_constraints).height();
219 child = next_child(self, app, current);
220 }
221 None
222 }
223
224 fn compute_dry_layout(
226 self: RenderHandle<Self>,
227 app: &mut App,
228 constraints: BoxConstraints,
229 ) -> Size {
230 debug_assert!(self.debug_check_constraints(app, constraints));
231 let mut main_axis_extent = 0.0;
232 let mut child = self.first_child(app);
233 match self.axis_direction(app) {
234 AxisDirection::Right | AxisDirection::Left => {
235 let inner_constraints =
236 BoxConstraints::tight_for(None, Some(constraints.max_height));
237 while let Some(current) = child {
238 let child_size = current.get_dry_layout(app, inner_constraints);
239 main_axis_extent += child_size.width();
240 child = self.child_after(app, current);
241 }
242 constraints.constrain(Size::new(main_axis_extent, constraints.max_height))
243 }
244 AxisDirection::Up | AxisDirection::Down => {
245 let inner_constraints =
246 BoxConstraints::tight_for(Some(constraints.max_width), None);
247 while let Some(current) = child {
248 let child_size = current.get_dry_layout(app, inner_constraints);
249 main_axis_extent += child_size.height();
250 child = self.child_after(app, current);
251 }
252 constraints.constrain(Size::new(constraints.max_width, main_axis_extent))
253 }
254 }
255 }
256
257 fn debug_check_constraints(
260 self: RenderHandle<Self>,
261 app: &App,
262 constraints: BoxConstraints,
263 ) -> bool {
264 if cfg!(debug_assertions) {
265 let unbounded_main_axis = match self.main_axis(app) {
266 Axis::Horizontal => !constraints.has_bounded_width(),
267 Axis::Vertical => !constraints.has_bounded_height(),
268 };
269 assert!(
270 unbounded_main_axis,
271 "RenderListBody must have unlimited space along its main axis.\n\
272 RenderListBody does not clip or resize its children, so it must be placed in a \
273 parent that does not constrain the main axis.\n\
274 You probably want to put the RenderListBody inside a RenderViewport with a \
275 matching main axis."
276 );
277 let bounded_cross_axis = match self.main_axis(app) {
278 Axis::Horizontal => constraints.has_bounded_height(),
279 Axis::Vertical => constraints.has_bounded_width(),
280 };
281 assert!(
282 bounded_cross_axis,
283 "RenderListBody must have a bounded constraint for its cross axis.\n\
284 RenderListBody forces its children to expand to fit the RenderListBody's \
285 container, so it must be placed in a parent that constrains the cross axis to a \
286 finite dimension.\n\
287 If you are attempting to nest a RenderListBody with one direction inside one of \
288 another direction, you will want to wrap the inner one inside a box that fixes \
289 the dimension in that direction, for example, a RenderIntrinsicWidth or \
290 RenderIntrinsicHeight object. This is relatively expensive, however."
291 );
292 }
293 true
294 }
295
296 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
298 let constraints = self.constraints(app);
299 debug_assert!(self.debug_check_constraints(app, constraints));
300 let mut main_axis_extent = 0.0;
301 let mut child = self.first_child(app);
302 match self.axis_direction(app) {
303 AxisDirection::Right => {
304 let inner_constraints =
305 BoxConstraints::tight_for(None, Some(constraints.max_height));
306 while let Some(current) = child {
307 current.layout(app, inner_constraints, true);
308 let child_parent_data = current
309 .as_object()
310 .parent_data_of_mut::<ListBodyParentData>(app);
311 child_parent_data.set_offset(Offset::new(main_axis_extent, 0.0));
312 let next_sibling = child_parent_data.next_sibling();
313 main_axis_extent += current.size(app).width();
314 child = next_sibling;
315 }
316 self.set_size(
317 app,
318 constraints.constrain(Size::new(main_axis_extent, constraints.max_height)),
319 );
320 }
321 AxisDirection::Left => {
322 let inner_constraints =
323 BoxConstraints::tight_for(None, Some(constraints.max_height));
324 while let Some(current) = child {
325 current.layout(app, inner_constraints, true);
326 main_axis_extent += current.size(app).width();
327 child = self.child_after(app, current);
328 }
329 let mut position = 0.0;
330 child = self.first_child(app);
331 while let Some(current) = child {
332 position += current.size(app).width();
333 let child_parent_data = current
334 .as_object()
335 .parent_data_of_mut::<ListBodyParentData>(app);
336 child_parent_data.set_offset(Offset::new(main_axis_extent - position, 0.0));
337 child = child_parent_data.next_sibling();
338 }
339 self.set_size(
340 app,
341 constraints.constrain(Size::new(main_axis_extent, constraints.max_height)),
342 );
343 }
344 AxisDirection::Down => {
345 let inner_constraints =
346 BoxConstraints::tight_for(Some(constraints.max_width), None);
347 while let Some(current) = child {
348 current.layout(app, inner_constraints, true);
349 let child_parent_data = current
350 .as_object()
351 .parent_data_of_mut::<ListBodyParentData>(app);
352 child_parent_data.set_offset(Offset::new(0.0, main_axis_extent));
353 let next_sibling = child_parent_data.next_sibling();
354 main_axis_extent += current.size(app).height();
355 child = next_sibling;
356 }
357 self.set_size(
358 app,
359 constraints.constrain(Size::new(constraints.max_width, main_axis_extent)),
360 );
361 }
362 AxisDirection::Up => {
363 let inner_constraints =
364 BoxConstraints::tight_for(Some(constraints.max_width), None);
365 while let Some(current) = child {
366 current.layout(app, inner_constraints, true);
367 main_axis_extent += current.size(app).height();
368 child = self.child_after(app, current);
369 }
370 let mut position = 0.0;
371 child = self.first_child(app);
372 while let Some(current) = child {
373 position += current.size(app).height();
374 let child_parent_data = current
375 .as_object()
376 .parent_data_of_mut::<ListBodyParentData>(app);
377 child_parent_data.set_offset(Offset::new(0.0, main_axis_extent - position));
378 child = child_parent_data.next_sibling();
379 }
380 self.set_size(
381 app,
382 constraints.constrain(Size::new(constraints.max_width, main_axis_extent)),
383 );
384 }
385 }
386 debug_assert!(self.size(app).is_finite());
387 }
388
389 fn get_intrinsic_cross_axis(
392 self: RenderHandle<Self>,
393 app: &mut App,
394 child_size: impl Fn(&mut App, AnyRenderBox) -> f64,
395 ) -> f64 {
396 let mut extent: f64 = 0.0;
397 let mut child = self.first_child(app);
398 while let Some(current) = child {
399 extent = extent.max(child_size(app, current));
400 child = self.child_after(app, current);
401 }
402 extent
403 }
404
405 fn get_intrinsic_main_axis(
408 self: RenderHandle<Self>,
409 app: &mut App,
410 child_size: impl Fn(&mut App, AnyRenderBox) -> f64,
411 ) -> f64 {
412 let mut extent = 0.0;
413 let mut child = self.first_child(app);
414 while let Some(current) = child {
415 extent += child_size(app, current);
416 child = self.child_after(app, current);
417 }
418 extent
419 }
420
421 fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
423 let child_size =
424 |app: &mut App, child: AnyRenderBox| child.get_min_intrinsic_width(app, height);
425 match self.main_axis(app) {
426 Axis::Horizontal => self.get_intrinsic_main_axis(app, child_size),
427 Axis::Vertical => self.get_intrinsic_cross_axis(app, child_size),
428 }
429 }
430
431 fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
433 let child_size =
434 |app: &mut App, child: AnyRenderBox| child.get_max_intrinsic_width(app, height);
435 match self.main_axis(app) {
436 Axis::Horizontal => self.get_intrinsic_main_axis(app, child_size),
437 Axis::Vertical => self.get_intrinsic_cross_axis(app, child_size),
438 }
439 }
440
441 fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
443 let child_size =
444 |app: &mut App, child: AnyRenderBox| child.get_min_intrinsic_height(app, width);
445 match self.main_axis(app) {
446 Axis::Horizontal => self.get_intrinsic_main_axis(app, child_size),
447 Axis::Vertical => self.get_intrinsic_cross_axis(app, child_size),
448 }
449 }
450
451 fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
453 let child_size =
454 |app: &mut App, child: AnyRenderBox| child.get_max_intrinsic_height(app, width);
455 match self.main_axis(app) {
456 Axis::Horizontal => self.get_intrinsic_main_axis(app, child_size),
457 Axis::Vertical => self.get_intrinsic_cross_axis(app, child_size),
458 }
459 }
460
461 fn compute_distance_to_actual_baseline(
463 self: RenderHandle<Self>,
464 app: &mut App,
465 baseline: TextBaseline,
466 ) -> Option<f64> {
467 self.default_compute_distance_to_first_actual_baseline(app, baseline)
468 }
469
470 fn paint(
472 self: RenderHandle<Self>,
473 app: &mut App,
474 context: &mut PaintingContext,
475 offset: Offset,
476 ) {
477 self.default_paint(app, context, offset);
478 }
479
480 fn hit_test_children(
482 self: RenderHandle<Self>,
483 app: &mut App,
484 result: &mut BoxHitTestResult<'_>,
485 position: Offset,
486 ) -> bool {
487 self.default_hit_test_children(app, result, position)
488 }
489}
490
491pub struct RenderListBody {
501 render_object: RenderObjectData,
502 render_box: RenderBoxData,
503 container: ContainerRenderObjectData<AnyRenderBox>,
504 list_body: RenderListBodyData,
505}
506
507impl RenderListBody {
508 pub fn new(app: &mut App, axis_direction: AxisDirection) -> RenderHandle<RenderListBody> {
515 RenderHandle::new_box(
516 app,
517 RenderListBody {
518 render_object: RenderObjectData::new(),
519 render_box: RenderBoxData::new(),
520 container: ContainerRenderObjectData::new(),
521 list_body: RenderListBodyData::new().axis_direction(axis_direction),
522 },
523 )
524 }
525
526 pub fn axis_direction(self: RenderHandle<Self>, app: &App) -> AxisDirection {
528 RenderListBodyMixin::axis_direction(self, app)
529 }
530
531 pub fn set_axis_direction(self: RenderHandle<Self>, app: &mut App, value: AxisDirection) {
533 RenderListBodyMixin::set_axis_direction(self, app, value)
534 }
535
536 pub fn main_axis(self: RenderHandle<Self>, app: &App) -> Axis {
538 RenderListBodyMixin::main_axis(self, app)
539 }
540}
541
542impl ContainerRenderObjectMixin for RenderListBody {
543 type ChildType = AnyRenderBox;
544 type ParentDataType = ListBodyParentData;
545
546 fn container_data(
547 self: RenderHandle<Self>,
548 app: &App,
549 ) -> &ContainerRenderObjectData<AnyRenderBox> {
550 &self.get(app).container
551 }
552
553 fn container_data_mut(
554 self: RenderHandle<Self>,
555 app: &mut App,
556 ) -> &mut ContainerRenderObjectData<AnyRenderBox> {
557 &mut self.get_mut(app).container
558 }
559}
560
561impl RenderBoxContainerDefaultsMixin for RenderListBody {}
562
563impl RenderListBodyMixin for RenderListBody {
564 fn list_body_data(self: RenderHandle<Self>, app: &App) -> &RenderListBodyData {
565 &self.get(app).list_body
566 }
567
568 fn list_body_data_mut(self: RenderHandle<Self>, app: &mut App) -> &mut RenderListBodyData {
569 &mut self.get_mut(app).list_body
570 }
571}
572
573impl RenderObject for RenderListBody {
574 crate::render_object_accessors!();
575
576 fn debug_fill_properties(
577 self: RenderHandle<Self>,
578 app: &App,
579 properties: &mut DiagnosticPropertiesBuilder,
580 ) {
581 RenderListBodyMixin::debug_fill_properties(self, app, properties);
582 }
583
584 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
585 RenderListBodyMixin::perform_layout(self, app)
586 }
587
588 fn visit_children(
589 self: RenderHandle<Self>,
590 app: &App,
591 visitor: &mut dyn FnMut(AnyRenderObject),
592 ) {
593 ContainerRenderObjectMixin::visit_children(self, app, visitor)
594 }
595
596 fn did_attach(self: RenderHandle<Self>, app: &mut App, owner: Handle<PipelineOwner>) {
597 ContainerRenderObjectMixin::did_attach(self, app, owner)
598 }
599
600 fn did_detach(self: RenderHandle<Self>, app: &mut App) {
601 ContainerRenderObjectMixin::did_detach(self, app)
602 }
603
604 fn redepth_children(self: RenderHandle<Self>, app: &mut App) {
605 ContainerRenderObjectMixin::redepth_children(self, app)
606 }
607
608 fn paint(
609 self: RenderHandle<Self>,
610 app: &mut App,
611 context: &mut PaintingContext,
612 offset: Offset,
613 ) {
614 RenderListBodyMixin::paint(self, app, context, offset)
615 }
616}
617
618impl RenderBox for RenderListBody {
619 crate::render_box_accessors!();
620
621 fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
622 RenderListBodyMixin::setup_parent_data(self, app, child)
623 }
624
625 fn compute_dry_baseline(
626 self: RenderHandle<Self>,
627 app: &mut App,
628 constraints: BoxConstraints,
629 baseline: TextBaseline,
630 ) -> Option<f64> {
631 RenderListBodyMixin::compute_dry_baseline(self, app, constraints, baseline)
632 }
633
634 fn compute_dry_layout(
635 self: RenderHandle<Self>,
636 app: &mut App,
637 constraints: BoxConstraints,
638 ) -> Size {
639 RenderListBodyMixin::compute_dry_layout(self, app, constraints)
640 }
641
642 fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
643 RenderListBodyMixin::compute_min_intrinsic_width(self, app, height)
644 }
645
646 fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
647 RenderListBodyMixin::compute_max_intrinsic_width(self, app, height)
648 }
649
650 fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
651 RenderListBodyMixin::compute_min_intrinsic_height(self, app, width)
652 }
653
654 fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
655 RenderListBodyMixin::compute_max_intrinsic_height(self, app, width)
656 }
657
658 fn compute_distance_to_actual_baseline(
659 self: RenderHandle<Self>,
660 app: &mut App,
661 baseline: TextBaseline,
662 ) -> Option<f64> {
663 RenderListBodyMixin::compute_distance_to_actual_baseline(self, app, baseline)
664 }
665
666 fn hit_test_children(
667 self: RenderHandle<Self>,
668 app: &mut App,
669 result: &mut BoxHitTestResult<'_>,
670 position: Offset,
671 ) -> bool {
672 RenderListBodyMixin::hit_test_children(self, app, result, position)
673 }
674}