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 layout_box: LayoutBox = node.get_layout_box();
96 let new_bounds: Rect = layout_box.get_bounds();
97 let new_content_bounds: Rect = layout_box.get_content_bounds();
98 let new_content_size: Size = layout_box.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 let border_halo: f32 = node.style_ref().paint_ref().get_border().get_width()
117 + node
118 .style_ref()
119 .paint_ref()
120 .box_shadow()
121 .map_or(0.0, |shadow_spec: Shadow| {
122 shadow_spec.get_blur_radius()
123 + shadow_spec.get_spread_radius().max(0.0)
124 + shadow_spec
125 .get_offset_x()
126 .abs()
127 .max(shadow_spec.get_offset_y().abs())
128 })
129 + 2.0;
130 if snap.has_old_screen {
131 let expanded_old_screen: Rect = Rect::new(
132 snap.old_screen.get_x() - border_halo,
133 snap.old_screen.get_y() - border_halo,
134 snap.old_screen.get_width() + border_halo * 2.0,
135 snap.old_screen.get_height() + border_halo * 2.0,
136 );
137 damage.add_rect(SceneTree::scale_logical_to_phys_rect(
138 expanded_old_screen,
139 scale,
140 ));
141 }
142 if new_bounds.get_width() > 0.0 && new_bounds.get_height() > 0.0 {
143 match tree.node_conservative_logical_rect(snap.id) {
144 Some(new_screen) => {
145 let expanded_new_screen: Rect = Rect::new(
146 new_screen.get_x() - border_halo,
147 new_screen.get_y() - border_halo,
148 new_screen.get_width() + border_halo * 2.0,
149 new_screen.get_height() + border_halo * 2.0,
150 );
151 damage.add_rect(SceneTree::scale_logical_to_phys_rect(
152 expanded_new_screen,
153 scale,
154 ));
155 }
156 None => {
157 final_fallback = LAYOUT_FALLBACK_UNSAFE;
158 break;
159 }
160 }
161 }
162 }
163 layout_snapshots.clear();
164 if final_fallback != LAYOUT_FALLBACK_NONE {
165 return (
166 stats,
167 LayoutDamageReport {
168 damage: DamageRegion::full(),
169 fallback_kind: final_fallback,
170 },
171 );
172 }
173 (
174 stats,
175 LayoutDamageReport {
176 damage,
177 fallback_kind: LAYOUT_FALLBACK_NONE,
178 },
179 )
180 }
181
182 pub fn compute_serial(&mut self, tree: &mut SceneTree, viewport: Size) -> LayoutStats {
193 let Self {
194 track_damage,
195 damage_fallback,
196 layout_snapshots,
197 ..
198 } = self;
199 *track_damage = false;
200 *damage_fallback = LAYOUT_FALLBACK_NONE;
201 layout_snapshots.clear();
202 let root_id: NodeId = tree.get_root();
203 let space: AvailableSpace = AvailableSpace::root(viewport);
204 {
205 let Self {
206 split_cache,
207 last_split_stats,
208 ..
209 } = self;
210 split_cache.begin_pass();
211 *last_split_stats = SplitCacheStats::default();
212 }
213 let stats: LayoutStats = self.compute_serial_body(tree, root_id, space);
214 self.finish_layout_pass(tree, root_id, stats);
215 stats
216 }
217
218 pub fn compute_parallel(&mut self, tree: &mut SceneTree, viewport: Size) -> LayoutStats {
229 let Self {
230 track_damage,
231 damage_fallback,
232 layout_snapshots,
233 split_cache,
234 last_split_stats,
235 layout_soa,
236 parallel_solver,
237 ..
238 } = self;
239 *track_damage = false;
240 *damage_fallback = LAYOUT_FALLBACK_NONE;
241 layout_snapshots.clear();
242 split_cache.begin_pass();
243 *last_split_stats = SplitCacheStats::default();
244 let root_id: NodeId = tree.get_root();
245 let solver: &mut ParallelLayoutSolver =
246 parallel_solver.get_or_insert_with(|| Box::new(ParallelLayoutSolver::default()));
247 let stats: LayoutStats =
248 solver.compute_synced(tree, layout_soa.as_mut(), split_cache, viewport);
249 self.finish_layout_pass(tree, root_id, stats);
250 stats
251 }
252
253 pub fn set_parallel_enabled(&mut self, enabled: bool) {
259 let Self {
260 parallel_disabled, ..
261 } = self;
262 *parallel_disabled = !enabled;
263 }
264
265 #[must_use]
271 pub fn is_parallel_enabled(&self) -> bool {
272 let Self {
273 parallel_disabled, ..
274 } = self;
275 !*parallel_disabled
276 }
277
278 pub fn sync_layout_soa(&mut self, tree: &SceneTree) -> &SceneLayoutSoa {
288 let Self { layout_soa, .. } = self;
289 let _: SoaSyncMode = layout_soa.sync_from_tree(tree);
290 layout_soa
291 }
292
293 #[must_use]
299 pub fn layout_soa(&self) -> &SceneLayoutSoa {
300 let Self { layout_soa, .. } = self;
301 layout_soa
302 }
303
304 pub fn layout_soa_mut(&mut self) -> &mut SceneLayoutSoa {
310 let Self { layout_soa, .. } = self;
311 layout_soa
312 }
313
314 #[must_use]
320 pub fn last_parallel_stats(&self) -> ParallelLayoutStats {
321 let Self {
322 parallel_solver, ..
323 } = self;
324 parallel_solver.as_deref().map_or(
325 ParallelLayoutStats::default(),
326 ParallelLayoutSolver::last_stats,
327 )
328 }
329
330 fn compute_inner(&mut self, tree: &mut SceneTree, viewport: Size) -> LayoutStats {
342 let root_id: NodeId = tree.get_root();
343 let space: AvailableSpace = AvailableSpace::root(viewport);
344 {
345 let Self {
346 split_cache,
347 last_split_stats,
348 ..
349 } = self;
350 split_cache.begin_pass();
351 *last_split_stats = SplitCacheStats::default();
352 }
353 let use_parallel: bool = self.should_use_parallel_pass(tree, root_id, space);
354 let stats: LayoutStats = if use_parallel {
355 let Self {
356 layout_soa,
357 parallel_solver,
358 split_cache,
359 ..
360 } = self;
361 let solver: &mut ParallelLayoutSolver =
362 parallel_solver.get_or_insert_with(|| Box::new(ParallelLayoutSolver::default()));
363 solver.compute_synced(tree, layout_soa.as_mut(), split_cache, viewport)
364 } else {
365 self.compute_serial_body(tree, root_id, space)
366 };
367 self.finish_layout_pass(tree, root_id, stats);
368 stats
369 }
370
371 fn should_use_parallel_pass(
383 &self,
384 tree: &SceneTree,
385 root_id: NodeId,
386 space: AvailableSpace,
387 ) -> bool {
388 let Self {
389 track_damage,
390 parallel_disabled,
391 last_topology_revision,
392 last_live_node_count,
393 ..
394 } = self;
395 if *track_damage || *parallel_disabled {
396 return false;
397 }
398 let live_nodes: usize = tree.live_node_count();
399 if live_nodes < PARALLEL_LAYOUT_TREE_THRESHOLD {
400 return false;
401 }
402 let Some(root_node) = tree.get_node(root_id) else {
403 return false;
404 };
405 let root_dirty: DirtyFlags = root_node.get_dirty();
406 let bulk_growth: bool = root_dirty.intersects(DIRTY_SUBTREE_LAYOUT)
407 && tree.get_topology_revision() != *last_topology_revision
408 && live_nodes >= last_live_node_count.saturating_add(PARALLEL_LAYOUT_TREE_THRESHOLD);
409 match root_node.cached_constraint() {
410 None => true,
411 Some(prev_space) => {
412 !prev_space.can_reuse(space) || root_dirty.intersects(DIRTY_LAYOUT) || bulk_growth
413 }
414 }
415 }
416
417 fn compute_serial_body(
429 &mut self,
430 tree: &mut SceneTree,
431 root_id: NodeId,
432 space: AvailableSpace,
433 ) -> LayoutStats {
434 let mut stats: LayoutStats = LayoutStats::default();
435 self.get_mut_children_scratch().clear();
436 self.get_mut_flow_scratch().clear();
437 self.get_mut_abs_scratch().clear();
438 self.get_mut_flex_items_scratch().clear();
439 self.get_mut_flex_lines_scratch().clear();
440 self.get_mut_translate_scratch().clear();
441 let _: (Size, u8, f32) = self.layout_node_mode(
442 tree,
443 root_id,
444 space,
445 LayoutRunMode::PerformLayout,
446 &mut stats,
447 );
448 debug_assert!(self.get_children_scratch().is_empty());
449 debug_assert!(self.get_flow_scratch().is_empty());
450 debug_assert!(self.get_abs_scratch().is_empty());
451 debug_assert!(self.get_flex_items_scratch().is_empty());
452 debug_assert!(self.get_flex_lines_scratch().is_empty());
453 debug_assert!(self.get_mut_translate_scratch().is_empty());
454 if stats.get_nodes_computed() > 0 {
455 tree.bump_serial_layout_epoch();
456 }
457 stats
458 }
459
460 fn finish_layout_pass(&mut self, tree: &SceneTree, root_id: NodeId, stats: LayoutStats) {
468 {
469 let Self {
470 split_cache,
471 last_topology_revision,
472 last_live_node_count,
473 ..
474 } = self;
475 split_cache.mark_synced(root_id, tree.get_revision());
476 *last_topology_revision = tree.get_topology_revision();
477 *last_live_node_count = tree.live_node_count();
478 }
479 self.set_last_stats(stats);
480 let invalidations: u64 = tree.get_layout_invalidations();
481 let changed_now: u64 = stats.get_nodes_changed() as u64;
482 let telemetry: &mut LayoutTelemetry = self.get_mut_telemetry();
483 telemetry.set_invalidations(invalidations);
484 telemetry.set_passes(telemetry.get_passes().saturating_add(1));
485 if changed_now > 0 {
486 telemetry.set_changed_passes(telemetry.get_changed_passes().saturating_add(1));
487 telemetry.set_changed_nodes_total(
488 telemetry
489 .get_changed_nodes_total()
490 .saturating_add(changed_now),
491 );
492 }
493 }
494
495 #[must_use]
505 pub fn layout_telemetry(&self) -> LayoutTelemetry {
506 self.get_telemetry()
507 }
508
509 pub fn reset_telemetry(&mut self) {
515 self.set_telemetry(LayoutTelemetry::default());
516 }
517
518 #[must_use]
528 pub fn split_cache_stats(&self) -> SplitCacheStats {
529 let Self {
530 last_split_stats, ..
531 } = self;
532 *last_split_stats
533 }
534
535 #[must_use]
546 pub fn subtree_bounds(&self, node_id: NodeId) -> Option<Rect> {
547 let Self { split_cache, .. } = self;
548 split_cache.subtree_bounds_for(node_id)
549 }
550
551 #[must_use]
563 pub fn hit_test(&self, tree: &SceneTree, point: Point) -> Option<NodeId> {
564 let mut visited: usize = 0;
565 let Self { split_cache, .. } = self;
566 hit_test_with_cache(tree, point, Some(split_cache), &mut visited)
567 }
568
569 #[must_use]
581 pub fn hit_test_with_visited(&self, tree: &SceneTree, point: Point) -> (Option<NodeId>, usize) {
582 let mut visited: usize = 0;
583 let Self { split_cache, .. } = self;
584 let hit: Option<NodeId> = hit_test_with_cache(tree, point, Some(split_cache), &mut visited);
585 (hit, visited)
586 }
587
588 #[inline]
596 fn record_layout_snapshot(&mut self, tree: &SceneTree, node_id: NodeId) {
597 let Self {
598 track_damage,
599 damage_fallback,
600 layout_snapshots,
601 split_cache,
602 ..
603 } = self;
604 if !*track_damage || *damage_fallback != LAYOUT_FALLBACK_NONE {
605 return;
606 }
607 if split_cache.is_snapshot_seen(node_id) {
608 return;
609 }
610 if layout_snapshots.len() >= DIRTY_PAINT_FULL_THRESHOLD {
611 *damage_fallback = LAYOUT_FALLBACK_OVERFLOW;
612 return;
613 }
614 let Some(node) = tree.get_node(node_id) else {
615 return;
616 };
617 let layout_box: LayoutBox = node.get_layout_box();
618 let old_bounds: Rect = layout_box.get_bounds();
619 let old_content_bounds: Rect = layout_box.get_content_bounds();
620 let old_content_size: Size = layout_box.get_content_size();
621 let (has_old_screen, old_screen): (bool, Rect) =
622 if old_bounds.get_width() > 0.0 && old_bounds.get_height() > 0.0 {
623 match tree.node_conservative_logical_rect(node_id) {
624 Some(rect) => (true, rect),
625 None => {
626 *damage_fallback = LAYOUT_FALLBACK_UNSAFE;
627 return;
628 }
629 }
630 } else {
631 (false, Rect::default())
632 };
633 split_cache.mark_snapshot_seen(node_id);
634 layout_snapshots.push(LayoutNodeSnapshot {
635 id: node_id,
636 old_bounds,
637 old_content_bounds,
638 old_content_size,
639 has_old_screen,
640 old_screen,
641 });
642 }
643
644 #[inline]
655 fn rect_approx_eq(rect_bounds: Rect, clip_bounds: Rect) -> bool {
656 (rect_bounds.get_x() - clip_bounds.get_x()).abs() <= 0.001
657 && (rect_bounds.get_y() - clip_bounds.get_y()).abs() <= 0.001
658 && (rect_bounds.get_width() - clip_bounds.get_width()).abs() <= 0.001
659 && (rect_bounds.get_height() - clip_bounds.get_height()).abs() <= 0.001
660 }
661
662 #[inline(always)]
672 fn is_viewport_length(length: Length) -> bool {
673 matches!(length, Length::ViewportWidth(_)) || matches!(length, Length::ViewportHeight(_))
674 }
675
676 #[inline]
687 fn size_approx_eq(box_size: Size, viewport_size: Size) -> bool {
688 (box_size.get_width() - viewport_size.get_width()).abs() <= 0.001
689 && (box_size.get_height() - viewport_size.get_height()).abs() <= 0.001
690 }
691
692 fn measure_node_text_cached(
707 &mut self,
708 node_id: NodeId,
709 text: &str,
710 style: TextStyle,
711 max_width: Option<f32>,
712 dirty_text: bool,
713 ) -> TextSizeMeasurement {
714 let is_ascii: bool = text.is_ascii();
715 let Self {
716 text_size_cache,
717 split_cache,
718 last_split_stats,
719 ..
720 } = self;
721 let Some((hash_a, hash_b, text_len, reused_hash)) =
722 split_cache.text_hash_for_node(node_id, text, dirty_text)
723 else {
724 let (measured, _): (Size, bool) =
725 measure_text_simd_or_fallback(text, is_ascii, style, max_width);
726 return TextSizeMeasurement {
727 size: measured,
728 unwrapped: false,
729 max_content_w: f32::INFINITY,
730 cache_hit: false,
731 };
732 };
733 if reused_hash {
734 last_split_stats.text_hash_reuses = last_split_stats.text_hash_reuses.saturating_add(1);
735 }
736 let result: TextSizeMeasurement = text_size_cache.get_or_measure_hashed(
737 text,
738 (hash_a, hash_b, text_len),
739 style,
740 max_width,
741 |raw: &str, text_style: TextStyle, limit: Option<f32>| {
742 measure_text_simd_or_fallback(raw, is_ascii, text_style, limit).0
743 },
744 );
745 if result.cache_hit {
746 last_split_stats.text_interval_hits =
747 last_split_stats.text_interval_hits.saturating_add(1);
748 }
749 result
750 }
751
752 fn layout_node_mode(
767 &mut self,
768 tree: &mut SceneTree,
769 node_id: NodeId,
770 space: AvailableSpace,
771 mode: LayoutRunMode,
772 stats: &mut LayoutStats,
773 ) -> (Size, u8, f32) {
774 let is_dirty: bool = match tree.get_node(node_id) {
775 Some(node) => node.get_dirty().intersects(DIRTY_LAYOUT_PASS_MASK),
776 None => return (Size::default(), LAYOUT_DEP_NONE, 0.0),
777 };
778 let (slot_idx, content_gen): (usize, u32) = {
779 let Self { split_cache, .. } = self;
780 split_cache.prepare_node(node_id, is_dirty)
781 };
782 if mode == LayoutRunMode::ComputeSize {
783 let measure_hit: Option<(Size, u8, f32)> = {
784 let Self { split_cache, .. } = self;
785 split_cache.lookup_measure(slot_idx, space, content_gen)
786 };
787 if let Some(hit) = measure_hit {
788 let Self {
789 last_split_stats, ..
790 } = self;
791 last_split_stats.measure_hits = last_split_stats.measure_hits.saturating_add(1);
792 stats.set_subtrees_skipped(stats.get_subtrees_skipped() + 1);
793 return hit;
794 }
795 if !is_dirty {
796 let final_hit: Option<FinalLayoutSlot> = {
797 let Self { split_cache, .. } = self;
798 split_cache.lookup_final(slot_idx, content_gen)
799 };
800 if let Some(fslot) = final_hit
801 && !fslot.hidden
802 && fslot.space.can_reuse(space)
803 {
804 let Self {
805 last_split_stats, ..
806 } = self;
807 last_split_stats.measure_hits = last_split_stats.measure_hits.saturating_add(1);
808 stats.set_subtrees_skipped(stats.get_subtrees_skipped() + 1);
809 return (fslot.size, fslot.dep_flags, fslot.min_avail_w);
810 }
811 }
812 {
813 let Self {
814 last_split_stats, ..
815 } = self;
816 last_split_stats.measure_misses = last_split_stats.measure_misses.saturating_add(1);
817 }
818 stats.set_nodes_computed(stats.get_nodes_computed() + 1);
819 } else {
820 if !is_dirty {
821 let final_hit: Option<FinalLayoutSlot> = {
822 let Self { split_cache, .. } = self;
823 split_cache.lookup_final(slot_idx, content_gen)
824 };
825 if let Some(fslot) = final_hit
826 && fslot.space.can_reuse(space)
827 {
828 if fslot.hidden {
829 let Self {
830 last_split_stats, ..
831 } = self;
832 last_split_stats.final_hits = last_split_stats.final_hits.saturating_add(1);
833 stats.set_subtrees_skipped(stats.get_subtrees_skipped() + 1);
834 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
835 }
836 if !fslot.space.same_origin(space) {
837 let dx: f32 = space.get_origin_x() - fslot.space.get_origin_x();
838 let dy: f32 = space.get_origin_y() - fslot.space.get_origin_y();
839 self.translate_subtree(tree, node_id, dx, dy);
840 if dx.abs() > 0.001 || dy.abs() > 0.001 {
841 stats.set_nodes_changed(stats.get_nodes_changed().saturating_add(1));
842 }
843 }
844 if fslot.post_stretched {
845 let restored_opt: Option<(Rect, Rect, Size)> = {
846 let Self { split_cache, .. } = self;
847 split_cache.lookup_final(slot_idx, content_gen).map(
848 |source_val: FinalLayoutSlot| {
849 (
850 source_val.bounds,
851 source_val.content_bounds,
852 source_val.content_size,
853 )
854 },
855 )
856 };
857 if let Some((r_bounds, r_content, r_csize)) = restored_opt {
858 self.record_layout_snapshot(tree, node_id);
859 if let Some(node) = tree.get_node_mut(node_id) {
860 node.get_mut_layout_box().set_bounds(r_bounds);
861 node.get_mut_layout_box().set_content_bounds(r_content);
862 node.get_mut_layout_box().set_content_size(r_csize);
863 }
864 let Self { split_cache, .. } = self;
865 if let Some(slot_mut) = split_cache.final_slots[slot_idx].as_mut() {
866 slot_mut.post_stretched = false;
867 }
868 }
869 }
870 let Self {
871 last_split_stats, ..
872 } = self;
873 last_split_stats.final_hits = last_split_stats.final_hits.saturating_add(1);
874 stats.set_subtrees_skipped(stats.get_subtrees_skipped() + 1);
875 return (fslot.size, fslot.dep_flags, fslot.min_avail_w);
876 }
877 }
878 {
879 let Self {
880 last_split_stats, ..
881 } = self;
882 last_split_stats.final_misses = last_split_stats.final_misses.saturating_add(1);
883 }
884 stats.set_nodes_computed(stats.get_nodes_computed() + 1);
885 }
886 let Some(node) = tree.get_node(node_id) else {
887 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
888 };
889 let style_ref: &Style = node.style_ref();
890 let Style {
891 layout: layout_ref,
892 paint: paint_ref,
893 text: text_ref,
894 } = style_ref;
895 if layout_ref.get_display() == Display::None {
896 {
897 let Self {
898 last_split_stats, ..
899 } = self;
900 last_split_stats.display_none_skips =
901 last_split_stats.display_none_skips.saturating_add(1);
902 }
903 if mode == LayoutRunMode::ComputeSize {
904 let Self { split_cache, .. } = self;
905 split_cache.store_measure(
906 slot_idx,
907 space,
908 LayoutRunMode::ComputeSize,
909 content_gen,
910 (Size::default(), LAYOUT_DEP_NONE, 0.0),
911 );
912 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
913 }
914 self.record_layout_snapshot(tree, node_id);
915 let (prev_content_size, had_subtree_dirty): (Size, bool) =
916 if let Some(hidden_node) = tree.get_node_mut(node_id) {
917 let old_b: Rect = hidden_node.get_layout_box().get_bounds();
918 let csize: Size = hidden_node.get_layout_box().get_content_size();
919 let sub_dirty: bool = hidden_node.get_dirty().intersects(DIRTY_SUBTREE_LAYOUT);
920 if !Self::rect_approx_eq(old_b, Rect::default()) {
921 stats.set_nodes_changed(stats.get_nodes_changed().saturating_add(1));
922 }
923 hidden_node.get_mut_layout_box().set_bounds(Rect::default());
924 hidden_node
925 .get_mut_layout_box()
926 .set_content_bounds(Rect::default());
927 hidden_node.set_last_constraint(Some(space));
928 hidden_node.get_mut_dirty().remove(DIRTY_LAYOUT_PASS_MASK);
929 (csize, sub_dirty)
930 } else {
931 (Size::default(), false)
932 };
933 if let Some(meta) = tree.get_mut_meta().get_mut(node_id.as_usize()) {
934 let next_dirty: u16 = meta.get_dirty() & !DIRTY_LAYOUT_PASS_MASK;
935 let _: &mut NodeMeta = meta.set_dirty(next_dirty);
936 }
937 if had_subtree_dirty {
938 tree.set_has_stale_hidden_dirty(true);
939 }
940 let Self { split_cache, .. } = self;
941 split_cache.store_final(
942 slot_idx,
943 FinalLayoutSlot {
944 space,
945 subtree_gen: content_gen,
946 size: Size::default(),
947 bounds: Rect::default(),
948 content_bounds: Rect::default(),
949 content_size: prev_content_size,
950 dep_flags: LAYOUT_DEP_NONE,
951 min_avail_w: 0.0,
952 post_stretched: false,
953 hidden: true,
954 },
955 );
956 split_cache.store_subtree_meta(slot_idx, Rect::default(), false);
957 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
958 }
959 let w_len: Length = layout_ref.get_width();
960 let h_len: Length = layout_ref.get_height();
961 let min_w_len: Length = layout_ref.get_min_width();
962 let max_w_len: Length = layout_ref.get_max_width();
963 let min_h_len: Length = layout_ref.get_min_height();
964 let max_h_len: Length = layout_ref.get_max_height();
965 let mut own_deps: u8 = LAYOUT_DEP_NONE;
966 if matches!(w_len, Length::Percent(_))
967 || matches!(min_w_len, Length::Percent(_))
968 || matches!(max_w_len, Length::Percent(_))
969 {
970 own_deps |= LAYOUT_DEP_AVAIL_W;
971 }
972 if matches!(h_len, Length::Percent(_))
973 || matches!(min_h_len, Length::Percent(_))
974 || matches!(max_h_len, Length::Percent(_))
975 {
976 own_deps |= LAYOUT_DEP_AVAIL_H;
977 }
978 if Self::is_viewport_length(w_len)
979 || Self::is_viewport_length(h_len)
980 || Self::is_viewport_length(min_w_len)
981 || Self::is_viewport_length(max_w_len)
982 || Self::is_viewport_length(min_h_len)
983 || Self::is_viewport_length(max_h_len)
984 {
985 own_deps |= LAYOUT_DEP_VIEWPORT;
986 }
987 let padding: Edges = layout_ref.get_padding();
988 let border_w: f32 = paint_ref.get_border().get_width();
989 let node_transform: Transform2D = paint_ref.get_transform();
990 let node_scroll: Point = node.get_layout_box().get_scroll_offset();
991 let self_is_portal: bool = matches!(node.get_kind(), NodeKind::Portal)
992 || layout_ref.get_position() == Position::Fixed;
993 let chrome_h: f32 = padding.horizontal() + border_w * 2.0;
994 let chrome_v: f32 = padding.vertical() + border_w * 2.0;
995 let explicit_w: Option<f32> = w_len.resolve(space.get_width(), space.get_viewport());
996 let explicit_h: Option<f32> = h_len.resolve(space.get_height(), space.get_viewport());
997 let mut resolved_w: Option<f32> = explicit_w;
998 let mut resolved_h: Option<f32> = explicit_h;
999 let stretched_w: bool = resolved_w.is_none() && space.get_stretch_width();
1000 let stretched_h: bool = resolved_h.is_none() && space.get_stretch_height();
1001 if stretched_w {
1002 resolved_w = Some(space.get_width());
1003 own_deps |= LAYOUT_DEP_AVAIL_W;
1004 }
1005 if stretched_h {
1006 resolved_h = Some(space.get_height());
1007 own_deps |= LAYOUT_DEP_AVAIL_H;
1008 }
1009 let min_w: f32 = min_w_len
1010 .resolve(space.get_width(), space.get_viewport())
1011 .unwrap_or(0.0);
1012 let min_h: f32 = min_h_len
1013 .resolve(space.get_height(), space.get_viewport())
1014 .unwrap_or(0.0);
1015 let max_w: f32 = max_w_len
1016 .resolve(space.get_width(), space.get_viewport())
1017 .unwrap_or(f32::INFINITY)
1018 .max(min_w);
1019 let max_h: f32 = max_h_len
1020 .resolve(space.get_height(), space.get_viewport())
1021 .unwrap_or(f32::INFINITY)
1022 .max(min_h);
1023 let aspect_ratio: Option<f32> = layout_ref.aspect_ratio_value();
1024 if let Some(ratio) = aspect_ratio
1025 && ratio > 0.0
1026 {
1027 if let Some(width_val) = resolved_w
1028 && resolved_h.is_none()
1029 {
1030 resolved_h = Some(width_val.clamp(min_w, max_w) / ratio);
1031 } else if let Some(height_val) = resolved_h
1032 && resolved_w.is_none()
1033 {
1034 resolved_w = Some(height_val.clamp(min_h, max_h) * ratio);
1035 }
1036 }
1037 let node_overflow: Overflow = layout_ref.get_overflow();
1038 let node_flex_grow: f32 = layout_ref.get_flex_grow();
1039 let width_independent: bool = explicit_w.is_some()
1040 || (stretched_w
1041 && resolved_w.is_some()
1042 && (node_overflow != Overflow::Visible || node_flex_grow > 0.0));
1043 let height_independent: bool = resolved_h.is_some() && h_len != Length::Auto;
1044 if mode == LayoutRunMode::ComputeSize && width_independent && resolved_h.is_some() {
1045 let final_w: f32 = resolved_w.unwrap_or(0.0).clamp(min_w, max_w).max(0.0);
1046 let final_h: f32 = resolved_h.unwrap_or(0.0).clamp(min_h, max_h).max(0.0);
1047 let final_size: Size = Size::new(final_w, final_h);
1048 let Self { split_cache, .. } = self;
1049 split_cache.store_measure(
1050 slot_idx,
1051 space,
1052 LayoutRunMode::ComputeSize,
1053 content_gen,
1054 (final_size, own_deps, 0.0),
1055 );
1056 return (final_size, own_deps, 0.0);
1057 }
1058 let avail_w_outer: f32 = resolved_w
1059 .unwrap_or(space.get_width())
1060 .max(min_w)
1061 .min(max_w);
1062 let avail_h_outer: f32 = resolved_h
1063 .unwrap_or(space.get_height())
1064 .max(min_h)
1065 .min(max_h);
1066 let inner_avail_w: f32 = (avail_w_outer - chrome_h).max(0.0);
1067 let inner_avail_h: f32 = (avail_h_outer - chrome_v).max(0.0);
1068 let origin_x: f32 = space.get_origin_x();
1069 let origin_y: f32 = space.get_origin_y();
1070 let content_origin_x: f32 = origin_x + padding.get_left() + border_w;
1071 let content_origin_y: f32 = origin_y + padding.get_top() + border_w;
1072 let shields_avail_w: bool = resolved_w.is_some() && (own_deps & LAYOUT_DEP_AVAIL_W) == 0;
1073 let shields_avail_h: bool = resolved_h.is_some() && (own_deps & LAYOUT_DEP_AVAIL_H) == 0;
1074 let skip_leaf_measure: bool =
1075 node_overflow != Overflow::Scroll && width_independent && height_independent;
1076 let dirty_text: bool = node.get_dirty().intersects(DIRTY_TEXT);
1077 let mut child_range: Option<(usize, usize)> = None;
1078 let mut child_deps: u8 = LAYOUT_DEP_NONE;
1079 let mut child_min_aw: f32 = 0.0;
1080 let leaf_content_size: Option<Size> = match node.get_kind() {
1081 NodeKind::Text(text) => {
1082 if skip_leaf_measure {
1083 Some(Size::new(inner_avail_w, inner_avail_h))
1084 } else {
1085 let measured: TextSizeMeasurement = self.measure_node_text_cached(
1086 node_id,
1087 text.as_str(),
1088 *text_ref,
1089 Some(inner_avail_w.max(1.0)),
1090 dirty_text,
1091 );
1092 if text_ref.get_overflow() != TextOverflow::Clip {
1093 if measured.unwrapped && measured.max_content_w.is_finite() {
1094 child_min_aw = measured.max_content_w + 0.5;
1095 } else {
1096 child_deps |= LAYOUT_DEP_AVAIL_W;
1097 }
1098 }
1099 Some(measured.size)
1100 }
1101 }
1102 NodeKind::Image(img) => {
1103 Some(Size::new(img.get_width() as f32, img.get_height() as f32))
1104 }
1105 NodeKind::Input(input_state) => {
1106 if skip_leaf_measure {
1107 Some(Size::new(inner_avail_w, inner_avail_h))
1108 } else {
1109 let sample: &str = if input_state.get_value().is_empty() {
1110 input_state.get_placeholder().as_str()
1111 } else {
1112 input_state.get_value().as_str()
1113 };
1114 let measured: TextSizeMeasurement = self.measure_node_text_cached(
1115 node_id,
1116 sample,
1117 *text_ref,
1118 Some(inner_avail_w.max(1.0)),
1119 dirty_text,
1120 );
1121 if text_ref.get_overflow() != TextOverflow::Clip {
1122 if measured.unwrapped && measured.max_content_w.is_finite() {
1123 child_min_aw = measured.max_content_w + 0.5;
1124 } else {
1125 child_deps |= LAYOUT_DEP_AVAIL_W;
1126 }
1127 }
1128 Some(Size::new(
1129 measured.size.get_width().max(120.0),
1130 measured.size.get_height().max(20.0),
1131 ))
1132 }
1133 }
1134 NodeKind::View
1135 | NodeKind::ScrollView
1136 | NodeKind::Portal
1137 | NodeKind::Fragment
1138 | NodeKind::Canvas(_) => {
1139 if !tree.has_children(node_id) {
1140 Some(Size::default())
1141 } else {
1142 let start: usize = self.get_children_scratch().len();
1143 self.get_mut_children_scratch()
1144 .extend(tree.children_iter(node_id));
1145 let end: usize = self.get_children_scratch().len();
1146 child_range = Some((start, end));
1147 None
1148 }
1149 }
1150 };
1151 let parent_layout_copy: LayoutStyle = *layout_ref;
1152 let content_size: Size = if let Some(size) = leaf_content_size {
1153 size
1154 } else if let Some((child_start, child_end)) = child_range {
1155 let min_inner_size: Size =
1156 Size::new((min_w - chrome_h).max(0.0), (min_h - chrome_v).max(0.0));
1157 let frame: ContainerFrame = ContainerFrame::new(
1158 parent_layout_copy,
1159 Size::new(inner_avail_w, inner_avail_h),
1160 space.get_viewport(),
1161 Point::new(content_origin_x, content_origin_y),
1162 resolved_w.is_some(),
1163 resolved_h.is_some(),
1164 min_inner_size,
1165 );
1166 let (size, c_deps, c_min_aw): (Size, u8, f32) =
1167 self.layout_children(tree, (child_start, child_end), frame, mode, stats);
1168 child_deps = c_deps;
1169 child_min_aw = c_min_aw;
1170 if mode == LayoutRunMode::ComputeSize {
1171 self.get_mut_children_scratch().truncate(child_start);
1172 }
1173 size
1174 } else {
1175 Size::default()
1176 };
1177 let content_outer_w: f32 = content_size.get_width() + chrome_h;
1178 let content_outer_h: f32 = content_size.get_height() + chrome_v;
1179 let mut final_w: f32 = match (explicit_w, stretched_w) {
1180 (Some(span_width), _) => span_width,
1181 (None, true) => {
1182 let base_w: f32 = resolved_w.unwrap_or(content_outer_w);
1183 if node_overflow == Overflow::Visible
1184 && node_flex_grow <= 0.0
1185 && content_outer_w > base_w + 0.5
1186 {
1187 content_outer_w
1188 } else {
1189 base_w
1190 }
1191 }
1192 (None, false) => content_outer_w,
1193 };
1194 let mut final_h: f32 = resolved_h.unwrap_or(content_outer_h);
1195 if let Some(ratio) = aspect_ratio
1196 && ratio > 0.0
1197 {
1198 if resolved_w.is_some() && resolved_h.is_none() {
1199 final_h = final_w / ratio;
1200 } else if resolved_h.is_some() && resolved_w.is_none() {
1201 final_w = final_h * ratio;
1202 }
1203 }
1204 final_w = final_w.clamp(min_w, max_w).max(0.0);
1205 final_h = final_h.clamp(min_h, max_h).max(0.0);
1206 let final_size: Size = Size::new(final_w, final_h);
1207 let mut final_deps: u8 = own_deps | (child_deps & LAYOUT_DEP_VIEWPORT);
1208 let mut final_min_aw: f32 = 0.0;
1209 if !shields_avail_w {
1210 if (child_deps & LAYOUT_DEP_AVAIL_W) != 0 {
1211 final_deps |= LAYOUT_DEP_AVAIL_W;
1212 } else if child_min_aw > 0.0 {
1213 final_min_aw = child_min_aw + chrome_h;
1214 }
1215 }
1216 if !shields_avail_h {
1217 final_deps |= child_deps & LAYOUT_DEP_AVAIL_H;
1218 }
1219 if mode == LayoutRunMode::ComputeSize {
1220 let Self { split_cache, .. } = self;
1221 split_cache.store_measure(
1222 slot_idx,
1223 space,
1224 LayoutRunMode::ComputeSize,
1225 content_gen,
1226 (final_size, final_deps, final_min_aw),
1227 );
1228 return (final_size, final_deps, final_min_aw);
1229 }
1230 let bounds: Rect = Rect::new(origin_x, origin_y, final_w, final_h);
1231 let content_bounds: Rect = Rect::new(
1232 content_origin_x,
1233 content_origin_y,
1234 (final_w - chrome_h).max(0.0),
1235 (final_h - chrome_v).max(0.0),
1236 );
1237 self.record_layout_snapshot(tree, node_id);
1238 if let Some(node) = tree.get_node_mut(node_id) {
1239 let prev_box: LayoutBox = node.get_layout_box();
1240 let changed: bool = !Self::rect_approx_eq(prev_box.get_bounds(), bounds)
1241 || !Self::rect_approx_eq(prev_box.get_content_bounds(), content_bounds)
1242 || !Self::size_approx_eq(prev_box.get_content_size(), content_size);
1243 if changed {
1244 stats.set_nodes_changed(stats.get_nodes_changed().saturating_add(1));
1245 }
1246 node.get_mut_layout_box().set_bounds(bounds);
1247 node.get_mut_layout_box().set_content_bounds(content_bounds);
1248 node.get_mut_layout_box().set_content_size(content_size);
1249 node.set_last_constraint(Some(space));
1250 node.get_mut_dirty().remove(DIRTY_LAYOUT_PASS_MASK);
1251 }
1252 if let Some(meta) = tree.get_mut_meta().get_mut(node_id.as_usize()) {
1253 let next_dirty: u16 = meta.get_dirty() & !DIRTY_LAYOUT_PASS_MASK;
1254 let _: &mut NodeMeta = meta.set_dirty(next_dirty);
1255 }
1256 let own_vis: Rect = shifted_node_bounds(bounds, Point::default(), node_transform);
1257 let is_clipped: bool = matches!(node_overflow, Overflow::Hidden | Overflow::Scroll);
1258 let mut sub_bounds: Rect = own_vis;
1259 let mut has_nonzero_z: bool = false;
1260 if let Some((c_start, c_end)) = child_range {
1261 let tx: f32 = node_transform.get_translate_x() - node_scroll.get_x();
1262 let ty: f32 = node_transform.get_translate_y() - node_scroll.get_y();
1263 for idx in c_start..c_end {
1264 let cid: NodeId = self.get_children_scratch()[idx];
1265 if let Some(cnode) = tree.get_node(cid) {
1266 let layout_style: &LayoutStyle = cnode.style_ref().layout_ref();
1267 if layout_style.get_z_index() != 0 {
1268 has_nonzero_z = true;
1269 }
1270 let c_is_portal: bool = matches!(cnode.get_kind(), NodeKind::Portal)
1271 || layout_style.get_position() == Position::Fixed;
1272 if !is_clipped
1273 && layout_style.get_display() != Display::None
1274 && (self_is_portal || !c_is_portal)
1275 {
1276 let child_sb: Rect = {
1277 let Self { split_cache, .. } = self;
1278 split_cache.subtree_bounds_for(cid)
1279 }
1280 .unwrap_or_else(|| {
1281 shifted_node_bounds(
1282 cnode.get_layout_box().get_bounds(),
1283 Point::default(),
1284 cnode.style_ref().paint_ref().get_transform(),
1285 )
1286 });
1287 if child_sb.get_width() > 0.0 && child_sb.get_height() > 0.0 {
1288 let shifted_child: Rect = Rect::new(
1289 child_sb.get_x() + tx,
1290 child_sb.get_y() + ty,
1291 child_sb.get_width(),
1292 child_sb.get_height(),
1293 );
1294 sub_bounds = sub_bounds.union(shifted_child);
1295 }
1296 }
1297 }
1298 }
1299 self.get_mut_children_scratch().truncate(c_start);
1300 }
1301 let Self { split_cache, .. } = self;
1302 split_cache.store_final(
1303 slot_idx,
1304 FinalLayoutSlot {
1305 space,
1306 subtree_gen: content_gen,
1307 size: final_size,
1308 bounds,
1309 content_bounds,
1310 content_size,
1311 dep_flags: final_deps,
1312 min_avail_w: final_min_aw,
1313 post_stretched: false,
1314 hidden: false,
1315 },
1316 );
1317 if split_cache
1318 .lookup_measure(slot_idx, space, content_gen)
1319 .is_none()
1320 {
1321 split_cache.store_measure(
1322 slot_idx,
1323 space,
1324 LayoutRunMode::PerformLayout,
1325 content_gen,
1326 (final_size, final_deps, final_min_aw),
1327 );
1328 }
1329 split_cache.store_subtree_meta(slot_idx, sub_bounds, has_nonzero_z);
1330 (final_size, final_deps, final_min_aw)
1331 }
1332
1333 fn layout_children(
1348 &mut self,
1349 tree: &mut SceneTree,
1350 child_range: (usize, usize),
1351 frame: ContainerFrame,
1352 mode: LayoutRunMode,
1353 stats: &mut LayoutStats,
1354 ) -> (Size, u8, f32) {
1355 let (child_start, child_end): (usize, usize) = child_range;
1356 let child_count: usize = child_end.saturating_sub(child_start);
1357 if child_count == 0 {
1358 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
1359 }
1360 let parent_layout: LayoutStyle = frame.get_parent_layout();
1361 let inner_avail: Size = frame.get_inner_avail();
1362 let viewport: Size = frame.get_viewport();
1363 let content_origin: Point = frame.get_content_origin();
1364 let flex_dir_opt: Option<FlexDirection> = match parent_layout.get_display() {
1365 Display::Grid => None,
1366 Display::Block => Some(FlexDirection::Column),
1367 Display::Flex | Display::None => Some(parent_layout.get_flex_direction()),
1368 };
1369 let is_row_dir: bool = matches!(
1370 flex_dir_opt,
1371 Some(FlexDirection::Row | FlexDirection::RowReverse)
1372 );
1373 let is_reverse_dir: bool = matches!(
1374 flex_dir_opt,
1375 Some(FlexDirection::RowReverse | FlexDirection::ColumnReverse)
1376 );
1377 let parent_align: AlignItems = parent_layout.get_align_items();
1378 let parent_height_auto: bool = parent_layout.get_height() == Length::Auto;
1379 let parent_bounded_main: bool = if is_row_dir {
1380 frame.get_definite_width() || parent_layout.get_max_width() != Length::Auto
1381 } else {
1382 frame.get_definite_height() || parent_layout.get_max_height() != Length::Auto
1383 };
1384 let mut single_pass_ok: bool = flex_dir_opt.is_some()
1385 && parent_layout.get_flex_wrap() != FlexWrap::Wrap
1386 && !is_reverse_dir
1387 && parent_layout.get_justify_content() == JustifyContent::Start;
1388 let mut shrinkable_flow_children: usize = 0;
1389 let flow_start: usize = self.get_flow_scratch().len();
1390 let abs_start: usize = self.get_abs_scratch().len();
1391 for idx in child_start..child_end {
1392 let child_id: NodeId = self.get_children_scratch()[idx];
1393 if let Some(child_node) = tree.get_node(child_id) {
1394 let Style {
1395 layout: child_layout,
1396 ..
1397 } = child_node.style_ref();
1398 if child_layout.get_display() == Display::None {
1399 if mode == LayoutRunMode::PerformLayout {
1400 let _: (Size, u8, f32) = self.layout_node_mode(
1401 tree,
1402 child_id,
1403 AvailableSpace::new(0.0, 0.0, viewport, 0.0, 0.0),
1404 LayoutRunMode::PerformLayout,
1405 stats,
1406 );
1407 }
1408 continue;
1409 }
1410 match child_layout.get_position() {
1411 Position::Relative => {
1412 if single_pass_ok {
1413 if parent_bounded_main && child_layout.get_flex_shrink() > 0.0 {
1414 let can_shrink_child: bool = if is_row_dir {
1415 child_layout.get_min_width() != Length::Auto
1416 || child_layout.get_overflow() != Overflow::Visible
1417 || !matches!(child_node.get_kind(), NodeKind::Text(_))
1418 || child_node.style_ref().text_ref().get_overflow()
1419 != TextOverflow::Clip
1420 } else {
1421 child_layout.get_min_height() != Length::Auto
1422 || child_layout.get_overflow() != Overflow::Visible
1423 };
1424 if can_shrink_child {
1425 shrinkable_flow_children += 1;
1426 }
1427 }
1428 if child_layout.get_flex_grow() > 0.0
1429 || child_layout.get_flex_basis() != Length::Auto
1430 {
1431 single_pass_ok = false;
1432 } else {
1433 let align: AlignItems = match child_layout.get_align_self() {
1434 AlignSelf::Auto => parent_align,
1435 AlignSelf::Stretch => AlignItems::Stretch,
1436 AlignSelf::Start => AlignItems::Start,
1437 AlignSelf::Center => AlignItems::Center,
1438 AlignSelf::End => AlignItems::End,
1439 };
1440 let non_start_align: bool =
1441 matches!(align, AlignItems::Center | AlignItems::End);
1442 let auto_row_stretch: bool = align == AlignItems::Stretch
1443 && is_row_dir
1444 && child_layout.get_height() == Length::Auto
1445 && (parent_height_auto
1446 || frame.get_min_inner_size().get_height() > 0.0);
1447 if non_start_align || auto_row_stretch {
1448 single_pass_ok = false;
1449 }
1450 }
1451 }
1452 self.get_mut_flow_scratch().push(child_id);
1453 }
1454 Position::Absolute | Position::Fixed => {
1455 if mode == LayoutRunMode::PerformLayout {
1456 self.get_mut_abs_scratch().push(child_id);
1457 }
1458 }
1459 }
1460 }
1461 }
1462 let flow_end: usize = self.get_flow_scratch().len();
1463 let flow_count: usize = flow_end.saturating_sub(flow_start);
1464 if single_pass_ok && shrinkable_flow_children > 0 && (!is_row_dir || flow_count >= 2) {
1465 single_pass_ok = false;
1466 }
1467 let abs_end: usize = self.get_abs_scratch().len();
1468 let flow_result: (Size, u8, f32) = if let Some(flex_dir) = flex_dir_opt {
1469 self.layout_flex(
1470 tree,
1471 (flow_start, flow_end),
1472 frame,
1473 flex_dir,
1474 (single_pass_ok, mode),
1475 stats,
1476 )
1477 } else {
1478 self.layout_grid(tree, (flow_start, flow_end), frame, mode, stats)
1479 };
1480 self.get_mut_flow_scratch().truncate(flow_start);
1481 if mode == LayoutRunMode::PerformLayout {
1482 for abs_idx in abs_start..abs_end {
1483 let abs_id: NodeId = self.get_abs_scratch()[abs_idx];
1484 let child_layout_opt: Option<LayoutStyle> = tree
1485 .get_node(abs_id)
1486 .map(|node: &SceneNode| node.style_ref().get_layout());
1487 let Some(child_layout) = child_layout_opt else {
1488 continue;
1489 };
1490 let is_fixed: bool = child_layout.get_position() == Position::Fixed;
1491 let ref_origin: Point = if is_fixed {
1492 Point::new(0.0, 0.0)
1493 } else {
1494 content_origin
1495 };
1496 let ref_size: Size = if is_fixed { viewport } else { inner_avail };
1497 let resolved_w: Option<f32> = child_layout
1498 .get_width()
1499 .resolve(ref_size.get_width(), viewport);
1500 let resolved_h: Option<f32> = child_layout
1501 .get_height()
1502 .resolve(ref_size.get_height(), viewport);
1503 let needs_intrinsic: bool = (resolved_w.is_none()
1504 && child_layout.left_offset().is_none()
1505 && child_layout.right_offset().is_some())
1506 || (resolved_h.is_none()
1507 && child_layout.top_offset().is_none()
1508 && child_layout.bottom_offset().is_some());
1509 let probed: Size = if needs_intrinsic {
1510 let probe_space: AvailableSpace = AvailableSpace::new(
1511 ref_size.get_width(),
1512 ref_size.get_height(),
1513 viewport,
1514 ref_origin.get_x(),
1515 ref_origin.get_y(),
1516 );
1517 self.layout_node_mode(
1518 tree,
1519 abs_id,
1520 probe_space,
1521 LayoutRunMode::ComputeSize,
1522 stats,
1523 )
1524 .0
1525 } else {
1526 Size::new(ref_size.get_width(), ref_size.get_height())
1527 };
1528 let child_w: f32 = resolved_w.unwrap_or(probed.get_width());
1529 let child_h: f32 = resolved_h.unwrap_or(probed.get_height());
1530 let pos_x: f32 = if let Some(left) = child_layout.left_offset() {
1531 ref_origin.get_x() + left
1532 } else if let Some(right) = child_layout.right_offset() {
1533 ref_origin.get_x() + (ref_size.get_width() - child_w - right)
1534 } else {
1535 ref_origin.get_x()
1536 };
1537 let pos_y: f32 = if let Some(top) = child_layout.top_offset() {
1538 ref_origin.get_y() + top
1539 } else if let Some(bottom) = child_layout.bottom_offset() {
1540 ref_origin.get_y() + (ref_size.get_height() - child_h - bottom)
1541 } else {
1542 ref_origin.get_y()
1543 };
1544 let abs_space: AvailableSpace = AvailableSpace::new(
1545 ref_size.get_width(),
1546 ref_size.get_height(),
1547 viewport,
1548 pos_x,
1549 pos_y,
1550 );
1551 let _: (Size, u8, f32) = self.layout_node_mode(
1552 tree,
1553 abs_id,
1554 abs_space,
1555 LayoutRunMode::PerformLayout,
1556 stats,
1557 );
1558 }
1559 self.get_mut_abs_scratch().truncate(abs_start);
1560 }
1561 flow_result
1562 }
1563
1564 fn layout_flex(
1580 &mut self,
1581 tree: &mut SceneTree,
1582 flow_range: (usize, usize),
1583 frame: ContainerFrame,
1584 direction: FlexDirection,
1585 mode_flags: (bool, LayoutRunMode),
1586 stats: &mut LayoutStats,
1587 ) -> (Size, u8, f32) {
1588 let (flow_start, flow_end): (usize, usize) = flow_range;
1589 let (single_pass_ok, mode): (bool, LayoutRunMode) = mode_flags;
1590 let count_all: usize = flow_end.saturating_sub(flow_start);
1591 if count_all == 0 {
1592 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
1593 }
1594 let parent_layout: LayoutStyle = frame.get_parent_layout();
1595 let inner_avail: Size = frame.get_inner_avail();
1596 let viewport: Size = frame.get_viewport();
1597 let content_origin: Point = frame.get_content_origin();
1598 let is_row: bool = matches!(direction, FlexDirection::Row | FlexDirection::RowReverse);
1599 let is_reverse: bool = matches!(
1600 direction,
1601 FlexDirection::RowReverse | FlexDirection::ColumnReverse
1602 );
1603 let main_gap: f32 = if is_row {
1604 parent_layout.get_column_gap()
1605 } else {
1606 parent_layout.get_row_gap()
1607 };
1608 let cross_gap: f32 = if is_row {
1609 parent_layout.get_row_gap()
1610 } else {
1611 parent_layout.get_column_gap()
1612 };
1613 let main_avail: f32 = if is_row {
1614 inner_avail.get_width()
1615 } else {
1616 inner_avail.get_height()
1617 };
1618 let cross_avail: f32 = if is_row {
1619 inner_avail.get_height()
1620 } else {
1621 inner_avail.get_width()
1622 };
1623 if is_reverse {
1624 self.get_mut_flow_scratch()[flow_start..flow_end].reverse();
1625 }
1626 let wrap_enabled: bool = parent_layout.get_flex_wrap() == FlexWrap::Wrap;
1627 if single_pass_ok {
1628 let parent_align: AlignItems = parent_layout.get_align_items();
1629 let mut main_cursor: f32 = 0.0;
1630 let mut max_cross: f32 = 0.0;
1631 let mut agg_deps: u8 = LAYOUT_DEP_NONE;
1632 let mut agg_min_aw: f32 = 0.0;
1633 let records_start: usize = self.get_flex_items_scratch().len();
1634 for offset in 0..count_all {
1635 let child_id: NodeId = self.get_flow_scratch()[flow_start + offset];
1636 let (margin, align, width_auto, height_auto, c_overflow, c_transform): (
1637 Edges,
1638 AlignItems,
1639 bool,
1640 bool,
1641 Overflow,
1642 Transform2D,
1643 ) = if let Some(node) = tree.get_node(child_id) {
1644 let Style {
1645 layout: child_layout,
1646 paint: child_paint,
1647 ..
1648 } = node.style_ref();
1649 let alpha_val: AlignItems = match child_layout.get_align_self() {
1650 AlignSelf::Auto => parent_align,
1651 AlignSelf::Stretch => AlignItems::Stretch,
1652 AlignSelf::Start => AlignItems::Start,
1653 AlignSelf::Center => AlignItems::Center,
1654 AlignSelf::End => AlignItems::End,
1655 };
1656 (
1657 child_layout.get_margin(),
1658 alpha_val,
1659 child_layout.get_width() == Length::Auto,
1660 child_layout.get_height() == Length::Auto,
1661 child_layout.get_overflow(),
1662 child_paint.get_transform(),
1663 )
1664 } else {
1665 (
1666 Edges::default(),
1667 parent_align,
1668 true,
1669 true,
1670 Overflow::Visible,
1671 Transform2D::default(),
1672 )
1673 };
1674 let (pos_x, pos_y): (f32, f32) = if is_row {
1675 (
1676 content_origin.get_x() + main_cursor + margin.get_left(),
1677 content_origin.get_y() + margin.get_top(),
1678 )
1679 } else {
1680 (
1681 content_origin.get_x() + margin.get_left(),
1682 content_origin.get_y() + main_cursor + margin.get_top(),
1683 )
1684 };
1685 let stretch_w: bool = !is_row && align == AlignItems::Stretch && width_auto;
1686 let stretch_h: bool = is_row && align == AlignItems::Stretch && height_auto;
1687 if stretch_w {
1688 agg_deps |= LAYOUT_DEP_AVAIL_W;
1689 }
1690 if stretch_h {
1691 agg_deps |= LAYOUT_DEP_AVAIL_H;
1692 }
1693 let space_w: f32 = if stretch_w {
1694 (inner_avail.get_width() - margin.horizontal()).max(0.0)
1695 } else {
1696 inner_avail.get_width()
1697 };
1698 let space_h: f32 = if stretch_h {
1699 (inner_avail.get_height() - margin.vertical()).max(0.0)
1700 } else {
1701 inner_avail.get_height()
1702 };
1703 let final_space: AvailableSpace =
1704 AvailableSpace::new(space_w, space_h, viewport, pos_x, pos_y)
1705 .with_stretch(stretch_w, stretch_h);
1706 let (placed, c_deps, c_min_aw): (Size, u8, f32) =
1707 self.layout_node_mode(tree, child_id, final_space, mode, stats);
1708 agg_deps |= c_deps;
1709 if c_min_aw > 0.0 {
1710 agg_min_aw = agg_min_aw.max(c_min_aw + margin.horizontal());
1711 }
1712 let target_w: f32 = if stretch_w {
1713 space_w.max(placed.get_width())
1714 } else {
1715 placed.get_width()
1716 };
1717 let target_h: f32 = if stretch_h {
1718 space_h.max(placed.get_height())
1719 } else {
1720 placed.get_height()
1721 };
1722 if mode == LayoutRunMode::PerformLayout {
1723 if (stretch_w && target_w > placed.get_width() + 0.01)
1724 || (stretch_h && target_h > placed.get_height() + 0.01)
1725 {
1726 self.record_layout_snapshot(tree, child_id);
1727 let updated_b: Option<Rect> = if let Some(node) =
1728 tree.get_node_mut(child_id)
1729 {
1730 let padding_edges: Edges = node.style_ref().layout_ref().get_padding();
1731 let border_width: f32 =
1732 node.style_ref().paint_ref().get_border().get_width();
1733 let mut bounds_rect: Rect = node.get_layout_box().get_bounds();
1734 bounds_rect.set_width(target_w);
1735 bounds_rect.set_height(target_h);
1736 let mut content_rect: Rect = node.get_layout_box().get_content_bounds();
1737 content_rect.set_width(
1738 (target_w - padding_edges.horizontal() - border_width * 2.0)
1739 .max(0.0),
1740 );
1741 content_rect.set_height(
1742 (target_h - padding_edges.vertical() - border_width * 2.0).max(0.0),
1743 );
1744 node.get_mut_layout_box().set_bounds(bounds_rect);
1745 node.get_mut_layout_box().set_content_bounds(content_rect);
1746 Some(bounds_rect)
1747 } else {
1748 None
1749 };
1750 if let Some(blue_val) = updated_b {
1751 let is_clipped: bool =
1752 matches!(c_overflow, Overflow::Hidden | Overflow::Scroll);
1753 let Self { split_cache, .. } = self;
1754 split_cache.mark_post_stretched(
1755 child_id,
1756 blue_val,
1757 c_transform,
1758 is_clipped,
1759 );
1760 }
1761 }
1762 if stretch_w {
1763 self.get_mut_flex_items_scratch().push(FlexItemRecord::new(
1764 child_id,
1765 Size::new(target_w, target_h),
1766 margin,
1767 AlignSelf::Stretch,
1768 c_overflow,
1769 0,
1770 ));
1771 }
1772 }
1773 if is_row {
1774 main_cursor += target_w + margin.horizontal();
1775 max_cross = max_cross.max(target_h + margin.vertical());
1776 } else {
1777 main_cursor += target_h + margin.vertical();
1778 max_cross = max_cross.max(target_w + margin.horizontal());
1779 }
1780 if offset + 1 < count_all {
1781 main_cursor += main_gap;
1782 }
1783 }
1784 let records_end: usize = self.get_flex_items_scratch().len();
1785 if mode == LayoutRunMode::PerformLayout
1786 && records_end > records_start
1787 && max_cross > cross_avail + 0.5
1788 {
1789 for rec_idx in records_start..records_end {
1790 let rec: FlexItemRecord = self.get_flex_items_scratch()[rec_idx];
1791 let widened: f32 =
1792 (max_cross - rec.get_margin().horizontal()).max(rec.get_size().get_width());
1793 self.record_layout_snapshot(tree, rec.get_node_id());
1794 let updated_opt: Option<(Rect, Transform2D)> = if let Some(node) =
1795 tree.get_node_mut(rec.get_node_id())
1796 {
1797 let transform_value: Transform2D =
1798 node.style_ref().paint_ref().get_transform();
1799 let padding_edges: Edges = node.style_ref().layout_ref().get_padding();
1800 let border_width: f32 =
1801 node.style_ref().paint_ref().get_border().get_width();
1802 let mut bounds_rect: Rect = node.get_layout_box().get_bounds();
1803 bounds_rect.set_width(widened);
1804 let mut content_rect: Rect = node.get_layout_box().get_content_bounds();
1805 content_rect.set_width(
1806 (widened - padding_edges.horizontal() - border_width * 2.0).max(0.0),
1807 );
1808 node.get_mut_layout_box().set_bounds(bounds_rect);
1809 node.get_mut_layout_box().set_content_bounds(content_rect);
1810 Some((bounds_rect, transform_value))
1811 } else {
1812 None
1813 };
1814 if let Some((byte_val, transform_val)) = updated_opt {
1815 let is_clipped: bool =
1816 matches!(rec.get_overflow(), Overflow::Hidden | Overflow::Scroll);
1817 let Self { split_cache, .. } = self;
1818 split_cache.mark_post_stretched(
1819 rec.get_node_id(),
1820 byte_val,
1821 transform_val,
1822 is_clipped,
1823 );
1824 }
1825 }
1826 }
1827 self.get_mut_flex_items_scratch().truncate(records_start);
1828 let out_size: Size = if is_row {
1829 Size::new(main_cursor, max_cross)
1830 } else {
1831 Size::new(max_cross, main_cursor)
1832 };
1833 return (out_size, agg_deps, agg_min_aw);
1834 }
1835 let items_start: usize = self.get_flex_items_scratch().len();
1836 let probe_template: AvailableSpace = AvailableSpace::new(
1837 inner_avail.get_width(),
1838 inner_avail.get_height(),
1839 viewport,
1840 content_origin.get_x(),
1841 content_origin.get_y(),
1842 );
1843 let mut total_grow: f32 = 0.0;
1844 let mut single_line_main: f32 = 0.0;
1845 let mut single_line_cross: f32 = 0.0;
1846 let mut pass1_deps: u8 = LAYOUT_DEP_NONE;
1847 let mut pass1_min_aw: f32 = 0.0;
1848 for flow_idx in flow_start..flow_end {
1849 let child_id: NodeId = self.get_flow_scratch()[flow_idx];
1850 let mut rec: FlexItemRecord = FlexItemRecord::new(
1851 child_id,
1852 Size::default(),
1853 Edges::default(),
1854 AlignSelf::Auto,
1855 Overflow::Visible,
1856 FLEX_ITEM_DEFAULT_FLAGS,
1857 );
1858 let _: &mut FlexItemRecord = rec.set_flex_shrink(1.0);
1859 let mut zero_basis_size: Option<Size> = None;
1860 let mut explicit_basis_main: Option<f32> = None;
1861 let mut explicit_min_main: Option<f32> = None;
1862 let mut explicit_max_main: Option<f32> = None;
1863 let mut is_unwrapped_clip_text: bool = false;
1864 let mut has_px_main: bool = false;
1865 if let Some(node) = tree.get_node(child_id) {
1866 let child_style: &Style = node.style_ref();
1867 let child_layout: &LayoutStyle = child_style.layout_ref();
1868 let child_text: &TextStyle = child_style.text_ref();
1869 let width_len: Length = child_layout.get_width();
1870 let height_len: Length = child_layout.get_height();
1871 let basis_len: Length = child_layout.get_flex_basis();
1872 has_px_main = matches!(if is_row { width_len } else { height_len }, Length::Px(_));
1873 let min_main_len: Length = if is_row {
1874 child_layout.get_min_width()
1875 } else {
1876 child_layout.get_min_height()
1877 };
1878 let max_main_len: Length = if is_row {
1879 child_layout.get_max_width()
1880 } else {
1881 child_layout.get_max_height()
1882 };
1883 if matches!(min_main_len, Length::Percent(_))
1884 || matches!(max_main_len, Length::Percent(_))
1885 || matches!(basis_len, Length::Percent(_))
1886 {
1887 pass1_deps |= if is_row {
1888 LAYOUT_DEP_AVAIL_W
1889 } else {
1890 LAYOUT_DEP_AVAIL_H
1891 };
1892 }
1893 if Self::is_viewport_length(min_main_len)
1894 || Self::is_viewport_length(max_main_len)
1895 || Self::is_viewport_length(basis_len)
1896 {
1897 pass1_deps |= LAYOUT_DEP_VIEWPORT;
1898 }
1899 explicit_min_main = min_main_len.resolve(main_avail, viewport);
1900 explicit_max_main = max_main_len.resolve(main_avail, viewport);
1901 explicit_basis_main = basis_len.resolve(main_avail, viewport);
1902 is_unwrapped_clip_text = matches!(node.get_kind(), NodeKind::Text(_))
1903 && child_text.get_overflow() == TextOverflow::Clip;
1904 let grow: f32 = child_layout.get_flex_grow();
1905 let shrink: f32 = child_layout.get_flex_shrink().max(0.0);
1906 let width_auto: bool = width_len == Length::Auto;
1907 let height_auto: bool = height_len == Length::Auto;
1908 let mut factor_val: u16 = 0;
1909 if width_auto {
1910 factor_val |= FLEX_ITEM_WIDTH_AUTO;
1911 }
1912 if height_auto {
1913 factor_val |= FLEX_ITEM_HEIGHT_AUTO;
1914 }
1915 if matches!(width_len, Length::Percent(_))
1916 || matches!(child_layout.get_min_width(), Length::Percent(_))
1917 || matches!(child_layout.get_max_width(), Length::Percent(_))
1918 {
1919 factor_val |= FLEX_ITEM_WIDTH_PERCENT;
1920 }
1921 if matches!(height_len, Length::Percent(_))
1922 || matches!(child_layout.get_min_height(), Length::Percent(_))
1923 || matches!(child_layout.get_max_height(), Length::Percent(_))
1924 {
1925 factor_val |= FLEX_ITEM_HEIGHT_PERCENT;
1926 }
1927 if let Some(basis_raw) = explicit_basis_main {
1928 let mut clamped_basis: f32 = basis_raw.max(explicit_min_main.unwrap_or(0.0));
1929 if let Some(max_bound) = explicit_max_main {
1930 clamped_basis = clamped_basis.min(max_bound);
1931 }
1932 if is_row {
1933 if matches!(height_len, Length::Percent(_)) {
1934 pass1_deps |= LAYOUT_DEP_AVAIL_H;
1935 }
1936 if Self::is_viewport_length(height_len) {
1937 pass1_deps |= LAYOUT_DEP_VIEWPORT;
1938 }
1939 if let Some(resolved_h) = height_len.resolve(cross_avail, viewport) {
1940 zero_basis_size = Some(Size::new(clamped_basis, resolved_h));
1941 } else if grow > 0.0 {
1942 zero_basis_size = Some(Size::new(clamped_basis, 0.0));
1943 }
1944 } else {
1945 if matches!(width_len, Length::Percent(_)) {
1946 pass1_deps |= LAYOUT_DEP_AVAIL_W;
1947 }
1948 if Self::is_viewport_length(width_len) {
1949 pass1_deps |= LAYOUT_DEP_VIEWPORT;
1950 }
1951 if let Some(resolved_w) = width_len.resolve(cross_avail, viewport) {
1952 zero_basis_size = Some(Size::new(resolved_w, clamped_basis));
1953 } else if grow > 0.0 {
1954 zero_basis_size = Some(Size::new(0.0, clamped_basis));
1955 }
1956 }
1957 } else {
1958 let zero_basis: bool = !wrap_enabled
1959 && grow > 0.0
1960 && if is_row { width_auto } else { height_auto };
1961 if zero_basis {
1962 if is_row {
1963 if matches!(height_len, Length::Percent(_)) {
1964 pass1_deps |= LAYOUT_DEP_AVAIL_H;
1965 }
1966 if Self::is_viewport_length(height_len) {
1967 pass1_deps |= LAYOUT_DEP_VIEWPORT;
1968 }
1969 let height_val: f32 =
1970 height_len.resolve(cross_avail, viewport).unwrap_or(0.0);
1971 zero_basis_size = Some(Size::new(0.0, height_val));
1972 } else {
1973 if matches!(width_len, Length::Percent(_)) {
1974 pass1_deps |= LAYOUT_DEP_AVAIL_W;
1975 }
1976 if Self::is_viewport_length(width_len) {
1977 pass1_deps |= LAYOUT_DEP_VIEWPORT;
1978 }
1979 let width_val: f32 =
1980 width_len.resolve(cross_avail, viewport).unwrap_or(0.0);
1981 zero_basis_size = Some(Size::new(width_val, 0.0));
1982 }
1983 }
1984 }
1985 let _: &mut FlexItemRecord = rec.set_margin(child_layout.get_margin());
1986 let _: &mut FlexItemRecord = rec.set_flex_grow(grow);
1987 let _: &mut FlexItemRecord = rec.set_flex_shrink(shrink);
1988 let _: &mut FlexItemRecord = rec.set_align_self(child_layout.get_align_self());
1989 let _: &mut FlexItemRecord = rec.set_overflow(child_layout.get_overflow());
1990 let _: &mut FlexItemRecord = rec.set_flags(factor_val);
1991 }
1992 total_grow += rec.get_flex_grow();
1993 let mut child_size: Size = if let Some(zb) = zero_basis_size {
1994 zb
1995 } else {
1996 let (measured, c_deps, c_min_aw): (Size, u8, f32) = self.layout_node_mode(
1997 tree,
1998 child_id,
1999 probe_template,
2000 LayoutRunMode::ComputeSize,
2001 stats,
2002 );
2003 pass1_deps |= c_deps;
2004 if c_min_aw > 0.0 {
2005 pass1_min_aw = pass1_min_aw.max(c_min_aw + rec.get_margin().horizontal());
2006 }
2007 measured
2008 };
2009 if let Some(basis_raw) = explicit_basis_main
2010 && zero_basis_size.is_none()
2011 {
2012 let mut clamped_basis: f32 = basis_raw.max(explicit_min_main.unwrap_or(0.0));
2013 if let Some(max_bound) = explicit_max_main {
2014 clamped_basis = clamped_basis.min(max_bound);
2015 }
2016 if is_row {
2017 child_size.set_width(clamped_basis);
2018 } else {
2019 child_size.set_height(clamped_basis);
2020 }
2021 }
2022 let resolved_min_main: f32 = if let Some(explicit_min) = explicit_min_main {
2023 explicit_min.max(0.0)
2024 } else if rec.get_overflow() == Overflow::Scroll
2025 || (rec.get_overflow() == Overflow::Hidden && !has_px_main)
2026 {
2027 0.0
2028 } else if is_row {
2029 if is_unwrapped_clip_text {
2030 child_size.get_width()
2031 } else {
2032 0.0
2033 }
2034 } else {
2035 child_size.get_height()
2036 };
2037 let _: &mut FlexItemRecord = rec.set_min_main(resolved_min_main);
2038 if !wrap_enabled {
2039 if is_row {
2040 single_line_main += child_size.get_width() + rec.get_margin().horizontal();
2041 single_line_cross = single_line_cross
2042 .max(child_size.get_height() + rec.get_margin().vertical());
2043 } else {
2044 single_line_main += child_size.get_height() + rec.get_margin().vertical();
2045 single_line_cross = single_line_cross
2046 .max(child_size.get_width() + rec.get_margin().horizontal());
2047 }
2048 }
2049 let _: &mut FlexItemRecord = rec.set_size(child_size);
2050 self.get_mut_flex_items_scratch().push(rec);
2051 }
2052 let items_end: usize = self.get_flex_items_scratch().len();
2053 if !wrap_enabled {
2054 let mut line_ctx: FlexLineContext = FlexLineContext::new(
2055 frame,
2056 is_row,
2057 main_gap,
2058 main_avail,
2059 cross_avail,
2060 total_grow,
2061 mode,
2062 );
2063 let _: &mut FlexLineContext = line_ctx.set_line_count(1);
2064 let (line_size, line_deps, line_min_aw): (Size, u8, f32) = self.layout_flex_line(
2065 tree,
2066 (items_start, items_end),
2067 0.0,
2068 Some((single_line_main, single_line_cross)),
2069 line_ctx,
2070 stats,
2071 );
2072 self.get_mut_flex_items_scratch().truncate(items_start);
2073 return (
2074 line_size,
2075 pass1_deps | line_deps,
2076 pass1_min_aw.max(line_min_aw),
2077 );
2078 }
2079 let lines_start: usize = self.get_flex_lines_scratch().len();
2080 let mut cur_line_start: usize = items_start;
2081 let mut line_main: f32 = 0.0;
2082 for item_idx in items_start..items_end {
2083 let item: FlexItemRecord = self.get_flex_items_scratch()[item_idx];
2084 let item_main: f32 = if is_row {
2085 item.get_size().get_width() + item.get_margin().horizontal()
2086 } else {
2087 item.get_size().get_height() + item.get_margin().vertical()
2088 };
2089 let line_empty: bool = item_idx == cur_line_start;
2090 let needed: f32 = if line_empty {
2091 item_main
2092 } else {
2093 line_main + main_gap + item_main
2094 };
2095 if !line_empty && needed > main_avail && main_avail > 0.0 {
2096 self.get_mut_flex_lines_scratch()
2097 .push((cur_line_start, item_idx));
2098 cur_line_start = item_idx;
2099 line_main = item_main;
2100 } else {
2101 line_main = needed;
2102 }
2103 }
2104 if cur_line_start < items_end {
2105 self.get_mut_flex_lines_scratch()
2106 .push((cur_line_start, items_end));
2107 }
2108 let lines_end: usize = self.get_flex_lines_scratch().len();
2109 let mut cross_cursor: f32 = 0.0;
2110 let mut max_main_extent: f32 = 0.0;
2111 let line_count: usize = lines_end.saturating_sub(lines_start);
2112 let mut wrap_deps: u8 = pass1_deps;
2113 let mut wrap_min_aw: f32 = pass1_min_aw;
2114 if is_row {
2115 if line_count > 1 {
2116 wrap_deps |= LAYOUT_DEP_AVAIL_W;
2117 } else {
2118 wrap_min_aw = wrap_min_aw.max(line_main + 0.5);
2119 }
2120 } else {
2121 wrap_deps |= LAYOUT_DEP_AVAIL_H;
2122 }
2123 for line_offset in 0..line_count {
2124 let line_range: (usize, usize) =
2125 self.get_flex_lines_scratch()[lines_start + line_offset];
2126 let mut line_grow: f32 = 0.0;
2127 for item_idx in line_range.0..line_range.1 {
2128 line_grow += self.get_flex_items_scratch()[item_idx].get_flex_grow();
2129 }
2130 let mut line_ctx: FlexLineContext = FlexLineContext::new(
2131 frame,
2132 is_row,
2133 main_gap,
2134 main_avail,
2135 cross_avail,
2136 line_grow,
2137 mode,
2138 );
2139 let _: &mut FlexLineContext = line_ctx.set_wrap_enabled(true);
2140 let _: &mut FlexLineContext = line_ctx.set_line_count(line_count);
2141 let (line_size, l_deps, l_min_aw): (Size, u8, f32) =
2142 self.layout_flex_line(tree, line_range, cross_cursor, None, line_ctx, stats);
2143 wrap_deps |= l_deps;
2144 wrap_min_aw = wrap_min_aw.max(l_min_aw);
2145 if is_row {
2146 max_main_extent = max_main_extent.max(line_size.get_width());
2147 cross_cursor += line_size.get_height();
2148 } else {
2149 max_main_extent = max_main_extent.max(line_size.get_height());
2150 cross_cursor += line_size.get_width();
2151 }
2152 if line_offset + 1 < line_count {
2153 cross_cursor += cross_gap;
2154 }
2155 }
2156 self.get_mut_flex_lines_scratch().truncate(lines_start);
2157 self.get_mut_flex_items_scratch().truncate(items_start);
2158 let out_size: Size = if is_row {
2159 Size::new(max_main_extent, cross_cursor)
2160 } else {
2161 Size::new(cross_cursor, max_main_extent)
2162 };
2163 (out_size, wrap_deps, wrap_min_aw)
2164 }
2165
2166 #[inline(always)]
2182 fn layout_flex_line(
2183 &mut self,
2184 tree: &mut SceneTree,
2185 line_range: (usize, usize),
2186 cross_cursor: f32,
2187 precomputed: Option<(f32, f32)>,
2188 ctx: FlexLineContext,
2189 stats: &mut LayoutStats,
2190 ) -> (Size, u8, f32) {
2191 let (line_start, line_end): (usize, usize) = line_range;
2192 let frame: ContainerFrame = ctx.get_frame();
2193 let is_row: bool = ctx.get_is_row();
2194 let wrap_enabled: bool = ctx.get_wrap_enabled();
2195 let main_gap: f32 = ctx.get_main_gap();
2196 let main_avail: f32 = ctx.get_main_avail();
2197 let cross_avail: f32 = ctx.get_cross_avail();
2198 let total_grow: f32 = ctx.get_total_grow();
2199 let line_count: usize = ctx.get_line_count();
2200 let mode: LayoutRunMode = ctx.get_mode();
2201 let parent_layout: LayoutStyle = frame.get_parent_layout();
2202 let inner_avail: Size = frame.get_inner_avail();
2203 let viewport: Size = frame.get_viewport();
2204 let content_origin: Point = frame.get_content_origin();
2205 let count: usize = line_end.saturating_sub(line_start);
2206 let gaps_total: f32 = if count > 1 {
2207 main_gap * ((count - 1) as f32)
2208 } else {
2209 0.0
2210 };
2211 let mut line_deps: u8 = LAYOUT_DEP_NONE;
2212 let mut line_min_aw: f32 = 0.0;
2213 let has_definite_or_grow: bool = frame.get_definite_width() || total_grow > 0.0;
2214 let effective_main_avail: f32 = if is_row {
2215 if has_definite_or_grow || wrap_enabled {
2216 if total_grow > 0.0 {
2217 line_deps |= LAYOUT_DEP_AVAIL_W;
2218 }
2219 main_avail
2220 } else {
2221 let min_w_len: Length = parent_layout.get_min_width();
2222 if matches!(min_w_len, Length::Percent(_)) || Self::is_viewport_length(min_w_len) {
2223 line_deps |= LAYOUT_DEP_VIEWPORT;
2224 }
2225 frame.get_min_inner_size().get_width()
2226 }
2227 } else if frame.get_definite_height() {
2228 if total_grow > 0.0 {
2229 line_deps |= LAYOUT_DEP_AVAIL_H;
2230 }
2231 main_avail
2232 } else {
2233 let min_h_len: Length = parent_layout.get_min_height();
2234 if matches!(min_h_len, Length::Percent(_)) || Self::is_viewport_length(min_h_len) {
2235 line_deps |= LAYOUT_DEP_VIEWPORT;
2236 }
2237 frame.get_min_inner_size().get_height()
2238 };
2239 let has_main_cap: bool = parent_layout.get_overflow() != Overflow::Scroll
2240 && if is_row {
2241 frame.get_definite_width() || parent_layout.get_max_width() != Length::Auto
2242 } else {
2243 frame.get_definite_height() || parent_layout.get_max_height() != Length::Auto
2244 };
2245 let pre_overflows: bool = !wrap_enabled
2246 && has_main_cap
2247 && precomputed
2248 .map(|(items_main, _): (f32, f32)| items_main + gaps_total > main_avail + 0.5)
2249 .unwrap_or(false);
2250 let mut line_shrunk: bool = false;
2251 let (final_used_main, line_cross): (f32, f32) = if let Some((items_main, pre_cross)) =
2252 precomputed
2253 && total_grow == 0.0
2254 && !pre_overflows
2255 {
2256 (items_main + gaps_total, pre_cross)
2257 } else {
2258 let used_main: f32 = if let Some((items_main, _)) = precomputed {
2259 items_main + gaps_total
2260 } else {
2261 let mut acc: f32 = gaps_total;
2262 for item_idx in line_start..line_end {
2263 let item: FlexItemRecord = self.get_flex_items_scratch()[item_idx];
2264 let sz: f32 = if is_row {
2265 item.get_size().get_width() + item.get_margin().horizontal()
2266 } else {
2267 item.get_size().get_height() + item.get_margin().vertical()
2268 };
2269 acc += sz;
2270 }
2271 acc
2272 };
2273 let free_main: f32 = (effective_main_avail - used_main).max(0.0);
2274 let mut any_main_resized: bool = false;
2275 if total_grow > 0.0 && free_main > 0.0 {
2276 for item_idx in line_start..line_end {
2277 let item: &mut FlexItemRecord =
2278 &mut self.get_mut_flex_items_scratch()[item_idx];
2279 let grow: f32 = item.get_flex_grow();
2280 if grow > 0.0 {
2281 let extra: f32 = free_main * (grow / total_grow);
2282 if is_row {
2283 let next_w: f32 = item.get_size().get_width() + extra;
2284 item.get_mut_size().set_width(next_w);
2285 } else {
2286 let next_h: f32 = item.get_size().get_height() + extra;
2287 item.get_mut_size().set_height(next_h);
2288 }
2289 any_main_resized = true;
2290 }
2291 }
2292 } else if !wrap_enabled && has_main_cap && used_main > main_avail + 0.5 {
2293 line_deps |= if is_row {
2294 LAYOUT_DEP_AVAIL_W
2295 } else {
2296 LAYOUT_DEP_AVAIL_H
2297 };
2298 let mut rem_overflow: f32 = used_main - main_avail;
2299 for _ in 0..4usize {
2300 if rem_overflow <= 0.05 {
2301 break;
2302 }
2303 let mut weighted_sum: f32 = 0.0;
2304 for item_idx in line_start..line_end {
2305 let item: FlexItemRecord = self.get_flex_items_scratch()[item_idx];
2306 let cur_main: f32 = if is_row {
2307 item.get_size().get_width()
2308 } else {
2309 item.get_size().get_height()
2310 };
2311 if item.get_flex_shrink() > 0.0 && cur_main > item.get_min_main() + 0.05 {
2312 weighted_sum += item.get_flex_shrink() * cur_main.max(1.0);
2313 }
2314 }
2315 if weighted_sum <= 0.0 {
2316 break;
2317 }
2318 let pass_overflow: f32 = rem_overflow;
2319 let mut absorbed: f32 = 0.0;
2320 for item_idx in line_start..line_end {
2321 let item: &mut FlexItemRecord =
2322 &mut self.get_mut_flex_items_scratch()[item_idx];
2323 let cur_main: f32 = if is_row {
2324 item.get_size().get_width()
2325 } else {
2326 item.get_size().get_height()
2327 };
2328 let min_m: f32 = item.get_min_main();
2329 let shrink_f: f32 = item.get_flex_shrink();
2330 if shrink_f > 0.0 && cur_main > min_m + 0.05 {
2331 let width_val: f32 = shrink_f * cur_main.max(1.0);
2332 let raw_cut: f32 = pass_overflow * (width_val / weighted_sum);
2333 let next_main: f32 = (cur_main - raw_cut).max(min_m);
2334 let cut: f32 = cur_main - next_main;
2335 if cut > 0.01 {
2336 if is_row {
2337 item.get_mut_size().set_width(next_main);
2338 } else {
2339 item.get_mut_size().set_height(next_main);
2340 }
2341 absorbed += cut;
2342 any_main_resized = true;
2343 line_shrunk = true;
2344 }
2345 }
2346 }
2347 if absorbed <= 0.01 {
2348 break;
2349 }
2350 rem_overflow = (rem_overflow - absorbed).max(0.0);
2351 }
2352 }
2353 if total_grow > 0.0 || any_main_resized {
2354 for item_idx in line_start..line_end {
2355 let item: FlexItemRecord = self.get_flex_items_scratch()[item_idx];
2356 let resized_item: bool = item.get_flex_grow() > 0.0 || any_main_resized;
2357 let auto_cross: bool = resized_item
2358 && if is_row {
2359 (item.get_flags() & FLEX_ITEM_HEIGHT_AUTO) != 0
2360 } else {
2361 (item.get_flags() & FLEX_ITEM_WIDTH_AUTO) != 0
2362 };
2363 if auto_cross {
2364 let probe_w: f32 = if is_row {
2365 item.get_size().get_width()
2366 } else {
2367 cross_avail
2368 };
2369 let probe_h: f32 = if is_row {
2370 cross_avail
2371 } else {
2372 item.get_size().get_height()
2373 };
2374 let stretch_pw: bool =
2375 is_row && (item.get_flags() & FLEX_ITEM_WIDTH_AUTO) != 0;
2376 let stretch_ph: bool =
2377 !is_row && (item.get_flags() & FLEX_ITEM_HEIGHT_AUTO) != 0;
2378 let probe_sp: AvailableSpace = AvailableSpace::new(
2379 probe_w,
2380 probe_h,
2381 viewport,
2382 content_origin.get_x(),
2383 content_origin.get_y(),
2384 )
2385 .with_stretch(stretch_pw, stretch_ph);
2386 let (measured, c_deps, c_min_aw): (Size, u8, f32) = self.layout_node_mode(
2387 tree,
2388 item.get_node_id(),
2389 probe_sp,
2390 LayoutRunMode::ComputeSize,
2391 stats,
2392 );
2393 line_deps |= c_deps;
2394 if c_min_aw > 0.0 {
2395 line_min_aw =
2396 line_min_aw.max(c_min_aw + item.get_margin().horizontal());
2397 }
2398 let slot: &mut FlexItemRecord =
2399 &mut self.get_mut_flex_items_scratch()[item_idx];
2400 if is_row {
2401 slot.get_mut_size().set_height(measured.get_height());
2402 } else {
2403 slot.get_mut_size().set_width(measured.get_width());
2404 }
2405 }
2406 }
2407 }
2408 let mut sum_main: f32 = gaps_total;
2409 let mut max_cross: f32 = 0.0;
2410 for item_idx in line_start..line_end {
2411 let item: FlexItemRecord = self.get_flex_items_scratch()[item_idx];
2412 if is_row {
2413 sum_main += item.get_size().get_width() + item.get_margin().horizontal();
2414 max_cross =
2415 max_cross.max(item.get_size().get_height() + item.get_margin().vertical());
2416 } else {
2417 sum_main += item.get_size().get_height() + item.get_margin().vertical();
2418 max_cross =
2419 max_cross.max(item.get_size().get_width() + item.get_margin().horizontal());
2420 }
2421 }
2422 (sum_main, max_cross)
2423 };
2424 if mode == LayoutRunMode::ComputeSize {
2425 let sz: Size = if is_row {
2426 Size::new(final_used_main, line_cross)
2427 } else {
2428 Size::new(line_cross, final_used_main)
2429 };
2430 return (sz, line_deps, line_min_aw);
2431 }
2432 let rem: f32 = (effective_main_avail - final_used_main).max(0.0);
2433 let (mut main_cursor, extra_step): (f32, f32) = match parent_layout.get_justify_content() {
2434 JustifyContent::Start => (0.0, 0.0),
2435 JustifyContent::Center => (rem * 0.5, 0.0),
2436 JustifyContent::End => (rem, 0.0),
2437 JustifyContent::SpaceBetween => {
2438 if count > 1 {
2439 (0.0, rem / ((count - 1) as f32))
2440 } else {
2441 (0.0, 0.0)
2442 }
2443 }
2444 JustifyContent::SpaceAround => {
2445 let slot: f32 = rem / (count.max(1) as f32);
2446 (slot * 0.5, slot)
2447 }
2448 JustifyContent::SpaceEvenly => {
2449 let slot: f32 = rem / ((count + 1) as f32);
2450 (slot, slot)
2451 }
2452 };
2453 let min_cross: f32 = if is_row {
2454 frame.get_min_inner_size().get_height()
2455 } else {
2456 frame.get_min_inner_size().get_width()
2457 };
2458 let definite_cross: bool = !is_row || frame.get_definite_height();
2459 let effective_line_cross: f32 = if line_count == 1 {
2460 let floor_cross: f32 = if definite_cross && (!wrap_enabled || is_row) {
2461 cross_avail.max(min_cross)
2462 } else {
2463 min_cross
2464 };
2465 line_cross.max(floor_cross)
2466 } else {
2467 line_cross
2468 };
2469 let mut actual_line_cross: f32 = line_cross;
2470 let parent_align: AlignItems = parent_layout.get_align_items();
2471 for idx in 0..count {
2472 let rec: FlexItemRecord = self.get_flex_items_scratch()[line_start + idx];
2473 let child_id: NodeId = rec.get_node_id();
2474 let child_size: Size = rec.get_size();
2475 let margin: Edges = rec.get_margin();
2476 let width_auto: bool = (rec.get_flags() & FLEX_ITEM_WIDTH_AUTO) != 0;
2477 let height_auto: bool = (rec.get_flags() & FLEX_ITEM_HEIGHT_AUTO) != 0;
2478 let item_cross: f32 = if is_row {
2479 child_size.get_height()
2480 } else {
2481 child_size.get_width()
2482 };
2483 let align: AlignItems = match rec.get_align_self() {
2484 AlignSelf::Auto => parent_align,
2485 AlignSelf::Stretch => AlignItems::Stretch,
2486 AlignSelf::Start => AlignItems::Start,
2487 AlignSelf::Center => AlignItems::Center,
2488 AlignSelf::End => AlignItems::End,
2489 };
2490 let avail_item_cross: f32 = (effective_line_cross
2491 - if is_row {
2492 margin.vertical()
2493 } else {
2494 margin.horizontal()
2495 })
2496 .max(0.0);
2497 let cross_offset: f32 = match align {
2498 AlignItems::Start | AlignItems::Stretch => 0.0,
2499 AlignItems::Center => ((avail_item_cross - item_cross) * 0.5).max(0.0),
2500 AlignItems::End => (avail_item_cross - item_cross).max(0.0),
2501 };
2502 let main_resized_item: bool = rec.get_flex_grow() > 0.0 || line_shrunk;
2503 let auto_cross_grow: bool =
2504 main_resized_item && if is_row { height_auto } else { width_auto };
2505 let target_w: f32 = if !is_row && align == AlignItems::Stretch && width_auto {
2506 avail_item_cross.max(child_size.get_width())
2507 } else {
2508 child_size.get_width()
2509 };
2510 let target_h: f32 = if is_row && align == AlignItems::Stretch && height_auto {
2511 avail_item_cross.max(child_size.get_height())
2512 } else {
2513 child_size.get_height()
2514 };
2515 let (pos_x, pos_y): (f32, f32) = if is_row {
2516 (
2517 content_origin.get_x() + main_cursor + margin.get_left(),
2518 content_origin.get_y() + cross_cursor + cross_offset + margin.get_top(),
2519 )
2520 } else {
2521 (
2522 content_origin.get_x() + cross_cursor + cross_offset + margin.get_left(),
2523 content_origin.get_y() + main_cursor + margin.get_top(),
2524 )
2525 };
2526 let stretch_child_w: bool = !(auto_cross_grow && !is_row)
2527 && ((!is_row && align == AlignItems::Stretch && width_auto)
2528 || (is_row && main_resized_item && width_auto));
2529 let stretch_child_h: bool = !(auto_cross_grow && is_row)
2530 && ((is_row && align == AlignItems::Stretch && height_auto)
2531 || (!is_row && main_resized_item && height_auto));
2532 let space_w: f32 =
2533 if !stretch_child_w && (rec.get_flags() & FLEX_ITEM_WIDTH_PERCENT) != 0 {
2534 inner_avail.get_width()
2535 } else if auto_cross_grow && !is_row {
2536 cross_avail
2537 } else {
2538 target_w
2539 };
2540 let space_h: f32 =
2541 if !stretch_child_h && (rec.get_flags() & FLEX_ITEM_HEIGHT_PERCENT) != 0 {
2542 inner_avail.get_height()
2543 } else if auto_cross_grow && is_row {
2544 cross_avail
2545 } else {
2546 target_h
2547 };
2548 let final_space: AvailableSpace =
2549 AvailableSpace::new(space_w, space_h, viewport, pos_x, pos_y)
2550 .with_stretch(stretch_child_w, stretch_child_h);
2551 let (placed, _, _): (Size, u8, f32) = self.layout_node_mode(
2552 tree,
2553 child_id,
2554 final_space,
2555 LayoutRunMode::PerformLayout,
2556 stats,
2557 );
2558 let is_clipped: bool =
2559 matches!(rec.get_overflow(), Overflow::Hidden | Overflow::Scroll);
2560 let mut placed_final: Size = placed;
2561 if auto_cross_grow && matches!(align, AlignItems::Center | AlignItems::End) {
2562 let actual_item_cross: f32 = if is_row {
2563 placed_final.get_height()
2564 } else {
2565 placed_final.get_width()
2566 };
2567 let refined_offset: f32 = match align {
2568 AlignItems::Center => ((avail_item_cross - actual_item_cross) * 0.5).max(0.0),
2569 AlignItems::End => (avail_item_cross - actual_item_cross).max(0.0),
2570 AlignItems::Start | AlignItems::Stretch => cross_offset,
2571 };
2572 let delta_cross: f32 = refined_offset - cross_offset;
2573 if delta_cross.abs() > 0.001 {
2574 let (next_x, next_y): (f32, f32) = if is_row {
2575 (pos_x, pos_y + delta_cross)
2576 } else {
2577 (pos_x + delta_cross, pos_y)
2578 };
2579 let shifted_space: AvailableSpace =
2580 AvailableSpace::new(space_w, space_h, viewport, next_x, next_y)
2581 .with_stretch(stretch_child_w, stretch_child_h);
2582 let (re_placed, _, _): (Size, u8, f32) = self.layout_node_mode(
2583 tree,
2584 child_id,
2585 shifted_space,
2586 LayoutRunMode::PerformLayout,
2587 stats,
2588 );
2589 placed_final = re_placed;
2590 }
2591 }
2592 let (desired_w, desired_h): (f32, f32) = if auto_cross_grow {
2593 if is_row {
2594 actual_line_cross =
2595 actual_line_cross.max(placed_final.get_height() + margin.vertical());
2596 (target_w, placed_final.get_height())
2597 } else {
2598 actual_line_cross =
2599 actual_line_cross.max(placed_final.get_width() + margin.horizontal());
2600 (placed_final.get_width(), target_h)
2601 }
2602 } else if is_clipped || main_resized_item {
2603 (target_w, target_h)
2604 } else {
2605 (
2606 target_w.max(placed_final.get_width()),
2607 target_h.max(placed_final.get_height()),
2608 )
2609 };
2610 let needs_box_adjust: bool =
2611 tree.get_node(child_id)
2612 .is_some_and(|scene_node: &SceneNode| {
2613 let rect_bounds: Rect = scene_node.get_layout_box().get_bounds();
2614 (rect_bounds.get_width() - desired_w).abs() > 0.001
2615 || (rect_bounds.get_height() - desired_h).abs() > 0.001
2616 });
2617 if needs_box_adjust {
2618 self.record_layout_snapshot(tree, child_id);
2619 let adjusted_opt: Option<(Rect, Transform2D)> = if let Some(node) =
2620 tree.get_node_mut(child_id)
2621 {
2622 let pad: Edges = node.style_ref().layout_ref().get_padding();
2623 let bloom_width: f32 = node.style_ref().paint_ref().get_border().get_width();
2624 let transform_val: Transform2D = node.style_ref().paint_ref().get_transform();
2625 let mut clip_bounds: Rect = node.get_layout_box().get_bounds();
2626 clip_bounds.set_width(desired_w);
2627 clip_bounds.set_height(desired_h);
2628 let mut region_bounds: Rect = node.get_layout_box().get_content_bounds();
2629 region_bounds
2630 .set_width((desired_w - pad.horizontal() - bloom_width * 2.0).max(0.0));
2631 region_bounds
2632 .set_height((desired_h - pad.vertical() - bloom_width * 2.0).max(0.0));
2633 node.get_mut_layout_box().set_bounds(clip_bounds);
2634 node.get_mut_layout_box().set_content_bounds(region_bounds);
2635 Some((clip_bounds, transform_val))
2636 } else {
2637 None
2638 };
2639 if let Some((blue_val, object_transform)) = adjusted_opt {
2640 let Self { split_cache, .. } = self;
2641 split_cache.mark_post_stretched(
2642 child_id,
2643 blue_val,
2644 object_transform,
2645 is_clipped,
2646 );
2647 }
2648 }
2649 let advance: f32 = if is_row {
2650 target_w + margin.horizontal()
2651 } else {
2652 target_h + margin.vertical()
2653 };
2654 main_cursor += advance;
2655 if idx + 1 < count {
2656 main_cursor += main_gap + extra_step;
2657 }
2658 }
2659 let sz: Size = if is_row {
2660 Size::new(final_used_main, actual_line_cross)
2661 } else {
2662 Size::new(actual_line_cross, final_used_main)
2663 };
2664 (sz, line_deps, line_min_aw)
2665 }
2666
2667 fn layout_grid(
2682 &mut self,
2683 tree: &mut SceneTree,
2684 flow_range: (usize, usize),
2685 frame: ContainerFrame,
2686 mode: LayoutRunMode,
2687 stats: &mut LayoutStats,
2688 ) -> (Size, u8, f32) {
2689 let (flow_start, flow_end): (usize, usize) = flow_range;
2690 if flow_start >= flow_end {
2691 return (Size::default(), LAYOUT_DEP_NONE, 0.0);
2692 }
2693 let parent_layout: LayoutStyle = frame.get_parent_layout();
2694 let inner_avail: Size = frame.get_inner_avail();
2695 let viewport: Size = frame.get_viewport();
2696 let content_origin: Point = frame.get_content_origin();
2697 let cols: usize = (parent_layout.get_grid_columns() as usize).max(1);
2698 let col_gap: f32 = parent_layout.get_column_gap();
2699 let row_gap: f32 = parent_layout.get_row_gap();
2700 let total_col_gaps: f32 = col_gap * ((cols.saturating_sub(1)) as f32);
2701 let col_w: f32 = ((inner_avail.get_width() - total_col_gaps) / (cols as f32)).max(0.0);
2702 let col_step: f32 = col_w + col_gap;
2703 let origin_x: f32 = content_origin.get_x();
2704 let origin_y: f32 = content_origin.get_y();
2705 let inner_h: f32 = inner_avail.get_height();
2706 let mut y_cursor: f32 = 0.0;
2707 let mut idx: usize = flow_start;
2708 let mut grid_deps: u8 = LAYOUT_DEP_AVAIL_W;
2709 while idx < flow_end {
2710 let row_end: usize = (idx + cols).min(flow_end);
2711 let mut row_h: f32 = 0.0;
2712 let mut min_row_h: f32 = f32::INFINITY;
2713 let pos_y: f32 = origin_y + y_cursor;
2714 let mut pos_x: f32 = origin_x;
2715 for pos in idx..row_end {
2716 let child_id: NodeId = self.get_flow_scratch()[pos];
2717 let auto_w: bool = tree
2718 .get_node(child_id)
2719 .is_some_and(|scene_node: &SceneNode| {
2720 scene_node.style_ref().layout_ref().get_width() == Length::Auto
2721 });
2722 let cell_space: AvailableSpace =
2723 AvailableSpace::new(col_w, inner_h, viewport, pos_x, pos_y)
2724 .with_stretch(auto_w, false);
2725 let (child_size, c_deps, _): (Size, u8, f32) =
2726 self.layout_node_mode(tree, child_id, cell_space, mode, stats);
2727 grid_deps |= c_deps;
2728 if mode == LayoutRunMode::PerformLayout
2729 && (child_size.get_width() - col_w).abs() > 0.001
2730 {
2731 self.record_layout_snapshot(tree, child_id);
2732 let adjusted_opt: Option<(Rect, Transform2D, bool)> =
2733 if let Some(node) = tree.get_node_mut(child_id) {
2734 let transform_value: Transform2D =
2735 node.style_ref().paint_ref().get_transform();
2736 let padding_edges: Edges = node.style_ref().layout_ref().get_padding();
2737 let border_width: f32 =
2738 node.style_ref().paint_ref().get_border().get_width();
2739 let clipped: bool = matches!(
2740 node.style_ref().layout_ref().get_overflow(),
2741 Overflow::Hidden | Overflow::Scroll
2742 );
2743 let mut bounds_rect: Rect = node.get_layout_box().get_bounds();
2744 bounds_rect.set_width(col_w);
2745 let mut content_rect: Rect = node.get_layout_box().get_content_bounds();
2746 content_rect.set_width(
2747 (col_w - padding_edges.horizontal() - border_width * 2.0).max(0.0),
2748 );
2749 node.get_mut_layout_box().set_bounds(bounds_rect);
2750 node.get_mut_layout_box().set_content_bounds(content_rect);
2751 Some((bounds_rect, transform_value, clipped))
2752 } else {
2753 None
2754 };
2755 if let Some((blue_val, transform_val, clipped)) = adjusted_opt {
2756 let Self { split_cache, .. } = self;
2757 split_cache.mark_post_stretched(child_id, blue_val, transform_val, clipped);
2758 }
2759 }
2760 let ch: f32 = child_size.get_height();
2761 row_h = row_h.max(ch);
2762 min_row_h = min_row_h.min(ch);
2763 pos_x += col_step;
2764 }
2765 if mode == LayoutRunMode::PerformLayout && row_end - idx > 1 && min_row_h < row_h {
2766 for pos in idx..row_end {
2767 let child_id: NodeId = self.get_flow_scratch()[pos];
2768 let needs_stretch: bool =
2769 tree.get_node(child_id)
2770 .is_some_and(|target_node: &SceneNode| {
2771 target_node.get_layout_box().get_bounds().get_height() < row_h
2772 });
2773 if needs_stretch {
2774 self.record_layout_snapshot(tree, child_id);
2775 let adjusted_opt: Option<(Rect, Transform2D, bool)> = if let Some(node) =
2776 tree.get_node_mut(child_id)
2777 {
2778 let transform_value: Transform2D =
2779 node.style_ref().paint_ref().get_transform();
2780 let padding_edges: Edges = node.style_ref().layout_ref().get_padding();
2781 let border_width: f32 =
2782 node.style_ref().paint_ref().get_border().get_width();
2783 let clipped: bool = matches!(
2784 node.style_ref().layout_ref().get_overflow(),
2785 Overflow::Hidden | Overflow::Scroll
2786 );
2787 let mut bounds_rect: Rect = node.get_layout_box().get_bounds();
2788 bounds_rect.set_height(row_h);
2789 let mut content_rect: Rect = node.get_layout_box().get_content_bounds();
2790 content_rect.set_height(
2791 (row_h - padding_edges.vertical() - border_width * 2.0).max(0.0),
2792 );
2793 node.get_mut_layout_box().set_bounds(bounds_rect);
2794 node.get_mut_layout_box().set_content_bounds(content_rect);
2795 Some((bounds_rect, transform_value, clipped))
2796 } else {
2797 None
2798 };
2799 if let Some((byte_val, object_transform, clipped)) = adjusted_opt {
2800 let Self { split_cache, .. } = self;
2801 split_cache.mark_post_stretched(
2802 child_id,
2803 byte_val,
2804 object_transform,
2805 clipped,
2806 );
2807 }
2808 }
2809 }
2810 }
2811 y_cursor += row_h;
2812 if row_end < flow_end {
2813 y_cursor += row_gap;
2814 }
2815 idx = row_end;
2816 }
2817 (Size::new(inner_avail.get_width(), y_cursor), grid_deps, 0.0)
2818 }
2819
2820 fn translate_subtree(&mut self, tree: &mut SceneTree, root_id: NodeId, dx: f32, dy: f32) {
2830 if dx == 0.0 && dy == 0.0 {
2831 return;
2832 }
2833 self.record_layout_snapshot(tree, root_id);
2834 let Some(root_node) = tree.get_node_mut(root_id) else {
2835 return;
2836 };
2837 let Style { layout, .. } = root_node.style_ref();
2838 let root_none_disp: bool = layout.get_display() == Display::None;
2839 if let Some(mut char_item) = root_node.cached_constraint() {
2840 char_item.set_origin_x(char_item.get_origin_x() + dx);
2841 char_item.set_origin_y(char_item.get_origin_y() + dy);
2842 root_node.set_last_constraint(Some(char_item));
2843 }
2844 if root_none_disp {
2845 let Self { split_cache, .. } = self;
2846 split_cache.translate_node_cache(root_id, dx, dy);
2847 return;
2848 }
2849 let mut root_bounds: Rect = root_node.get_layout_box().get_bounds();
2850 root_bounds.set_x(root_bounds.get_x() + dx);
2851 root_bounds.set_y(root_bounds.get_y() + dy);
2852 root_node.get_mut_layout_box().set_bounds(root_bounds);
2853 let mut root_content: Rect = root_node.get_layout_box().get_content_bounds();
2854 root_content.set_x(root_content.get_x() + dx);
2855 root_content.set_y(root_content.get_y() + dy);
2856 root_node
2857 .get_mut_layout_box()
2858 .set_content_bounds(root_content);
2859 let root_children_empty: bool = !tree.has_children(root_id);
2860 if !root_children_empty {
2861 self.get_mut_translate_scratch().clear();
2862 self.get_mut_translate_scratch()
2863 .extend(tree.children_iter(root_id));
2864 }
2865 {
2866 let Self { split_cache, .. } = self;
2867 split_cache.translate_node_cache(root_id, dx, dy);
2868 }
2869 if root_children_empty {
2870 return;
2871 }
2872 while let Some(nid) = self.get_mut_translate_scratch().pop() {
2873 let (is_fixed_pos, is_none_disp): (bool, bool) = match tree.get_node(nid) {
2874 Some(node) => {
2875 let Style { layout, .. } = node.style_ref();
2876 (
2877 layout.get_position() == Position::Fixed,
2878 layout.get_display() == Display::None,
2879 )
2880 }
2881 None => continue,
2882 };
2883 if is_fixed_pos {
2884 continue;
2885 }
2886 if !is_none_disp {
2887 self.record_layout_snapshot(tree, nid);
2888 }
2889 let Some(node) = tree.get_node_mut(nid) else {
2890 continue;
2891 };
2892 if let Some(mut color_item) = node.cached_constraint() {
2893 color_item.set_origin_x(color_item.get_origin_x() + dx);
2894 color_item.set_origin_y(color_item.get_origin_y() + dy);
2895 node.set_last_constraint(Some(color_item));
2896 }
2897 if is_none_disp {
2898 let Self { split_cache, .. } = self;
2899 split_cache.translate_node_cache(nid, dx, dy);
2900 continue;
2901 }
2902 let mut bounds: Rect = node.get_layout_box().get_bounds();
2903 bounds.set_x(bounds.get_x() + dx);
2904 bounds.set_y(bounds.get_y() + dy);
2905 node.get_mut_layout_box().set_bounds(bounds);
2906 let mut content: Rect = node.get_layout_box().get_content_bounds();
2907 content.set_x(content.get_x() + dx);
2908 content.set_y(content.get_y() + dy);
2909 node.get_mut_layout_box().set_content_bounds(content);
2910 let has_children: bool = tree.has_children(nid);
2911 if has_children {
2912 self.get_mut_translate_scratch()
2913 .extend(tree.children_iter(nid));
2914 }
2915 let Self { split_cache, .. } = self;
2916 split_cache.translate_node_cache(nid, dx, dy);
2917 }
2918 }
2919}