1use crate::*;
2
3pub trait Layout<C: Context>: 'static {
4 type Item: Default + 'static;
7
8 fn fit(&self, cx: &mut LayoutCx<'_, C, Self::Item>, axis: Axis) -> Fit<C::Scalar>;
10
11 fn distribute(&self, cx: &mut LayoutCx<'_, C, Self::Item>, axis: Axis, extent: C::Scalar);
13}
14
15impl<C: Context> Layout<C> for () {
17 type Item = ();
18
19 fn fit(&self, cx: &mut LayoutCx<'_, C, ()>, axis: Axis) -> Fit<C::Scalar> {
20 let mut extent = Fit::ZERO;
21 for child in cx.children() {
22 extent = extent.max(cx.fit(child, axis));
23 }
24 extent
25 }
26
27 fn distribute(&self, cx: &mut LayoutCx<'_, C, ()>, _axis: Axis, extent: C::Scalar) {
28 for child in cx.children() {
29 cx.set_extent(child, extent);
30 cx.set_offset(child, C::Scalar::ZERO);
31 }
32 }
33}
34
35pub trait Place<C: Context>: 'static {
37 fn place(&self, cx: &mut PlaceCx<'_, C>, target: Rect<C::Scalar>) -> Point<C::Scalar>;
39}
40
41impl<C: Context> Place<C> for () {
43 fn place(&self, cx: &mut PlaceCx<'_, C>, target: Rect<C::Scalar>) -> Point<C::Scalar> {
44 cx.resolve(Constraints::loose(Size::uniform(C::Scalar::UNBOUNDED)));
45 Point::new(target.x, target.y)
46 }
47}
48
49#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
51pub struct Fit<T> {
52 pub min: T,
54 pub natural: T,
56}
57
58impl<T> Fit<T> {
59 #[inline]
60 pub const fn new(min: T, natural: T) -> Self {
61 Self { min, natural }
62 }
63
64 #[inline]
65 pub const fn exact(extent: T) -> Self
66 where
67 T: Copy,
68 {
69 Self {
70 min: extent,
71 natural: extent,
72 }
73 }
74}
75
76impl<T: Copy> From<T> for Fit<T> {
77 #[inline]
78 fn from(extent: T) -> Self {
79 Self::exact(extent)
80 }
81}
82
83impl<T: Scalar> core::ops::Add for Fit<T> {
84 type Output = Self;
85
86 #[inline]
87 fn add(self, rhs: Self) -> Self {
88 Self {
89 min: self.min + rhs.min,
90 natural: self.natural + rhs.natural,
91 }
92 }
93}
94
95impl<T: Scalar> core::ops::AddAssign for Fit<T> {
96 #[inline]
97 fn add_assign(&mut self, rhs: Self) {
98 self.min += rhs.min;
99 self.natural += rhs.natural;
100 }
101}
102
103impl<T: Scalar> Fit<T> {
104 pub const ZERO: Self = Self {
105 min: T::ZERO,
106 natural: T::ZERO,
107 };
108
109 #[inline]
110 pub fn max(self, other: Self) -> Self {
111 Self {
112 min: self.min.max(other.min),
113 natural: self.natural.max(other.natural),
114 }
115 }
116
117 #[inline]
118 pub fn clamp(self, min: T, max: T) -> Self {
119 Self {
120 min: self.min.clamp(min, max),
121 natural: self.natural.clamp(min, max),
122 }
123 }
124
125 #[inline]
126 pub fn endpoint(self, offset: T) -> Self {
127 Self {
128 min: self.min.endpoint(offset),
129 natural: self.natural.endpoint(offset),
130 }
131 }
132}
133
134#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
135pub enum Axis {
136 Horizontal,
137 #[default]
138 Vertical,
139}
140
141impl Axis {
142 #[inline]
143 pub const fn other(self) -> Self {
144 match self {
145 Self::Horizontal => Self::Vertical,
146 Self::Vertical => Self::Horizontal,
147 }
148 }
149
150 #[inline]
151 pub fn extent<T: Copy>(self, size: Size<T>) -> T {
152 match self {
153 Self::Horizontal => size.width,
154 Self::Vertical => size.height,
155 }
156 }
157
158 #[inline]
159 pub fn set_extent<T>(self, size: &mut Size<T>, extent: T) {
160 match self {
161 Self::Horizontal => size.width = extent,
162 Self::Vertical => size.height = extent,
163 }
164 }
165}
166
167#[repr(transparent)]
168pub struct LayoutCx<'a, C: Context, I = ()> {
169 inner: LayoutCxInner<'a, C>,
170 item: PhantomData<fn() -> I>,
171}
172
173impl<'a, C: Context, I: 'static> LayoutCx<'a, C, I> {
174 pub fn node(&self) -> NodeId {
175 self.inner.node
176 }
177
178 pub fn size(&self, child: NodeId) -> Size<C::Scalar> {
179 self.inner.placements[child.index()].size()
180 }
181
182 #[inline]
183 pub fn fit(&self, child: NodeId, axis: Axis) -> Fit<C::Scalar> {
184 axis.extent(self.inner.fits[child.index() - self.inner.root.index()])
185 }
186
187 #[inline]
188 pub fn children(&self) -> Children<'a> {
189 Children {
190 nodes: &self.inner.tree.nodes[..=self.inner.tree.nodes[self.inner.node.index()].subtree_end as usize],
191 next: NodeId::new(self.inner.node.index() + 1),
192 }
193 }
194
195 #[inline]
196 pub fn item(&self, child: NodeId) -> &'a I {
197 let stored = &self.inner.tree.nodes[child.index()];
198 assert_eq!(stored.parent, self.inner.node, "layout items belong to direct children");
200 let id = stored.item;
201 self.inner.bump.load(if id.offset().is_some() {
202 id
203 } else {
204 self.inner.default_item
205 })
206 }
207
208 #[inline(always)]
210 pub fn fixed_extent(&self, child: NodeId, axis: Axis) -> Option<C::Scalar> {
211 self.inner.fixed_extent(child, axis)
212 }
213
214 #[inline(always)]
219 pub fn across(&mut self) -> C::Scalar {
220 self.inner.across()
221 }
222
223 #[inline(always)]
224 pub fn set_extent(&mut self, child: NodeId, extent: C::Scalar) -> C::Scalar {
225 self.inner.set_extent(child, extent)
226 }
227
228 #[inline(always)]
229 pub fn set_offset(&mut self, child: NodeId, offset: C::Scalar) {
230 self.inner.set_offset(child, offset);
231 }
232
233 pub fn request_frame(&mut self) {
234 *self.inner.frame_requested = true;
235 }
236
237 pub fn context(&self) -> &C {
238 self.inner.context
239 }
240
241 pub fn context_mut(&mut self) -> &mut C {
242 self.inner.context
243 }
244
245 pub fn scratch(&self) -> &'a ScratchBuffer {
246 &self.inner.bump.storage
247 }
248
249 #[inline(always)]
250 pub fn set_child_z_index(&mut self, child: NodeId, z_index: i16) {
251 self.inner.set_child_z_index(child, z_index);
252 }
253}
254
255pub struct PlaceCx<'a, C: Context> {
257 tree: &'a Tree<C>,
258 bump: &'a BumpArena,
259 placements: &'a mut [Rect<C::Scalar>],
260 z_indices: &'a mut Vec<i16>,
261 has_custom_order: &'a mut bool,
262 context: &'a mut C,
263 frame_requested: &'a mut bool,
264 animated: &'a [MotionProps],
265 node: NodeId,
266 parent: Size<C::Scalar>,
267}
268
269impl<C: Context> PlaceCx<'_, C> {
270 pub fn node(&self) -> NodeId {
271 self.node
272 }
273
274 pub fn parent(&self) -> Size<C::Scalar> {
276 self.parent
277 }
278
279 pub fn screen(&self) -> Rect<C::Scalar> {
280 self.tree.screen
281 }
282
283 pub fn resolve(&mut self, bounds: Constraints<C::Scalar>) -> Size<C::Scalar> {
285 solve_subtree(
286 self.tree,
287 self.bump,
288 self.placements,
289 self.z_indices,
290 self.has_custom_order,
291 self.context,
292 self.frame_requested,
293 self.animated,
294 self.node,
295 bounds,
296 )
297 }
298
299 pub fn request_frame(&mut self) {
300 *self.frame_requested = true;
301 }
302
303 pub fn context(&self) -> &C {
304 self.context
305 }
306
307 pub fn context_mut(&mut self) -> &mut C {
308 self.context
309 }
310}
311
312#[derive(Clone, Copy)]
314pub struct Children<'a> {
315 nodes: &'a [StoredNode],
316 next: NodeId,
317}
318
319impl Iterator for Children<'_> {
320 type Item = NodeId;
321
322 #[inline]
323 fn next(&mut self) -> Option<NodeId> {
324 while self.next.index() < self.nodes.len() {
325 let node = self.next;
326 let stored = &self.nodes[node.index()];
327 self.next = NodeId::new(stored.subtree_end as usize + 1);
328 if stored.place.index().is_none() {
329 return Some(node);
330 }
331 }
332 None
333 }
334}
335
336struct LayoutCxInner<'a, C: Context> {
341 tree: &'a Tree<C>,
342 bump: &'a BumpArena,
343 placements: &'a mut [Rect<C::Scalar>],
344 z_indices: &'a mut Vec<i16>,
345 has_custom_order: &'a mut bool,
346 fits: &'a mut [Size<Fit<C::Scalar>>],
347 context: &'a mut C,
348 frame_requested: &'a mut bool,
349 animated: &'a [MotionProps],
351 root: NodeId,
352 node: NodeId,
353 axis: Axis,
354 across: C::Scalar,
355 round: u8,
356 relayout: bool,
357 squeezed: bool,
358 content_origin: Point<C::Scalar>,
359 default_item: BumpId,
360}
361
362impl<'a, C: Context> LayoutCxInner<'a, C> {
363 #[inline(always)]
364 fn as_cx<J>(&mut self) -> &mut LayoutCx<'a, C, J> {
365 unsafe { &mut *core::ptr::from_mut(self).cast::<LayoutCx<'a, C, J>>() }
367 }
368
369 fn fixed_extent(&self, child: NodeId, axis: Axis) -> Option<C::Scalar> {
370 let stored = &self.tree.nodes[child.index()];
371 self.size_override(child, axis).or_else(|| {
372 let limits = self.tree.aux[stored.aux.index()?].limits;
373 let min = axis.extent(limits.min);
374 (min == axis.extent(limits.max)).then_some(min)
375 })
376 }
377
378 fn across(&mut self) -> C::Scalar {
379 if self.axis == Axis::Vertical {
380 return self.across;
381 }
382 let pad = self.padding(self.node).size().height;
383 if self.round > 0 {
384 return (self.placements[self.node.index()].height - pad).max(C::Scalar::ZERO);
385 }
386 self.relayout = true;
387 let max = self.size_override(self.node, Axis::Vertical).or_else(|| {
388 let max = self.limits(self.node).max.height;
389 max.is_finite().then_some(max)
390 });
391 max.map_or(C::Scalar::UNBOUNDED, |height| (height - pad).max(C::Scalar::ZERO))
392 }
393
394 #[inline]
395 fn set_extent(&mut self, child: NodeId, extent: C::Scalar) -> C::Scalar {
396 let clamped = self.constrain(child, self.axis, extent.max(C::Scalar::ZERO));
397 let area = &mut self.placements[child.index()];
398 match self.axis {
399 Axis::Horizontal => {
400 area.width = clamped;
401 if clamped < self.fits[child.index() - self.root.index()].width.natural {
402 self.squeezed = true;
403 }
404 }
405 Axis::Vertical => area.height = clamped,
406 }
407 clamped
408 }
409
410 #[inline]
411 fn set_offset(&mut self, child: NodeId, offset: C::Scalar) {
412 let area = &mut self.placements[child.index()];
413 match self.axis {
414 Axis::Horizontal => area.x = offset.endpoint(self.content_origin.x),
415 Axis::Vertical => area.y = offset.endpoint(self.content_origin.y),
416 }
417 }
418
419 fn set_child_z_index(&mut self, child: NodeId, z_index: i16) {
420 if z_index != 0 {
421 *self.has_custom_order = true;
422 } else if self.z_indices.is_empty() {
423 return;
424 }
425 let index = child.index();
426 if self.z_indices.len() <= index {
427 self.z_indices.resize(index + 1, 0);
428 }
429 self.z_indices[index] = z_index;
430 }
431
432 fn distribute_axis(&mut self, axis: Axis, containers: &[u32], start: usize, bounds: Constraints<C::Scalar>) {
433 let root_fit = axis
434 .extent(self.fits[0])
435 .natural
436 .clamp(axis.extent(bounds.min), axis.extent(bounds.max));
437 match axis {
438 Axis::Horizontal => {
439 self.placements[start].width = root_fit;
440 if root_fit < self.fits[0].width.natural {
441 self.squeezed = true;
442 }
443 }
444 Axis::Vertical => self.placements[start].height = root_fit,
445 }
446 for &index in containers.iter().rev() {
448 let index = index as usize;
449 let stored = &self.tree.nodes[index];
450 let node = NodeId::new(index);
451 let padding = self.padding(node);
452 let pad = padding.size();
453 let size = self.placements[index].size();
454 let inner_w = (size.width - pad.width).max(C::Scalar::ZERO);
455 let inner = (axis.extent(size) - axis.extent(pad)).max(C::Scalar::ZERO);
456 let kind = &self.tree.layout_kinds[stored.layout_kind as usize];
457 self.node = node;
458 self.axis = axis;
459 self.across = inner_w;
460 self.content_origin = Point::new(padding.left.min(size.width), padding.top.min(size.height));
461 self.default_item = kind.default_item;
462 (kind.distribute)(self.as_cx(), stored.layout, axis, inner);
463 }
464 }
465
466 #[inline]
467 fn run_fit(&mut self, node: NodeId, axis: Axis, across: C::Scalar) -> Fit<C::Scalar> {
468 let stored = &self.tree.nodes[node.index()];
469 let kind = &self.tree.layout_kinds[stored.layout_kind as usize];
470 self.node = node;
471 self.axis = axis;
472 self.across = across;
473 self.default_item = kind.default_item;
474 let fit = (kind.fit)(self.as_cx(), stored.layout, axis);
475 Fit {
476 min: fit.min.min(fit.natural),
477 natural: fit.natural,
478 }
479 }
480
481 #[inline]
482 fn measure(&mut self, node: NodeId, constraints: Constraints<C::Scalar>) -> Size<Fit<C::Scalar>> {
483 if constraints.min == constraints.max {
484 return Size::new(Fit::exact(constraints.min.width), Fit::exact(constraints.min.height));
485 }
486 let mut measure = self.tree.nodes[node.index()].first_measure;
487 let mut fit = Fit::exact(Size::ZERO);
488 while let Some(index) = measure.index() {
489 let stored = self.tree.measures[index];
490 let next = (stored.run)(self.bump, stored.bump_id, self.context, constraints);
491 fit.min = fit.min.max(next.min);
492 fit.natural = fit.natural.max(next.natural);
493 measure = stored.next;
494 }
495 let natural = constraints.constrain(fit.natural);
496 let min = constraints.constrain(fit.min);
497 Size::new(
498 Fit::new(min.width.min(natural.width), natural.width),
499 Fit::new(min.height.min(natural.height), natural.height),
500 )
501 }
502
503 #[inline]
504 fn constrain(&self, node: NodeId, axis: Axis, value: C::Scalar) -> C::Scalar {
505 if let Some(fixed) = self.size_override(node, axis) {
506 return fixed;
507 }
508 let Some(index) = self.tree.nodes[node.index()].aux.index() else {
509 return value;
510 };
511 let limits = self.tree.aux[index].limits;
512 value.clamp(axis.extent(limits.min), axis.extent(limits.max))
513 }
514
515 #[inline]
516 fn size_override(&self, node: NodeId, axis: Axis) -> Option<C::Scalar> {
517 let &props = self.animated.get(node.index())?;
518 let flag = match axis {
519 Axis::Horizontal => MotionProps::WIDTH,
520 Axis::Vertical => MotionProps::HEIGHT,
521 };
522 props
523 .intersects(flag)
524 .then(|| axis.extent(self.placements[node.index()].size()))
525 }
526
527 #[inline]
528 fn padding(&self, node: NodeId) -> Sides<C::Scalar> {
529 self.tree.nodes[node.index()]
530 .aux
531 .index()
532 .map_or(Sides::all(C::Scalar::ZERO), |index| self.tree.aux[index].padding)
533 }
534
535 #[inline]
536 fn limits(&self, node: NodeId) -> Constraints<C::Scalar> {
537 self.tree.nodes[node.index()]
538 .aux
539 .index()
540 .map_or(Constraints::loose(Size::uniform(C::Scalar::UNBOUNDED)), |index| {
541 self.tree.aux[index].limits
542 })
543 }
544}
545
546pub fn resolve<C: Context>(
552 tree: &mut Tree<C>,
553 bump: &BumpArena,
554 context: &mut C,
555 root: NodeId,
556 bounds: Constraints<C::Scalar>,
557 commit: bool,
558) -> Size<C::Scalar> {
559 tree.placements.resize(tree.nodes.len(), Rect::default());
561 let mut size = arrange(tree, bump, context, root, bounds, &[]);
562 if tree.node_motions.is_empty() {
563 return size;
564 }
565 let start = root.index();
566 let end = tree.nodes[start].subtree_end as usize + 1;
567 let nodes = &tree.nodes[..end];
568 let mut sizes = None;
570 let mut index = start;
571 while index < end {
572 let stored = &nodes[index];
573 if !commit && start != 0 && stored.place.index().is_some() {
574 index = stored.subtree_end as usize + 1;
575 continue;
576 }
577 index += 1;
578 let Some(record) = stored.aux.index().and_then(|aux| tree.aux[aux].motion.index()) else {
579 continue;
580 };
581 let NodeMotion {
582 node,
583 motion: mut config,
584 } = tree.node_motions[record];
585 if !commit && !config.properties.intersects(MotionProps::SIZE) {
586 continue;
587 }
588 let id = stored.id;
589 if stored.parent == node {
590 config.properties = config
591 .properties
592 .intersection(MotionProps::POSITION | MotionProps::RESIZE);
593 }
594 let found = tree.motions.binary_search_by_key(&id, |state| state.id);
595 let mut initial = MotionState::new(id, node);
596 let state = match found {
597 Ok(index) => &mut tree.motions[index],
598 Err(_) => &mut initial,
599 };
600 let (values, animated) = state.advance(tree.placements[node.index()], config, tree.time, tree.resized, commit);
601 if commit {
602 state.node = node;
603 state.properties = config.properties;
604 state.seen = true;
605 }
606 let properties = animated.intersection(MotionProps::SIZE);
607 if !properties.is_empty() {
608 let sizes = sizes.get_or_insert_with(|| {
609 let mut sizes = bump.storage.alloc(end);
610 sizes.resize(end, MotionProps::NONE);
611 sizes
612 });
613 let area = &mut tree.placements[node.index()];
614 if properties.intersects(MotionProps::WIDTH) {
615 area.width = values[2];
616 }
617 if properties.intersects(MotionProps::HEIGHT) {
618 area.height = values[3];
619 }
620 sizes[node.index()] = properties;
621 }
622 if let Err(index) = found
623 && commit
624 {
625 tree.motions.insert(index, initial);
626 }
627 }
628 if let Some(sizes) = &sizes {
629 size = arrange(tree, bump, context, root, bounds, sizes);
630 }
631 if commit {
633 for state in tree.motions.iter().filter(|state| state.seen) {
634 let area = &mut tree.placements[state.node.index()];
635 if state.properties.intersects(MotionProps::X) {
636 area.x = state.animator.values[0];
637 }
638 if state.properties.intersects(MotionProps::Y) {
639 area.y = state.animator.values[1];
640 }
641 }
642 }
643 size
644}
645
646pub fn position<C: Context>(tree: &mut Tree<C>) {
648 let count = tree.nodes.len();
649 let placements = &mut tree.placements[..count];
650 if tree.node_motions.is_empty() {
651 for index in 1..count {
652 let reference = placements[tree.nodes[index].target.index()];
653 let area = &mut placements[index];
654 area.x += reference.x;
655 area.y += reference.y;
656 }
657 return;
658 }
659 for index in 1..count {
660 let stored = &tree.nodes[index];
661 let reference = placements[stored.target.index()];
662 let area = &mut placements[index];
663 area.x += reference.x;
664 area.y += reference.y;
665 let Some(record) = stored.aux.index().and_then(|aux| tree.aux[aux].motion.index()) else {
666 continue;
667 };
668 let motion = tree.node_motions[record].motion;
669 let id = stored.id;
671 let Some(old) = tree.slots.old(id) else {
672 continue;
673 };
674 let old_reference = tree.old_nodes[tree.old_nodes[old].target.index()].id;
676 if old_reference == tree.nodes[stored.target.index()].id {
677 continue;
678 }
679 let Ok(state) = tree.motions.binary_search_by_key(&id, |state| state.id) else {
680 continue;
681 };
682 tree.motions[state].flying = true;
683 let animator = &mut tree.motions[state].animator;
684 let start = tree.old_placements[old];
685 if motion.properties.intersects(MotionProps::X) && area.x != start.x {
686 animator.displace(0, area.x.delta(start.x), tree.time);
687 area.x = start.x;
688 }
689 if motion.properties.intersects(MotionProps::Y) && area.y != start.y {
690 animator.displace(1, area.y.delta(start.y), tree.time);
691 area.y = start.y;
692 }
693 }
694}
695
696pub fn fit<'a, C: Context, L: Layout<C>>(cx: &mut LayoutCx<'a, C>, id: BumpId, axis: Axis) -> Fit<C::Scalar> {
697 let layout = cx.inner.bump.load::<L>(id);
698 layout.fit(cx.inner.as_cx(), axis)
699}
700
701pub fn distribute<'a, C: Context, L: Layout<C>>(cx: &mut LayoutCx<'a, C>, id: BumpId, axis: Axis, extent: C::Scalar) {
702 let layout = cx.inner.bump.load::<L>(id);
703 layout.distribute(cx.inner.as_cx(), axis, extent);
704}
705
706pub fn place<C: Context, P: Place<C>>(
707 cx: &mut PlaceCx<'_, C>,
708 id: BumpId,
709 target: Rect<C::Scalar>,
710) -> Point<C::Scalar> {
711 let bump = cx.bump;
712 bump.load::<P>(id).place(cx, target)
713}
714
715fn arrange<C: Context>(
716 tree: &mut Tree<C>,
717 bump: &BumpArena,
718 context: &mut C,
719 node: NodeId,
720 bounds: Constraints<C::Scalar>,
721 animated: &[MotionProps],
722) -> Size<C::Scalar> {
723 let mut placements = core::mem::take(&mut tree.placements);
724 let mut z_indices = core::mem::take(&mut tree.z_indices);
725 let mut has_custom_order = tree.has_custom_order;
726 let mut requested = tree.frame_requested;
727 let size = solve_subtree(
728 tree,
729 bump,
730 &mut placements,
731 &mut z_indices,
732 &mut has_custom_order,
733 context,
734 &mut requested,
735 animated,
736 node,
737 bounds,
738 );
739 if node.index() == 0 && !tree.places.is_empty() {
740 for index in 1..tree.nodes.len() {
741 let stored = &tree.nodes[index];
742 let Some(place) = stored.place.index() else {
743 continue;
744 };
745 let place = tree.places[place];
746 let mut reference = stored.target;
748 let mut origin = Point::ZERO;
749 loop {
750 let area = placements[reference.index()];
751 origin = Point::new(origin.x + area.x, origin.y + area.y);
752 let next = tree.nodes[reference.index()].target;
753 if next == reference {
754 break;
755 }
756 debug_assert!(next.index() < reference.index());
757 reference = next;
758 }
759 let size = placements[stored.target.index()].size();
760 let parent = placements[visual_parent(&tree.aux, stored).index()].size();
761 let mut place_cx = PlaceCx {
762 tree,
763 bump,
764 placements: &mut placements,
765 z_indices: &mut z_indices,
766 has_custom_order: &mut has_custom_order,
767 context,
768 frame_requested: &mut requested,
769 animated,
770 node: NodeId::new(index),
771 parent,
772 };
773 let target = Rect::new(origin.x, origin.y, size.width, size.height);
774 let position = (place.run)(&mut place_cx, place.bump_id, target);
775 let area = &mut placements[index];
776 area.x = position.x - origin.x;
777 area.y = position.y - origin.y;
778 }
779 }
780 tree.placements = placements;
781 tree.z_indices = z_indices;
782 tree.has_custom_order = has_custom_order;
783 tree.frame_requested = requested;
784 size
785}
786
787#[allow(clippy::too_many_arguments)]
788fn solve_subtree<C: Context>(
789 tree: &Tree<C>,
790 bump: &BumpArena,
791 placements: &mut [Rect<C::Scalar>],
792 z_indices: &mut Vec<i16>,
793 has_custom_order: &mut bool,
794 context: &mut C,
795 frame_requested: &mut bool,
796 animated: &[MotionProps],
797 root: NodeId,
798 mut bounds: Constraints<C::Scalar>,
799) -> Size<C::Scalar> {
800 let start = root.index();
801 let end = tree.nodes[start].subtree_end as usize + 1;
802 let len = end - start;
803 let root_scope = tree.nodes[start].place_scope;
804
805 let mut fits = bump.storage.alloc(len);
806 fits.resize(len, Size::new(Fit::ZERO, Fit::ZERO));
807 let mut containers = bump.storage.alloc(0);
808 let mut cx = LayoutCxInner {
809 tree,
810 bump,
811 placements,
812 z_indices,
813 has_custom_order,
814 fits: &mut fits,
815 context,
816 frame_requested,
817 animated,
818 root,
819 node: root,
820 axis: Axis::Horizontal,
821 across: C::Scalar::ZERO,
822 round: 0,
823 relayout: false,
824 squeezed: false,
825 content_origin: Point::ZERO,
826 default_item: BumpId::NONE,
827 };
828
829 if let Some(index) = tree.nodes[start].aux.index() {
830 let limits = tree.aux[index].limits;
831 bounds = Constraints {
832 min: limits.constrain(bounds.min),
833 max: limits.constrain(bounds.max),
834 };
835 }
836 if let Some(width) = cx.size_override(root, Axis::Horizontal) {
837 bounds.min.width = width;
838 bounds.max.width = width;
839 }
840 if let Some(height) = cx.size_override(root, Axis::Vertical) {
841 bounds.min.height = height;
842 bounds.max.height = height;
843 }
844
845 let clamp_fit = |fit: Fit<C::Scalar>, aux: Option<&Aux<C::Scalar>>, axis: Axis| {
846 let Some(aux) = aux else {
847 return fit;
848 };
849 let fit = if aux.clip.is_some() {
850 let pad = axis.extent(aux.padding.size());
851 Fit::new(pad.min(fit.min), fit.natural)
852 } else {
853 fit
854 };
855 fit.clamp(axis.extent(aux.limits.min), axis.extent(aux.limits.max))
856 };
857
858 loop {
859 cx.squeezed = false;
860 for index in (start..end).rev() {
862 let stored = &tree.nodes[index];
863 if stored.place_scope != root_scope {
864 continue;
865 }
866 let node = NodeId::new(index);
867 let local = index - start;
868 let has_layout = stored.layout.offset().is_some();
869 if has_layout && cx.round == 0 {
870 containers.push(index as u32);
871 }
872 let aux = stored.aux.index().map(|index| &tree.aux[index]);
873 let mut fit_x = Fit::ZERO;
874 let mut leaf_h = Fit::ZERO;
875 if stored.first_measure.index().is_some() {
876 let mut limits = aux.map_or(Constraints::loose(Size::uniform(C::Scalar::UNBOUNDED)), |aux| {
877 aux.limits
878 });
879 if let Some(width) = cx.size_override(node, Axis::Horizontal) {
880 limits.min.width = width;
881 limits.max.width = width;
882 }
883 if let Some(height) = cx.size_override(node, Axis::Vertical) {
884 limits.min.height = height;
885 limits.max.height = height;
886 }
887 let measured = cx.measure(node, limits);
888 fit_x = measured.width;
889 leaf_h = measured.height;
890 }
891 if has_layout {
892 let fixed = cx.size_override(node, Axis::Horizontal).or_else(|| {
893 let limits = aux?.limits;
894 (limits.min.width == limits.max.width).then_some(limits.min.width)
895 });
896 cx.fits[local].width = if let Some(fixed) = fixed {
897 Fit::exact(fixed)
898 } else {
899 let content_x = cx.run_fit(node, Axis::Horizontal, C::Scalar::ZERO);
900 let pad_w = aux.map_or(C::Scalar::ZERO, |aux| aux.padding.size().width);
901 clamp_fit(fit_x.max(content_x.endpoint(pad_w)), aux, Axis::Horizontal)
902 };
903 if cx.round == 0 {
904 cx.fits[local].height = leaf_h;
905 }
906 } else {
907 cx.fits[local].width = cx
908 .size_override(node, Axis::Horizontal)
909 .map_or_else(|| clamp_fit(fit_x, aux, Axis::Horizontal), Fit::exact);
910 cx.fits[local].height = cx
911 .size_override(node, Axis::Vertical)
912 .map_or_else(|| clamp_fit(leaf_h, aux, Axis::Vertical), Fit::exact);
913 }
914 }
915
916 cx.distribute_axis(Axis::Horizontal, &containers, start, bounds);
918
919 if cx.squeezed {
921 for index in (start..end).rev() {
922 let stored = &tree.nodes[index];
923 if stored.place_scope != root_scope {
924 continue;
925 }
926 let node = NodeId::new(index);
927 let local = index - start;
928 let aux = stored.aux.index().map(|index| &tree.aux[index]);
929 let assigned_w = cx.placements[index].width;
930 let mut fit_y = Fit::ZERO;
931 if stored.first_measure.index().is_some() {
932 fit_y = if assigned_w < cx.fits[local].width.natural
933 || (cx.round > 0 && stored.layout.offset().is_some())
934 {
935 let mut limits = aux.map_or(Constraints::loose(Size::uniform(C::Scalar::UNBOUNDED)), |aux| {
936 aux.limits
937 });
938 limits.min.width = assigned_w;
939 limits.max.width = assigned_w;
940 if let Some(height) = cx.size_override(node, Axis::Vertical) {
941 limits.min.height = height;
942 limits.max.height = height;
943 }
944 cx.measure(node, limits).height
945 } else {
946 cx.fits[local].height
947 };
948 }
949 if stored.layout.offset().is_some() {
950 let fixed = cx.size_override(node, Axis::Vertical).or_else(|| {
951 let limits = aux?.limits;
952 (limits.min.height == limits.max.height).then_some(limits.min.height)
953 });
954 if let Some(fixed) = fixed {
955 cx.fits[local].height = Fit::exact(fixed);
956 continue;
957 }
958 let pad = aux.map_or(Size::ZERO, |aux| aux.padding.size());
959 let inner_w = (assigned_w - pad.width).max(C::Scalar::ZERO);
960 let content_y = cx.run_fit(node, Axis::Vertical, inner_w);
961 fit_y = fit_y.max(content_y.endpoint(pad.height));
962 }
963 cx.fits[local].height = cx
964 .size_override(node, Axis::Vertical)
965 .map_or_else(|| clamp_fit(fit_y, aux, Axis::Vertical), Fit::exact);
966 }
967 } else {
968 for &index in containers.iter() {
969 let index = index as usize;
970 let stored = &tree.nodes[index];
971 let node = NodeId::new(index);
972 let local = index - start;
973 let aux = stored.aux.index().map(|index| &tree.aux[index]);
974 let fixed = cx.size_override(node, Axis::Vertical).or_else(|| {
975 let limits = aux?.limits;
976 (limits.min.height == limits.max.height).then_some(limits.min.height)
977 });
978 if let Some(fixed) = fixed {
979 cx.fits[local].height = Fit::exact(fixed);
980 continue;
981 }
982 let pad = aux.map_or(Size::ZERO, |aux| aux.padding.size());
983 let inner_w = (cx.placements[index].width - pad.width).max(C::Scalar::ZERO);
984 let content_y = cx.run_fit(node, Axis::Vertical, inner_w);
985 let mut fit_y = content_y.endpoint(pad.height);
986 if stored.first_measure.index().is_some() {
987 let leaf_h = if cx.round > 0 {
988 let mut limits = aux.map_or(Constraints::loose(Size::uniform(C::Scalar::UNBOUNDED)), |aux| {
989 aux.limits
990 });
991 if let Some(width) = cx.size_override(node, Axis::Horizontal) {
992 limits.min.width = width;
993 limits.max.width = width;
994 }
995 if let Some(height) = cx.size_override(node, Axis::Vertical) {
996 limits.min.height = height;
997 limits.max.height = height;
998 }
999 cx.measure(node, limits).height
1000 } else {
1001 cx.fits[local].height
1002 };
1003 fit_y = fit_y.max(leaf_h);
1004 }
1005 cx.fits[local].height = clamp_fit(fit_y, aux, Axis::Vertical);
1006 }
1007 }
1008
1009 cx.distribute_axis(Axis::Vertical, &containers, start, bounds);
1011 if !cx.relayout || cx.round >= 1 {
1012 break;
1013 }
1014 cx.relayout = false;
1015 cx.round += 1;
1016 }
1017
1018 cx.placements[start].size()
1019}