1use std::any::{Any, TypeId};
4use std::fmt;
5
6use waft_embedder::{Offset, Size, TextBaseline};
7use waft_foundation::{App, 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 axis_direction(self: RenderHandle<Self>, app: &App) -> AxisDirection {
147 self.list_body_data(app).axis_direction
148 }
149
150 fn set_axis_direction(self: RenderHandle<Self>, app: &mut App, value: AxisDirection) {
152 if self.list_body_data(app).axis_direction == value {
153 return;
154 }
155 self.list_body_data_mut(app).axis_direction = value;
156 self.mark_needs_layout(app);
157 }
158
159 fn main_axis(self: RenderHandle<Self>, app: &App) -> Axis {
162 axis_direction_to_axis(self.axis_direction(app))
163 }
164
165 fn compute_dry_baseline(
167 self: RenderHandle<Self>,
168 app: &mut App,
169 constraints: BoxConstraints,
170 baseline: TextBaseline,
171 ) -> Option<f64> {
172 debug_assert!(self.debug_check_constraints(app, constraints));
173 let mut child;
174 let next_child: fn(RenderHandle<Self>, &App, AnyRenderBox) -> Option<AnyRenderBox>;
175 match self.axis_direction(app) {
176 AxisDirection::Right | AxisDirection::Left => {
177 let child_constraints =
178 BoxConstraints::tight_for(None, Some(constraints.max_height));
179 let mut baseline_offset = BaselineOffset::NO_BASELINE;
180 child = self.first_child(app);
181 while let Some(current) = child {
182 baseline_offset = baseline_offset.min_of(BaselineOffset(
183 current.get_dry_baseline(app, child_constraints, baseline),
184 ));
185 child = self.child_after(app, current);
186 }
187 return baseline_offset.offset();
188 }
189 AxisDirection::Up => {
190 child = self.last_child(app);
191 next_child = |this, app, child| this.child_before(app, child);
192 }
193 AxisDirection::Down => {
194 child = self.first_child(app);
195 next_child = |this, app, child| this.child_after(app, child);
196 }
197 }
198 let child_constraints = BoxConstraints::tight_for(Some(constraints.max_width), None);
199 let mut main_axis_extent = 0.0;
200 while let Some(current) = child {
201 let child_baseline = current.get_dry_baseline(app, child_constraints, baseline);
202 if let Some(child_baseline) = child_baseline {
203 return Some(child_baseline + main_axis_extent);
204 }
205 main_axis_extent += current.get_dry_layout(app, child_constraints).height();
206 child = next_child(self, app, current);
207 }
208 None
209 }
210
211 fn compute_dry_layout(
213 self: RenderHandle<Self>,
214 app: &mut App,
215 constraints: BoxConstraints,
216 ) -> Size {
217 debug_assert!(self.debug_check_constraints(app, constraints));
218 let mut main_axis_extent = 0.0;
219 let mut child = self.first_child(app);
220 match self.axis_direction(app) {
221 AxisDirection::Right | AxisDirection::Left => {
222 let inner_constraints =
223 BoxConstraints::tight_for(None, Some(constraints.max_height));
224 while let Some(current) = child {
225 let child_size = current.get_dry_layout(app, inner_constraints);
226 main_axis_extent += child_size.width();
227 child = self.child_after(app, current);
228 }
229 constraints.constrain(Size::new(main_axis_extent, constraints.max_height))
230 }
231 AxisDirection::Up | AxisDirection::Down => {
232 let inner_constraints =
233 BoxConstraints::tight_for(Some(constraints.max_width), None);
234 while let Some(current) = child {
235 let child_size = current.get_dry_layout(app, inner_constraints);
236 main_axis_extent += child_size.height();
237 child = self.child_after(app, current);
238 }
239 constraints.constrain(Size::new(constraints.max_width, main_axis_extent))
240 }
241 }
242 }
243
244 fn debug_check_constraints(
247 self: RenderHandle<Self>,
248 app: &App,
249 constraints: BoxConstraints,
250 ) -> bool {
251 if cfg!(debug_assertions) {
252 let unbounded_main_axis = match self.main_axis(app) {
253 Axis::Horizontal => !constraints.has_bounded_width(),
254 Axis::Vertical => !constraints.has_bounded_height(),
255 };
256 assert!(
257 unbounded_main_axis,
258 "RenderListBody must have unlimited space along its main axis.\n\
259 RenderListBody does not clip or resize its children, so it must be placed in a \
260 parent that does not constrain the main axis.\n\
261 You probably want to put the RenderListBody inside a RenderViewport with a \
262 matching main axis."
263 );
264 let bounded_cross_axis = match self.main_axis(app) {
265 Axis::Horizontal => constraints.has_bounded_height(),
266 Axis::Vertical => constraints.has_bounded_width(),
267 };
268 assert!(
269 bounded_cross_axis,
270 "RenderListBody must have a bounded constraint for its cross axis.\n\
271 RenderListBody forces its children to expand to fit the RenderListBody's \
272 container, so it must be placed in a parent that constrains the cross axis to a \
273 finite dimension.\n\
274 If you are attempting to nest a RenderListBody with one direction inside one of \
275 another direction, you will want to wrap the inner one inside a box that fixes \
276 the dimension in that direction, for example, a RenderIntrinsicWidth or \
277 RenderIntrinsicHeight object. This is relatively expensive, however."
278 );
279 }
280 true
281 }
282
283 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
285 let constraints = self.constraints(app);
286 debug_assert!(self.debug_check_constraints(app, constraints));
287 let mut main_axis_extent = 0.0;
288 let mut child = self.first_child(app);
289 match self.axis_direction(app) {
290 AxisDirection::Right => {
291 let inner_constraints =
292 BoxConstraints::tight_for(None, Some(constraints.max_height));
293 while let Some(current) = child {
294 current.layout(app, inner_constraints, true);
295 let child_parent_data = current
296 .as_object()
297 .parent_data_of_mut::<ListBodyParentData>(app);
298 child_parent_data.set_offset(Offset::new(main_axis_extent, 0.0));
299 let next_sibling = child_parent_data.next_sibling();
300 main_axis_extent += current.size(app).width();
301 child = next_sibling;
302 }
303 self.set_size(
304 app,
305 constraints.constrain(Size::new(main_axis_extent, constraints.max_height)),
306 );
307 }
308 AxisDirection::Left => {
309 let inner_constraints =
310 BoxConstraints::tight_for(None, Some(constraints.max_height));
311 while let Some(current) = child {
312 current.layout(app, inner_constraints, true);
313 main_axis_extent += current.size(app).width();
314 child = self.child_after(app, current);
315 }
316 let mut position = 0.0;
317 child = self.first_child(app);
318 while let Some(current) = child {
319 position += current.size(app).width();
320 let child_parent_data = current
321 .as_object()
322 .parent_data_of_mut::<ListBodyParentData>(app);
323 child_parent_data.set_offset(Offset::new(main_axis_extent - position, 0.0));
324 child = child_parent_data.next_sibling();
325 }
326 self.set_size(
327 app,
328 constraints.constrain(Size::new(main_axis_extent, constraints.max_height)),
329 );
330 }
331 AxisDirection::Down => {
332 let inner_constraints =
333 BoxConstraints::tight_for(Some(constraints.max_width), None);
334 while let Some(current) = child {
335 current.layout(app, inner_constraints, true);
336 let child_parent_data = current
337 .as_object()
338 .parent_data_of_mut::<ListBodyParentData>(app);
339 child_parent_data.set_offset(Offset::new(0.0, main_axis_extent));
340 let next_sibling = child_parent_data.next_sibling();
341 main_axis_extent += current.size(app).height();
342 child = next_sibling;
343 }
344 self.set_size(
345 app,
346 constraints.constrain(Size::new(constraints.max_width, main_axis_extent)),
347 );
348 }
349 AxisDirection::Up => {
350 let inner_constraints =
351 BoxConstraints::tight_for(Some(constraints.max_width), None);
352 while let Some(current) = child {
353 current.layout(app, inner_constraints, true);
354 main_axis_extent += current.size(app).height();
355 child = self.child_after(app, current);
356 }
357 let mut position = 0.0;
358 child = self.first_child(app);
359 while let Some(current) = child {
360 position += current.size(app).height();
361 let child_parent_data = current
362 .as_object()
363 .parent_data_of_mut::<ListBodyParentData>(app);
364 child_parent_data.set_offset(Offset::new(0.0, main_axis_extent - position));
365 child = child_parent_data.next_sibling();
366 }
367 self.set_size(
368 app,
369 constraints.constrain(Size::new(constraints.max_width, main_axis_extent)),
370 );
371 }
372 }
373 debug_assert!(self.size(app).is_finite());
374 }
375
376 fn get_intrinsic_cross_axis(
379 self: RenderHandle<Self>,
380 app: &mut App,
381 child_size: impl Fn(&mut App, AnyRenderBox) -> f64,
382 ) -> f64 {
383 let mut extent: f64 = 0.0;
384 let mut child = self.first_child(app);
385 while let Some(current) = child {
386 extent = extent.max(child_size(app, current));
387 child = self.child_after(app, current);
388 }
389 extent
390 }
391
392 fn get_intrinsic_main_axis(
395 self: RenderHandle<Self>,
396 app: &mut App,
397 child_size: impl Fn(&mut App, AnyRenderBox) -> f64,
398 ) -> f64 {
399 let mut extent = 0.0;
400 let mut child = self.first_child(app);
401 while let Some(current) = child {
402 extent += child_size(app, current);
403 child = self.child_after(app, current);
404 }
405 extent
406 }
407
408 fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
410 let child_size =
411 |app: &mut App, child: AnyRenderBox| child.get_min_intrinsic_width(app, height);
412 match self.main_axis(app) {
413 Axis::Horizontal => self.get_intrinsic_main_axis(app, child_size),
414 Axis::Vertical => self.get_intrinsic_cross_axis(app, child_size),
415 }
416 }
417
418 fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
420 let child_size =
421 |app: &mut App, child: AnyRenderBox| child.get_max_intrinsic_width(app, height);
422 match self.main_axis(app) {
423 Axis::Horizontal => self.get_intrinsic_main_axis(app, child_size),
424 Axis::Vertical => self.get_intrinsic_cross_axis(app, child_size),
425 }
426 }
427
428 fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
430 let child_size =
431 |app: &mut App, child: AnyRenderBox| child.get_min_intrinsic_height(app, width);
432 match self.main_axis(app) {
433 Axis::Horizontal => self.get_intrinsic_main_axis(app, child_size),
434 Axis::Vertical => self.get_intrinsic_cross_axis(app, child_size),
435 }
436 }
437
438 fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
440 let child_size =
441 |app: &mut App, child: AnyRenderBox| child.get_max_intrinsic_height(app, width);
442 match self.main_axis(app) {
443 Axis::Horizontal => self.get_intrinsic_main_axis(app, child_size),
444 Axis::Vertical => self.get_intrinsic_cross_axis(app, child_size),
445 }
446 }
447
448 fn compute_distance_to_actual_baseline(
450 self: RenderHandle<Self>,
451 app: &mut App,
452 baseline: TextBaseline,
453 ) -> Option<f64> {
454 self.default_compute_distance_to_first_actual_baseline(app, baseline)
455 }
456
457 fn paint(
459 self: RenderHandle<Self>,
460 app: &mut App,
461 context: &mut PaintingContext,
462 offset: Offset,
463 ) {
464 self.default_paint(app, context, offset);
465 }
466
467 fn hit_test_children(
469 self: RenderHandle<Self>,
470 app: &mut App,
471 result: &mut BoxHitTestResult<'_>,
472 position: Offset,
473 ) -> bool {
474 self.default_hit_test_children(app, result, position)
475 }
476}
477
478pub struct RenderListBody {
488 render_object: RenderObjectData,
489 render_box: RenderBoxData,
490 container: ContainerRenderObjectData<AnyRenderBox>,
491 list_body: RenderListBodyData,
492}
493
494impl RenderListBody {
495 pub fn new(app: &mut App, axis_direction: AxisDirection) -> RenderHandle<RenderListBody> {
502 RenderHandle::new_box(
503 app,
504 RenderListBody {
505 render_object: RenderObjectData::new(),
506 render_box: RenderBoxData::new(),
507 container: ContainerRenderObjectData::new(),
508 list_body: RenderListBodyData::new().axis_direction(axis_direction),
509 },
510 )
511 }
512
513 pub fn axis_direction(self: RenderHandle<Self>, app: &App) -> AxisDirection {
515 RenderListBodyMixin::axis_direction(self, app)
516 }
517
518 pub fn set_axis_direction(self: RenderHandle<Self>, app: &mut App, value: AxisDirection) {
520 RenderListBodyMixin::set_axis_direction(self, app, value)
521 }
522
523 pub fn main_axis(self: RenderHandle<Self>, app: &App) -> Axis {
525 RenderListBodyMixin::main_axis(self, app)
526 }
527}
528
529impl ContainerRenderObjectMixin for RenderListBody {
530 type ChildType = AnyRenderBox;
531 type ParentDataType = ListBodyParentData;
532
533 fn container_data(
534 self: RenderHandle<Self>,
535 app: &App,
536 ) -> &ContainerRenderObjectData<AnyRenderBox> {
537 &self.get(app).container
538 }
539
540 fn container_data_mut(
541 self: RenderHandle<Self>,
542 app: &mut App,
543 ) -> &mut ContainerRenderObjectData<AnyRenderBox> {
544 &mut self.get_mut(app).container
545 }
546}
547
548impl RenderBoxContainerDefaultsMixin for RenderListBody {}
549
550impl RenderListBodyMixin for RenderListBody {
551 fn list_body_data(self: RenderHandle<Self>, app: &App) -> &RenderListBodyData {
552 &self.get(app).list_body
553 }
554
555 fn list_body_data_mut(self: RenderHandle<Self>, app: &mut App) -> &mut RenderListBodyData {
556 &mut self.get_mut(app).list_body
557 }
558}
559
560impl RenderObject for RenderListBody {
561 crate::render_object_accessors!();
562
563 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
564 RenderListBodyMixin::perform_layout(self, app)
565 }
566
567 fn visit_children(
568 self: RenderHandle<Self>,
569 app: &App,
570 visitor: &mut dyn FnMut(AnyRenderObject),
571 ) {
572 ContainerRenderObjectMixin::visit_children(self, app, visitor)
573 }
574
575 fn did_attach(self: RenderHandle<Self>, app: &mut App, owner: Handle<PipelineOwner>) {
576 ContainerRenderObjectMixin::did_attach(self, app, owner)
577 }
578
579 fn did_detach(self: RenderHandle<Self>, app: &mut App) {
580 ContainerRenderObjectMixin::did_detach(self, app)
581 }
582
583 fn redepth_children(self: RenderHandle<Self>, app: &mut App) {
584 ContainerRenderObjectMixin::redepth_children(self, app)
585 }
586
587 fn paint(
588 self: RenderHandle<Self>,
589 app: &mut App,
590 context: &mut PaintingContext,
591 offset: Offset,
592 ) {
593 RenderListBodyMixin::paint(self, app, context, offset)
594 }
595}
596
597impl RenderBox for RenderListBody {
598 crate::render_box_accessors!();
599
600 fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
601 RenderListBodyMixin::setup_parent_data(self, app, child)
602 }
603
604 fn compute_dry_baseline(
605 self: RenderHandle<Self>,
606 app: &mut App,
607 constraints: BoxConstraints,
608 baseline: TextBaseline,
609 ) -> Option<f64> {
610 RenderListBodyMixin::compute_dry_baseline(self, app, constraints, baseline)
611 }
612
613 fn compute_dry_layout(
614 self: RenderHandle<Self>,
615 app: &mut App,
616 constraints: BoxConstraints,
617 ) -> Size {
618 RenderListBodyMixin::compute_dry_layout(self, app, constraints)
619 }
620
621 fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
622 RenderListBodyMixin::compute_min_intrinsic_width(self, app, height)
623 }
624
625 fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
626 RenderListBodyMixin::compute_max_intrinsic_width(self, app, height)
627 }
628
629 fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
630 RenderListBodyMixin::compute_min_intrinsic_height(self, app, width)
631 }
632
633 fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
634 RenderListBodyMixin::compute_max_intrinsic_height(self, app, width)
635 }
636
637 fn compute_distance_to_actual_baseline(
638 self: RenderHandle<Self>,
639 app: &mut App,
640 baseline: TextBaseline,
641 ) -> Option<f64> {
642 RenderListBodyMixin::compute_distance_to_actual_baseline(self, app, baseline)
643 }
644
645 fn hit_test_children(
646 self: RenderHandle<Self>,
647 app: &mut App,
648 result: &mut BoxHitTestResult<'_>,
649 position: Offset,
650 ) -> bool {
651 RenderListBodyMixin::hit_test_children(self, app, result, position)
652 }
653}