1use super::*;
2
3impl LayoutEngine {
5 pub fn compute(&mut self, tree: &mut SceneTree, viewport: Size) -> LayoutStats {
17 let Self {
18 track_damage,
19 damage_fallback,
20 layout_snapshots,
21 ..
22 } = self;
23 *track_damage = false;
24 *damage_fallback = LAYOUT_FALLBACK_NONE;
25 layout_snapshots.clear();
26 if layout_snapshots.capacity() < DIRTY_PAINT_FULL_THRESHOLD {
27 layout_snapshots.reserve(DIRTY_PAINT_FULL_THRESHOLD);
28 }
29 self.compute_inner(tree, viewport)
30 }
31
32 pub(crate) fn compute_with_damage(
45 &mut self,
46 tree: &mut SceneTree,
47 viewport: Size,
48 scale: f32,
49 ) -> (LayoutStats, LayoutDamageReport) {
50 {
51 let Self {
52 track_damage,
53 damage_fallback,
54 layout_snapshots,
55 split_cache,
56 ..
57 } = self;
58 *track_damage = true;
59 *damage_fallback = LAYOUT_FALLBACK_NONE;
60 layout_snapshots.clear();
61 split_cache.begin_snapshot_pass();
62 }
63 let stats: LayoutStats = self.compute_inner(tree, viewport);
64 let fallback_after: u8 = {
65 let Self {
66 track_damage,
67 damage_fallback,
68 ..
69 } = self;
70 *track_damage = false;
71 *damage_fallback
72 };
73 if fallback_after != LAYOUT_FALLBACK_NONE {
74 let Self {
75 layout_snapshots, ..
76 } = self;
77 layout_snapshots.clear();
78 return (
79 stats,
80 LayoutDamageReport {
81 damage: DamageRegion::full(),
82 fallback_kind: fallback_after,
83 },
84 );
85 }
86 let mut damage: DamageRegion = DamageRegion::default();
87 let mut final_fallback: u8 = LAYOUT_FALLBACK_NONE;
88 let Self {
89 layout_snapshots, ..
90 } = self;
91 for snap in layout_snapshots.iter().copied() {
92 let Some(node) = tree.get_node(snap.id) else {
93 continue;
94 };
95 let lbox: LayoutBox = node.get_layout_box();
96 let new_bounds: Rect = lbox.get_bounds();
97 let new_content_bounds: Rect = lbox.get_content_bounds();
98 let new_content_size: Size = lbox.get_content_size();
99 let bounds_changed: bool = !Self::rect_approx_eq(snap.old_bounds, new_bounds);
100 let content_changed: bool =
101 !Self::rect_approx_eq(snap.old_content_bounds, new_content_bounds);
102 let is_scroll_box: bool =
103 node.style_ref().layout_ref().get_overflow() == Overflow::Scroll;
104 let scroll_content_changed: bool =
105 is_scroll_box && !Self::size_approx_eq(snap.old_content_size, new_content_size);
106 let geom_changed: bool = bounds_changed || content_changed;
107 if !geom_changed && !scroll_content_changed {
108 continue;
109 }
110 let is_portal_or_fixed: bool = matches!(node.get_kind(), NodeKind::Portal)
111 || node.style_ref().layout_ref().get_position() == Position::Fixed;
112 if is_portal_or_fixed {
113 final_fallback = LAYOUT_FALLBACK_PORTAL;
114 break;
115 }
116 if snap.has_old_screen {
117 damage.add_rect(SceneTree::scale_logical_to_phys_rect(
118 snap.old_screen,
119 scale,
120 ));
121 }
122 if new_bounds.get_width() > 0.0 && new_bounds.get_height() > 0.0 {
123 match tree.node_conservative_logical_rect(snap.id) {
124 Some(new_screen) => {
125 damage.add_rect(SceneTree::scale_logical_to_phys_rect(new_screen, scale));
126 }
127 None => {
128 final_fallback = LAYOUT_FALLBACK_UNSAFE;
129 break;
130 }
131 }
132 }
133 }
134 layout_snapshots.clear();
135 if final_fallback != LAYOUT_FALLBACK_NONE {
136 return (
137 stats,
138 LayoutDamageReport {
139 damage: DamageRegion::full(),
140 fallback_kind: final_fallback,
141 },
142 );
143 }
144 (
145 stats,
146 LayoutDamageReport {
147 damage,
148 fallback_kind: LAYOUT_FALLBACK_NONE,
149 },
150 )
151 }
152
153 fn compute_inner(&mut self, tree: &mut SceneTree, viewport: Size) -> LayoutStats {
165 let root_id: NodeId = tree.get_root();
166 let space: AvailableSpace = AvailableSpace::root(viewport);
167 let mut stats: LayoutStats = LayoutStats::default();
168 {
169 let Self {
170 split_cache,
171 last_split_stats,
172 ..
173 } = self;
174 split_cache.begin_pass();
175 *last_split_stats = SplitCacheStats::default();
176 }
177 self.get_mut_children_scratch().clear();
178 self.get_mut_flow_scratch().clear();
179 self.get_mut_abs_scratch().clear();
180 self.get_mut_flex_items_scratch().clear();
181 self.get_mut_flex_lines_scratch().clear();
182 self.get_mut_translate_scratch().clear();
183 let _: (Size, u8, f32) = self.layout_node_mode(
184 tree,
185 root_id,
186 space,
187 LayoutRunMode::PerformLayout,
188 &mut stats,
189 );
190 debug_assert!(self.get_children_scratch().is_empty());
191 debug_assert!(self.get_flow_scratch().is_empty());
192 debug_assert!(self.get_abs_scratch().is_empty());
193 debug_assert!(self.get_flex_items_scratch().is_empty());
194 debug_assert!(self.get_flex_lines_scratch().is_empty());
195 debug_assert!(self.get_mut_translate_scratch().is_empty());
196 {
197 let Self { split_cache, .. } = self;
198 split_cache.mark_synced(root_id, tree.get_revision());
199 }
200 self.set_last_stats(stats);
201 let invalidations: u64 = tree.get_layout_invalidations();
202 let changed_now: u64 = stats.get_nodes_changed() as u64;
203 let telemetry: &mut LayoutTelemetry = self.get_mut_telemetry();
204 telemetry.set_invalidations(invalidations);
205 telemetry.set_passes(telemetry.get_passes().saturating_add(1));
206 if changed_now > 0 {
207 telemetry.set_changed_passes(telemetry.get_changed_passes().saturating_add(1));
208 telemetry.set_changed_nodes_total(
209 telemetry
210 .get_changed_nodes_total()
211 .saturating_add(changed_now),
212 );
213 }
214 stats
215 }
216
217 #[must_use]
227 pub fn layout_telemetry(&self) -> LayoutTelemetry {
228 self.get_telemetry()
229 }
230
231 pub fn reset_telemetry(&mut self) {
237 self.set_telemetry(LayoutTelemetry::default());
238 }
239
240 #[must_use]
250 pub fn split_cache_stats(&self) -> SplitCacheStats {
251 let Self {
252 last_split_stats, ..
253 } = self;
254 *last_split_stats
255 }
256
257 #[must_use]
268 pub fn subtree_bounds(&self, node_id: NodeId) -> Option<Rect> {
269 let Self { split_cache, .. } = self;
270 split_cache.subtree_bounds_for(node_id)
271 }
272
273 #[must_use]
285 pub fn hit_test(&self, tree: &SceneTree, point: Point) -> Option<NodeId> {
286 let mut visited: usize = 0;
287 let Self { split_cache, .. } = self;
288 hit_test_with_cache(tree, point, Some(split_cache), &mut visited)
289 }
290
291 #[must_use]
303 pub fn hit_test_with_visited(&self, tree: &SceneTree, point: Point) -> (Option<NodeId>, usize) {
304 let mut visited: usize = 0;
305 let Self { split_cache, .. } = self;
306 let hit: Option<NodeId> = hit_test_with_cache(tree, point, Some(split_cache), &mut visited);
307 (hit, visited)
308 }
309
310 #[inline]
318 fn record_layout_snapshot(&mut self, tree: &SceneTree, node_id: NodeId) {
319 let Self {
320 track_damage,
321 damage_fallback,
322 layout_snapshots,
323 split_cache,
324 ..
325 } = self;
326 if !*track_damage || *damage_fallback != LAYOUT_FALLBACK_NONE {
327 return;
328 }
329 if split_cache.is_snapshot_seen(node_id) {
330 return;
331 }
332 if layout_snapshots.len() >= DIRTY_PAINT_FULL_THRESHOLD {
333 *damage_fallback = LAYOUT_FALLBACK_OVERFLOW;
334 return;
335 }
336 let Some(node) = tree.get_node(node_id) else {
337 return;
338 };
339 let lbox: LayoutBox = node.get_layout_box();
340 let old_bounds: Rect = lbox.get_bounds();
341 let old_content_bounds: Rect = lbox.get_content_bounds();
342 let old_content_size: Size = lbox.get_content_size();
343 let (has_old_screen, old_screen): (bool, Rect) =
344 if old_bounds.get_width() > 0.0 && old_bounds.get_height() > 0.0 {
345 match tree.node_conservative_logical_rect(node_id) {
346 Some(rect) => (true, rect),
347 None => {
348 *damage_fallback = LAYOUT_FALLBACK_UNSAFE;
349 return;
350 }
351 }
352 } else {
353 (false, Rect::default())
354 };
355 split_cache.mark_snapshot_seen(node_id);
356 layout_snapshots.push(LayoutNodeSnapshot {
357 id: node_id,
358 old_bounds,
359 old_content_bounds,
360 old_content_size,
361 has_old_screen,
362 old_screen,
363 });
364 }
365
366 #[inline]
377 fn rect_approx_eq(a: Rect, b: Rect) -> bool {
378 (a.get_x() - b.get_x()).abs() <= 0.001
379 && (a.get_y() - b.get_y()).abs() <= 0.001
380 && (a.get_width() - b.get_width()).abs() <= 0.001
381 && (a.get_height() - b.get_height()).abs() <= 0.001
382 }
383
384 #[inline(always)]
394 fn is_viewport_length(length: Length) -> bool {
395 matches!(length, Length::ViewportWidth(_)) || matches!(length, Length::ViewportHeight(_))
396 }
397
398 #[inline]
409 fn size_approx_eq(a: Size, b: Size) -> bool {
410 (a.get_width() - b.get_width()).abs() <= 0.001
411 && (a.get_height() - b.get_height()).abs() <= 0.001
412 }
413
414 fn measure_node_text_cached(
429 &mut self,
430 node_id: NodeId,
431 text: &str,
432 style: TextStyle,
433 max_width: Option<f32>,
434 dirty_text: bool,
435 ) -> TextSizeMeasurement {
436 let measurer: DeterministicTextMeasurer = self.get_measurer();
437 let Self {
438 text_size_cache,
439 split_cache,
440 last_split_stats,
441 ..
442 } = self;
443 let Some((hash_a, hash_b, text_len, reused_hash)) =
444 split_cache.text_hash_for_node(node_id, text, dirty_text)
445 else {
446 let measured: Size = measurer.measure_size(text, style, max_width);
447 return TextSizeMeasurement {
448 size: measured,
449 unwrapped: false,
450 max_content_w: f32::INFINITY,
451 cache_hit: false,
452 };
453 };
454 if reused_hash {
455 last_split_stats.text_hash_reuses = last_split_stats.text_hash_reuses.saturating_add(1);
456 }
457 let result: TextSizeMeasurement = text_size_cache.get_or_measure_hashed(
458 text,
459 (hash_a, hash_b, text_len),
460 style,
461 max_width,
462 |raw: &str, text_style: TextStyle, limit: Option<f32>| {
463 measurer.measure_size(raw, text_style, limit)
464 },
465 );
466 if result.cache_hit {
467 last_split_stats.text_interval_hits =
468 last_split_stats.text_interval_hits.saturating_add(1);
469 }
470 result
471 }
472
473 fn layout_node_mode(
488 &mut self,
489 tree: &mut SceneTree,
490 node_id: NodeId,
491 space: AvailableSpace,
492 mode: LayoutRunMode,
493 stats: &mut LayoutStats,
494 ) -> (Size, u8, f32) {
495 let is_dirty: bool = match tree.get_node(node_id) {
496 Some(node) => node.get_dirty().intersects(DIRTY_LAYOUT_PASS_MASK),
497 None => return (Size::default(), LAYOUT_DEP_NONE, 0.0),
498 };
499 let (slot_idx, content_gen): (usize, u32) = {
500 let Self { split_cache, .. } = self;
501 split_cache.prepare_node(node_id, is_dirty)
502 };
503 if mode == LayoutRunMode::ComputeSize {
504 let measure_hit: Option<(Size, u8, f32)> = {
505 let Self { split_cache, .. } = self;
506 split_cache.lookup_measure(slot_idx, space, content_gen)
507 };
508 if let Some(hit) = measure_hit {
509 let Self {
510 last_split_stats, ..
511 } = self;
512 last_split_stats.measure_hits = last_split_stats.measure_hits.saturating_add(1);
513 stats.set_subtrees_skipped(stats.get_subtrees_skipped() + 1);
514 return hit;
515 }
516 if !is_dirty {
517 let final_hit: Option<FinalLayoutSlot> = {
518 let Self { split_cache, .. } = self;
519 split_cache.lookup_final(slot_idx, content_gen)
520 };
521 if let Some(fslot) = final_hit
522 && !fslot.hidden
523 && fslot.space.can_reuse(space)
524 {
525 let Self {
526 last_split_stats, ..
527 } = self;
528 last_split_stats.measure_hits = last_split_stats.measure_hits.saturating_add(1);
529 stats.set_subtrees_skipped(stats.get_subtrees_skipped() + 1);
530 return (fslot.size, fslot.dep_flags, fslot.min_avail_w);
531 }
532 }
533 {
534 let Self {
535 last_split_stats, ..
536 } = self;
537 last_split_stats.measure_misses = last_split_stats.measure_misses.saturating_add(1);
538 }
539 stats.set_nodes_computed(stats.get_nodes_computed() + 1);
540 } else {
541 if !is_dirty {
542 let final_hit: Option<FinalLayoutSlot> = {
543 let Self { split_cache, .. } = self;
544 split_cache.lookup_final(slot_idx, content_gen)
545 };
546 if let Some(fslot) = final_hit
547 && fslot.space.can_reuse(space)
548 {
549 if fslot.hidden {
550 let Self {
551 last_split_stats, ..
552 } = self;
553 last_split_stats.final_hits = last_split_stats.final_hits.saturating_add(1);
554 stats.set_subtrees_skipped(stats.get_subtrees_skipped() + 1);
555 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
556 }
557 if !fslot.space.same_origin(space) {
558 let dx: f32 = space.get_origin_x() - fslot.space.get_origin_x();
559 let dy: f32 = space.get_origin_y() - fslot.space.get_origin_y();
560 self.translate_subtree(tree, node_id, dx, dy);
561 if dx.abs() > 0.001 || dy.abs() > 0.001 {
562 stats.set_nodes_changed(stats.get_nodes_changed().saturating_add(1));
563 }
564 }
565 if fslot.post_stretched {
566 let restored_opt: Option<(Rect, Rect, Size)> = {
567 let Self { split_cache, .. } = self;
568 split_cache.lookup_final(slot_idx, content_gen).map(
569 |s: FinalLayoutSlot| (s.bounds, s.content_bounds, s.content_size),
570 )
571 };
572 if let Some((r_bounds, r_content, r_csize)) = restored_opt {
573 self.record_layout_snapshot(tree, node_id);
574 if let Some(node) = tree.get_node_mut(node_id) {
575 node.get_mut_layout_box().set_bounds(r_bounds);
576 node.get_mut_layout_box().set_content_bounds(r_content);
577 node.get_mut_layout_box().set_content_size(r_csize);
578 }
579 let Self { split_cache, .. } = self;
580 if let Some(slot_mut) = split_cache.final_slots[slot_idx].as_mut() {
581 slot_mut.post_stretched = false;
582 }
583 }
584 }
585 let Self {
586 last_split_stats, ..
587 } = self;
588 last_split_stats.final_hits = last_split_stats.final_hits.saturating_add(1);
589 stats.set_subtrees_skipped(stats.get_subtrees_skipped() + 1);
590 return (fslot.size, fslot.dep_flags, fslot.min_avail_w);
591 }
592 }
593 {
594 let Self {
595 last_split_stats, ..
596 } = self;
597 last_split_stats.final_misses = last_split_stats.final_misses.saturating_add(1);
598 }
599 stats.set_nodes_computed(stats.get_nodes_computed() + 1);
600 }
601 let Some(node) = tree.get_node(node_id) else {
602 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
603 };
604 let style_ref: &Style = node.style_ref();
605 let Style {
606 layout: layout_ref,
607 paint: paint_ref,
608 text: text_ref,
609 } = style_ref;
610 if layout_ref.get_display() == Display::None {
611 {
612 let Self {
613 last_split_stats, ..
614 } = self;
615 last_split_stats.display_none_skips =
616 last_split_stats.display_none_skips.saturating_add(1);
617 }
618 if mode == LayoutRunMode::ComputeSize {
619 let Self { split_cache, .. } = self;
620 split_cache.store_measure(
621 slot_idx,
622 space,
623 LayoutRunMode::ComputeSize,
624 content_gen,
625 (Size::default(), LAYOUT_DEP_NONE, 0.0),
626 );
627 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
628 }
629 self.record_layout_snapshot(tree, node_id);
630 let (prev_content_size, had_subtree_dirty): (Size, bool) =
631 if let Some(hidden_node) = tree.get_node_mut(node_id) {
632 let old_b: Rect = hidden_node.get_layout_box().get_bounds();
633 let csize: Size = hidden_node.get_layout_box().get_content_size();
634 let sub_dirty: bool = hidden_node.get_dirty().intersects(DIRTY_SUBTREE_LAYOUT);
635 if !Self::rect_approx_eq(old_b, Rect::default()) {
636 stats.set_nodes_changed(stats.get_nodes_changed().saturating_add(1));
637 }
638 hidden_node.get_mut_layout_box().set_bounds(Rect::default());
639 hidden_node
640 .get_mut_layout_box()
641 .set_content_bounds(Rect::default());
642 hidden_node.set_last_constraint(Some(space));
643 hidden_node.get_mut_dirty().remove(DIRTY_LAYOUT_PASS_MASK);
644 (csize, sub_dirty)
645 } else {
646 (Size::default(), false)
647 };
648 if let Some(meta) = tree.get_mut_meta().get_mut(node_id.as_usize()) {
649 let next_dirty: u16 = meta.get_dirty() & !DIRTY_LAYOUT_PASS_MASK;
650 let _: &mut NodeMeta = meta.set_dirty(next_dirty);
651 }
652 if had_subtree_dirty {
653 tree.set_has_stale_hidden_dirty(true);
654 }
655 let Self { split_cache, .. } = self;
656 split_cache.store_final(
657 slot_idx,
658 FinalLayoutSlot {
659 space,
660 subtree_gen: content_gen,
661 size: Size::default(),
662 bounds: Rect::default(),
663 content_bounds: Rect::default(),
664 content_size: prev_content_size,
665 dep_flags: LAYOUT_DEP_NONE,
666 min_avail_w: 0.0,
667 post_stretched: false,
668 hidden: true,
669 },
670 );
671 split_cache.store_subtree_meta(slot_idx, Rect::default(), false);
672 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
673 }
674 let w_len: Length = layout_ref.get_width();
675 let h_len: Length = layout_ref.get_height();
676 let min_w_len: Length = layout_ref.get_min_width();
677 let max_w_len: Length = layout_ref.get_max_width();
678 let min_h_len: Length = layout_ref.get_min_height();
679 let max_h_len: Length = layout_ref.get_max_height();
680 let mut own_deps: u8 = LAYOUT_DEP_NONE;
681 if matches!(w_len, Length::Percent(_))
682 || matches!(min_w_len, Length::Percent(_))
683 || matches!(max_w_len, Length::Percent(_))
684 {
685 own_deps |= LAYOUT_DEP_AVAIL_W;
686 }
687 if matches!(h_len, Length::Percent(_))
688 || matches!(min_h_len, Length::Percent(_))
689 || matches!(max_h_len, Length::Percent(_))
690 {
691 own_deps |= LAYOUT_DEP_AVAIL_H;
692 }
693 if Self::is_viewport_length(w_len)
694 || Self::is_viewport_length(h_len)
695 || Self::is_viewport_length(min_w_len)
696 || Self::is_viewport_length(max_w_len)
697 || Self::is_viewport_length(min_h_len)
698 || Self::is_viewport_length(max_h_len)
699 {
700 own_deps |= LAYOUT_DEP_VIEWPORT;
701 }
702 let padding: Edges = layout_ref.get_padding();
703 let border_w: f32 = paint_ref.get_border().get_width();
704 let node_transform: Transform2D = paint_ref.get_transform();
705 let node_scroll: Point = node.get_layout_box().get_scroll_offset();
706 let self_is_portal: bool = matches!(node.get_kind(), NodeKind::Portal)
707 || layout_ref.get_position() == Position::Fixed;
708 let chrome_h: f32 = padding.horizontal() + border_w * 2.0;
709 let chrome_v: f32 = padding.vertical() + border_w * 2.0;
710 let explicit_w: Option<f32> = w_len.resolve(space.get_width(), space.get_viewport());
711 let explicit_h: Option<f32> = h_len.resolve(space.get_height(), space.get_viewport());
712 let mut resolved_w: Option<f32> = explicit_w;
713 let mut resolved_h: Option<f32> = explicit_h;
714 let stretched_w: bool = resolved_w.is_none() && space.get_stretch_width();
715 let stretched_h: bool = resolved_h.is_none() && space.get_stretch_height();
716 if stretched_w {
717 resolved_w = Some(space.get_width());
718 own_deps |= LAYOUT_DEP_AVAIL_W;
719 }
720 if stretched_h {
721 resolved_h = Some(space.get_height());
722 own_deps |= LAYOUT_DEP_AVAIL_H;
723 }
724 let min_w: f32 = min_w_len
725 .resolve(space.get_width(), space.get_viewport())
726 .unwrap_or(0.0);
727 let min_h: f32 = min_h_len
728 .resolve(space.get_height(), space.get_viewport())
729 .unwrap_or(0.0);
730 let max_w: f32 = max_w_len
731 .resolve(space.get_width(), space.get_viewport())
732 .unwrap_or(f32::INFINITY)
733 .max(min_w);
734 let max_h: f32 = max_h_len
735 .resolve(space.get_height(), space.get_viewport())
736 .unwrap_or(f32::INFINITY)
737 .max(min_h);
738 let aspect_ratio: Option<f32> = layout_ref.aspect_ratio_value();
739 if let Some(ratio) = aspect_ratio
740 && ratio > 0.0
741 {
742 if let Some(w) = resolved_w
743 && resolved_h.is_none()
744 {
745 resolved_h = Some(w.clamp(min_w, max_w) / ratio);
746 } else if let Some(h) = resolved_h
747 && resolved_w.is_none()
748 {
749 resolved_w = Some(h.clamp(min_h, max_h) * ratio);
750 }
751 }
752 let node_overflow: Overflow = layout_ref.get_overflow();
753 let node_flex_grow: f32 = layout_ref.get_flex_grow();
754 let width_independent: bool = explicit_w.is_some()
755 || (stretched_w
756 && resolved_w.is_some()
757 && (node_overflow != Overflow::Visible || node_flex_grow > 0.0));
758 let height_independent: bool = resolved_h.is_some() && h_len != Length::Auto;
759 if mode == LayoutRunMode::ComputeSize && width_independent && resolved_h.is_some() {
760 let final_w: f32 = resolved_w.unwrap_or(0.0).clamp(min_w, max_w).max(0.0);
761 let final_h: f32 = resolved_h.unwrap_or(0.0).clamp(min_h, max_h).max(0.0);
762 let final_size: Size = Size::new(final_w, final_h);
763 let Self { split_cache, .. } = self;
764 split_cache.store_measure(
765 slot_idx,
766 space,
767 LayoutRunMode::ComputeSize,
768 content_gen,
769 (final_size, own_deps, 0.0),
770 );
771 return (final_size, own_deps, 0.0);
772 }
773 let avail_w_outer: f32 = resolved_w
774 .unwrap_or(space.get_width())
775 .max(min_w)
776 .min(max_w);
777 let avail_h_outer: f32 = resolved_h
778 .unwrap_or(space.get_height())
779 .max(min_h)
780 .min(max_h);
781 let inner_avail_w: f32 = (avail_w_outer - chrome_h).max(0.0);
782 let inner_avail_h: f32 = (avail_h_outer - chrome_v).max(0.0);
783 let origin_x: f32 = space.get_origin_x();
784 let origin_y: f32 = space.get_origin_y();
785 let content_origin_x: f32 = origin_x + padding.get_left() + border_w;
786 let content_origin_y: f32 = origin_y + padding.get_top() + border_w;
787 let shields_avail_w: bool = resolved_w.is_some() && (own_deps & LAYOUT_DEP_AVAIL_W) == 0;
788 let shields_avail_h: bool = resolved_h.is_some() && (own_deps & LAYOUT_DEP_AVAIL_H) == 0;
789 let skip_leaf_measure: bool =
790 node_overflow != Overflow::Scroll && width_independent && height_independent;
791 let dirty_text: bool = node.get_dirty().intersects(DIRTY_TEXT);
792 let mut child_range: Option<(usize, usize)> = None;
793 let mut child_deps: u8 = LAYOUT_DEP_NONE;
794 let mut child_min_aw: f32 = 0.0;
795 let leaf_content_size: Option<Size> = match node.get_kind() {
796 NodeKind::Text(text) => {
797 if skip_leaf_measure {
798 Some(Size::new(inner_avail_w, inner_avail_h))
799 } else {
800 let measured: TextSizeMeasurement = self.measure_node_text_cached(
801 node_id,
802 text.as_str(),
803 *text_ref,
804 Some(inner_avail_w.max(1.0)),
805 dirty_text,
806 );
807 if text_ref.get_overflow() != TextOverflow::Clip {
808 if measured.unwrapped && measured.max_content_w.is_finite() {
809 child_min_aw = measured.max_content_w + 0.5;
810 } else {
811 child_deps |= LAYOUT_DEP_AVAIL_W;
812 }
813 }
814 Some(measured.size)
815 }
816 }
817 NodeKind::Image(img) => {
818 Some(Size::new(img.get_width() as f32, img.get_height() as f32))
819 }
820 NodeKind::Input(input_state) => {
821 if skip_leaf_measure {
822 Some(Size::new(inner_avail_w, inner_avail_h))
823 } else {
824 let sample: &str = if input_state.get_value().is_empty() {
825 input_state.get_placeholder().as_str()
826 } else {
827 input_state.get_value().as_str()
828 };
829 let measured: TextSizeMeasurement = self.measure_node_text_cached(
830 node_id,
831 sample,
832 *text_ref,
833 Some(inner_avail_w.max(1.0)),
834 dirty_text,
835 );
836 if text_ref.get_overflow() != TextOverflow::Clip {
837 if measured.unwrapped && measured.max_content_w.is_finite() {
838 child_min_aw = measured.max_content_w + 0.5;
839 } else {
840 child_deps |= LAYOUT_DEP_AVAIL_W;
841 }
842 }
843 Some(Size::new(
844 measured.size.get_width().max(120.0),
845 measured.size.get_height().max(20.0),
846 ))
847 }
848 }
849 NodeKind::View
850 | NodeKind::ScrollView
851 | NodeKind::Portal
852 | NodeKind::Fragment
853 | NodeKind::Canvas(_) => {
854 if !tree.has_children(node_id) {
855 Some(Size::default())
856 } else {
857 let start: usize = self.get_children_scratch().len();
858 self.get_mut_children_scratch()
859 .extend(tree.children_iter(node_id));
860 let end: usize = self.get_children_scratch().len();
861 child_range = Some((start, end));
862 None
863 }
864 }
865 };
866 let parent_layout_copy: LayoutStyle = *layout_ref;
867 let content_size: Size = if let Some(size) = leaf_content_size {
868 size
869 } else if let Some((child_start, child_end)) = child_range {
870 let frame: ContainerFrame = ContainerFrame::new(
871 parent_layout_copy,
872 Size::new(inner_avail_w, inner_avail_h),
873 space.get_viewport(),
874 Point::new(content_origin_x, content_origin_y),
875 resolved_w.is_some(),
876 resolved_h.is_some(),
877 );
878 let (size, c_deps, c_min_aw): (Size, u8, f32) =
879 self.layout_children(tree, (child_start, child_end), frame, mode, stats);
880 child_deps = c_deps;
881 child_min_aw = c_min_aw;
882 if mode == LayoutRunMode::ComputeSize {
883 self.get_mut_children_scratch().truncate(child_start);
884 }
885 size
886 } else {
887 Size::default()
888 };
889 let content_outer_w: f32 = content_size.get_width() + chrome_h;
890 let content_outer_h: f32 = content_size.get_height() + chrome_v;
891 let mut final_w: f32 = match (explicit_w, stretched_w) {
892 (Some(w), _) => w,
893 (None, true) => {
894 let base_w: f32 = resolved_w.unwrap_or(content_outer_w);
895 if node_overflow == Overflow::Visible
896 && node_flex_grow <= 0.0
897 && content_outer_w > base_w + 0.5
898 {
899 content_outer_w
900 } else {
901 base_w
902 }
903 }
904 (None, false) => content_outer_w,
905 };
906 let mut final_h: f32 = resolved_h.unwrap_or(content_outer_h);
907 if let Some(ratio) = aspect_ratio
908 && ratio > 0.0
909 {
910 if resolved_w.is_some() && resolved_h.is_none() {
911 final_h = final_w / ratio;
912 } else if resolved_h.is_some() && resolved_w.is_none() {
913 final_w = final_h * ratio;
914 }
915 }
916 final_w = final_w.clamp(min_w, max_w).max(0.0);
917 final_h = final_h.clamp(min_h, max_h).max(0.0);
918 let final_size: Size = Size::new(final_w, final_h);
919 let mut final_deps: u8 = own_deps | (child_deps & LAYOUT_DEP_VIEWPORT);
920 let mut final_min_aw: f32 = 0.0;
921 if !shields_avail_w {
922 if (child_deps & LAYOUT_DEP_AVAIL_W) != 0 {
923 final_deps |= LAYOUT_DEP_AVAIL_W;
924 } else if child_min_aw > 0.0 {
925 final_min_aw = child_min_aw + chrome_h;
926 }
927 }
928 if !shields_avail_h {
929 final_deps |= child_deps & LAYOUT_DEP_AVAIL_H;
930 }
931 if mode == LayoutRunMode::ComputeSize {
932 let Self { split_cache, .. } = self;
933 split_cache.store_measure(
934 slot_idx,
935 space,
936 LayoutRunMode::ComputeSize,
937 content_gen,
938 (final_size, final_deps, final_min_aw),
939 );
940 return (final_size, final_deps, final_min_aw);
941 }
942 let bounds: Rect = Rect::new(origin_x, origin_y, final_w, final_h);
943 let content_bounds: Rect = Rect::new(
944 content_origin_x,
945 content_origin_y,
946 (final_w - chrome_h).max(0.0),
947 (final_h - chrome_v).max(0.0),
948 );
949 self.record_layout_snapshot(tree, node_id);
950 if let Some(node) = tree.get_node_mut(node_id) {
951 let prev_box: LayoutBox = node.get_layout_box();
952 let changed: bool = !Self::rect_approx_eq(prev_box.get_bounds(), bounds)
953 || !Self::rect_approx_eq(prev_box.get_content_bounds(), content_bounds)
954 || !Self::size_approx_eq(prev_box.get_content_size(), content_size);
955 if changed {
956 stats.set_nodes_changed(stats.get_nodes_changed().saturating_add(1));
957 }
958 node.get_mut_layout_box().set_bounds(bounds);
959 node.get_mut_layout_box().set_content_bounds(content_bounds);
960 node.get_mut_layout_box().set_content_size(content_size);
961 node.set_last_constraint(Some(space));
962 node.get_mut_dirty().remove(DIRTY_LAYOUT_PASS_MASK);
963 }
964 if let Some(meta) = tree.get_mut_meta().get_mut(node_id.as_usize()) {
965 let next_dirty: u16 = meta.get_dirty() & !DIRTY_LAYOUT_PASS_MASK;
966 let _: &mut NodeMeta = meta.set_dirty(next_dirty);
967 }
968 let own_vis: Rect = shifted_node_bounds(bounds, Point::default(), node_transform);
969 let is_clipped: bool = matches!(node_overflow, Overflow::Hidden | Overflow::Scroll);
970 let mut sub_bounds: Rect = own_vis;
971 let mut has_nonzero_z: bool = false;
972 if let Some((c_start, c_end)) = child_range {
973 let tx: f32 = node_transform.get_translate_x() - node_scroll.get_x();
974 let ty: f32 = node_transform.get_translate_y() - node_scroll.get_y();
975 for idx in c_start..c_end {
976 let cid: NodeId = self.get_children_scratch()[idx];
977 if let Some(cnode) = tree.get_node(cid) {
978 let clayout: &LayoutStyle = cnode.style_ref().layout_ref();
979 if clayout.get_z_index() != 0 {
980 has_nonzero_z = true;
981 }
982 let c_is_portal: bool = matches!(cnode.get_kind(), NodeKind::Portal)
983 || clayout.get_position() == Position::Fixed;
984 if !is_clipped
985 && clayout.get_display() != Display::None
986 && (self_is_portal || !c_is_portal)
987 {
988 let child_sb: Rect = {
989 let Self { split_cache, .. } = self;
990 split_cache.subtree_bounds_for(cid)
991 }
992 .unwrap_or_else(|| {
993 shifted_node_bounds(
994 cnode.get_layout_box().get_bounds(),
995 Point::default(),
996 cnode.style_ref().paint_ref().get_transform(),
997 )
998 });
999 if child_sb.get_width() > 0.0 && child_sb.get_height() > 0.0 {
1000 let shifted_child: Rect = Rect::new(
1001 child_sb.get_x() + tx,
1002 child_sb.get_y() + ty,
1003 child_sb.get_width(),
1004 child_sb.get_height(),
1005 );
1006 sub_bounds = sub_bounds.union(shifted_child);
1007 }
1008 }
1009 }
1010 }
1011 self.get_mut_children_scratch().truncate(c_start);
1012 }
1013 let Self { split_cache, .. } = self;
1014 split_cache.store_final(
1015 slot_idx,
1016 FinalLayoutSlot {
1017 space,
1018 subtree_gen: content_gen,
1019 size: final_size,
1020 bounds,
1021 content_bounds,
1022 content_size,
1023 dep_flags: final_deps,
1024 min_avail_w: final_min_aw,
1025 post_stretched: false,
1026 hidden: false,
1027 },
1028 );
1029 if split_cache
1030 .lookup_measure(slot_idx, space, content_gen)
1031 .is_none()
1032 {
1033 split_cache.store_measure(
1034 slot_idx,
1035 space,
1036 LayoutRunMode::PerformLayout,
1037 content_gen,
1038 (final_size, final_deps, final_min_aw),
1039 );
1040 }
1041 split_cache.store_subtree_meta(slot_idx, sub_bounds, has_nonzero_z);
1042 (final_size, final_deps, final_min_aw)
1043 }
1044
1045 fn layout_children(
1060 &mut self,
1061 tree: &mut SceneTree,
1062 child_range: (usize, usize),
1063 frame: ContainerFrame,
1064 mode: LayoutRunMode,
1065 stats: &mut LayoutStats,
1066 ) -> (Size, u8, f32) {
1067 let (child_start, child_end): (usize, usize) = child_range;
1068 let child_count: usize = child_end.saturating_sub(child_start);
1069 if child_count == 0 {
1070 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
1071 }
1072 let parent_layout: LayoutStyle = frame.get_parent_layout();
1073 let inner_avail: Size = frame.get_inner_avail();
1074 let viewport: Size = frame.get_viewport();
1075 let content_origin: Point = frame.get_content_origin();
1076 let flex_dir_opt: Option<FlexDirection> = match parent_layout.get_display() {
1077 Display::Grid => None,
1078 Display::Block => Some(FlexDirection::Column),
1079 Display::Flex | Display::None => Some(parent_layout.get_flex_direction()),
1080 };
1081 let is_row_dir: bool = matches!(
1082 flex_dir_opt,
1083 Some(FlexDirection::Row | FlexDirection::RowReverse)
1084 );
1085 let is_reverse_dir: bool = matches!(
1086 flex_dir_opt,
1087 Some(FlexDirection::RowReverse | FlexDirection::ColumnReverse)
1088 );
1089 let parent_align: AlignItems = parent_layout.get_align_items();
1090 let parent_height_auto: bool = parent_layout.get_height() == Length::Auto;
1091 let mut single_pass_ok: bool = flex_dir_opt.is_some()
1092 && parent_layout.get_flex_wrap() != FlexWrap::Wrap
1093 && !is_reverse_dir
1094 && parent_layout.get_justify_content() == JustifyContent::Start;
1095 let flow_start: usize = self.get_flow_scratch().len();
1096 let abs_start: usize = self.get_abs_scratch().len();
1097 for idx in child_start..child_end {
1098 let child_id: NodeId = self.get_children_scratch()[idx];
1099 if let Some(child_node) = tree.get_node(child_id) {
1100 let Style {
1101 layout: child_layout,
1102 ..
1103 } = child_node.style_ref();
1104 if child_layout.get_display() == Display::None {
1105 if mode == LayoutRunMode::PerformLayout {
1106 let _: (Size, u8, f32) = self.layout_node_mode(
1107 tree,
1108 child_id,
1109 AvailableSpace::new(0.0, 0.0, viewport, 0.0, 0.0),
1110 LayoutRunMode::PerformLayout,
1111 stats,
1112 );
1113 }
1114 continue;
1115 }
1116 match child_layout.get_position() {
1117 Position::Relative => {
1118 if single_pass_ok {
1119 if child_layout.get_flex_grow() > 0.0 {
1120 single_pass_ok = false;
1121 } else {
1122 let align: AlignItems = match child_layout.get_align_self() {
1123 AlignSelf::Auto => parent_align,
1124 AlignSelf::Stretch => AlignItems::Stretch,
1125 AlignSelf::Start => AlignItems::Start,
1126 AlignSelf::Center => AlignItems::Center,
1127 AlignSelf::End => AlignItems::End,
1128 };
1129 let non_start_align: bool =
1130 matches!(align, AlignItems::Center | AlignItems::End);
1131 let auto_row_stretch: bool = align == AlignItems::Stretch
1132 && is_row_dir
1133 && child_layout.get_height() == Length::Auto
1134 && parent_height_auto;
1135 if non_start_align || auto_row_stretch {
1136 single_pass_ok = false;
1137 }
1138 }
1139 }
1140 self.get_mut_flow_scratch().push(child_id);
1141 }
1142 Position::Absolute | Position::Fixed => {
1143 if mode == LayoutRunMode::PerformLayout {
1144 self.get_mut_abs_scratch().push(child_id);
1145 }
1146 }
1147 }
1148 }
1149 }
1150 let flow_end: usize = self.get_flow_scratch().len();
1151 let abs_end: usize = self.get_abs_scratch().len();
1152 let flow_result: (Size, u8, f32) = if let Some(flex_dir) = flex_dir_opt {
1153 self.layout_flex(
1154 tree,
1155 (flow_start, flow_end),
1156 frame,
1157 flex_dir,
1158 (single_pass_ok, mode),
1159 stats,
1160 )
1161 } else {
1162 self.layout_grid(tree, (flow_start, flow_end), frame, mode, stats)
1163 };
1164 self.get_mut_flow_scratch().truncate(flow_start);
1165 if mode == LayoutRunMode::PerformLayout {
1166 for abs_idx in abs_start..abs_end {
1167 let abs_id: NodeId = self.get_abs_scratch()[abs_idx];
1168 let child_layout_opt: Option<LayoutStyle> = tree
1169 .get_node(abs_id)
1170 .map(|node: &SceneNode| node.style_ref().get_layout());
1171 let Some(child_layout) = child_layout_opt else {
1172 continue;
1173 };
1174 let is_fixed: bool = child_layout.get_position() == Position::Fixed;
1175 let ref_origin: Point = if is_fixed {
1176 Point::new(0.0, 0.0)
1177 } else {
1178 content_origin
1179 };
1180 let ref_size: Size = if is_fixed { viewport } else { inner_avail };
1181 let resolved_w: Option<f32> = child_layout
1182 .get_width()
1183 .resolve(ref_size.get_width(), viewport);
1184 let resolved_h: Option<f32> = child_layout
1185 .get_height()
1186 .resolve(ref_size.get_height(), viewport);
1187 let needs_intrinsic: bool = (resolved_w.is_none()
1188 && child_layout.left_offset().is_none()
1189 && child_layout.right_offset().is_some())
1190 || (resolved_h.is_none()
1191 && child_layout.top_offset().is_none()
1192 && child_layout.bottom_offset().is_some());
1193 let probed: Size = if needs_intrinsic {
1194 let probe_space: AvailableSpace = AvailableSpace::new(
1195 ref_size.get_width(),
1196 ref_size.get_height(),
1197 viewport,
1198 ref_origin.get_x(),
1199 ref_origin.get_y(),
1200 );
1201 self.layout_node_mode(
1202 tree,
1203 abs_id,
1204 probe_space,
1205 LayoutRunMode::ComputeSize,
1206 stats,
1207 )
1208 .0
1209 } else {
1210 Size::new(ref_size.get_width(), ref_size.get_height())
1211 };
1212 let child_w: f32 = resolved_w.unwrap_or(probed.get_width());
1213 let child_h: f32 = resolved_h.unwrap_or(probed.get_height());
1214 let x: f32 = if let Some(left) = child_layout.left_offset() {
1215 ref_origin.get_x() + left
1216 } else if let Some(right) = child_layout.right_offset() {
1217 ref_origin.get_x() + (ref_size.get_width() - child_w - right)
1218 } else {
1219 ref_origin.get_x()
1220 };
1221 let y: f32 = if let Some(top) = child_layout.top_offset() {
1222 ref_origin.get_y() + top
1223 } else if let Some(bottom) = child_layout.bottom_offset() {
1224 ref_origin.get_y() + (ref_size.get_height() - child_h - bottom)
1225 } else {
1226 ref_origin.get_y()
1227 };
1228 let abs_space: AvailableSpace = AvailableSpace::new(
1229 ref_size.get_width(),
1230 ref_size.get_height(),
1231 viewport,
1232 x,
1233 y,
1234 );
1235 let _: (Size, u8, f32) = self.layout_node_mode(
1236 tree,
1237 abs_id,
1238 abs_space,
1239 LayoutRunMode::PerformLayout,
1240 stats,
1241 );
1242 }
1243 self.get_mut_abs_scratch().truncate(abs_start);
1244 }
1245 flow_result
1246 }
1247
1248 fn layout_flex(
1264 &mut self,
1265 tree: &mut SceneTree,
1266 flow_range: (usize, usize),
1267 frame: ContainerFrame,
1268 direction: FlexDirection,
1269 mode_flags: (bool, LayoutRunMode),
1270 stats: &mut LayoutStats,
1271 ) -> (Size, u8, f32) {
1272 let (flow_start, flow_end): (usize, usize) = flow_range;
1273 let (single_pass_ok, mode): (bool, LayoutRunMode) = mode_flags;
1274 let count_all: usize = flow_end.saturating_sub(flow_start);
1275 if count_all == 0 {
1276 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
1277 }
1278 let parent_layout: LayoutStyle = frame.get_parent_layout();
1279 let inner_avail: Size = frame.get_inner_avail();
1280 let viewport: Size = frame.get_viewport();
1281 let content_origin: Point = frame.get_content_origin();
1282 let is_row: bool = matches!(direction, FlexDirection::Row | FlexDirection::RowReverse);
1283 let is_reverse: bool = matches!(
1284 direction,
1285 FlexDirection::RowReverse | FlexDirection::ColumnReverse
1286 );
1287 let main_gap: f32 = if is_row {
1288 parent_layout.get_column_gap()
1289 } else {
1290 parent_layout.get_row_gap()
1291 };
1292 let cross_gap: f32 = if is_row {
1293 parent_layout.get_row_gap()
1294 } else {
1295 parent_layout.get_column_gap()
1296 };
1297 let main_avail: f32 = if is_row {
1298 inner_avail.get_width()
1299 } else {
1300 inner_avail.get_height()
1301 };
1302 let cross_avail: f32 = if is_row {
1303 inner_avail.get_height()
1304 } else {
1305 inner_avail.get_width()
1306 };
1307 if is_reverse {
1308 self.get_mut_flow_scratch()[flow_start..flow_end].reverse();
1309 }
1310 let wrap_enabled: bool = parent_layout.get_flex_wrap() == FlexWrap::Wrap;
1311 if single_pass_ok {
1312 let parent_align: AlignItems = parent_layout.get_align_items();
1313 let mut main_cursor: f32 = 0.0;
1314 let mut max_cross: f32 = 0.0;
1315 let mut agg_deps: u8 = LAYOUT_DEP_NONE;
1316 let mut agg_min_aw: f32 = 0.0;
1317 let records_start: usize = self.get_flex_items_scratch().len();
1318 for offset in 0..count_all {
1319 let child_id: NodeId = self.get_flow_scratch()[flow_start + offset];
1320 let (margin, align, width_auto, height_auto, c_overflow, c_transform): (
1321 Edges,
1322 AlignItems,
1323 bool,
1324 bool,
1325 Overflow,
1326 Transform2D,
1327 ) = if let Some(node) = tree.get_node(child_id) {
1328 let Style {
1329 layout: child_layout,
1330 paint: child_paint,
1331 ..
1332 } = node.style_ref();
1333 let a: AlignItems = match child_layout.get_align_self() {
1334 AlignSelf::Auto => parent_align,
1335 AlignSelf::Stretch => AlignItems::Stretch,
1336 AlignSelf::Start => AlignItems::Start,
1337 AlignSelf::Center => AlignItems::Center,
1338 AlignSelf::End => AlignItems::End,
1339 };
1340 (
1341 child_layout.get_margin(),
1342 a,
1343 child_layout.get_width() == Length::Auto,
1344 child_layout.get_height() == Length::Auto,
1345 child_layout.get_overflow(),
1346 child_paint.get_transform(),
1347 )
1348 } else {
1349 (
1350 Edges::default(),
1351 parent_align,
1352 true,
1353 true,
1354 Overflow::Visible,
1355 Transform2D::default(),
1356 )
1357 };
1358 let (x, y): (f32, f32) = if is_row {
1359 (
1360 content_origin.get_x() + main_cursor + margin.get_left(),
1361 content_origin.get_y() + margin.get_top(),
1362 )
1363 } else {
1364 (
1365 content_origin.get_x() + margin.get_left(),
1366 content_origin.get_y() + main_cursor + margin.get_top(),
1367 )
1368 };
1369 let stretch_w: bool = !is_row && align == AlignItems::Stretch && width_auto;
1370 let stretch_h: bool = is_row && align == AlignItems::Stretch && height_auto;
1371 if stretch_w {
1372 agg_deps |= LAYOUT_DEP_AVAIL_W;
1373 }
1374 if stretch_h {
1375 agg_deps |= LAYOUT_DEP_AVAIL_H;
1376 }
1377 let space_w: f32 = if stretch_w {
1378 (inner_avail.get_width() - margin.horizontal()).max(0.0)
1379 } else {
1380 inner_avail.get_width()
1381 };
1382 let space_h: f32 = if stretch_h {
1383 (inner_avail.get_height() - margin.vertical()).max(0.0)
1384 } else {
1385 inner_avail.get_height()
1386 };
1387 let final_space: AvailableSpace =
1388 AvailableSpace::new(space_w, space_h, viewport, x, y)
1389 .with_stretch(stretch_w, stretch_h);
1390 let (placed, c_deps, c_min_aw): (Size, u8, f32) =
1391 self.layout_node_mode(tree, child_id, final_space, mode, stats);
1392 agg_deps |= c_deps;
1393 if c_min_aw > 0.0 {
1394 agg_min_aw = agg_min_aw.max(c_min_aw + margin.horizontal());
1395 }
1396 let target_w: f32 = if stretch_w {
1397 space_w.max(placed.get_width())
1398 } else {
1399 placed.get_width()
1400 };
1401 let target_h: f32 = if stretch_h {
1402 space_h.max(placed.get_height())
1403 } else {
1404 placed.get_height()
1405 };
1406 if mode == LayoutRunMode::PerformLayout {
1407 if (stretch_w && target_w > placed.get_width() + 0.01)
1408 || (stretch_h && target_h > placed.get_height() + 0.01)
1409 {
1410 self.record_layout_snapshot(tree, child_id);
1411 let updated_b: Option<Rect> =
1412 if let Some(node) = tree.get_node_mut(child_id) {
1413 let mut b: Rect = node.get_layout_box().get_bounds();
1414 b.set_width(target_w);
1415 b.set_height(target_h);
1416 node.get_mut_layout_box().set_bounds(b);
1417 Some(b)
1418 } else {
1419 None
1420 };
1421 if let Some(b) = updated_b {
1422 let is_clipped: bool =
1423 matches!(c_overflow, Overflow::Hidden | Overflow::Scroll);
1424 let Self { split_cache, .. } = self;
1425 split_cache.mark_post_stretched(child_id, b, c_transform, is_clipped);
1426 }
1427 }
1428 if stretch_w {
1429 self.get_mut_flex_items_scratch().push(FlexItemRecord {
1430 node_id: child_id,
1431 size: Size::new(target_w, target_h),
1432 margin,
1433 flex_grow: 0.0,
1434 align_self: AlignSelf::Stretch,
1435 overflow: c_overflow,
1436 flags: 0,
1437 });
1438 }
1439 }
1440 if is_row {
1441 main_cursor += target_w + margin.horizontal();
1442 max_cross = max_cross.max(target_h + margin.vertical());
1443 } else {
1444 main_cursor += target_h + margin.vertical();
1445 max_cross = max_cross.max(target_w + margin.horizontal());
1446 }
1447 if offset + 1 < count_all {
1448 main_cursor += main_gap;
1449 }
1450 }
1451 let records_end: usize = self.get_flex_items_scratch().len();
1452 if mode == LayoutRunMode::PerformLayout
1453 && records_end > records_start
1454 && max_cross > cross_avail + 0.5
1455 {
1456 for rec_idx in records_start..records_end {
1457 let rec: FlexItemRecord = self.get_flex_items_scratch()[rec_idx];
1458 let widened: f32 =
1459 (max_cross - rec.margin.horizontal()).max(rec.size.get_width());
1460 self.record_layout_snapshot(tree, rec.node_id);
1461 let updated_opt: Option<(Rect, Transform2D)> =
1462 if let Some(node) = tree.get_node_mut(rec.node_id) {
1463 let tr: Transform2D = node.style_ref().paint_ref().get_transform();
1464 let mut b: Rect = node.get_layout_box().get_bounds();
1465 b.set_width(widened);
1466 node.get_mut_layout_box().set_bounds(b);
1467 Some((b, tr))
1468 } else {
1469 None
1470 };
1471 if let Some((b, tr)) = updated_opt {
1472 let is_clipped: bool =
1473 matches!(rec.overflow, Overflow::Hidden | Overflow::Scroll);
1474 let Self { split_cache, .. } = self;
1475 split_cache.mark_post_stretched(rec.node_id, b, tr, is_clipped);
1476 }
1477 }
1478 }
1479 self.get_mut_flex_items_scratch().truncate(records_start);
1480 let out_size: Size = if is_row {
1481 Size::new(main_cursor, max_cross)
1482 } else {
1483 Size::new(max_cross, main_cursor)
1484 };
1485 return (out_size, agg_deps, agg_min_aw);
1486 }
1487 let items_start: usize = self.get_flex_items_scratch().len();
1488 let probe_template: AvailableSpace = AvailableSpace::new(
1489 inner_avail.get_width(),
1490 inner_avail.get_height(),
1491 viewport,
1492 content_origin.get_x(),
1493 content_origin.get_y(),
1494 );
1495 let mut total_grow: f32 = 0.0;
1496 let mut single_line_main: f32 = 0.0;
1497 let mut single_line_cross: f32 = 0.0;
1498 let mut pass1_deps: u8 = LAYOUT_DEP_NONE;
1499 let mut pass1_min_aw: f32 = 0.0;
1500 for flow_idx in flow_start..flow_end {
1501 let child_id: NodeId = self.get_flow_scratch()[flow_idx];
1502 let mut rec: FlexItemRecord = FlexItemRecord {
1503 node_id: child_id,
1504 size: Size::default(),
1505 margin: Edges::default(),
1506 flex_grow: 0.0,
1507 align_self: AlignSelf::Auto,
1508 overflow: Overflow::Visible,
1509 flags: FLEX_ITEM_DEFAULT_FLAGS,
1510 };
1511 let mut zero_basis_size: Option<Size> = None;
1512 if let Some(node) = tree.get_node(child_id) {
1513 let Style {
1514 layout: child_layout,
1515 ..
1516 } = node.style_ref();
1517 let width_len: Length = child_layout.get_width();
1518 let height_len: Length = child_layout.get_height();
1519 let grow: f32 = child_layout.get_flex_grow();
1520 let width_auto: bool = width_len == Length::Auto;
1521 let height_auto: bool = height_len == Length::Auto;
1522 let mut f: u16 = 0;
1523 if width_auto {
1524 f |= FLEX_ITEM_WIDTH_AUTO;
1525 }
1526 if height_auto {
1527 f |= FLEX_ITEM_HEIGHT_AUTO;
1528 }
1529 if matches!(width_len, Length::Percent(_)) {
1530 f |= FLEX_ITEM_WIDTH_PERCENT;
1531 }
1532 if matches!(height_len, Length::Percent(_)) {
1533 f |= FLEX_ITEM_HEIGHT_PERCENT;
1534 }
1535 let zero_basis: bool =
1536 !wrap_enabled && grow > 0.0 && if is_row { width_auto } else { height_auto };
1537 if zero_basis {
1538 if is_row {
1539 if matches!(height_len, Length::Percent(_)) {
1540 pass1_deps |= LAYOUT_DEP_AVAIL_H;
1541 }
1542 if Self::is_viewport_length(height_len) {
1543 pass1_deps |= LAYOUT_DEP_VIEWPORT;
1544 }
1545 let h: f32 = height_len.resolve(cross_avail, viewport).unwrap_or(0.0);
1546 zero_basis_size = Some(Size::new(0.0, h));
1547 } else {
1548 if matches!(width_len, Length::Percent(_)) {
1549 pass1_deps |= LAYOUT_DEP_AVAIL_W;
1550 }
1551 if Self::is_viewport_length(width_len) {
1552 pass1_deps |= LAYOUT_DEP_VIEWPORT;
1553 }
1554 let w: f32 = width_len.resolve(cross_avail, viewport).unwrap_or(0.0);
1555 zero_basis_size = Some(Size::new(w, 0.0));
1556 }
1557 }
1558 rec.margin = child_layout.get_margin();
1559 rec.flex_grow = grow;
1560 rec.align_self = child_layout.get_align_self();
1561 rec.overflow = child_layout.get_overflow();
1562 rec.flags = f;
1563 }
1564 total_grow += rec.flex_grow;
1565 let child_size: Size = if let Some(zb) = zero_basis_size {
1566 zb
1567 } else {
1568 let (measured, c_deps, c_min_aw): (Size, u8, f32) = self.layout_node_mode(
1569 tree,
1570 child_id,
1571 probe_template,
1572 LayoutRunMode::ComputeSize,
1573 stats,
1574 );
1575 pass1_deps |= c_deps;
1576 if c_min_aw > 0.0 {
1577 pass1_min_aw = pass1_min_aw.max(c_min_aw + rec.margin.horizontal());
1578 }
1579 measured
1580 };
1581 if !wrap_enabled {
1582 if is_row {
1583 single_line_main += child_size.get_width() + rec.margin.horizontal();
1584 single_line_cross =
1585 single_line_cross.max(child_size.get_height() + rec.margin.vertical());
1586 } else {
1587 single_line_main += child_size.get_height() + rec.margin.vertical();
1588 single_line_cross =
1589 single_line_cross.max(child_size.get_width() + rec.margin.horizontal());
1590 }
1591 }
1592 rec.size = child_size;
1593 self.get_mut_flex_items_scratch().push(rec);
1594 }
1595 let items_end: usize = self.get_flex_items_scratch().len();
1596 if !wrap_enabled {
1597 let line_ctx: FlexLineContext = FlexLineContext {
1598 frame,
1599 is_row,
1600 wrap_enabled: false,
1601 main_gap,
1602 main_avail,
1603 cross_avail,
1604 total_grow,
1605 line_count: 1,
1606 mode,
1607 };
1608 let (line_size, line_deps, line_min_aw): (Size, u8, f32) = self.layout_flex_line(
1609 tree,
1610 (items_start, items_end),
1611 0.0,
1612 Some((single_line_main, single_line_cross)),
1613 line_ctx,
1614 stats,
1615 );
1616 self.get_mut_flex_items_scratch().truncate(items_start);
1617 return (
1618 line_size,
1619 pass1_deps | line_deps,
1620 pass1_min_aw.max(line_min_aw),
1621 );
1622 }
1623 let lines_start: usize = self.get_flex_lines_scratch().len();
1624 let mut cur_line_start: usize = items_start;
1625 let mut line_main: f32 = 0.0;
1626 for item_idx in items_start..items_end {
1627 let item: FlexItemRecord = self.get_flex_items_scratch()[item_idx];
1628 let item_main: f32 = if is_row {
1629 item.size.get_width() + item.margin.horizontal()
1630 } else {
1631 item.size.get_height() + item.margin.vertical()
1632 };
1633 let line_empty: bool = item_idx == cur_line_start;
1634 let needed: f32 = if line_empty {
1635 item_main
1636 } else {
1637 line_main + main_gap + item_main
1638 };
1639 if !line_empty && needed > main_avail && main_avail > 0.0 {
1640 self.get_mut_flex_lines_scratch()
1641 .push((cur_line_start, item_idx));
1642 cur_line_start = item_idx;
1643 line_main = item_main;
1644 } else {
1645 line_main = needed;
1646 }
1647 }
1648 if cur_line_start < items_end {
1649 self.get_mut_flex_lines_scratch()
1650 .push((cur_line_start, items_end));
1651 }
1652 let lines_end: usize = self.get_flex_lines_scratch().len();
1653 let mut cross_cursor: f32 = 0.0;
1654 let mut max_main_extent: f32 = 0.0;
1655 let line_count: usize = lines_end.saturating_sub(lines_start);
1656 let mut wrap_deps: u8 = pass1_deps;
1657 let mut wrap_min_aw: f32 = pass1_min_aw;
1658 if is_row {
1659 if line_count > 1 {
1660 wrap_deps |= LAYOUT_DEP_AVAIL_W;
1661 } else {
1662 wrap_min_aw = wrap_min_aw.max(line_main + 0.5);
1663 }
1664 } else {
1665 wrap_deps |= LAYOUT_DEP_AVAIL_H;
1666 }
1667 for line_offset in 0..line_count {
1668 let line_range: (usize, usize) =
1669 self.get_flex_lines_scratch()[lines_start + line_offset];
1670 let mut line_grow: f32 = 0.0;
1671 for item_idx in line_range.0..line_range.1 {
1672 line_grow += self.get_flex_items_scratch()[item_idx].flex_grow;
1673 }
1674 let line_ctx: FlexLineContext = FlexLineContext {
1675 frame,
1676 is_row,
1677 wrap_enabled: true,
1678 main_gap,
1679 main_avail,
1680 cross_avail,
1681 total_grow: line_grow,
1682 line_count,
1683 mode,
1684 };
1685 let (line_size, l_deps, l_min_aw): (Size, u8, f32) =
1686 self.layout_flex_line(tree, line_range, cross_cursor, None, line_ctx, stats);
1687 wrap_deps |= l_deps;
1688 wrap_min_aw = wrap_min_aw.max(l_min_aw);
1689 if is_row {
1690 max_main_extent = max_main_extent.max(line_size.get_width());
1691 cross_cursor += line_size.get_height();
1692 } else {
1693 max_main_extent = max_main_extent.max(line_size.get_height());
1694 cross_cursor += line_size.get_width();
1695 }
1696 if line_offset + 1 < line_count {
1697 cross_cursor += cross_gap;
1698 }
1699 }
1700 self.get_mut_flex_lines_scratch().truncate(lines_start);
1701 self.get_mut_flex_items_scratch().truncate(items_start);
1702 let out_size: Size = if is_row {
1703 Size::new(max_main_extent, cross_cursor)
1704 } else {
1705 Size::new(cross_cursor, max_main_extent)
1706 };
1707 (out_size, wrap_deps, wrap_min_aw)
1708 }
1709
1710 #[inline(always)]
1726 fn layout_flex_line(
1727 &mut self,
1728 tree: &mut SceneTree,
1729 line_range: (usize, usize),
1730 cross_cursor: f32,
1731 precomputed: Option<(f32, f32)>,
1732 ctx: FlexLineContext,
1733 stats: &mut LayoutStats,
1734 ) -> (Size, u8, f32) {
1735 let (line_start, line_end): (usize, usize) = line_range;
1736 let frame: ContainerFrame = ctx.frame;
1737 let is_row: bool = ctx.is_row;
1738 let wrap_enabled: bool = ctx.wrap_enabled;
1739 let main_gap: f32 = ctx.main_gap;
1740 let main_avail: f32 = ctx.main_avail;
1741 let cross_avail: f32 = ctx.cross_avail;
1742 let total_grow: f32 = ctx.total_grow;
1743 let line_count: usize = ctx.line_count;
1744 let mode: LayoutRunMode = ctx.mode;
1745 let parent_layout: LayoutStyle = frame.get_parent_layout();
1746 let inner_avail: Size = frame.get_inner_avail();
1747 let viewport: Size = frame.get_viewport();
1748 let content_origin: Point = frame.get_content_origin();
1749 let count: usize = line_end.saturating_sub(line_start);
1750 let gaps_total: f32 = if count > 1 {
1751 main_gap * ((count - 1) as f32)
1752 } else {
1753 0.0
1754 };
1755 let mut line_deps: u8 = LAYOUT_DEP_NONE;
1756 let mut line_min_aw: f32 = 0.0;
1757 let has_definite_or_grow: bool = frame.get_definite_width() || total_grow > 0.0;
1758 let effective_main_avail: f32 = if is_row {
1759 if has_definite_or_grow || wrap_enabled {
1760 if total_grow > 0.0 {
1761 line_deps |= LAYOUT_DEP_AVAIL_W;
1762 }
1763 main_avail
1764 } else {
1765 let min_w_len: Length = parent_layout.get_min_width();
1766 if matches!(min_w_len, Length::Percent(_)) || Self::is_viewport_length(min_w_len) {
1767 line_deps |= LAYOUT_DEP_VIEWPORT;
1768 }
1769 min_w_len
1770 .resolve(viewport.get_width(), viewport)
1771 .map(|mw: f32| (mw - parent_layout.get_padding().horizontal()).max(0.0))
1772 .unwrap_or(0.0)
1773 }
1774 } else if frame.get_definite_height() {
1775 if total_grow > 0.0 {
1776 line_deps |= LAYOUT_DEP_AVAIL_H;
1777 }
1778 main_avail
1779 } else {
1780 let min_h_len: Length = parent_layout.get_min_height();
1781 if matches!(min_h_len, Length::Percent(_)) || Self::is_viewport_length(min_h_len) {
1782 line_deps |= LAYOUT_DEP_VIEWPORT;
1783 }
1784 min_h_len
1785 .resolve(viewport.get_height(), viewport)
1786 .map(|mh: f32| (mh - parent_layout.get_padding().vertical()).max(0.0))
1787 .unwrap_or(0.0)
1788 };
1789 let (final_used_main, line_cross): (f32, f32) = if let Some((items_main, pre_cross)) =
1790 precomputed
1791 && total_grow == 0.0
1792 {
1793 (items_main + gaps_total, pre_cross)
1794 } else {
1795 let used_main: f32 = if let Some((items_main, _)) = precomputed {
1796 items_main + gaps_total
1797 } else {
1798 let mut acc: f32 = gaps_total;
1799 for item_idx in line_start..line_end {
1800 let item: FlexItemRecord = self.get_flex_items_scratch()[item_idx];
1801 let sz: f32 = if is_row {
1802 item.size.get_width() + item.margin.horizontal()
1803 } else {
1804 item.size.get_height() + item.margin.vertical()
1805 };
1806 acc += sz;
1807 }
1808 acc
1809 };
1810 let free_main: f32 = (effective_main_avail - used_main).max(0.0);
1811 if total_grow > 0.0 && free_main > 0.0 {
1812 for item_idx in line_start..line_end {
1813 let item: &mut FlexItemRecord =
1814 &mut self.get_mut_flex_items_scratch()[item_idx];
1815 let grow: f32 = item.flex_grow;
1816 if grow > 0.0 {
1817 let extra: f32 = free_main * (grow / total_grow);
1818 if is_row {
1819 item.size.set_width(item.size.get_width() + extra);
1820 } else {
1821 item.size.set_height(item.size.get_height() + extra);
1822 }
1823 }
1824 }
1825 }
1826 if total_grow > 0.0 {
1827 for item_idx in line_start..line_end {
1828 let item: FlexItemRecord = self.get_flex_items_scratch()[item_idx];
1829 let auto_cross: bool = item.flex_grow > 0.0
1830 && if is_row {
1831 (item.flags & FLEX_ITEM_HEIGHT_AUTO) != 0
1832 } else {
1833 (item.flags & FLEX_ITEM_WIDTH_AUTO) != 0
1834 };
1835 if auto_cross {
1836 let probe_w: f32 = if is_row {
1837 item.size.get_width()
1838 } else {
1839 cross_avail
1840 };
1841 let probe_h: f32 = if is_row {
1842 cross_avail
1843 } else {
1844 item.size.get_height()
1845 };
1846 let stretch_pw: bool = is_row && (item.flags & FLEX_ITEM_WIDTH_AUTO) != 0;
1847 let stretch_ph: bool = !is_row && (item.flags & FLEX_ITEM_HEIGHT_AUTO) != 0;
1848 let probe_sp: AvailableSpace = AvailableSpace::new(
1849 probe_w,
1850 probe_h,
1851 viewport,
1852 content_origin.get_x(),
1853 content_origin.get_y(),
1854 )
1855 .with_stretch(stretch_pw, stretch_ph);
1856 let (measured, c_deps, c_min_aw): (Size, u8, f32) = self.layout_node_mode(
1857 tree,
1858 item.node_id,
1859 probe_sp,
1860 LayoutRunMode::ComputeSize,
1861 stats,
1862 );
1863 line_deps |= c_deps;
1864 if c_min_aw > 0.0 {
1865 line_min_aw = line_min_aw.max(c_min_aw + item.margin.horizontal());
1866 }
1867 let slot: &mut FlexItemRecord =
1868 &mut self.get_mut_flex_items_scratch()[item_idx];
1869 if is_row {
1870 slot.size.set_height(measured.get_height());
1871 } else {
1872 slot.size.set_width(measured.get_width());
1873 }
1874 }
1875 }
1876 }
1877 let mut sum_main: f32 = gaps_total;
1878 let mut max_cross: f32 = 0.0;
1879 for item_idx in line_start..line_end {
1880 let item: FlexItemRecord = self.get_flex_items_scratch()[item_idx];
1881 if is_row {
1882 sum_main += item.size.get_width() + item.margin.horizontal();
1883 max_cross = max_cross.max(item.size.get_height() + item.margin.vertical());
1884 } else {
1885 sum_main += item.size.get_height() + item.margin.vertical();
1886 max_cross = max_cross.max(item.size.get_width() + item.margin.horizontal());
1887 }
1888 }
1889 (sum_main, max_cross)
1890 };
1891 if mode == LayoutRunMode::ComputeSize {
1892 let sz: Size = if is_row {
1893 Size::new(final_used_main, line_cross)
1894 } else {
1895 Size::new(line_cross, final_used_main)
1896 };
1897 return (sz, line_deps, line_min_aw);
1898 }
1899 let rem: f32 = (effective_main_avail - final_used_main).max(0.0);
1900 let (mut main_cursor, extra_step): (f32, f32) = match parent_layout.get_justify_content() {
1901 JustifyContent::Start => (0.0, 0.0),
1902 JustifyContent::Center => (rem * 0.5, 0.0),
1903 JustifyContent::End => (rem, 0.0),
1904 JustifyContent::SpaceBetween => {
1905 if count > 1 {
1906 (0.0, rem / ((count - 1) as f32))
1907 } else {
1908 (0.0, 0.0)
1909 }
1910 }
1911 JustifyContent::SpaceAround => {
1912 let slot: f32 = rem / (count.max(1) as f32);
1913 (slot * 0.5, slot)
1914 }
1915 JustifyContent::SpaceEvenly => {
1916 let slot: f32 = rem / ((count + 1) as f32);
1917 (slot, slot)
1918 }
1919 };
1920 let definite_cross: bool = !is_row || frame.get_definite_height();
1921 let effective_line_cross: f32 = if line_count == 1 && !wrap_enabled && definite_cross {
1922 line_cross.max(cross_avail)
1923 } else {
1924 line_cross
1925 };
1926 let mut actual_line_cross: f32 = line_cross;
1927 let parent_align: AlignItems = parent_layout.get_align_items();
1928 for idx in 0..count {
1929 let rec: FlexItemRecord = self.get_flex_items_scratch()[line_start + idx];
1930 let child_id: NodeId = rec.node_id;
1931 let child_size: Size = rec.size;
1932 let margin: Edges = rec.margin;
1933 let width_auto: bool = (rec.flags & FLEX_ITEM_WIDTH_AUTO) != 0;
1934 let height_auto: bool = (rec.flags & FLEX_ITEM_HEIGHT_AUTO) != 0;
1935 let item_cross: f32 = if is_row {
1936 child_size.get_height()
1937 } else {
1938 child_size.get_width()
1939 };
1940 let align: AlignItems = match rec.align_self {
1941 AlignSelf::Auto => parent_align,
1942 AlignSelf::Stretch => AlignItems::Stretch,
1943 AlignSelf::Start => AlignItems::Start,
1944 AlignSelf::Center => AlignItems::Center,
1945 AlignSelf::End => AlignItems::End,
1946 };
1947 let avail_item_cross: f32 = (effective_line_cross
1948 - if is_row {
1949 margin.vertical()
1950 } else {
1951 margin.horizontal()
1952 })
1953 .max(0.0);
1954 let cross_offset: f32 = match align {
1955 AlignItems::Start | AlignItems::Stretch => 0.0,
1956 AlignItems::Center => ((avail_item_cross - item_cross) * 0.5).max(0.0),
1957 AlignItems::End => (avail_item_cross - item_cross).max(0.0),
1958 };
1959 let auto_cross_grow: bool =
1960 rec.flex_grow > 0.0 && if is_row { height_auto } else { width_auto };
1961 let target_w: f32 = if !is_row && align == AlignItems::Stretch && width_auto {
1962 avail_item_cross.max(child_size.get_width())
1963 } else {
1964 child_size.get_width()
1965 };
1966 let target_h: f32 = if is_row && align == AlignItems::Stretch && height_auto {
1967 avail_item_cross.max(child_size.get_height())
1968 } else {
1969 child_size.get_height()
1970 };
1971 let (x, y): (f32, f32) = if is_row {
1972 (
1973 content_origin.get_x() + main_cursor + margin.get_left(),
1974 content_origin.get_y() + cross_cursor + cross_offset + margin.get_top(),
1975 )
1976 } else {
1977 (
1978 content_origin.get_x() + cross_cursor + cross_offset + margin.get_left(),
1979 content_origin.get_y() + main_cursor + margin.get_top(),
1980 )
1981 };
1982 let space_w: f32 = if (rec.flags & FLEX_ITEM_WIDTH_PERCENT) != 0 {
1983 inner_avail.get_width()
1984 } else if auto_cross_grow && !is_row {
1985 cross_avail
1986 } else {
1987 target_w
1988 };
1989 let space_h: f32 = if (rec.flags & FLEX_ITEM_HEIGHT_PERCENT) != 0 {
1990 inner_avail.get_height()
1991 } else if auto_cross_grow && is_row {
1992 cross_avail
1993 } else {
1994 target_h
1995 };
1996 let stretch_child_w: bool = !(auto_cross_grow && !is_row)
1997 && ((!is_row && align == AlignItems::Stretch && width_auto)
1998 || (is_row && rec.flex_grow > 0.0 && width_auto));
1999 let stretch_child_h: bool = !(auto_cross_grow && is_row)
2000 && ((is_row && align == AlignItems::Stretch && height_auto)
2001 || (!is_row && rec.flex_grow > 0.0 && height_auto));
2002 let final_space: AvailableSpace = AvailableSpace::new(space_w, space_h, viewport, x, y)
2003 .with_stretch(stretch_child_w, stretch_child_h);
2004 let (placed, _, _): (Size, u8, f32) = self.layout_node_mode(
2005 tree,
2006 child_id,
2007 final_space,
2008 LayoutRunMode::PerformLayout,
2009 stats,
2010 );
2011 let is_clipped: bool = matches!(rec.overflow, Overflow::Hidden | Overflow::Scroll);
2012 let (desired_w, desired_h): (f32, f32) = if auto_cross_grow {
2013 if is_row {
2014 actual_line_cross =
2015 actual_line_cross.max(placed.get_height() + margin.vertical());
2016 (target_w, placed.get_height())
2017 } else {
2018 actual_line_cross =
2019 actual_line_cross.max(placed.get_width() + margin.horizontal());
2020 (placed.get_width(), target_h)
2021 }
2022 } else if is_clipped || rec.flex_grow > 0.0 {
2023 (target_w, target_h)
2024 } else {
2025 (
2026 target_w.max(placed.get_width()),
2027 target_h.max(placed.get_height()),
2028 )
2029 };
2030 let needs_box_adjust: bool = tree.get_node(child_id).is_some_and(|n: &SceneNode| {
2031 let b: Rect = n.get_layout_box().get_bounds();
2032 (b.get_width() - desired_w).abs() > 0.001
2033 || (b.get_height() - desired_h).abs() > 0.001
2034 });
2035 if needs_box_adjust {
2036 self.record_layout_snapshot(tree, child_id);
2037 let adjusted_opt: Option<(Rect, Transform2D)> =
2038 if let Some(node) = tree.get_node_mut(child_id) {
2039 let tr: Transform2D = node.style_ref().paint_ref().get_transform();
2040 let mut b: Rect = node.get_layout_box().get_bounds();
2041 b.set_width(desired_w);
2042 b.set_height(desired_h);
2043 node.get_mut_layout_box().set_bounds(b);
2044 Some((b, tr))
2045 } else {
2046 None
2047 };
2048 if let Some((b, tr)) = adjusted_opt {
2049 let Self { split_cache, .. } = self;
2050 split_cache.mark_post_stretched(child_id, b, tr, is_clipped);
2051 }
2052 }
2053 let advance: f32 = if is_row {
2054 target_w + margin.horizontal()
2055 } else {
2056 target_h + margin.vertical()
2057 };
2058 main_cursor += advance;
2059 if idx + 1 < count {
2060 main_cursor += main_gap + extra_step;
2061 }
2062 }
2063 let sz: Size = if is_row {
2064 Size::new(final_used_main, actual_line_cross)
2065 } else {
2066 Size::new(actual_line_cross, final_used_main)
2067 };
2068 (sz, line_deps, line_min_aw)
2069 }
2070
2071 fn layout_grid(
2086 &mut self,
2087 tree: &mut SceneTree,
2088 flow_range: (usize, usize),
2089 frame: ContainerFrame,
2090 mode: LayoutRunMode,
2091 stats: &mut LayoutStats,
2092 ) -> (Size, u8, f32) {
2093 let (flow_start, flow_end): (usize, usize) = flow_range;
2094 if flow_start >= flow_end {
2095 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
2096 }
2097 let parent_layout: LayoutStyle = frame.get_parent_layout();
2098 let inner_avail: Size = frame.get_inner_avail();
2099 let viewport: Size = frame.get_viewport();
2100 let content_origin: Point = frame.get_content_origin();
2101 let cols: usize = (parent_layout.get_grid_columns() as usize).max(1);
2102 let col_gap: f32 = parent_layout.get_column_gap();
2103 let row_gap: f32 = parent_layout.get_row_gap();
2104 let total_col_gaps: f32 = col_gap * ((cols.saturating_sub(1)) as f32);
2105 let col_w: f32 = ((inner_avail.get_width() - total_col_gaps) / (cols as f32)).max(0.0);
2106 let col_step: f32 = col_w + col_gap;
2107 let origin_x: f32 = content_origin.get_x();
2108 let origin_y: f32 = content_origin.get_y();
2109 let inner_h: f32 = inner_avail.get_height();
2110 let mut y_cursor: f32 = 0.0;
2111 let mut idx: usize = flow_start;
2112 let mut grid_deps: u8 = LAYOUT_DEP_AVAIL_W;
2113 while idx < flow_end {
2114 let row_end: usize = (idx + cols).min(flow_end);
2115 let mut row_h: f32 = 0.0;
2116 let mut min_row_h: f32 = f32::INFINITY;
2117 let y: f32 = origin_y + y_cursor;
2118 let mut x: f32 = origin_x;
2119 for pos in idx..row_end {
2120 let child_id: NodeId = self.get_flow_scratch()[pos];
2121 let auto_w: bool = tree.get_node(child_id).is_some_and(|n: &SceneNode| {
2122 n.style_ref().layout_ref().get_width() == Length::Auto
2123 });
2124 let cell_space: AvailableSpace =
2125 AvailableSpace::new(col_w, inner_h, viewport, x, y).with_stretch(auto_w, false);
2126 let (child_size, c_deps, _): (Size, u8, f32) =
2127 self.layout_node_mode(tree, child_id, cell_space, mode, stats);
2128 grid_deps |= c_deps;
2129 if mode == LayoutRunMode::PerformLayout
2130 && (child_size.get_width() - col_w).abs() > 0.001
2131 {
2132 self.record_layout_snapshot(tree, child_id);
2133 let adjusted_opt: Option<(Rect, Transform2D, bool)> =
2134 if let Some(node) = tree.get_node_mut(child_id) {
2135 let tr: Transform2D = node.style_ref().paint_ref().get_transform();
2136 let clipped: bool = matches!(
2137 node.style_ref().layout_ref().get_overflow(),
2138 Overflow::Hidden | Overflow::Scroll
2139 );
2140 let mut b: Rect = node.get_layout_box().get_bounds();
2141 b.set_width(col_w);
2142 node.get_mut_layout_box().set_bounds(b);
2143 Some((b, tr, clipped))
2144 } else {
2145 None
2146 };
2147 if let Some((b, tr, clipped)) = adjusted_opt {
2148 let Self { split_cache, .. } = self;
2149 split_cache.mark_post_stretched(child_id, b, tr, clipped);
2150 }
2151 }
2152 let ch: f32 = child_size.get_height();
2153 row_h = row_h.max(ch);
2154 min_row_h = min_row_h.min(ch);
2155 x += col_step;
2156 }
2157 if mode == LayoutRunMode::PerformLayout && row_end - idx > 1 && min_row_h < row_h {
2158 for pos in idx..row_end {
2159 let child_id: NodeId = self.get_flow_scratch()[pos];
2160 let needs_stretch: bool =
2161 tree.get_node(child_id).is_some_and(|n: &SceneNode| {
2162 n.get_layout_box().get_bounds().get_height() < row_h
2163 });
2164 if needs_stretch {
2165 self.record_layout_snapshot(tree, child_id);
2166 let adjusted_opt: Option<(Rect, Transform2D, bool)> =
2167 if let Some(node) = tree.get_node_mut(child_id) {
2168 let tr: Transform2D = node.style_ref().paint_ref().get_transform();
2169 let clipped: bool = matches!(
2170 node.style_ref().layout_ref().get_overflow(),
2171 Overflow::Hidden | Overflow::Scroll
2172 );
2173 let mut b: Rect = node.get_layout_box().get_bounds();
2174 b.set_height(row_h);
2175 node.get_mut_layout_box().set_bounds(b);
2176 Some((b, tr, clipped))
2177 } else {
2178 None
2179 };
2180 if let Some((b, tr, clipped)) = adjusted_opt {
2181 let Self { split_cache, .. } = self;
2182 split_cache.mark_post_stretched(child_id, b, tr, clipped);
2183 }
2184 }
2185 }
2186 }
2187 y_cursor += row_h;
2188 if row_end < flow_end {
2189 y_cursor += row_gap;
2190 }
2191 idx = row_end;
2192 }
2193 (Size::new(inner_avail.get_width(), y_cursor), grid_deps, 0.0)
2194 }
2195
2196 fn translate_subtree(&mut self, tree: &mut SceneTree, root_id: NodeId, dx: f32, dy: f32) {
2206 if dx == 0.0 && dy == 0.0 {
2207 return;
2208 }
2209 self.record_layout_snapshot(tree, root_id);
2210 let Some(root_node) = tree.get_node_mut(root_id) else {
2211 return;
2212 };
2213 let Style { layout, .. } = root_node.style_ref();
2214 let root_none_disp: bool = layout.get_display() == Display::None;
2215 if let Some(mut c) = root_node.cached_constraint() {
2216 c.set_origin_x(c.get_origin_x() + dx);
2217 c.set_origin_y(c.get_origin_y() + dy);
2218 root_node.set_last_constraint(Some(c));
2219 }
2220 if root_none_disp {
2221 let Self { split_cache, .. } = self;
2222 split_cache.translate_node_cache(root_id, dx, dy);
2223 return;
2224 }
2225 let mut root_bounds: Rect = root_node.get_layout_box().get_bounds();
2226 root_bounds.set_x(root_bounds.get_x() + dx);
2227 root_bounds.set_y(root_bounds.get_y() + dy);
2228 root_node.get_mut_layout_box().set_bounds(root_bounds);
2229 let mut root_content: Rect = root_node.get_layout_box().get_content_bounds();
2230 root_content.set_x(root_content.get_x() + dx);
2231 root_content.set_y(root_content.get_y() + dy);
2232 root_node
2233 .get_mut_layout_box()
2234 .set_content_bounds(root_content);
2235 let root_children_empty: bool = !tree.has_children(root_id);
2236 if !root_children_empty {
2237 self.get_mut_translate_scratch().clear();
2238 self.get_mut_translate_scratch()
2239 .extend(tree.children_iter(root_id));
2240 }
2241 {
2242 let Self { split_cache, .. } = self;
2243 split_cache.translate_node_cache(root_id, dx, dy);
2244 }
2245 if root_children_empty {
2246 return;
2247 }
2248 while let Some(nid) = self.get_mut_translate_scratch().pop() {
2249 let (is_fixed_pos, is_none_disp): (bool, bool) = match tree.get_node(nid) {
2250 Some(node) => {
2251 let Style { layout, .. } = node.style_ref();
2252 (
2253 layout.get_position() == Position::Fixed,
2254 layout.get_display() == Display::None,
2255 )
2256 }
2257 None => continue,
2258 };
2259 if is_fixed_pos {
2260 continue;
2261 }
2262 if !is_none_disp {
2263 self.record_layout_snapshot(tree, nid);
2264 }
2265 let Some(node) = tree.get_node_mut(nid) else {
2266 continue;
2267 };
2268 if let Some(mut c) = node.cached_constraint() {
2269 c.set_origin_x(c.get_origin_x() + dx);
2270 c.set_origin_y(c.get_origin_y() + dy);
2271 node.set_last_constraint(Some(c));
2272 }
2273 if is_none_disp {
2274 let Self { split_cache, .. } = self;
2275 split_cache.translate_node_cache(nid, dx, dy);
2276 continue;
2277 }
2278 let mut bounds: Rect = node.get_layout_box().get_bounds();
2279 bounds.set_x(bounds.get_x() + dx);
2280 bounds.set_y(bounds.get_y() + dy);
2281 node.get_mut_layout_box().set_bounds(bounds);
2282 let mut content: Rect = node.get_layout_box().get_content_bounds();
2283 content.set_x(content.get_x() + dx);
2284 content.set_y(content.get_y() + dy);
2285 node.get_mut_layout_box().set_content_bounds(content);
2286 let has_children: bool = tree.has_children(nid);
2287 if has_children {
2288 self.get_mut_translate_scratch()
2289 .extend(tree.children_iter(nid));
2290 }
2291 let Self { split_cache, .. } = self;
2292 split_cache.translate_node_cache(nid, dx, dy);
2293 }
2294 }
2295}