Skip to main content

cranpose_ui/layout/
mod.rs

1pub mod core;
2pub mod policies;
3
4use std::{
5    cell::{Cell, RefCell},
6    fmt,
7    mem::size_of,
8    rc::Rc,
9    sync::OnceLock,
10};
11
12use cranpose_core::{
13    Applier, ApplierHost, Composer, ConcreteApplierHost, MemoryApplier, Node, NodeError, NodeId,
14    Phase, RuntimeHandle, SlotTable, SlotsHost, SnapshotStateObserver,
15};
16
17use self::core::Measurable;
18use self::core::Placeable;
19#[cfg(test)]
20use self::core::{HorizontalAlignment, VerticalAlignment};
21use crate::modifier::{
22    collect_semantics_from_modifier, DimensionConstraint, EdgeInsets, Modifier, ModifierNodeSlices,
23    ModifierNodeSlicesDebugStats, Point, Rect as GeometryRect, ResolvedModifiers, Size,
24};
25
26use crate::subcompose_layout::{CachedBatchMeasureInputs, SubcomposeLayoutNode};
27use crate::widgets::nodes::{IntrinsicKind, LayoutNode, LayoutNodeCacheHandles, LayoutState};
28use cranpose_foundation::{
29    text::TextRange, InvalidationKind, ModifierNodeContext, NodeCapabilities,
30    SemanticsConfiguration,
31};
32use cranpose_ui_layout::{Constraints, MeasurePolicy, Placement};
33use web_time::Instant;
34
35/// Runtime context for modifier nodes during measurement.
36///
37/// Unlike `BasicModifierNodeContext`, this context accumulates invalidations
38/// that can be processed after measurement to set dirty flags on the LayoutNode.
39#[derive(Default)]
40pub(crate) struct LayoutNodeContext {
41    invalidations: Vec<InvalidationKind>,
42    update_requested: bool,
43    active_capabilities: Vec<NodeCapabilities>,
44}
45
46impl LayoutNodeContext {
47    pub(crate) fn new() -> Self {
48        Self::default()
49    }
50
51    pub(crate) fn take_invalidations(&mut self) -> Vec<InvalidationKind> {
52        std::mem::take(&mut self.invalidations)
53    }
54}
55
56impl ModifierNodeContext for LayoutNodeContext {
57    fn invalidate(&mut self, kind: InvalidationKind) {
58        if !self.invalidations.contains(&kind) {
59            self.invalidations.push(kind);
60        }
61    }
62
63    fn request_update(&mut self) {
64        self.update_requested = true;
65    }
66
67    fn push_active_capabilities(&mut self, capabilities: NodeCapabilities) {
68        self.active_capabilities.push(capabilities);
69    }
70
71    fn pop_active_capabilities(&mut self) {
72        self.active_capabilities.pop();
73    }
74}
75
76/// Forces all layout caches to be invalidated on the next measure by incrementing the epoch.
77///
78/// # ⚠️ Internal Use Only - NOT Public API
79///
80/// **This function is hidden from public documentation and MUST NOT be called by external code.**
81///
82/// Only `cranpose-app-shell` may call this for rare global events:
83/// - Window/viewport resize
84/// - Global font scale or density changes
85/// - Debug toggles that affect all layout
86///
87/// **This is O(entire app size) - extremely expensive!**
88///
89/// # For Local Changes
90///
91/// **Do NOT use this for scroll, single-node mutations, or any local layout change.**
92/// Instead, use the scoped repass mechanism:
93/// ```text
94/// cranpose_ui::schedule_layout_repass(node_id);
95/// ```
96///
97/// The scoped path bubbles dirty flags without invalidating all caches, giving you O(subtree) instead of O(app).
98#[doc(hidden)]
99pub fn invalidate_all_layout_caches() {
100    crate::render_state::invalidate_layout_cache_epoch();
101}
102
103fn layout_measure_telemetry_threshold_ms() -> Option<f64> {
104    static THRESHOLD_MS: OnceLock<Option<f64>> = OnceLock::new();
105    *THRESHOLD_MS.get_or_init(|| {
106        std::env::var("CRANPOSE_LAYOUT_MEASURE_TELEMETRY_MS")
107            .ok()
108            .and_then(|value| value.parse::<f64>().ok())
109            .filter(|value| value.is_finite() && *value >= 0.0)
110            .or_else(|| {
111                std::env::var_os("CRANPOSE_LAYOUT_MEASURE_TELEMETRY")
112                    .is_some()
113                    .then_some(4.0)
114            })
115    })
116}
117
118struct LayoutMeasureTelemetry {
119    root: NodeId,
120    start: Instant,
121    after_repasses: Instant,
122    after_guard: Instant,
123    after_builder: Instant,
124    after_measure: Instant,
125    after_root_place: Instant,
126    after_aux: Instant,
127    after_builder_drop: Instant,
128    after_guard_drop: Instant,
129}
130
131fn log_layout_measure_telemetry(times: LayoutMeasureTelemetry) {
132    let Some(threshold_ms) = layout_measure_telemetry_threshold_ms() else {
133        return;
134    };
135
136    let total_ms = times
137        .after_guard_drop
138        .duration_since(times.start)
139        .as_secs_f64()
140        * 1000.0;
141    if total_ms < threshold_ms {
142        return;
143    }
144
145    let repass_ms = times
146        .after_repasses
147        .duration_since(times.start)
148        .as_secs_f64()
149        * 1000.0;
150    let guard_ms = times
151        .after_guard
152        .duration_since(times.after_repasses)
153        .as_secs_f64()
154        * 1000.0;
155    let builder_ms = times
156        .after_builder
157        .duration_since(times.after_guard)
158        .as_secs_f64()
159        * 1000.0;
160    let measure_ms = times
161        .after_measure
162        .duration_since(times.after_builder)
163        .as_secs_f64()
164        * 1000.0;
165    let root_place_ms = times
166        .after_root_place
167        .duration_since(times.after_measure)
168        .as_secs_f64()
169        * 1000.0;
170    let aux_ms = times
171        .after_aux
172        .duration_since(times.after_root_place)
173        .as_secs_f64()
174        * 1000.0;
175    let builder_drop_ms = times
176        .after_builder_drop
177        .duration_since(times.after_aux)
178        .as_secs_f64()
179        * 1000.0;
180    let guard_drop_ms = times
181        .after_guard_drop
182        .duration_since(times.after_builder_drop)
183        .as_secs_f64()
184        * 1000.0;
185    log::warn!(
186        "[layout-measure-telemetry] root={} total_ms={total_ms:.2} repass_ms={repass_ms:.2} guard_ms={guard_ms:.2} builder_ms={builder_ms:.2} measure_ms={measure_ms:.2} root_place_ms={root_place_ms:.2} aux_ms={aux_ms:.2} builder_drop_ms={builder_drop_ms:.2} guard_drop_ms={guard_drop_ms:.2}",
187        times.root
188    );
189}
190
191fn log_node_measure_telemetry(
192    kind: &'static str,
193    node_id: NodeId,
194    constraints: Constraints,
195    size: Size,
196    children: usize,
197    start: Instant,
198) {
199    let Some(threshold_ms) = layout_measure_telemetry_threshold_ms() else {
200        return;
201    };
202
203    let total_ms = start.elapsed().as_secs_f64() * 1000.0;
204    if total_ms < threshold_ms {
205        return;
206    }
207
208    log::warn!(
209        "[layout-node-telemetry] kind={kind} node={} total_ms={total_ms:.2} constraints=({:.1},{:.1},{:.1},{:.1}) size=({:.1},{:.1}) children={children}",
210        node_id,
211        constraints.min_width,
212        constraints.max_width,
213        constraints.min_height,
214        constraints.max_height,
215        size.width,
216        size.height,
217    );
218}
219
220/// RAII guard that:
221/// - moves the current MemoryApplier into a ConcreteApplierHost
222/// - holds a shared handle to the `SlotTable` used by `LayoutBuilder`
223/// - on Drop, always:
224///   * restores slots into the host from the shared handle
225///   * moves the original MemoryApplier back into the Composition
226///
227/// This makes `measure_layout` panic/Err-safe wrt both the applier and slots.
228/// The key invariant: guard and builder share the same `Rc<RefCell<SlotTable>>`,
229/// so the guard never loses access to the authoritative slots even on panic.
230struct ApplierSlotGuard<'a> {
231    /// The `MemoryApplier` inside the Composition::applier that we must restore into.
232    target: &'a mut MemoryApplier,
233    /// Host that owns the original MemoryApplier while layout is running.
234    host: Rc<ConcreteApplierHost<MemoryApplier>>,
235    /// Shared handle to the slot table. Both the guard and the builder hold a clone.
236    /// On Drop, we write whatever is in this handle back into the applier.
237    slots: Rc<RefCell<SlotTable>>,
238}
239
240impl<'a> ApplierSlotGuard<'a> {
241    /// Creates a new guard:
242    /// - moves the current MemoryApplier out of `target` into a host
243    /// - takes the current slots out of the host and wraps them in a shared handle
244    fn new(target: &'a mut MemoryApplier) -> Self {
245        // Move the original applier into a host; leave `target` with a fresh one
246        let original_applier = std::mem::replace(target, MemoryApplier::new());
247        let host = Rc::new(ConcreteApplierHost::new(original_applier));
248
249        // Take slots from the host into a shared handle
250        let slots = {
251            let mut applier_ref = host.borrow_typed();
252            std::mem::take(applier_ref.slots())
253        };
254        let slots = Rc::new(RefCell::new(slots));
255
256        Self {
257            target,
258            host,
259            slots,
260        }
261    }
262
263    /// Rc to pass into LayoutBuilder::new_with_epoch
264    fn host(&self) -> Rc<ConcreteApplierHost<MemoryApplier>> {
265        Rc::clone(&self.host)
266    }
267
268    /// Returns the shared handle to slots for the builder to use.
269    /// The builder clones this Rc, so both guard and builder share the same slots.
270    fn slots_handle(&self) -> Rc<RefCell<SlotTable>> {
271        Rc::clone(&self.slots)
272    }
273}
274
275impl Drop for ApplierSlotGuard<'_> {
276    fn drop(&mut self) {
277        // 1) Restore slots into the host's MemoryApplier from the shared handle.
278        // This works correctly whether we're on the success path or panic/error path,
279        // because we always have the shared handle.
280        {
281            let mut applier_ref = self.host.borrow_typed();
282            *applier_ref.slots() = std::mem::take(&mut *self.slots.borrow_mut());
283        }
284
285        // 2) Move the original MemoryApplier (with restored/updated slots) back into `target`
286        {
287            let mut applier_ref = self.host.borrow_typed();
288            let original_applier = std::mem::take(&mut *applier_ref);
289            let _ = std::mem::replace(self.target, original_applier);
290        }
291        // No Rc::try_unwrap in Drop → no "panic during panic" risk.
292    }
293}
294
295/// Result of measuring through the modifier node chain.
296struct ModifierChainMeasurement {
297    size: Size,
298    /// Content offset for scroll/inner transforms - NOT padding semantics
299    content_offset: Point,
300    /// Node's own offset (from OffsetNode, affects position in parent)
301    offset: Point,
302}
303
304type LayoutModifierNodeData = (
305    usize,
306    Rc<RefCell<Box<dyn cranpose_foundation::ModifierNode>>>,
307);
308
309struct ScratchVecPool<T> {
310    available: Vec<Vec<T>>,
311}
312
313impl<T> ScratchVecPool<T> {
314    fn acquire(&mut self) -> Vec<T> {
315        self.available.pop().unwrap_or_default()
316    }
317
318    fn release(&mut self, mut values: Vec<T>) {
319        values.clear();
320        self.available.push(values);
321    }
322
323    #[cfg(test)]
324    fn available_count(&self) -> usize {
325        self.available.len()
326    }
327}
328
329impl<T> Default for ScratchVecPool<T> {
330    fn default() -> Self {
331        Self {
332            available: Vec::new(),
333        }
334    }
335}
336
337#[derive(Default)]
338pub(crate) struct FrameLayoutArena {
339    tmp_records: ScratchVecPool<(NodeId, ChildRecord)>,
340    tmp_child_ids: ScratchVecPool<NodeId>,
341    tmp_layout_node_data: ScratchVecPool<LayoutModifierNodeData>,
342    tmp_placements: ScratchVecPool<Placement>,
343}
344
345#[cfg(test)]
346impl FrameLayoutArena {
347    pub(crate) fn available_placement_scratch_count(&self) -> usize {
348        self.tmp_placements.available_count()
349    }
350
351    pub(crate) fn seed_placement_scratch_for_test(&mut self) {
352        self.tmp_placements.release(Vec::with_capacity(1));
353    }
354}
355
356/// Discrete event callback reference produced during semantics extraction.
357#[derive(Clone, Debug, PartialEq, Eq)]
358pub struct SemanticsCallback {
359    node_id: NodeId,
360}
361
362impl SemanticsCallback {
363    pub fn new(node_id: NodeId) -> Self {
364        Self { node_id }
365    }
366
367    pub fn node_id(&self) -> NodeId {
368        self.node_id
369    }
370}
371
372/// Semantics action exposed to the input system.
373#[derive(Clone, Debug, PartialEq, Eq)]
374pub enum SemanticsAction {
375    Click { handler: SemanticsCallback },
376}
377
378/// Semantic role describing how a node should participate in accessibility and hit testing.
379/// Roles are now derived from SemanticsConfiguration rather than widget types.
380#[derive(Clone, Debug, PartialEq, Eq)]
381pub enum SemanticsRole {
382    /// Generic container or layout node
383    Layout,
384    /// Subcomposition boundary
385    Subcompose,
386    /// Text content derived from the text node semantics payload.
387    Text { value: String },
388    /// Spacer (non-interactive)
389    Spacer,
390    /// Button (derived from is_button semantics flag)
391    Button,
392    /// Unknown or unspecified role
393    Unknown,
394}
395
396/// A single node within the semantics tree.
397#[derive(Clone, Debug, PartialEq, Eq)]
398pub struct SemanticsNode {
399    pub node_id: NodeId,
400    pub role: SemanticsRole,
401    pub actions: Vec<SemanticsAction>,
402    pub children: Vec<SemanticsNode>,
403    pub description: Option<String>,
404    pub editable_text: bool,
405    pub text_selection: Option<TextRange>,
406}
407
408impl SemanticsNode {
409    fn new(
410        node_id: NodeId,
411        role: SemanticsRole,
412        actions: Vec<SemanticsAction>,
413        children: Vec<SemanticsNode>,
414        description: Option<String>,
415        editable_text: bool,
416        text_selection: Option<TextRange>,
417    ) -> Self {
418        Self {
419            node_id,
420            role,
421            actions,
422            children,
423            description,
424            editable_text,
425            text_selection,
426        }
427    }
428}
429
430/// Rooted semantics tree extracted after layout.
431#[derive(Clone, Debug, PartialEq, Eq)]
432pub struct SemanticsTree {
433    root: SemanticsNode,
434}
435
436impl SemanticsTree {
437    fn new(root: SemanticsNode) -> Self {
438        Self { root }
439    }
440
441    pub fn root(&self) -> &SemanticsNode {
442        &self.root
443    }
444}
445
446#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
447pub struct LayoutAllocationDebugStats {
448    pub layout_box_count: usize,
449    pub layout_box_child_count: usize,
450    pub layout_box_child_capacity: usize,
451    pub layout_box_heap_bytes: usize,
452    pub modifier_slice_count: usize,
453    pub modifier_slice_heap_bytes: usize,
454    pub modifier_draw_command_count: usize,
455    pub modifier_draw_command_capacity: usize,
456    pub modifier_pointer_input_count: usize,
457    pub modifier_pointer_input_capacity: usize,
458    pub modifier_click_handler_count: usize,
459    pub modifier_click_handler_capacity: usize,
460    pub modifier_text_content_count: usize,
461    pub modifier_text_style_count: usize,
462    pub modifier_text_layout_options_count: usize,
463    pub modifier_prepared_text_layout_count: usize,
464    pub modifier_graphics_layer_count: usize,
465    pub modifier_graphics_layer_resolver_count: usize,
466    pub semantics_node_count: usize,
467    pub semantics_action_count: usize,
468    pub semantics_action_capacity: usize,
469    pub semantics_child_count: usize,
470    pub semantics_child_capacity: usize,
471    pub semantics_description_count: usize,
472    pub semantics_description_bytes: usize,
473    pub semantics_text_role_bytes: usize,
474    pub semantics_heap_bytes: usize,
475}
476
477impl LayoutAllocationDebugStats {
478    fn add_modifier_slice(&mut self, stats: ModifierNodeSlicesDebugStats) {
479        self.modifier_slice_count += 1;
480        self.modifier_slice_heap_bytes += stats.heap_bytes;
481        self.modifier_draw_command_count += stats.draw_command_count;
482        self.modifier_draw_command_capacity += stats.draw_command_capacity;
483        self.modifier_pointer_input_count += stats.pointer_input_count;
484        self.modifier_pointer_input_capacity += stats.pointer_input_capacity;
485        self.modifier_click_handler_count += stats.click_handler_count;
486        self.modifier_click_handler_capacity += stats.click_handler_capacity;
487        self.modifier_text_content_count += usize::from(stats.has_text_content);
488        self.modifier_text_style_count += usize::from(stats.has_text_style);
489        self.modifier_text_layout_options_count += usize::from(stats.has_text_layout_options);
490        self.modifier_prepared_text_layout_count += usize::from(stats.has_prepared_text_layout);
491        self.modifier_graphics_layer_count += usize::from(stats.has_graphics_layer);
492        self.modifier_graphics_layer_resolver_count +=
493            usize::from(stats.has_graphics_layer_resolver);
494    }
495}
496
497/// Result of running layout for a Compose tree.
498#[derive(Debug, Clone)]
499pub struct LayoutTree {
500    root: LayoutBox,
501}
502
503impl LayoutTree {
504    pub fn new(root: LayoutBox) -> Self {
505        Self { root }
506    }
507
508    pub fn root(&self) -> &LayoutBox {
509        &self.root
510    }
511
512    pub fn root_mut(&mut self) -> &mut LayoutBox {
513        &mut self.root
514    }
515
516    pub fn into_root(self) -> LayoutBox {
517        self.root
518    }
519
520    pub fn debug_allocation_stats(&self) -> LayoutAllocationDebugStats {
521        let mut stats = LayoutAllocationDebugStats::default();
522        record_layout_box_allocation_stats(&self.root, &mut stats);
523        stats
524    }
525}
526
527/// Layout information for a single node.
528#[derive(Debug, Clone)]
529pub struct LayoutBox {
530    pub node_id: NodeId,
531    pub rect: GeometryRect,
532    /// Content offset for scroll/inner transforms (applies to children, NOT this node's position)
533    pub content_offset: Point,
534    pub node_data: LayoutNodeData,
535    pub children: Vec<LayoutBox>,
536}
537
538impl LayoutBox {
539    pub fn new(
540        node_id: NodeId,
541        rect: GeometryRect,
542        content_offset: Point,
543        node_data: LayoutNodeData,
544        children: Vec<LayoutBox>,
545    ) -> Self {
546        Self {
547            node_id,
548            rect,
549            content_offset,
550            node_data,
551            children,
552        }
553    }
554}
555
556/// Snapshot of the data required to render a layout node.
557#[derive(Debug, Clone)]
558pub struct LayoutNodeData {
559    pub modifier: Modifier,
560    pub resolved_modifiers: ResolvedModifiers,
561    pub modifier_slices: Rc<ModifierNodeSlices>,
562    pub kind: LayoutNodeKind,
563}
564
565impl LayoutNodeData {
566    pub fn new(
567        modifier: Modifier,
568        resolved_modifiers: ResolvedModifiers,
569        modifier_slices: Rc<ModifierNodeSlices>,
570        kind: LayoutNodeKind,
571    ) -> Self {
572        Self {
573            modifier,
574            resolved_modifiers,
575            modifier_slices,
576            kind,
577        }
578    }
579
580    pub fn resolved_modifiers(&self) -> ResolvedModifiers {
581        self.resolved_modifiers
582    }
583
584    pub fn modifier_slices(&self) -> &ModifierNodeSlices {
585        &self.modifier_slices
586    }
587}
588
589/// Classification of the node captured inside a [`LayoutBox`].
590///
591/// Note: Text content is no longer represented as a distinct LayoutNodeKind.
592/// Text nodes now use `LayoutNodeKind::Layout` with their content stored in
593/// `modifier_slices.text_content()` via TextModifierNode, following Jetpack
594/// Compose's pattern where text is a modifier node capability.
595#[derive(Clone)]
596pub enum LayoutNodeKind {
597    Layout,
598    Subcompose,
599    Spacer,
600    Button { on_click: Rc<RefCell<dyn FnMut()>> },
601    Unknown,
602}
603
604impl fmt::Debug for LayoutNodeKind {
605    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
606        match self {
607            LayoutNodeKind::Layout => f.write_str("Layout"),
608            LayoutNodeKind::Subcompose => f.write_str("Subcompose"),
609            LayoutNodeKind::Spacer => f.write_str("Spacer"),
610            LayoutNodeKind::Button { .. } => f.write_str("Button"),
611            LayoutNodeKind::Unknown => f.write_str("Unknown"),
612        }
613    }
614}
615
616/// Extension trait that equips `MemoryApplier` with layout computation.
617pub trait LayoutEngine {
618    fn compute_layout(&mut self, root: NodeId, max_size: Size) -> Result<LayoutTree, NodeError>;
619}
620
621impl LayoutEngine for MemoryApplier {
622    fn compute_layout(&mut self, root: NodeId, max_size: Size) -> Result<LayoutTree, NodeError> {
623        let measurements = measure_layout(self, root, max_size)?;
624        measurements
625            .into_layout_tree()
626            .ok_or(NodeError::MissingContext {
627                id: root,
628                reason: "layout tree was not requested",
629            })
630    }
631}
632
633/// Result of running the measure pass for a Compose layout tree.
634#[derive(Debug, Clone)]
635pub struct LayoutMeasurements {
636    root: Rc<MeasuredNode>,
637    semantics: Option<SemanticsTree>,
638    layout_tree: Option<LayoutTree>,
639}
640
641impl LayoutMeasurements {
642    fn new(
643        root: Rc<MeasuredNode>,
644        semantics: Option<SemanticsTree>,
645        layout_tree: Option<LayoutTree>,
646    ) -> Self {
647        Self {
648            root,
649            semantics,
650            layout_tree,
651        }
652    }
653
654    /// Returns the measured size of the root node.
655    pub fn root_size(&self) -> Size {
656        self.root.size
657    }
658
659    pub fn semantics_tree(&self) -> Option<&SemanticsTree> {
660        self.semantics.as_ref()
661    }
662
663    pub fn debug_allocation_stats(&self) -> LayoutAllocationDebugStats {
664        let mut stats = self
665            .layout_tree
666            .as_ref()
667            .map(LayoutTree::debug_allocation_stats)
668            .unwrap_or_default();
669        if let Some(semantics) = &self.semantics {
670            record_semantics_allocation_stats(semantics.root(), &mut stats);
671        }
672        stats
673    }
674
675    /// Consumes the measurements and returns the built [`LayoutTree`], if requested.
676    pub fn into_layout_tree(self) -> Option<LayoutTree> {
677        self.layout_tree
678    }
679
680    /// Returns a cloned [`LayoutTree`] for rendering/debug consumers, if requested.
681    pub fn layout_tree(&self) -> Option<LayoutTree> {
682        self.layout_tree.clone()
683    }
684}
685
686/// Builds a semantics tree from an existing [`LayoutTree`].
687///
688/// This is useful for consumers that need semantics on demand without forcing
689/// every layout pass to eagerly allocate a full [`SemanticsTree`].
690pub fn build_semantics_tree_from_layout_tree(layout_tree: &LayoutTree) -> SemanticsTree {
691    SemanticsTree::new(build_semantics_node_from_layout_box(layout_tree.root()))
692}
693
694/// Builds a layout snapshot from retained layout state in the live applier tree.
695///
696/// Renderers use retained node state directly. This function exists for debug,
697/// robot, and tests that need an owned [`LayoutTree`] without forcing every
698/// layout pass to allocate one.
699pub fn build_layout_tree_from_applier(
700    applier: &mut MemoryApplier,
701    root: NodeId,
702) -> Result<Option<LayoutTree>, NodeError> {
703    fn snapshot(
704        applier: &mut MemoryApplier,
705        node_id: NodeId,
706    ) -> Result<Option<(crate::widgets::nodes::layout_node::LayoutState, Vec<NodeId>)>, NodeError>
707    {
708        match applier.with_node::<LayoutNode, _>(node_id, |node| {
709            (node.layout_state(), node.children.clone())
710        }) {
711            Ok(snapshot) => return Ok(Some(snapshot)),
712            Err(NodeError::TypeMismatch { .. }) | Err(NodeError::Missing { .. }) => {}
713            Err(err) => return Err(err),
714        }
715
716        match applier.with_node::<SubcomposeLayoutNode, _>(node_id, |node| {
717            (node.layout_state(), node.active_children())
718        }) {
719            Ok(snapshot) => Ok(Some(snapshot)),
720            Err(NodeError::TypeMismatch { .. }) | Err(NodeError::Missing { .. }) => Ok(None),
721            Err(err) => Err(err),
722        }
723    }
724
725    fn place(
726        applier: &mut MemoryApplier,
727        node_id: NodeId,
728        parent_content_origin: Point,
729    ) -> Result<Option<LayoutBox>, NodeError> {
730        let Some((state, child_ids)) = snapshot(applier, node_id)? else {
731            return Ok(None);
732        };
733        if !state.is_placed {
734            return Ok(None);
735        }
736
737        let top_left = Point {
738            x: parent_content_origin.x + state.position.x,
739            y: parent_content_origin.y + state.position.y,
740        };
741        let rect = GeometryRect {
742            x: top_left.x,
743            y: top_left.y,
744            width: state.size.width,
745            height: state.size.height,
746        };
747        let info = runtime_metadata_for(applier, node_id)?;
748        let kind = layout_kind_from_metadata(node_id, &info);
749        let RuntimeNodeMetadata {
750            modifier,
751            resolved_modifiers,
752            modifier_slices,
753            ..
754        } = info;
755        let data = LayoutNodeData::new(modifier, resolved_modifiers, modifier_slices, kind);
756        let child_origin = Point {
757            x: top_left.x + state.content_offset.x,
758            y: top_left.y + state.content_offset.y,
759        };
760        let mut children = Vec::with_capacity(child_ids.len());
761        for child_id in child_ids {
762            if let Some(child) = place(applier, child_id, child_origin)? {
763                children.push(child);
764            }
765        }
766
767        Ok(Some(LayoutBox::new(
768            node_id,
769            rect,
770            state.content_offset,
771            data,
772            children,
773        )))
774    }
775
776    place(applier, root, Point::default()).map(|root| root.map(LayoutTree::new))
777}
778
779/// Builds a semantics snapshot from retained layout state in the live applier tree.
780///
781/// This is the on-demand counterpart to [`build_layout_tree_from_applier`].
782/// It follows the currently placed child set, including subcompose active
783/// children, and clears semantics dirty flags for nodes it visits.
784pub fn build_semantics_tree_from_applier(
785    applier: &mut MemoryApplier,
786    root: NodeId,
787) -> Result<Option<SemanticsTree>, NodeError> {
788    fn node(
789        applier: &mut MemoryApplier,
790        node_id: NodeId,
791    ) -> Result<Option<SemanticsNode>, NodeError> {
792        match applier.with_node::<LayoutNode, _>(node_id, |layout| {
793            let state = layout.layout_state();
794            if !state.is_placed {
795                return None;
796            }
797            let role = role_from_modifier_slices(&layout.modifier_slices_snapshot());
798            let config = layout.semantics_configuration();
799            let children = layout.children.clone();
800            layout.clear_needs_semantics();
801            Some((role, config, children))
802        }) {
803            Ok(Some((role, config, child_ids))) => {
804                let mut children = Vec::with_capacity(child_ids.len());
805                for child_id in child_ids {
806                    if let Some(child) = node(applier, child_id)? {
807                        children.push(child);
808                    }
809                }
810                return Ok(Some(semantics_node_from_parts(
811                    node_id, role, config, children,
812                )));
813            }
814            Ok(None) => return Ok(None),
815            Err(NodeError::TypeMismatch { .. }) | Err(NodeError::Missing { .. }) => {}
816            Err(err) => return Err(err),
817        }
818
819        match applier.with_node::<SubcomposeLayoutNode, _>(node_id, |subcompose| {
820            let state = subcompose.layout_state();
821            if !state.is_placed {
822                return None;
823            }
824            let config = collect_semantics_from_modifier(&subcompose.modifier());
825            let children = subcompose.active_children();
826            subcompose.clear_needs_semantics();
827            Some((config, children))
828        }) {
829            Ok(Some((config, child_ids))) => {
830                let mut children = Vec::with_capacity(child_ids.len());
831                for child_id in child_ids {
832                    if let Some(child) = node(applier, child_id)? {
833                        children.push(child);
834                    }
835                }
836                Ok(Some(semantics_node_from_parts(
837                    node_id,
838                    SemanticsRole::Subcompose,
839                    config,
840                    children,
841                )))
842            }
843            Ok(None) | Err(NodeError::TypeMismatch { .. }) | Err(NodeError::Missing { .. }) => {
844                Ok(None)
845            }
846            Err(err) => Err(err),
847        }
848    }
849
850    node(applier, root).map(|root| root.map(SemanticsTree::new))
851}
852
853#[derive(Clone, Copy, Debug, PartialEq, Eq)]
854pub struct MeasureLayoutOptions {
855    pub collect_semantics: bool,
856    pub build_layout_tree: bool,
857}
858
859impl Default for MeasureLayoutOptions {
860    fn default() -> Self {
861        Self {
862            collect_semantics: true,
863            build_layout_tree: true,
864        }
865    }
866}
867
868/// Check if a node or any of its descendants needs measure (selective measure optimization).
869/// This can be used by the app shell to skip layout when the tree is clean.
870///
871/// O(1) check - just looks at root's dirty flag.
872/// Works because all mutation paths bubble dirty flags to root via composer commands.
873///
874/// Returns Result to force caller to handle errors explicitly. No more unwrap_or(true) safety net.
875pub fn tree_needs_layout(applier: &mut dyn Applier, root: NodeId) -> Result<bool, NodeError> {
876    Ok(applier.get_mut(root)?.needs_layout())
877}
878
879/// Check if the root semantics snapshot is dirty.
880///
881/// Semantics invalidations bubble to the root the same way layout invalidations do,
882/// so a root check is sufficient to determine whether the next layout pass needs to
883/// rebuild semantic data even when geometry is otherwise unchanged.
884pub fn tree_needs_semantics(applier: &mut dyn Applier, root: NodeId) -> Result<bool, NodeError> {
885    Ok(applier.get_mut(root)?.needs_semantics())
886}
887
888/// Test helper: bubbles layout dirty flag to root.
889#[cfg(test)]
890pub(crate) fn bubble_layout_dirty(applier: &mut MemoryApplier, node_id: NodeId) {
891    cranpose_core::bubble_layout_dirty(applier as &mut dyn Applier, node_id);
892}
893
894/// Runs the measure phase for the subtree rooted at `root`.
895pub fn measure_layout(
896    applier: &mut MemoryApplier,
897    root: NodeId,
898    max_size: Size,
899) -> Result<LayoutMeasurements, NodeError> {
900    measure_layout_with_options(applier, root, max_size, MeasureLayoutOptions::default())
901}
902
903pub fn measure_layout_with_options(
904    applier: &mut MemoryApplier,
905    root: NodeId,
906    max_size: Size,
907    options: MeasureLayoutOptions,
908) -> Result<LayoutMeasurements, NodeError> {
909    let telemetry_start = Instant::now();
910    process_pending_layout_repasses(applier, root)?;
911    let after_repasses = Instant::now();
912
913    let constraints = Constraints {
914        min_width: 0.0,
915        max_width: max_size.width,
916        min_height: 0.0,
917        max_height: max_size.height,
918    };
919
920    // Selective measure: only increment epoch if something needs MEASURING (not just layout)
921    // O(1) check - just look at root's dirty flag (bubbling ensures correctness)
922    //
923    // CRITICAL: We check needs_MEASURE, not needs_LAYOUT!
924    // - needs_measure: size may change, caches must be invalidated
925    // - needs_layout: position may change but size is cached (e.g., scroll)
926    //
927    // Scroll operations bubble needs_layout to ancestors, but NOT needs_measure.
928    // Using needs_layout here would wipe ALL caches on every scroll frame, causing
929    // O(N) full remeasurement instead of O(changed nodes).
930    let (needs_remeasure, _needs_semantics, cached_epoch) = match applier
931        .with_node::<LayoutNode, _>(root, |node| {
932            (
933                node.needs_measure(), // CORRECT: check needs_measure, not needs_layout
934                node.needs_semantics(),
935                node.cache_handles().epoch(),
936            )
937        }) {
938        Ok(tuple) => tuple,
939        Err(NodeError::TypeMismatch { .. }) => {
940            let node = applier.get_mut(root)?;
941            // Non-LayoutNode roots still expose Node dirty flags.
942            // Use needs_measure here so layout-only subtree repasses can reuse
943            // the existing cache epoch instead of invalidating the whole tree.
944            let measure_dirty = node.needs_measure();
945            let semantics_dirty = node.needs_semantics();
946            (measure_dirty, semantics_dirty, 0)
947        }
948        Err(err) => return Err(err),
949    };
950
951    let epoch = if needs_remeasure {
952        crate::render_state::next_layout_cache_epoch()
953    } else if cached_epoch != 0 {
954        cached_epoch
955    } else {
956        // Fallback when caller root isn't a LayoutNode (e.g. tests using Spacer directly).
957        crate::render_state::current_layout_cache_epoch()
958    };
959
960    // Move the current applier into a host and set up a guard that will
961    // ALWAYS restore:
962    // - the MemoryApplier back into `applier`
963    // - the SlotTable back into that MemoryApplier
964    //
965    // IMPORTANT: Declare the guard *before* the builder so the builder
966    // is dropped first (both on Ok and on unwind).
967    let guard = ApplierSlotGuard::new(applier);
968    let applier_host = guard.host();
969    let slots_handle = guard.slots_handle();
970    let after_guard = Instant::now();
971
972    // Give the builder the shared slots handle - both guard and builder
973    // now share access to the same SlotTable via Rc<RefCell<_>>.
974    let frame_arena = crate::render_state::take_layout_frame_arena();
975    let mut builder = LayoutBuilder::new_with_epoch(
976        Rc::clone(&applier_host),
977        epoch,
978        Rc::clone(&slots_handle),
979        frame_arena,
980    );
981    let after_builder = Instant::now();
982
983    // ---- Measurement -------------------------------------------------------
984    // If measurement fails, the guard will restore slots from the shared handle
985    // on drop - this is safe because the handle always contains valid slots.
986
987    let measured = builder.measure_node(root, normalize_constraints(constraints))?;
988    let after_measure = Instant::now();
989
990    // Root node has no parent to place it, so we must explicitly place it at (0,0).
991    // This ensures is_placed=true, allowing the renderer to traverse the tree.
992    // Handle both LayoutNode and SubcomposeLayoutNode as potential roots.
993    if let Ok(mut applier) = applier_host.try_borrow_typed() {
994        if applier
995            .with_node::<LayoutNode, _>(root, |node| {
996                node.set_position(Point::default());
997            })
998            .is_err()
999        {
1000            let _ = applier.with_node::<SubcomposeLayoutNode, _>(root, |node| {
1001                node.set_position(Point::default());
1002            });
1003        }
1004    }
1005    let after_root_place = Instant::now();
1006
1007    let (layout_tree, semantics) = {
1008        let mut applier_ref = applier_host.borrow_typed();
1009        let layout_tree = if options.build_layout_tree {
1010            Some(build_layout_tree(&mut applier_ref, &measured)?)
1011        } else {
1012            None
1013        };
1014        let semantics = if options.collect_semantics {
1015            let semantics_tree = if let Some(layout_tree) = layout_tree.as_ref() {
1016                clear_semantics_dirty_flags(&mut applier_ref, &measured)?;
1017                build_semantics_tree_from_layout_tree(layout_tree)
1018            } else {
1019                build_semantics_tree_from_live_nodes(&mut applier_ref, &measured)?
1020            };
1021            Some(semantics_tree)
1022        } else {
1023            None
1024        };
1025        (layout_tree, semantics)
1026    };
1027    let after_aux = Instant::now();
1028
1029    // Drop builder before guard - slots are already in the shared handle.
1030    // Guard's Drop will write them back to the applier.
1031    drop(builder);
1032    let after_builder_drop = Instant::now();
1033
1034    // DO NOT manually unwrap `applier_host` or replace `applier` here.
1035    // `ApplierSlotGuard::drop` will restore everything when this function returns.
1036    drop(guard);
1037    let after_guard_drop = Instant::now();
1038
1039    log_layout_measure_telemetry(LayoutMeasureTelemetry {
1040        root,
1041        start: telemetry_start,
1042        after_repasses,
1043        after_guard,
1044        after_builder,
1045        after_measure,
1046        after_root_place,
1047        after_aux,
1048        after_builder_drop,
1049        after_guard_drop,
1050    });
1051
1052    Ok(LayoutMeasurements::new(measured, semantics, layout_tree))
1053}
1054
1055fn process_pending_layout_repasses(
1056    applier: &mut MemoryApplier,
1057    root: NodeId,
1058) -> Result<(), NodeError> {
1059    for node_id in crate::render_state::take_modifier_slice_repass_nodes() {
1060        if let Ok(node) = applier.get_mut(node_id) {
1061            let any = node.as_any_mut();
1062            if let Some(layout) = any.downcast_mut::<crate::widgets::nodes::LayoutNode>() {
1063                layout.mark_modifier_slices_dirty();
1064            } else if let Some(subcompose) =
1065                any.downcast_mut::<crate::subcompose_layout::SubcomposeLayoutNode>()
1066            {
1067                subcompose.mark_modifier_slices_dirty();
1068            }
1069        }
1070    }
1071    let repass_nodes = crate::take_layout_repass_nodes();
1072    if repass_nodes.is_empty() {
1073        return Ok(());
1074    }
1075    for node_id in repass_nodes {
1076        cranpose_core::bubble_layout_dirty(applier as &mut dyn Applier, node_id);
1077    }
1078    applier.get_mut(root)?.mark_needs_layout();
1079    Ok(())
1080}
1081
1082struct LayoutBuilder {
1083    state: Rc<RefCell<LayoutBuilderState>>,
1084}
1085
1086impl LayoutBuilder {
1087    fn new_with_epoch(
1088        applier: Rc<ConcreteApplierHost<MemoryApplier>>,
1089        epoch: u64,
1090        slots: Rc<RefCell<SlotTable>>,
1091        frame_arena: FrameLayoutArena,
1092    ) -> Self {
1093        Self {
1094            state: Rc::new(RefCell::new(LayoutBuilderState::new_with_epoch(
1095                applier,
1096                epoch,
1097                slots,
1098                frame_arena,
1099            ))),
1100        }
1101    }
1102
1103    fn measure_node(
1104        &mut self,
1105        node_id: NodeId,
1106        constraints: Constraints,
1107    ) -> Result<Rc<MeasuredNode>, NodeError> {
1108        LayoutBuilderState::measure_node(Rc::clone(&self.state), node_id, constraints)
1109    }
1110
1111    fn set_runtime_handle(&mut self, handle: Option<RuntimeHandle>) {
1112        self.state.borrow_mut().runtime_handle = handle;
1113    }
1114}
1115
1116impl Drop for LayoutBuilder {
1117    fn drop(&mut self) {
1118        if Rc::strong_count(&self.state) != 1 {
1119            return;
1120        }
1121        let Ok(mut state) = self.state.try_borrow_mut() else {
1122            return;
1123        };
1124        crate::render_state::replace_layout_frame_arena(std::mem::take(&mut state.frame_arena));
1125    }
1126}
1127
1128struct LayoutBuilderState {
1129    applier: Rc<ConcreteApplierHost<MemoryApplier>>,
1130    runtime_handle: Option<RuntimeHandle>,
1131    /// Shared handle to the slot table. This is shared with ApplierSlotGuard
1132    /// to ensure panic-safety: even if we panic, the guard can restore slots.
1133    slots: Rc<RefCell<SlotTable>>,
1134    cache_epoch: u64,
1135    frame_arena: FrameLayoutArena,
1136}
1137
1138struct LayoutRuntimeFrameBindingCleanup {
1139    state: Rc<RefCell<LayoutRuntimeState>>,
1140}
1141
1142impl LayoutRuntimeFrameBindingCleanup {
1143    fn new(state: Rc<RefCell<LayoutRuntimeState>>) -> Self {
1144        Self { state }
1145    }
1146}
1147
1148impl Drop for LayoutRuntimeFrameBindingCleanup {
1149    fn drop(&mut self) {
1150        self.state.borrow().clear_frame_bindings();
1151    }
1152}
1153
1154impl LayoutBuilderState {
1155    fn new_with_epoch(
1156        applier: Rc<ConcreteApplierHost<MemoryApplier>>,
1157        epoch: u64,
1158        slots: Rc<RefCell<SlotTable>>,
1159        frame_arena: FrameLayoutArena,
1160    ) -> Self {
1161        let runtime_handle = applier.borrow_typed().runtime_handle();
1162
1163        Self {
1164            applier,
1165            runtime_handle,
1166            slots,
1167            cache_epoch: epoch,
1168            frame_arena,
1169        }
1170    }
1171
1172    fn try_with_applier_result<R>(
1173        state_rc: &Rc<RefCell<Self>>,
1174        f: impl FnOnce(&mut MemoryApplier) -> Result<R, NodeError>,
1175    ) -> Option<Result<R, NodeError>> {
1176        let host = {
1177            let state = state_rc.borrow();
1178            Rc::clone(&state.applier)
1179        };
1180
1181        // Try to borrow - if already borrowed (nested call), return None
1182        let Ok(mut applier) = host.try_borrow_typed() else {
1183            return None;
1184        };
1185
1186        Some(f(&mut applier))
1187    }
1188
1189    fn with_applier_result<R>(
1190        state_rc: &Rc<RefCell<Self>>,
1191        f: impl FnOnce(&mut MemoryApplier) -> Result<R, NodeError>,
1192    ) -> Result<R, NodeError> {
1193        Self::try_with_applier_result(state_rc, f).unwrap_or_else(|| {
1194            Err(NodeError::MissingContext {
1195                id: NodeId::default(),
1196                reason: "applier already borrowed",
1197            })
1198        })
1199    }
1200
1201    /// Clears the is_placed flag for a node at the start of measurement.
1202    /// This ensures nodes that drop out of placement won't render with stale geometry.
1203    fn clear_node_placed(state_rc: &Rc<RefCell<Self>>, node_id: NodeId) {
1204        let host = {
1205            let state = state_rc.borrow();
1206            Rc::clone(&state.applier)
1207        };
1208        let Ok(mut applier) = host.try_borrow_typed() else {
1209            return;
1210        };
1211        // Try LayoutNode first, then SubcomposeLayoutNode
1212        if applier
1213            .with_node::<LayoutNode, _>(node_id, |node| {
1214                node.clear_placed();
1215            })
1216            .is_err()
1217        {
1218            let _ = applier.with_node::<SubcomposeLayoutNode, _>(node_id, |node| {
1219                node.clear_placed();
1220            });
1221        }
1222    }
1223
1224    fn measure_node(
1225        state_rc: Rc<RefCell<Self>>,
1226        node_id: NodeId,
1227        constraints: Constraints,
1228    ) -> Result<Rc<MeasuredNode>, NodeError> {
1229        let telemetry_start = Instant::now();
1230        // Clear is_placed at the start of measurement.
1231        // Nodes that are placed will have is_placed set to true via Placeable::place().
1232        // Nodes that drop out of placement (not placed this pass) will remain is_placed=false.
1233        Self::clear_node_placed(&state_rc, node_id);
1234
1235        // Try SubcomposeLayoutNode first
1236        if let Some(subcompose) =
1237            Self::try_measure_subcompose(Rc::clone(&state_rc), node_id, constraints)?
1238        {
1239            log_node_measure_telemetry(
1240                "subcompose",
1241                node_id,
1242                constraints,
1243                subcompose.size,
1244                subcompose.children.len(),
1245                telemetry_start,
1246            );
1247            return Ok(subcompose);
1248        }
1249
1250        // Try LayoutNode (the primary modern path)
1251        if let Some(result) = Self::try_with_applier_result(&state_rc, |applier| {
1252            match applier.with_node::<LayoutNode, _>(node_id, |layout_node| {
1253                LayoutNodeSnapshot::from_layout_node(layout_node)
1254            }) {
1255                Ok(snapshot) => Ok(Some(snapshot)),
1256                Err(NodeError::TypeMismatch { .. }) | Err(NodeError::Missing { .. }) => Ok(None),
1257                Err(err) => Err(err),
1258            }
1259        }) {
1260            // Applier was available, process the result
1261            if let Some(snapshot) = result? {
1262                let measured = Self::measure_layout_node(
1263                    Rc::clone(&state_rc),
1264                    node_id,
1265                    snapshot,
1266                    constraints,
1267                )?;
1268                log_node_measure_telemetry(
1269                    "layout",
1270                    node_id,
1271                    constraints,
1272                    measured.size,
1273                    measured.children.len(),
1274                    telemetry_start,
1275                );
1276                return Ok(measured);
1277            }
1278        }
1279        // If applier was busy (None) or snapshot was None, fall through to fallback
1280
1281        // No alternate fallbacks - all widgets use LayoutNode or SubcomposeLayoutNode
1282        // If we reach here, it's an unknown node type (shouldn't happen in normal use)
1283        let measured = Rc::new(MeasuredNode::new(
1284            node_id,
1285            Size::default(),
1286            Point { x: 0.0, y: 0.0 },
1287            Point::default(), // No content offset for fallback nodes
1288            Vec::new(),
1289        ));
1290        log_node_measure_telemetry(
1291            "fallback",
1292            node_id,
1293            constraints,
1294            measured.size,
1295            measured.children.len(),
1296            telemetry_start,
1297        );
1298        Ok(measured)
1299    }
1300
1301    fn cached_measure_node_with_applier(
1302        applier: &mut MemoryApplier,
1303        node_id: NodeId,
1304        constraints: Constraints,
1305    ) -> Result<Option<Rc<MeasuredNode>>, NodeError> {
1306        let Some(data) = Self::layout_child_measure_data(applier, node_id)? else {
1307            return Ok(None);
1308        };
1309        if data.needs_measure || data.cache.epoch() == 0 {
1310            return Ok(None);
1311        }
1312
1313        let Some(measured) = data.cache.get_measurement(constraints) else {
1314            return Ok(None);
1315        };
1316
1317        if let Some(layout_state) = data.layout_state {
1318            let mut layout_state = layout_state.borrow_mut();
1319            layout_state.size = measured.size;
1320            layout_state.measurement_constraints = constraints;
1321            drop(layout_state);
1322            let _ = applier.with_node::<LayoutNode, _>(node_id, |node| {
1323                if data.needs_layout {
1324                    node.clear_needs_layout();
1325                }
1326            });
1327        } else {
1328            let _ = applier.with_node::<SubcomposeLayoutNode, _>(node_id, |node| {
1329                node.set_measured_size(measured.size);
1330                if data.needs_layout {
1331                    node.clear_needs_layout();
1332                }
1333            });
1334        }
1335
1336        Ok(Some(measured))
1337    }
1338
1339    fn try_measure_subcompose(
1340        state_rc: Rc<RefCell<Self>>,
1341        node_id: NodeId,
1342        constraints: Constraints,
1343    ) -> Result<Option<Rc<MeasuredNode>>, NodeError> {
1344        let applier_host = {
1345            let state = state_rc.borrow();
1346            Rc::clone(&state.applier)
1347        };
1348
1349        let (node_handle, resolved_modifiers) = {
1350            // Try to borrow - if already borrowed (nested measurement), return None
1351            let Ok(mut applier) = applier_host.try_borrow_typed() else {
1352                return Ok(None);
1353            };
1354            let node = match applier.get_mut(node_id) {
1355                Ok(node) => node,
1356                Err(NodeError::Missing { .. }) => return Ok(None),
1357                Err(err) => return Err(err),
1358            };
1359            let any = node.as_any_mut();
1360            if let Some(subcompose) =
1361                any.downcast_mut::<crate::subcompose_layout::SubcomposeLayoutNode>()
1362            {
1363                let handle = subcompose.handle();
1364                let resolved_modifiers = handle.resolved_modifiers();
1365                (handle, resolved_modifiers)
1366            } else {
1367                return Ok(None);
1368            }
1369        };
1370
1371        let runtime_handle = {
1372            let mut state = state_rc.borrow_mut();
1373            if state.runtime_handle.is_none() {
1374                // Try to borrow - if already borrowed, we can't get runtime handle
1375                if let Ok(applier) = applier_host.try_borrow_typed() {
1376                    state.runtime_handle = applier.runtime_handle();
1377                }
1378            }
1379            state
1380                .runtime_handle
1381                .clone()
1382                .ok_or(NodeError::MissingContext {
1383                    id: node_id,
1384                    reason: "runtime handle required for subcomposition",
1385                })?
1386        };
1387
1388        let props = resolved_modifiers.layout_properties();
1389        let padding = resolved_modifiers.padding();
1390        let offset = resolved_modifiers.offset();
1391        let mut inner_constraints = normalize_constraints(subtract_padding(constraints, padding));
1392
1393        if let DimensionConstraint::Points(width) = props.width() {
1394            let constrained_width = width - padding.horizontal_sum();
1395            inner_constraints.max_width = inner_constraints.max_width.min(constrained_width);
1396            inner_constraints.min_width = inner_constraints.min_width.min(constrained_width);
1397        }
1398        if let DimensionConstraint::Points(height) = props.height() {
1399            let constrained_height = height - padding.vertical_sum();
1400            inner_constraints.max_height = inner_constraints.max_height.min(constrained_height);
1401            inner_constraints.min_height = inner_constraints.min_height.min(constrained_height);
1402        }
1403
1404        let mut slots_guard = SlotsGuard::take(Rc::clone(&state_rc));
1405        let slots_host = slots_guard.host();
1406        let applier_host_dyn: Rc<dyn ApplierHost> = applier_host.clone();
1407        let observer = SnapshotStateObserver::new(|callback| callback());
1408        let composer = Composer::new(
1409            Rc::clone(&slots_host),
1410            applier_host_dyn,
1411            runtime_handle.clone(),
1412            observer,
1413            Some(node_id),
1414        );
1415        composer.enter_phase(Phase::Measure);
1416
1417        let state_rc_clone = Rc::clone(&state_rc);
1418        let measure_error = RefCell::new(None);
1419        let state_rc_for_subcompose = Rc::clone(&state_rc_clone);
1420        let error_for_subcompose = &measure_error;
1421        let measured_children = node_handle.measured_children_scratch();
1422        let measured_children_for_subcompose = Rc::clone(&measured_children);
1423        let state_rc_for_cached = Rc::clone(&state_rc_clone);
1424        let error_for_cached = &measure_error;
1425        let measured_children_for_cached = Rc::clone(&measured_children);
1426        let measured_children_for_lookup = Rc::clone(&measured_children);
1427        let measured_children_for_retained = Rc::clone(&measured_children);
1428
1429        let measure_result = node_handle.measure_with_cached_batch(
1430            &composer,
1431            node_id,
1432            inner_constraints,
1433            CachedBatchMeasureInputs {
1434                measurer: Box::new(
1435                    move |child_id: NodeId, child_constraints: Constraints| -> Size {
1436                        match Self::measure_node(
1437                            Rc::clone(&state_rc_for_subcompose),
1438                            child_id,
1439                            child_constraints,
1440                        ) {
1441                            Ok(measured) => {
1442                                measured_children_for_subcompose
1443                                    .borrow_mut()
1444                                    .insert(child_id, Rc::clone(&measured));
1445                                measured.size
1446                            }
1447                            Err(err) => {
1448                                let mut slot = error_for_subcompose.borrow_mut();
1449                                if slot.is_none() {
1450                                    *slot = Some(err);
1451                                }
1452                                Size::default()
1453                            }
1454                        }
1455                    },
1456                ),
1457                cached_measure_batch_registrar: Box::new(
1458                    move |child_ids: &[NodeId],
1459                          child_constraints: Constraints,
1460                          out: &mut Vec<Option<Size>>| {
1461                        out.clear();
1462                        out.resize(child_ids.len(), None);
1463
1464                        let applier_host = {
1465                            let state = state_rc_for_cached.borrow();
1466                            Rc::clone(&state.applier)
1467                        };
1468                        let Ok(mut applier) = applier_host.try_borrow_typed() else {
1469                            return;
1470                        };
1471
1472                        let mut measured_children = measured_children_for_cached.borrow_mut();
1473                        for (index, &child_id) in child_ids.iter().enumerate() {
1474                            match Self::cached_measure_node_with_applier(
1475                                &mut applier,
1476                                child_id,
1477                                child_constraints,
1478                            ) {
1479                                Ok(Some(measured)) => {
1480                                    out[index] = Some(measured.size);
1481                                    measured_children.insert(child_id, Rc::clone(&measured));
1482                                }
1483                                Ok(None) => {}
1484                                Err(err) => {
1485                                    let mut slot = error_for_cached.borrow_mut();
1486                                    if slot.is_none() {
1487                                        *slot = Some(err);
1488                                    }
1489                                    break;
1490                                }
1491                            }
1492                        }
1493                    },
1494                ),
1495                retained_measure_lookup: Box::new(move |child_id| {
1496                    measured_children_for_lookup
1497                        .borrow()
1498                        .get(&child_id)
1499                        .cloned()
1500                }),
1501                retained_measure_registrar: Box::new(move |measurements| {
1502                    let mut measured_children = measured_children_for_retained.borrow_mut();
1503                    for measured in measurements {
1504                        measured_children.insert(measured.node_id(), Rc::clone(measured));
1505                    }
1506                }),
1507                error: &measure_error,
1508            },
1509        )?;
1510        drop(composer);
1511        slots_guard.restore(slots_host.into_table()?);
1512
1513        if let Some(err) = measure_error.borrow_mut().take() {
1514            return Err(err);
1515        }
1516
1517        // NOTE: Children are now managed by the composer via insert_child commands
1518        // (from parent_stack initialization with root). set_active_children is no longer used.
1519
1520        let cranpose_ui_layout::MeasureResult {
1521            size: measured_size,
1522            placements,
1523        } = measure_result;
1524
1525        let mut width = measured_size.width + padding.horizontal_sum();
1526        let mut height = measured_size.height + padding.vertical_sum();
1527
1528        width = resolve_dimension(
1529            width,
1530            props.width(),
1531            props.min_width(),
1532            props.max_width(),
1533            constraints.min_width,
1534            constraints.max_width,
1535        );
1536        height = resolve_dimension(
1537            height,
1538            props.height(),
1539            props.min_height(),
1540            props.max_height(),
1541            constraints.min_height,
1542            constraints.max_height,
1543        );
1544
1545        let mut children = Vec::with_capacity(placements.len());
1546        let mut measured_children_by_id = measured_children.borrow_mut();
1547
1548        // Update the SubcomposeLayoutNode's size (position will be set by parent's placement)
1549        if let Ok(mut applier) = applier_host.try_borrow_typed() {
1550            let _ = applier.with_node::<SubcomposeLayoutNode, _>(node_id, |parent_node| {
1551                parent_node.set_measured_size(Size { width, height });
1552                parent_node.clear_needs_measure();
1553                parent_node.clear_needs_layout();
1554            });
1555        }
1556
1557        for placement in &placements {
1558            let child = if let Some(measured) = measured_children_by_id.remove(&placement.node_id) {
1559                measured
1560            } else {
1561                // Policies may place subcomposed children without calling `measure()` first
1562                // (for example, when they only need a slot's rendered content). Keep the
1563                // existing fallback for that case, but preserve the policy-time measurement
1564                // whenever it exists so we don't silently remeasure lazy items with the
1565                // container's tighter constraints.
1566                Self::measure_node(Rc::clone(&state_rc), placement.node_id, inner_constraints)?
1567            };
1568            let position = Point {
1569                x: padding.left + placement.x,
1570                y: padding.top + placement.y,
1571            };
1572
1573            // Critical: Update the child LayoutNode's retained state.
1574            // Standard layouts do this via Placeable::place(), but SubcomposeLayout logic
1575            // bypasses Placeables and returns raw Placements.
1576            if let Ok(mut applier) = applier_host.try_borrow_typed() {
1577                let _ = applier.with_node::<LayoutNode, _>(placement.node_id, |node| {
1578                    node.set_position(position);
1579                });
1580            }
1581
1582            children.push(MeasuredChild {
1583                node: child,
1584                offset: position,
1585            });
1586        }
1587
1588        // Update the SubcomposeLayoutNode's active children for rendering
1589        node_handle.set_active_children(children.iter().map(|c| c.node.node_id));
1590        node_handle.recycle_placement_scratch(placements);
1591
1592        Ok(Some(Rc::new(MeasuredNode::new(
1593            node_id,
1594            Size { width, height },
1595            offset,
1596            Point::default(), // Subcompose nodes: content_offset handled by child layout
1597            children,
1598        ))))
1599    }
1600    /// Measures through the layout modifier coordinator chain using reconciled modifier nodes.
1601    /// Iterates through LayoutModifierNode instances from the ModifierNodeChain and calls
1602    /// their measure() methods through the retained coordinator chain.
1603    ///
1604    /// Always succeeds, measuring either directly or through retained layout modifier nodes.
1605    ///
1606    fn measure_through_modifier_chain(
1607        state_rc: &Rc<RefCell<Self>>,
1608        node_id: NodeId,
1609        runtime_state: &mut LayoutRuntimeState,
1610        measure_policy: &Rc<dyn MeasurePolicy>,
1611        constraints: Constraints,
1612        layout_node_data: &mut Vec<LayoutModifierNodeData>,
1613        placements: &mut Vec<Placement>,
1614    ) -> ModifierChainMeasurement {
1615        use cranpose_foundation::NodeCapabilities;
1616
1617        // Collect layout node information from the modifier chain
1618        layout_node_data.clear();
1619        let mut offset = Point::default();
1620
1621        {
1622            let state = state_rc.borrow();
1623            let mut applier = state.applier.borrow_typed();
1624
1625            let _ = applier.with_node::<LayoutNode, _>(node_id, |layout_node| {
1626                let chain_handle = layout_node.modifier_chain();
1627
1628                if !chain_handle.has_layout_nodes() {
1629                    return;
1630                }
1631
1632                // Collect indices and node Rc clones for layout modifier nodes
1633                chain_handle.chain().for_each_forward_matching(
1634                    NodeCapabilities::LAYOUT,
1635                    |node_ref| {
1636                        if let Some(index) = node_ref.entry_index() {
1637                            // Get the Rc clone for this node
1638                            if let Some(node_rc) = chain_handle.chain().get_node_rc(index) {
1639                                layout_node_data.push((index, Rc::clone(&node_rc)));
1640                            }
1641
1642                            // Extract offset from OffsetNode for the node's own position
1643                            // The coordinator chain handles placement_offset (for children),
1644                            // but the node's offset affects where IT is positioned in the parent
1645                            node_ref.with_node(|node| {
1646                                if let Some(offset_node) =
1647                                    node.as_any()
1648                                        .downcast_ref::<crate::modifier_nodes::OffsetNode>()
1649                                {
1650                                    let delta = offset_node.offset();
1651                                    offset.x += delta.x;
1652                                    offset.y += delta.y;
1653                                }
1654                            });
1655                        }
1656                    },
1657                );
1658            });
1659        }
1660
1661        // Fast path: if there are no layout modifiers, measure directly without the
1662        // retained coordinator chain frame.
1663        if layout_node_data.is_empty() {
1664            let final_size = measure_policy.measure_into(
1665                runtime_state.child_measurables(),
1666                constraints,
1667                placements,
1668            );
1669
1670            return ModifierChainMeasurement {
1671                size: final_size,
1672                content_offset: Point::default(),
1673                offset,
1674            };
1675        }
1676
1677        runtime_state.reconcile_coordinator_chain(layout_node_data.as_slice());
1678        let frame = CoordinatorFrame::new(
1679            measure_policy,
1680            runtime_state.child_measurables(),
1681            placements,
1682        );
1683
1684        // Measure through the complete coordinator chain
1685        let placeable = runtime_state
1686            .coordinator_chain()
1687            .measure_from(0, &frame, constraints);
1688        let final_size = Size {
1689            width: placeable.width(),
1690            height: placeable.height(),
1691        };
1692
1693        // Get accumulated content offset from the placeable (computed during measure)
1694        let content_offset = placeable.content_offset();
1695        let all_placement_offset = Point {
1696            x: content_offset.0,
1697            y: content_offset.1,
1698        };
1699
1700        // The content_offset for scroll/inner transforms is the accumulated placement offset
1701        // MINUS the node's own offset (which affects its position in the parent, not content position).
1702        // This properly separates: node position (offset) vs inner content position (content_offset).
1703        let content_offset = Point {
1704            x: all_placement_offset.x - offset.x,
1705            y: all_placement_offset.y - offset.y,
1706        };
1707
1708        // offset was already extracted from OffsetNode above
1709
1710        // Process any invalidations requested during measurement
1711        let invalidations = frame.take_invalidations();
1712        if !invalidations.is_empty() {
1713            // Mark the LayoutNode as needing the appropriate passes
1714            Self::with_applier_result(state_rc, |applier| {
1715                applier.with_node::<LayoutNode, _>(node_id, |layout_node| {
1716                    for kind in invalidations {
1717                        match kind {
1718                            InvalidationKind::Layout => layout_node.mark_needs_measure(),
1719                            InvalidationKind::Draw => layout_node.mark_needs_redraw(),
1720                            InvalidationKind::Semantics => layout_node.mark_needs_semantics(),
1721                            InvalidationKind::PointerInput => layout_node.mark_needs_pointer_pass(),
1722                            InvalidationKind::Focus => layout_node.mark_needs_focus_sync(),
1723                        }
1724                    }
1725                })
1726            })
1727            .ok();
1728        }
1729
1730        ModifierChainMeasurement {
1731            size: final_size,
1732            content_offset,
1733            offset,
1734        }
1735    }
1736
1737    fn layout_child_measure_data(
1738        applier: &mut MemoryApplier,
1739        child_id: NodeId,
1740    ) -> Result<Option<LayoutChildMeasureData>, NodeError> {
1741        match applier.with_node::<LayoutNode, _>(child_id, |n| LayoutChildMeasureData {
1742            cache: n.cache_handles(),
1743            layout_state: Some(n.layout_state_handle()),
1744            needs_layout: n.needs_layout(),
1745            needs_measure: n.needs_measure(),
1746        }) {
1747            Ok(data) => Ok(Some(data)),
1748            Err(NodeError::TypeMismatch { .. }) => {
1749                match applier.with_node::<SubcomposeLayoutNode, _>(child_id, |n| {
1750                    LayoutChildMeasureData {
1751                        cache: n.cache_handles(),
1752                        layout_state: None,
1753                        needs_layout: n.needs_layout(),
1754                        needs_measure: n.needs_measure(),
1755                    }
1756                }) {
1757                    Ok(data) => Ok(Some(data)),
1758                    Err(NodeError::TypeMismatch { .. }) | Err(NodeError::Missing { .. }) => {
1759                        Ok(None)
1760                    }
1761                    Err(err) => Err(err),
1762                }
1763            }
1764            Err(NodeError::Missing { .. }) => Ok(None),
1765            Err(err) => Err(err),
1766        }
1767    }
1768
1769    fn measure_layout_node(
1770        state_rc: Rc<RefCell<Self>>,
1771        node_id: NodeId,
1772        snapshot: LayoutNodeSnapshot,
1773        constraints: Constraints,
1774    ) -> Result<Rc<MeasuredNode>, NodeError> {
1775        let cache_epoch = {
1776            let state = state_rc.borrow();
1777            state.cache_epoch
1778        };
1779        let LayoutNodeSnapshot {
1780            measure_policy,
1781            cache,
1782            layout_runtime_state,
1783            needs_layout,
1784            needs_measure,
1785        } = snapshot;
1786        cache.activate(cache_epoch);
1787
1788        if needs_measure {
1789            // Node has needs_measure=true
1790        }
1791
1792        // Only check cache when the node is fully clean.
1793        // needs_layout=true means either the node itself or one of its descendants
1794        // must be revisited even if the node's own measured size can stay cached.
1795        if !needs_measure && !needs_layout {
1796            // Check cache for current constraints
1797            if let Some(cached) = cache.get_measurement(constraints) {
1798                // Clear dirty flag after successful cache hit
1799                Self::with_applier_result(&state_rc, |applier| {
1800                    applier.with_node::<LayoutNode, _>(node_id, |node| {
1801                        node.clear_needs_measure();
1802                        node.clear_needs_layout();
1803                    })
1804                })
1805                .ok();
1806                return Ok(cached);
1807            }
1808        }
1809
1810        let (runtime_handle, applier_host) = {
1811            let state = state_rc.borrow();
1812            (state.runtime_handle.clone(), Rc::clone(&state.applier))
1813        };
1814
1815        let measure_handle = LayoutMeasureHandle::new(Rc::clone(&state_rc));
1816        let error = Rc::new(RefCell::new(None));
1817        let mut pools = VecPools::acquire(Rc::clone(&state_rc));
1818        let (records, child_ids, layout_node_data, placements) = pools.parts();
1819
1820        applier_host
1821            .borrow_typed()
1822            .with_node::<LayoutNode, _>(node_id, |node| {
1823                child_ids.extend_from_slice(&node.children);
1824            })?;
1825
1826        let mut valid_child_count = 0;
1827        for index in 0..child_ids.len() {
1828            let child_id = child_ids[index];
1829            let child_exists = {
1830                let mut applier = applier_host.borrow_typed();
1831                Self::layout_child_measure_data(&mut applier, child_id)?.is_some()
1832            };
1833            if child_exists {
1834                child_ids[valid_child_count] = child_id;
1835                valid_child_count += 1;
1836            }
1837        }
1838        child_ids.truncate(valid_child_count);
1839
1840        let _frame_binding_cleanup =
1841            LayoutRuntimeFrameBindingCleanup::new(Rc::clone(&layout_runtime_state));
1842
1843        {
1844            let mut runtime_state = layout_runtime_state.borrow_mut();
1845            runtime_state.reconcile_child_measurables(child_ids.as_slice());
1846
1847            for (index, &child_id) in child_ids.iter().enumerate() {
1848                let data = {
1849                    let mut applier = applier_host.borrow_typed();
1850                    Self::layout_child_measure_data(&mut applier, child_id)?
1851                };
1852                let Some(data) = data else {
1853                    continue;
1854                };
1855
1856                let child_is_dirty = data.needs_layout || data.needs_measure;
1857                let child_cache_epoch = if child_is_dirty {
1858                    cache_epoch
1859                } else {
1860                    data.cache.epoch()
1861                };
1862                let child_state = runtime_state.child_state(index);
1863                child_state.configure(LayoutChildMeasureConfig {
1864                    applier: Rc::clone(&applier_host),
1865                    node_id: child_id,
1866                    error: Rc::clone(&error),
1867                    runtime_handle: runtime_handle.clone(),
1868                    cache: data.cache,
1869                    cache_epoch: child_cache_epoch,
1870                    force_remeasure: child_is_dirty,
1871                    measure_handle: Some(measure_handle.clone()),
1872                    layout_state: data.layout_state,
1873                });
1874                records.push((child_id, ChildRecord { state: child_state }));
1875            }
1876        }
1877
1878        let chain_constraints = constraints;
1879
1880        let modifier_chain_result = {
1881            let mut runtime_state = layout_runtime_state.borrow_mut();
1882            Self::measure_through_modifier_chain(
1883                &state_rc,
1884                node_id,
1885                &mut runtime_state,
1886                &measure_policy,
1887                chain_constraints,
1888                layout_node_data,
1889                placements,
1890            )
1891        };
1892
1893        // Modifier chain always succeeds - use the node-driven measurement.
1894        let (width, height, content_offset, offset) = {
1895            let result = modifier_chain_result;
1896            // The size is already correct from the modifier chain (modifiers like SizeNode
1897            // have already enforced their constraints), so we use it directly.
1898            if let Some(err) = error.borrow_mut().take() {
1899                return Err(err);
1900            }
1901
1902            (
1903                result.size.width,
1904                result.size.height,
1905                result.content_offset,
1906                result.offset,
1907            )
1908        };
1909
1910        let mut measured_children = Vec::with_capacity(records.len());
1911        for (child_id, record) in records.iter() {
1912            if let Some(measured) = record.state.take_measured() {
1913                let base_position = placements
1914                    .iter()
1915                    .find(|placement| placement.node_id == *child_id)
1916                    .map(|placement| Point {
1917                        x: placement.x,
1918                        y: placement.y,
1919                    })
1920                    .or_else(|| record.state.last_position())
1921                    .unwrap_or(Point { x: 0.0, y: 0.0 });
1922                // Apply content_offset (from scroll/transforms) to child positioning
1923                let position = Point {
1924                    x: content_offset.x + base_position.x,
1925                    y: content_offset.y + base_position.y,
1926                };
1927                measured_children.push(MeasuredChild {
1928                    node: measured,
1929                    offset: position,
1930                });
1931            }
1932        }
1933
1934        let measured = Rc::new(MeasuredNode::new(
1935            node_id,
1936            Size { width, height },
1937            offset,
1938            content_offset,
1939            measured_children,
1940        ));
1941
1942        cache.store_measurement(constraints, Rc::clone(&measured));
1943
1944        // Clear dirty flags and update derived state
1945        Self::with_applier_result(&state_rc, |applier| {
1946            applier.with_node::<LayoutNode, _>(node_id, |node| {
1947                node.clear_needs_measure();
1948                node.clear_needs_layout();
1949                node.set_measured_size(Size { width, height });
1950                node.set_content_offset(content_offset);
1951            })
1952        })
1953        .ok();
1954
1955        Ok(measured)
1956    }
1957}
1958
1959struct LayoutChildMeasureData {
1960    cache: LayoutNodeCacheHandles,
1961    layout_state: Option<Rc<RefCell<LayoutState>>>,
1962    needs_layout: bool,
1963    needs_measure: bool,
1964}
1965
1966/// Snapshot of a LayoutNode's data for measuring.
1967/// This is a temporary copy used during the measure phase, not a live node.
1968///
1969/// Note: We capture `needs_measure` here because it's checked during measure to enable
1970/// selective measure optimization at the individual node level. Even if the tree is partially
1971/// dirty (some nodes changed), clean nodes can skip measure and use cached results.
1972struct LayoutNodeSnapshot {
1973    measure_policy: Rc<dyn MeasurePolicy>,
1974    cache: LayoutNodeCacheHandles,
1975    layout_runtime_state: Rc<RefCell<LayoutRuntimeState>>,
1976    needs_layout: bool,
1977    /// Whether this specific node needs to be measured (vs using cached measurement)
1978    needs_measure: bool,
1979}
1980
1981impl LayoutNodeSnapshot {
1982    fn from_layout_node(node: &LayoutNode) -> Self {
1983        Self {
1984            measure_policy: Rc::clone(&node.measure_policy),
1985            cache: node.cache_handles(),
1986            layout_runtime_state: node.layout_runtime_state_handle(),
1987            needs_layout: node.needs_layout(),
1988            needs_measure: node.needs_measure(),
1989        }
1990    }
1991}
1992
1993// Helper types for accessing subsets of LayoutBuilderState
1994struct VecPools {
1995    state: Rc<RefCell<LayoutBuilderState>>,
1996    records: Vec<(NodeId, ChildRecord)>,
1997    child_ids: Vec<NodeId>,
1998    layout_node_data: Vec<LayoutModifierNodeData>,
1999    placements: Vec<Placement>,
2000}
2001
2002impl VecPools {
2003    fn acquire(state: Rc<RefCell<LayoutBuilderState>>) -> Self {
2004        let (records, child_ids, layout_node_data, placements) = {
2005            let mut state_mut = state.borrow_mut();
2006            (
2007                state_mut.frame_arena.tmp_records.acquire(),
2008                state_mut.frame_arena.tmp_child_ids.acquire(),
2009                state_mut.frame_arena.tmp_layout_node_data.acquire(),
2010                state_mut.frame_arena.tmp_placements.acquire(),
2011            )
2012        };
2013        Self {
2014            state,
2015            records,
2016            child_ids,
2017            layout_node_data,
2018            placements,
2019        }
2020    }
2021
2022    #[allow(clippy::type_complexity)] // Returns internal Vec references for layout operations
2023    fn parts(
2024        &mut self,
2025    ) -> (
2026        &mut Vec<(NodeId, ChildRecord)>,
2027        &mut Vec<NodeId>,
2028        &mut Vec<LayoutModifierNodeData>,
2029        &mut Vec<Placement>,
2030    ) {
2031        (
2032            &mut self.records,
2033            &mut self.child_ids,
2034            &mut self.layout_node_data,
2035            &mut self.placements,
2036        )
2037    }
2038}
2039
2040impl Drop for VecPools {
2041    fn drop(&mut self) {
2042        let mut state = self.state.borrow_mut();
2043        state
2044            .frame_arena
2045            .tmp_records
2046            .release(std::mem::take(&mut self.records));
2047        state
2048            .frame_arena
2049            .tmp_child_ids
2050            .release(std::mem::take(&mut self.child_ids));
2051        state
2052            .frame_arena
2053            .tmp_layout_node_data
2054            .release(std::mem::take(&mut self.layout_node_data));
2055        state
2056            .frame_arena
2057            .tmp_placements
2058            .release(std::mem::take(&mut self.placements));
2059    }
2060}
2061
2062struct SlotsGuard {
2063    state: Rc<RefCell<LayoutBuilderState>>,
2064    slots: Option<SlotTable>,
2065}
2066
2067impl SlotsGuard {
2068    fn take(state: Rc<RefCell<LayoutBuilderState>>) -> Self {
2069        let slots = {
2070            let state_ref = state.borrow();
2071            let mut slots_ref = state_ref.slots.borrow_mut();
2072            std::mem::take(&mut *slots_ref)
2073        };
2074        Self {
2075            state,
2076            slots: Some(slots),
2077        }
2078    }
2079
2080    fn host(&mut self) -> Rc<SlotsHost> {
2081        let slots = self.slots.take().unwrap_or_default();
2082        Rc::new(SlotsHost::new(slots))
2083    }
2084
2085    fn restore(&mut self, slots: SlotTable) {
2086        debug_assert!(self.slots.is_none());
2087        self.slots = Some(slots);
2088    }
2089}
2090
2091impl Drop for SlotsGuard {
2092    fn drop(&mut self) {
2093        if let Some(slots) = self.slots.take() {
2094            let state_ref = self.state.borrow();
2095            *state_ref.slots.borrow_mut() = slots;
2096        }
2097    }
2098}
2099
2100#[derive(Clone)]
2101struct LayoutMeasureHandle {
2102    state: Rc<RefCell<LayoutBuilderState>>,
2103}
2104
2105impl LayoutMeasureHandle {
2106    fn new(state: Rc<RefCell<LayoutBuilderState>>) -> Self {
2107        Self { state }
2108    }
2109
2110    fn measure(
2111        &self,
2112        node_id: NodeId,
2113        constraints: Constraints,
2114    ) -> Result<Rc<MeasuredNode>, NodeError> {
2115        LayoutBuilderState::measure_node(Rc::clone(&self.state), node_id, constraints)
2116    }
2117}
2118
2119#[derive(Debug, Clone)]
2120pub(crate) struct MeasuredNode {
2121    node_id: NodeId,
2122    size: Size,
2123    /// Node's position offset relative to parent (from OffsetNode etc.)
2124    offset: Point,
2125    /// Content offset for scroll/inner transforms (NOT node position)
2126    content_offset: Point,
2127    children: Vec<MeasuredChild>,
2128}
2129
2130impl MeasuredNode {
2131    fn new(
2132        node_id: NodeId,
2133        size: Size,
2134        offset: Point,
2135        content_offset: Point,
2136        children: Vec<MeasuredChild>,
2137    ) -> Self {
2138        Self {
2139            node_id,
2140            size,
2141            offset,
2142            content_offset,
2143            children,
2144        }
2145    }
2146
2147    #[cfg(test)]
2148    pub(crate) fn leaf(node_id: NodeId, size: Size) -> Self {
2149        Self::new(
2150            node_id,
2151            size,
2152            Point::default(),
2153            Point::default(),
2154            Vec::new(),
2155        )
2156    }
2157
2158    pub(crate) fn node_id(&self) -> NodeId {
2159        self.node_id
2160    }
2161
2162    pub(crate) fn size(&self) -> Size {
2163        self.size
2164    }
2165}
2166
2167#[derive(Debug, Clone)]
2168struct MeasuredChild {
2169    node: Rc<MeasuredNode>,
2170    offset: Point,
2171}
2172
2173struct ChildRecord {
2174    state: Rc<LayoutChildMeasureState>,
2175}
2176
2177struct CoordinatorFrame<'a> {
2178    measure_policy: &'a Rc<dyn MeasurePolicy>,
2179    measurables: &'a [Box<dyn Measurable>],
2180    placements: RefCell<&'a mut Vec<Placement>>,
2181    context: RefCell<LayoutNodeContext>,
2182}
2183
2184impl<'a> CoordinatorFrame<'a> {
2185    fn new(
2186        measure_policy: &'a Rc<dyn MeasurePolicy>,
2187        measurables: &'a [Box<dyn Measurable>],
2188        placements: &'a mut Vec<Placement>,
2189    ) -> Self {
2190        Self {
2191            measure_policy,
2192            measurables,
2193            placements: RefCell::new(placements),
2194            context: RefCell::new(LayoutNodeContext::new()),
2195        }
2196    }
2197
2198    fn take_invalidations(&self) -> Vec<InvalidationKind> {
2199        self.context.borrow_mut().take_invalidations()
2200    }
2201}
2202
2203struct CoordinatorLink<'chain, 'frame_ref, 'frame_data> {
2204    chain: &'chain CoordinatorChain,
2205    frame: &'frame_ref CoordinatorFrame<'frame_data>,
2206    index: usize,
2207}
2208
2209impl Measurable for CoordinatorLink<'_, '_, '_> {
2210    fn measure(&self, constraints: Constraints) -> Placeable {
2211        self.chain.measure_from(self.index, self.frame, constraints)
2212    }
2213
2214    fn min_intrinsic_width(&self, height: f32) -> f32 {
2215        self.chain
2216            .min_intrinsic_width_from(self.index, self.frame, height)
2217    }
2218
2219    fn max_intrinsic_width(&self, height: f32) -> f32 {
2220        self.chain
2221            .max_intrinsic_width_from(self.index, self.frame, height)
2222    }
2223
2224    fn min_intrinsic_height(&self, width: f32) -> f32 {
2225        self.chain
2226            .min_intrinsic_height_from(self.index, self.frame, width)
2227    }
2228
2229    fn max_intrinsic_height(&self, width: f32) -> f32 {
2230        self.chain
2231            .max_intrinsic_height_from(self.index, self.frame, width)
2232    }
2233}
2234
2235struct CoordinatorNode {
2236    modifier_index: usize,
2237    node: Rc<RefCell<Box<dyn cranpose_foundation::ModifierNode>>>,
2238    measured_size: Cell<Size>,
2239    accumulated_offset: Cell<Point>,
2240}
2241
2242impl CoordinatorNode {
2243    fn new(
2244        modifier_index: usize,
2245        node: Rc<RefCell<Box<dyn cranpose_foundation::ModifierNode>>>,
2246    ) -> Self {
2247        Self {
2248            modifier_index,
2249            node,
2250            measured_size: Cell::new(Size::default()),
2251            accumulated_offset: Cell::new(Point::default()),
2252        }
2253    }
2254
2255    fn matches(
2256        &self,
2257        modifier_index: usize,
2258        node: &Rc<RefCell<Box<dyn cranpose_foundation::ModifierNode>>>,
2259    ) -> bool {
2260        self.modifier_index == modifier_index && Rc::ptr_eq(&self.node, node)
2261    }
2262
2263    #[cfg(test)]
2264    fn ptr(&self) -> usize {
2265        Rc::as_ptr(&self.node) as *const () as usize
2266    }
2267}
2268
2269#[derive(Default)]
2270struct CoordinatorChain {
2271    nodes: Vec<CoordinatorNode>,
2272}
2273
2274impl CoordinatorChain {
2275    fn reconcile(&mut self, layout_node_data: &[LayoutModifierNodeData]) {
2276        if self.matches(layout_node_data) {
2277            return;
2278        }
2279
2280        let mut previous_nodes = std::mem::take(&mut self.nodes);
2281        self.nodes.reserve(layout_node_data.len());
2282
2283        for (modifier_index, node) in layout_node_data.iter() {
2284            if let Some(position) = previous_nodes
2285                .iter()
2286                .position(|candidate| candidate.matches(*modifier_index, node))
2287            {
2288                self.nodes.push(previous_nodes.swap_remove(position));
2289            } else {
2290                self.nodes
2291                    .push(CoordinatorNode::new(*modifier_index, Rc::clone(node)));
2292            }
2293        }
2294    }
2295
2296    fn matches(&self, layout_node_data: &[LayoutModifierNodeData]) -> bool {
2297        self.nodes.len() == layout_node_data.len()
2298            && self
2299                .nodes
2300                .iter()
2301                .zip(layout_node_data.iter())
2302                .all(|(node, (modifier_index, node_rc))| node.matches(*modifier_index, node_rc))
2303    }
2304
2305    fn measure_from(
2306        &self,
2307        index: usize,
2308        frame: &CoordinatorFrame<'_>,
2309        constraints: Constraints,
2310    ) -> Placeable {
2311        let Some(node) = self.nodes.get(index) else {
2312            let mut placements = frame.placements.borrow_mut();
2313            let size =
2314                frame
2315                    .measure_policy
2316                    .measure_into(frame.measurables, constraints, &mut placements);
2317            return Placeable::value(size.width, size.height, NodeId::default());
2318        };
2319
2320        let wrapped = CoordinatorLink {
2321            chain: self,
2322            frame,
2323            index: index + 1,
2324        };
2325        let node_borrow = node.node.borrow();
2326
2327        let Some(layout_node) = node_borrow.as_layout_node() else {
2328            let placeable = wrapped.measure(constraints);
2329            let child_accumulated = self.total_content_offset_from(index + 1);
2330            node.accumulated_offset.set(child_accumulated);
2331            return Placeable::value_with_offset(
2332                placeable.width(),
2333                placeable.height(),
2334                NodeId::default(),
2335                (child_accumulated.x, child_accumulated.y),
2336            );
2337        };
2338
2339        let result = match frame.context.try_borrow_mut() {
2340            Ok(mut context) => layout_node.measure(&mut *context, &wrapped, constraints),
2341            Err(_) => {
2342                let mut temp = LayoutNodeContext::new();
2343                let result = layout_node.measure(&mut temp, &wrapped, constraints);
2344                if let Ok(mut context) = frame.context.try_borrow_mut() {
2345                    for kind in temp.take_invalidations() {
2346                        context.invalidate(kind);
2347                    }
2348                }
2349                result
2350            }
2351        };
2352
2353        node.measured_size.set(result.size);
2354        let local_offset = Point {
2355            x: result.placement_offset_x,
2356            y: result.placement_offset_y,
2357        };
2358        let child_accumulated = self.total_content_offset_from(index + 1);
2359        let accumulated = Point {
2360            x: local_offset.x + child_accumulated.x,
2361            y: local_offset.y + child_accumulated.y,
2362        };
2363        node.accumulated_offset.set(accumulated);
2364
2365        Placeable::value_with_offset(
2366            result.size.width,
2367            result.size.height,
2368            NodeId::default(),
2369            (accumulated.x, accumulated.y),
2370        )
2371    }
2372
2373    fn min_intrinsic_width_from(
2374        &self,
2375        index: usize,
2376        frame: &CoordinatorFrame<'_>,
2377        height: f32,
2378    ) -> f32 {
2379        let Some(node) = self.nodes.get(index) else {
2380            return frame
2381                .measure_policy
2382                .min_intrinsic_width(frame.measurables, height);
2383        };
2384        let wrapped = CoordinatorLink {
2385            chain: self,
2386            frame,
2387            index: index + 1,
2388        };
2389        let node_borrow = node.node.borrow();
2390        node_borrow
2391            .as_layout_node()
2392            .map(|layout_node| layout_node.min_intrinsic_width(&wrapped, height))
2393            .unwrap_or_else(|| wrapped.min_intrinsic_width(height))
2394    }
2395
2396    fn max_intrinsic_width_from(
2397        &self,
2398        index: usize,
2399        frame: &CoordinatorFrame<'_>,
2400        height: f32,
2401    ) -> f32 {
2402        let Some(node) = self.nodes.get(index) else {
2403            return frame
2404                .measure_policy
2405                .max_intrinsic_width(frame.measurables, height);
2406        };
2407        let wrapped = CoordinatorLink {
2408            chain: self,
2409            frame,
2410            index: index + 1,
2411        };
2412        let node_borrow = node.node.borrow();
2413        node_borrow
2414            .as_layout_node()
2415            .map(|layout_node| layout_node.max_intrinsic_width(&wrapped, height))
2416            .unwrap_or_else(|| wrapped.max_intrinsic_width(height))
2417    }
2418
2419    fn min_intrinsic_height_from(
2420        &self,
2421        index: usize,
2422        frame: &CoordinatorFrame<'_>,
2423        width: f32,
2424    ) -> f32 {
2425        let Some(node) = self.nodes.get(index) else {
2426            return frame
2427                .measure_policy
2428                .min_intrinsic_height(frame.measurables, width);
2429        };
2430        let wrapped = CoordinatorLink {
2431            chain: self,
2432            frame,
2433            index: index + 1,
2434        };
2435        let node_borrow = node.node.borrow();
2436        node_borrow
2437            .as_layout_node()
2438            .map(|layout_node| layout_node.min_intrinsic_height(&wrapped, width))
2439            .unwrap_or_else(|| wrapped.min_intrinsic_height(width))
2440    }
2441
2442    fn max_intrinsic_height_from(
2443        &self,
2444        index: usize,
2445        frame: &CoordinatorFrame<'_>,
2446        width: f32,
2447    ) -> f32 {
2448        let Some(node) = self.nodes.get(index) else {
2449            return frame
2450                .measure_policy
2451                .max_intrinsic_height(frame.measurables, width);
2452        };
2453        let wrapped = CoordinatorLink {
2454            chain: self,
2455            frame,
2456            index: index + 1,
2457        };
2458        let node_borrow = node.node.borrow();
2459        node_borrow
2460            .as_layout_node()
2461            .map(|layout_node| layout_node.max_intrinsic_height(&wrapped, width))
2462            .unwrap_or_else(|| wrapped.max_intrinsic_height(width))
2463    }
2464
2465    fn total_content_offset_from(&self, index: usize) -> Point {
2466        self.nodes
2467            .get(index)
2468            .map(|node| node.accumulated_offset.get())
2469            .unwrap_or_default()
2470    }
2471
2472    #[cfg(test)]
2473    fn debug_ptrs(&self) -> Vec<usize> {
2474        self.nodes.iter().map(CoordinatorNode::ptr).collect()
2475    }
2476}
2477
2478#[derive(Default)]
2479pub(crate) struct LayoutRuntimeState {
2480    child_ids: Vec<NodeId>,
2481    child_states: Vec<Rc<LayoutChildMeasureState>>,
2482    child_measurables: Vec<Box<dyn Measurable>>,
2483    coordinator_chain: CoordinatorChain,
2484}
2485
2486impl LayoutRuntimeState {
2487    fn reconcile_child_measurables(&mut self, child_ids: &[NodeId]) {
2488        if self.child_ids == child_ids {
2489            return;
2490        }
2491
2492        let mut previous_ids = std::mem::take(&mut self.child_ids);
2493        let mut previous_states = std::mem::take(&mut self.child_states);
2494        let mut previous_measurables = std::mem::take(&mut self.child_measurables);
2495
2496        self.child_ids.reserve(child_ids.len());
2497        self.child_states.reserve(child_ids.len());
2498        self.child_measurables.reserve(child_ids.len());
2499
2500        for &child_id in child_ids {
2501            if let Some(position) = previous_ids.iter().position(|&id| id == child_id) {
2502                self.child_ids.push(previous_ids.swap_remove(position));
2503                self.child_states
2504                    .push(previous_states.swap_remove(position));
2505                self.child_measurables
2506                    .push(previous_measurables.swap_remove(position));
2507            } else {
2508                let state = LayoutChildMeasureState::new(child_id);
2509                self.child_ids.push(child_id);
2510                self.child_states.push(Rc::clone(&state));
2511                self.child_measurables
2512                    .push(Box::new(LayoutChildMeasurable::new(state)));
2513            }
2514        }
2515    }
2516
2517    fn child_state(&self, index: usize) -> Rc<LayoutChildMeasureState> {
2518        Rc::clone(&self.child_states[index])
2519    }
2520
2521    fn child_measurables(&self) -> &[Box<dyn Measurable>] {
2522        self.child_measurables.as_slice()
2523    }
2524
2525    fn reconcile_coordinator_chain(&mut self, layout_node_data: &[LayoutModifierNodeData]) {
2526        self.coordinator_chain.reconcile(layout_node_data);
2527    }
2528
2529    fn coordinator_chain(&self) -> &CoordinatorChain {
2530        &self.coordinator_chain
2531    }
2532
2533    fn clear_frame_bindings(&self) {
2534        for child_state in &self.child_states {
2535            child_state.clear_frame_bindings();
2536        }
2537    }
2538
2539    #[cfg(test)]
2540    pub(crate) fn debug_stats(&self) -> LayoutRuntimeDebugStats {
2541        LayoutRuntimeDebugStats {
2542            child_ids: self.child_ids.clone(),
2543            child_state_ptrs: self
2544                .child_states
2545                .iter()
2546                .map(|state| Rc::as_ptr(state) as *const () as usize)
2547                .collect(),
2548            child_measurable_ptrs: self
2549                .child_measurables
2550                .iter()
2551                .map(|measurable| {
2552                    measurable.as_ref() as *const dyn Measurable as *const () as usize
2553                })
2554                .collect(),
2555            child_measurable_count: self.child_measurables.len(),
2556            coordinator_node_ptrs: self.coordinator_chain.debug_ptrs(),
2557            coordinator_node_count: self.coordinator_chain.nodes.len(),
2558        }
2559    }
2560}
2561
2562#[cfg(test)]
2563#[derive(Debug, Clone, PartialEq, Eq)]
2564pub(crate) struct LayoutRuntimeDebugStats {
2565    pub(crate) child_ids: Vec<NodeId>,
2566    pub(crate) child_state_ptrs: Vec<usize>,
2567    pub(crate) child_measurable_ptrs: Vec<usize>,
2568    pub(crate) child_measurable_count: usize,
2569    pub(crate) coordinator_node_ptrs: Vec<usize>,
2570    pub(crate) coordinator_node_count: usize,
2571}
2572
2573struct LayoutChildMeasureConfig {
2574    applier: Rc<ConcreteApplierHost<MemoryApplier>>,
2575    node_id: NodeId,
2576    error: Rc<RefCell<Option<NodeError>>>,
2577    runtime_handle: Option<RuntimeHandle>,
2578    cache: LayoutNodeCacheHandles,
2579    cache_epoch: u64,
2580    force_remeasure: bool,
2581    measure_handle: Option<LayoutMeasureHandle>,
2582    layout_state: Option<Rc<RefCell<LayoutState>>>,
2583}
2584
2585struct LayoutChildMeasureState {
2586    applier: RefCell<Option<Rc<ConcreteApplierHost<MemoryApplier>>>>,
2587    node_id: Cell<NodeId>,
2588    measured: RefCell<Option<Rc<MeasuredNode>>>,
2589    last_position: Cell<Option<Point>>,
2590    error: RefCell<Option<Rc<RefCell<Option<NodeError>>>>>,
2591    runtime_handle: RefCell<Option<RuntimeHandle>>,
2592    cache: RefCell<LayoutNodeCacheHandles>,
2593    cache_epoch: Cell<u64>,
2594    force_remeasure: Cell<bool>,
2595    measure_handle: RefCell<Option<LayoutMeasureHandle>>,
2596    layout_state: RefCell<Option<Rc<RefCell<LayoutState>>>>,
2597}
2598
2599impl LayoutChildMeasureState {
2600    fn new(node_id: NodeId) -> Rc<Self> {
2601        Rc::new(Self {
2602            applier: RefCell::new(None),
2603            node_id: Cell::new(node_id),
2604            measured: RefCell::new(None),
2605            last_position: Cell::new(None),
2606            error: RefCell::new(None),
2607            runtime_handle: RefCell::new(None),
2608            cache: RefCell::new(LayoutNodeCacheHandles::default()),
2609            cache_epoch: Cell::new(0),
2610            force_remeasure: Cell::new(true),
2611            measure_handle: RefCell::new(None),
2612            layout_state: RefCell::new(None),
2613        })
2614    }
2615
2616    fn configure(&self, config: LayoutChildMeasureConfig) {
2617        config.cache.activate(config.cache_epoch);
2618        *self.applier.borrow_mut() = Some(config.applier);
2619        self.node_id.set(config.node_id);
2620        self.measured.borrow_mut().take();
2621        self.last_position.set(None);
2622        *self.error.borrow_mut() = Some(config.error);
2623        *self.runtime_handle.borrow_mut() = config.runtime_handle;
2624        *self.cache.borrow_mut() = config.cache;
2625        self.cache_epoch.set(config.cache_epoch);
2626        self.force_remeasure.set(config.force_remeasure);
2627        *self.measure_handle.borrow_mut() = config.measure_handle;
2628        *self.layout_state.borrow_mut() = config.layout_state;
2629    }
2630
2631    fn clear_frame_bindings(&self) {
2632        self.measured.borrow_mut().take();
2633        *self.applier.borrow_mut() = None;
2634        *self.error.borrow_mut() = None;
2635        *self.runtime_handle.borrow_mut() = None;
2636        *self.measure_handle.borrow_mut() = None;
2637        *self.layout_state.borrow_mut() = None;
2638    }
2639
2640    fn node_id(&self) -> NodeId {
2641        self.node_id.get()
2642    }
2643
2644    fn cache(&self) -> LayoutNodeCacheHandles {
2645        self.cache.borrow().clone()
2646    }
2647
2648    fn applier(&self) -> Option<Rc<ConcreteApplierHost<MemoryApplier>>> {
2649        self.applier.borrow().clone()
2650    }
2651
2652    fn layout_state(&self) -> Option<Rc<RefCell<LayoutState>>> {
2653        self.layout_state.borrow().clone()
2654    }
2655
2656    fn take_measured(&self) -> Option<Rc<MeasuredNode>> {
2657        self.measured.borrow_mut().take()
2658    }
2659
2660    fn last_position(&self) -> Option<Point> {
2661        self.last_position.get()
2662    }
2663
2664    fn set_last_position(&self, position: Point) {
2665        self.last_position.set(Some(position));
2666    }
2667
2668    fn set_measured(&self, measured: Option<Rc<MeasuredNode>>) {
2669        *self.measured.borrow_mut() = measured;
2670    }
2671
2672    fn record_error(&self, err: NodeError) {
2673        let Some(error) = self.error.borrow().clone() else {
2674            return;
2675        };
2676        let mut slot = error.borrow_mut();
2677        if slot.is_none() {
2678            *slot = Some(err);
2679        }
2680    }
2681
2682    fn perform_measure(&self, constraints: Constraints) -> Result<Rc<MeasuredNode>, NodeError> {
2683        let node_id = self.node_id();
2684        if let Some(handle) = self.measure_handle.borrow().clone() {
2685            return handle.measure(node_id, constraints);
2686        }
2687        let applier = self.applier().ok_or(NodeError::MissingContext {
2688            id: node_id,
2689            reason: "layout child applier not configured",
2690        })?;
2691        measure_node_with_host(
2692            applier,
2693            self.runtime_handle.borrow().clone(),
2694            node_id,
2695            constraints,
2696            self.cache_epoch.get(),
2697        )
2698    }
2699
2700    fn intrinsic_measure(&self, constraints: Constraints) -> Option<Rc<MeasuredNode>> {
2701        let cache = self.cache();
2702        cache.activate(self.cache_epoch.get());
2703        if !self.force_remeasure.get() {
2704            if let Some(cached) = cache.get_measurement(constraints) {
2705                return Some(cached);
2706            }
2707        }
2708
2709        match self.perform_measure(constraints) {
2710            Ok(measured) => {
2711                self.force_remeasure.set(false);
2712                cache.store_measurement(constraints, Rc::clone(&measured));
2713                Some(measured)
2714            }
2715            Err(err) => {
2716                self.record_error(err);
2717                None
2718            }
2719        }
2720    }
2721}
2722
2723struct LayoutChildMeasurable {
2724    state: Rc<LayoutChildMeasureState>,
2725}
2726
2727impl LayoutChildMeasurable {
2728    fn new(state: Rc<LayoutChildMeasureState>) -> Self {
2729        Self { state }
2730    }
2731}
2732
2733impl Measurable for LayoutChildMeasurable {
2734    fn measure(&self, constraints: Constraints) -> Placeable {
2735        let state = &self.state;
2736        let cache = state.cache();
2737        cache.activate(state.cache_epoch.get());
2738        let measured_size;
2739        if !state.force_remeasure.get() {
2740            if let Some(cached) = cache.get_measurement(constraints) {
2741                measured_size = cached.size;
2742                state.set_measured(Some(Rc::clone(&cached)));
2743            } else {
2744                match state.perform_measure(constraints) {
2745                    Ok(measured) => {
2746                        state.force_remeasure.set(false);
2747                        measured_size = measured.size;
2748                        cache.store_measurement(constraints, Rc::clone(&measured));
2749                        state.set_measured(Some(measured));
2750                    }
2751                    Err(err) => {
2752                        state.record_error(err);
2753                        state.set_measured(None);
2754                        measured_size = Size {
2755                            width: 0.0,
2756                            height: 0.0,
2757                        };
2758                    }
2759                }
2760            }
2761        } else {
2762            match state.perform_measure(constraints) {
2763                Ok(measured) => {
2764                    state.force_remeasure.set(false);
2765                    measured_size = measured.size;
2766                    cache.store_measurement(constraints, Rc::clone(&measured));
2767                    state.set_measured(Some(measured));
2768                }
2769                Err(err) => {
2770                    state.record_error(err);
2771                    state.set_measured(None);
2772                    measured_size = Size {
2773                        width: 0.0,
2774                        height: 0.0,
2775                    };
2776                }
2777            }
2778        }
2779
2780        if let Some(layout_state) = state.layout_state() {
2781            let mut layout_state = layout_state.borrow_mut();
2782            layout_state.size = measured_size;
2783            layout_state.measurement_constraints = constraints;
2784        } else if let Some(applier) = state.applier() {
2785            let Ok(mut applier) = applier.try_borrow_typed() else {
2786                return Placeable::value(
2787                    measured_size.width,
2788                    measured_size.height,
2789                    state.node_id(),
2790                );
2791            };
2792            let _ = applier.with_node::<LayoutNode, _>(state.node_id(), |node| {
2793                node.set_measured_size(measured_size);
2794                node.set_measurement_constraints(constraints);
2795            });
2796        }
2797
2798        let state = Rc::clone(&self.state);
2799        let applier = state.applier();
2800        let node_id = state.node_id();
2801        let layout_state = state.layout_state();
2802
2803        let place_fn = Rc::new(move |x: f32, y: f32| {
2804            let internal_offset = state
2805                .measured
2806                .borrow()
2807                .as_ref()
2808                .map(|m| m.offset)
2809                .unwrap_or_default();
2810
2811            let position = Point {
2812                x: x + internal_offset.x,
2813                y: y + internal_offset.y,
2814            };
2815            state.set_last_position(position);
2816
2817            if let Some(layout_state) = &layout_state {
2818                let mut layout_state = layout_state.borrow_mut();
2819                layout_state.position = position;
2820                layout_state.is_placed = true;
2821            } else if let Some(applier) = &applier {
2822                let Ok(mut applier) = applier.try_borrow_typed() else {
2823                    return;
2824                };
2825                if applier
2826                    .with_node::<LayoutNode, _>(node_id, |node| {
2827                        node.set_position(position);
2828                    })
2829                    .is_err()
2830                {
2831                    let _ = applier.with_node::<SubcomposeLayoutNode, _>(node_id, |node| {
2832                        node.set_position(position);
2833                    });
2834                }
2835            }
2836        });
2837
2838        Placeable::with_place_fn(measured_size.width, measured_size.height, node_id, place_fn)
2839    }
2840
2841    fn min_intrinsic_width(&self, height: f32) -> f32 {
2842        let kind = IntrinsicKind::MinWidth(height);
2843        let cache = self.state.cache();
2844        cache.activate(self.state.cache_epoch.get());
2845        if !self.state.force_remeasure.get() {
2846            if let Some(value) = cache.get_intrinsic(&kind) {
2847                return value;
2848            }
2849        }
2850        let constraints = Constraints {
2851            min_width: 0.0,
2852            max_width: f32::INFINITY,
2853            min_height: height,
2854            max_height: height,
2855        };
2856        if let Some(node) = self.state.intrinsic_measure(constraints) {
2857            let value = node.size.width;
2858            cache.store_intrinsic(kind, value);
2859            value
2860        } else {
2861            0.0
2862        }
2863    }
2864
2865    fn max_intrinsic_width(&self, height: f32) -> f32 {
2866        let kind = IntrinsicKind::MaxWidth(height);
2867        let cache = self.state.cache();
2868        cache.activate(self.state.cache_epoch.get());
2869        if !self.state.force_remeasure.get() {
2870            if let Some(value) = cache.get_intrinsic(&kind) {
2871                return value;
2872            }
2873        }
2874        let constraints = Constraints {
2875            min_width: 0.0,
2876            max_width: f32::INFINITY,
2877            min_height: 0.0,
2878            max_height: height,
2879        };
2880        if let Some(node) = self.state.intrinsic_measure(constraints) {
2881            let value = node.size.width;
2882            cache.store_intrinsic(kind, value);
2883            value
2884        } else {
2885            0.0
2886        }
2887    }
2888
2889    fn min_intrinsic_height(&self, width: f32) -> f32 {
2890        let kind = IntrinsicKind::MinHeight(width);
2891        let cache = self.state.cache();
2892        cache.activate(self.state.cache_epoch.get());
2893        if !self.state.force_remeasure.get() {
2894            if let Some(value) = cache.get_intrinsic(&kind) {
2895                return value;
2896            }
2897        }
2898        let constraints = Constraints {
2899            min_width: width,
2900            max_width: width,
2901            min_height: 0.0,
2902            max_height: f32::INFINITY,
2903        };
2904        if let Some(node) = self.state.intrinsic_measure(constraints) {
2905            let value = node.size.height;
2906            cache.store_intrinsic(kind, value);
2907            value
2908        } else {
2909            0.0
2910        }
2911    }
2912
2913    fn max_intrinsic_height(&self, width: f32) -> f32 {
2914        let kind = IntrinsicKind::MaxHeight(width);
2915        let cache = self.state.cache();
2916        cache.activate(self.state.cache_epoch.get());
2917        if !self.state.force_remeasure.get() {
2918            if let Some(value) = cache.get_intrinsic(&kind) {
2919                return value;
2920            }
2921        }
2922        let constraints = Constraints {
2923            min_width: 0.0,
2924            max_width: width,
2925            min_height: 0.0,
2926            max_height: f32::INFINITY,
2927        };
2928        if let Some(node) = self.state.intrinsic_measure(constraints) {
2929            let value = node.size.height;
2930            cache.store_intrinsic(kind, value);
2931            value
2932        } else {
2933            0.0
2934        }
2935    }
2936
2937    fn flex_parent_data(&self) -> Option<cranpose_ui_layout::FlexParentData> {
2938        let applier = self.state.applier()?;
2939        let node_id = self.state.node_id();
2940        let Ok(mut applier) = applier.try_borrow_typed() else {
2941            return None;
2942        };
2943
2944        applier
2945            .with_node::<LayoutNode, _>(node_id, |layout_node| {
2946                let props = layout_node.resolved_modifiers().layout_properties();
2947                props.weight().map(|weight_data| {
2948                    cranpose_ui_layout::FlexParentData::new(weight_data.weight, weight_data.fill)
2949                })
2950            })
2951            .ok()
2952            .flatten()
2953    }
2954}
2955
2956fn measure_node_with_host(
2957    applier: Rc<ConcreteApplierHost<MemoryApplier>>,
2958    runtime_handle: Option<RuntimeHandle>,
2959    node_id: NodeId,
2960    constraints: Constraints,
2961    epoch: u64,
2962) -> Result<Rc<MeasuredNode>, NodeError> {
2963    let runtime_handle = match runtime_handle {
2964        Some(handle) => Some(handle),
2965        None => applier.borrow_typed().runtime_handle(),
2966    };
2967    let mut builder = LayoutBuilder::new_with_epoch(
2968        applier,
2969        epoch,
2970        Rc::new(RefCell::new(SlotTable::default())),
2971        FrameLayoutArena::default(),
2972    );
2973    builder.set_runtime_handle(runtime_handle);
2974    builder.measure_node(node_id, constraints)
2975}
2976
2977#[derive(Clone)]
2978struct RuntimeNodeMetadata {
2979    modifier: Modifier,
2980    resolved_modifiers: ResolvedModifiers,
2981    modifier_slices: Rc<ModifierNodeSlices>,
2982    role: SemanticsRole,
2983    button_handler: Option<Rc<RefCell<dyn FnMut()>>>,
2984}
2985
2986impl Default for RuntimeNodeMetadata {
2987    fn default() -> Self {
2988        Self {
2989            modifier: Modifier::empty(),
2990            resolved_modifiers: ResolvedModifiers::default(),
2991            modifier_slices: Rc::default(),
2992            role: SemanticsRole::Unknown,
2993            button_handler: None,
2994        }
2995    }
2996}
2997
2998fn role_from_modifier_slices(modifier_slices: &ModifierNodeSlices) -> SemanticsRole {
2999    modifier_slices
3000        .text_content()
3001        .map(|text| SemanticsRole::Text {
3002            value: text.to_string(),
3003        })
3004        .unwrap_or(SemanticsRole::Layout)
3005}
3006
3007fn runtime_metadata_for(
3008    applier: &mut MemoryApplier,
3009    node_id: NodeId,
3010) -> Result<RuntimeNodeMetadata, NodeError> {
3011    // Try LayoutNode (the primary modern path)
3012    // IMPORTANT: We use with_node (reference) instead of try_clone because cloning
3013    // LayoutNode creates a NEW ModifierChainHandle with NEW nodes and NEW handlers,
3014    // which would lose gesture state like press_position.
3015    if let Ok(meta) = applier.with_node::<LayoutNode, _>(node_id, |layout| {
3016        let modifier = layout.modifier.clone();
3017        let resolved_modifiers = layout.resolved_modifiers();
3018        let modifier_slices = layout.modifier_slices_snapshot();
3019        let role = role_from_modifier_slices(&modifier_slices);
3020
3021        RuntimeNodeMetadata {
3022            modifier,
3023            resolved_modifiers,
3024            modifier_slices,
3025            role,
3026            button_handler: None,
3027        }
3028    }) {
3029        return Ok(meta);
3030    }
3031
3032    // Try SubcomposeLayoutNode
3033    if let Ok((modifier, resolved_modifiers, modifier_slices)) = applier
3034        .with_node::<SubcomposeLayoutNode, _>(node_id, |node| {
3035            (
3036                node.modifier(),
3037                node.resolved_modifiers(),
3038                node.modifier_slices_snapshot(),
3039            )
3040        })
3041    {
3042        return Ok(RuntimeNodeMetadata {
3043            modifier,
3044            resolved_modifiers,
3045            modifier_slices,
3046            role: SemanticsRole::Subcompose,
3047            button_handler: None,
3048        });
3049    }
3050    Ok(RuntimeNodeMetadata::default())
3051}
3052
3053fn clear_semantics_dirty_flags(
3054    applier: &mut MemoryApplier,
3055    node: &MeasuredNode,
3056) -> Result<(), NodeError> {
3057    match applier.with_node::<LayoutNode, _>(node.node_id, |layout| {
3058        layout.clear_needs_semantics();
3059    }) {
3060        Ok(()) => {}
3061        Err(NodeError::Missing { .. }) => {}
3062        Err(NodeError::TypeMismatch { .. }) => {
3063            match applier.with_node::<SubcomposeLayoutNode, _>(node.node_id, |subcompose| {
3064                subcompose.clear_needs_semantics();
3065            }) {
3066                Ok(()) | Err(NodeError::Missing { .. }) | Err(NodeError::TypeMismatch { .. }) => {}
3067                Err(err) => return Err(err),
3068            }
3069        }
3070        Err(err) => return Err(err),
3071    }
3072
3073    for child in &node.children {
3074        clear_semantics_dirty_flags(applier, &child.node)?;
3075    }
3076
3077    Ok(())
3078}
3079
3080fn build_semantics_tree_from_live_nodes(
3081    applier: &mut MemoryApplier,
3082    node: &MeasuredNode,
3083) -> Result<SemanticsTree, NodeError> {
3084    Ok(SemanticsTree::new(build_semantics_node_from_live_nodes(
3085        applier, node,
3086    )?))
3087}
3088
3089fn semantics_node_from_parts(
3090    node_id: NodeId,
3091    mut role: SemanticsRole,
3092    config: Option<SemanticsConfiguration>,
3093    children: Vec<SemanticsNode>,
3094) -> SemanticsNode {
3095    let mut actions = Vec::new();
3096    let mut description = None;
3097    let mut editable_text = false;
3098    let mut text_selection = None;
3099
3100    if let Some(config) = config {
3101        if config.is_button {
3102            role = SemanticsRole::Button;
3103        }
3104        if config.is_clickable {
3105            actions.push(SemanticsAction::Click {
3106                handler: SemanticsCallback::new(node_id),
3107            });
3108        }
3109        description = config.content_description;
3110        editable_text = config.is_editable_text;
3111        text_selection = config.text_selection;
3112    }
3113
3114    SemanticsNode::new(
3115        node_id,
3116        role,
3117        actions,
3118        children,
3119        description,
3120        editable_text,
3121        text_selection,
3122    )
3123}
3124
3125fn build_semantics_node_from_live_nodes(
3126    applier: &mut MemoryApplier,
3127    node: &MeasuredNode,
3128) -> Result<SemanticsNode, NodeError> {
3129    let (role, config) = match applier.with_node::<LayoutNode, _>(node.node_id, |layout| {
3130        let role = role_from_modifier_slices(&layout.modifier_slices_snapshot());
3131        let config = layout.semantics_configuration();
3132        layout.clear_needs_semantics();
3133        (role, config)
3134    }) {
3135        Ok(data) => data,
3136        Err(NodeError::TypeMismatch { .. }) | Err(NodeError::Missing { .. }) => {
3137            match applier.with_node::<SubcomposeLayoutNode, _>(node.node_id, |subcompose| {
3138                subcompose.clear_needs_semantics();
3139                (
3140                    SemanticsRole::Subcompose,
3141                    collect_semantics_from_modifier(&subcompose.modifier()),
3142                )
3143            }) {
3144                Ok(data) => data,
3145                Err(NodeError::TypeMismatch { .. }) | Err(NodeError::Missing { .. }) => {
3146                    (SemanticsRole::Unknown, None)
3147                }
3148                Err(err) => return Err(err),
3149            }
3150        }
3151        Err(err) => return Err(err),
3152    };
3153
3154    let mut children = Vec::with_capacity(node.children.len());
3155    for child in &node.children {
3156        children.push(build_semantics_node_from_live_nodes(applier, &child.node)?);
3157    }
3158
3159    Ok(semantics_node_from_parts(
3160        node.node_id,
3161        role,
3162        config,
3163        children,
3164    ))
3165}
3166
3167fn record_semantics_allocation_stats(node: &SemanticsNode, stats: &mut LayoutAllocationDebugStats) {
3168    stats.semantics_node_count += 1;
3169    stats.semantics_action_count += node.actions.len();
3170    stats.semantics_action_capacity += node.actions.capacity();
3171    stats.semantics_child_count += node.children.len();
3172    stats.semantics_child_capacity += node.children.capacity();
3173    stats.semantics_heap_bytes += node.actions.capacity() * size_of::<SemanticsAction>();
3174    stats.semantics_heap_bytes += node.children.capacity() * size_of::<SemanticsNode>();
3175
3176    if let Some(description) = &node.description {
3177        stats.semantics_description_count += 1;
3178        stats.semantics_description_bytes += description.capacity();
3179        stats.semantics_heap_bytes += description.capacity();
3180    }
3181    if let SemanticsRole::Text { value } = &node.role {
3182        stats.semantics_text_role_bytes += value.capacity();
3183        stats.semantics_heap_bytes += value.capacity();
3184    }
3185
3186    for child in &node.children {
3187        record_semantics_allocation_stats(child, stats);
3188    }
3189}
3190
3191fn record_layout_box_allocation_stats(
3192    layout_box: &LayoutBox,
3193    stats: &mut LayoutAllocationDebugStats,
3194) {
3195    stats.layout_box_count += 1;
3196    stats.layout_box_child_count += layout_box.children.len();
3197    stats.layout_box_child_capacity += layout_box.children.capacity();
3198    stats.layout_box_heap_bytes += layout_box.children.capacity() * size_of::<LayoutBox>();
3199    stats.add_modifier_slice(layout_box.node_data.modifier_slices().debug_stats());
3200
3201    for child in &layout_box.children {
3202        record_layout_box_allocation_stats(child, stats);
3203    }
3204}
3205
3206fn build_layout_tree(
3207    applier: &mut MemoryApplier,
3208    node: &MeasuredNode,
3209) -> Result<LayoutTree, NodeError> {
3210    fn place(
3211        applier: &mut MemoryApplier,
3212        node: &MeasuredNode,
3213        origin: Point,
3214    ) -> Result<LayoutBox, NodeError> {
3215        // Include the node's own offset (from OffsetNode) in its position
3216        let top_left = Point {
3217            x: origin.x + node.offset.x,
3218            y: origin.y + node.offset.y,
3219        };
3220        let rect = GeometryRect {
3221            x: top_left.x,
3222            y: top_left.y,
3223            width: node.size.width,
3224            height: node.size.height,
3225        };
3226        let info = runtime_metadata_for(applier, node.node_id)?;
3227        let kind = layout_kind_from_metadata(node.node_id, &info);
3228        let RuntimeNodeMetadata {
3229            modifier,
3230            resolved_modifiers,
3231            modifier_slices,
3232            ..
3233        } = info;
3234        let data = LayoutNodeData::new(modifier, resolved_modifiers, modifier_slices, kind);
3235        let mut children = Vec::with_capacity(node.children.len());
3236        for child in &node.children {
3237            let child_origin = Point {
3238                x: top_left.x + child.offset.x,
3239                y: top_left.y + child.offset.y,
3240            };
3241            children.push(place(applier, &child.node, child_origin)?);
3242        }
3243        Ok(LayoutBox::new(
3244            node.node_id,
3245            rect,
3246            node.content_offset,
3247            data,
3248            children,
3249        ))
3250    }
3251
3252    Ok(LayoutTree::new(place(
3253        applier,
3254        node,
3255        Point { x: 0.0, y: 0.0 },
3256    )?))
3257}
3258
3259fn semantics_role_from_layout_box(layout_box: &LayoutBox) -> SemanticsRole {
3260    match &layout_box.node_data.kind {
3261        LayoutNodeKind::Subcompose => SemanticsRole::Subcompose,
3262        LayoutNodeKind::Spacer => SemanticsRole::Spacer,
3263        LayoutNodeKind::Unknown => SemanticsRole::Unknown,
3264        LayoutNodeKind::Button { .. } => SemanticsRole::Button,
3265        LayoutNodeKind::Layout => layout_box
3266            .node_data
3267            .modifier_slices()
3268            .text_content()
3269            .map(|text| SemanticsRole::Text {
3270                value: text.to_string(),
3271            })
3272            .unwrap_or(SemanticsRole::Layout),
3273    }
3274}
3275
3276fn build_semantics_node_from_layout_box(layout_box: &LayoutBox) -> SemanticsNode {
3277    let children = layout_box
3278        .children
3279        .iter()
3280        .map(build_semantics_node_from_layout_box)
3281        .collect();
3282
3283    semantics_node_from_parts(
3284        layout_box.node_id,
3285        semantics_role_from_layout_box(layout_box),
3286        collect_semantics_from_modifier(&layout_box.node_data.modifier),
3287        children,
3288    )
3289}
3290
3291fn layout_kind_from_metadata(_node_id: NodeId, info: &RuntimeNodeMetadata) -> LayoutNodeKind {
3292    match &info.role {
3293        SemanticsRole::Layout => LayoutNodeKind::Layout,
3294        SemanticsRole::Subcompose => LayoutNodeKind::Subcompose,
3295        SemanticsRole::Text { .. } => {
3296            // Text content is now handled via TextModifierNode in the modifier chain
3297            // and collected in modifier_slices.text_content(). LayoutNodeKind should
3298            // reflect the layout policy (EmptyMeasurePolicy), not the content type.
3299            LayoutNodeKind::Layout
3300        }
3301        SemanticsRole::Spacer => LayoutNodeKind::Spacer,
3302        SemanticsRole::Button => {
3303            let handler = info
3304                .button_handler
3305                .as_ref()
3306                .cloned()
3307                .unwrap_or_else(|| Rc::new(RefCell::new(|| {})));
3308            LayoutNodeKind::Button { on_click: handler }
3309        }
3310        SemanticsRole::Unknown => LayoutNodeKind::Unknown,
3311    }
3312}
3313
3314fn subtract_padding(constraints: Constraints, padding: EdgeInsets) -> Constraints {
3315    let horizontal = padding.horizontal_sum();
3316    let vertical = padding.vertical_sum();
3317    let min_width = (constraints.min_width - horizontal).max(0.0);
3318    let mut max_width = constraints.max_width;
3319    if max_width.is_finite() {
3320        max_width = (max_width - horizontal).max(0.0);
3321    }
3322    let min_height = (constraints.min_height - vertical).max(0.0);
3323    let mut max_height = constraints.max_height;
3324    if max_height.is_finite() {
3325        max_height = (max_height - vertical).max(0.0);
3326    }
3327    normalize_constraints(Constraints {
3328        min_width,
3329        max_width,
3330        min_height,
3331        max_height,
3332    })
3333}
3334
3335#[cfg(test)]
3336pub(crate) fn align_horizontal(alignment: HorizontalAlignment, available: f32, child: f32) -> f32 {
3337    match alignment {
3338        HorizontalAlignment::Start => 0.0,
3339        HorizontalAlignment::CenterHorizontally => ((available - child) / 2.0).max(0.0),
3340        HorizontalAlignment::End => (available - child).max(0.0),
3341    }
3342}
3343
3344#[cfg(test)]
3345pub(crate) fn align_vertical(alignment: VerticalAlignment, available: f32, child: f32) -> f32 {
3346    match alignment {
3347        VerticalAlignment::Top => 0.0,
3348        VerticalAlignment::CenterVertically => ((available - child) / 2.0).max(0.0),
3349        VerticalAlignment::Bottom => (available - child).max(0.0),
3350    }
3351}
3352
3353fn resolve_dimension(
3354    base: f32,
3355    explicit: DimensionConstraint,
3356    min_override: Option<f32>,
3357    max_override: Option<f32>,
3358    min_limit: f32,
3359    max_limit: f32,
3360) -> f32 {
3361    let mut min_bound = min_limit;
3362    if let Some(min_value) = min_override {
3363        min_bound = min_bound.max(min_value);
3364    }
3365
3366    let mut max_bound = if max_limit.is_finite() {
3367        max_limit
3368    } else {
3369        max_override.unwrap_or(max_limit)
3370    };
3371    if let Some(max_value) = max_override {
3372        if max_bound.is_finite() {
3373            max_bound = max_bound.min(max_value);
3374        } else {
3375            max_bound = max_value;
3376        }
3377    }
3378    if max_bound < min_bound {
3379        max_bound = min_bound;
3380    }
3381
3382    let mut size = match explicit {
3383        DimensionConstraint::Points(points) => points,
3384        DimensionConstraint::Fraction(fraction) => {
3385            if max_limit.is_finite() {
3386                max_limit * fraction.clamp(0.0, 1.0)
3387            } else {
3388                base
3389            }
3390        }
3391        DimensionConstraint::Unspecified => base,
3392        // Intrinsic sizing is resolved at a higher level where we have access to children.
3393        // At this point we just use the base size as a fallback.
3394        DimensionConstraint::Intrinsic(_) => base,
3395    };
3396
3397    size = clamp_dimension(size, min_bound, max_bound);
3398    size = clamp_dimension(size, min_limit, max_limit);
3399    size.max(0.0)
3400}
3401
3402fn clamp_dimension(value: f32, min: f32, max: f32) -> f32 {
3403    let mut result = value.max(min);
3404    if max.is_finite() {
3405        result = result.min(max);
3406    }
3407    result
3408}
3409
3410fn normalize_constraints(mut constraints: Constraints) -> Constraints {
3411    if constraints.max_width < constraints.min_width {
3412        constraints.max_width = constraints.min_width;
3413    }
3414    if constraints.max_height < constraints.min_height {
3415        constraints.max_height = constraints.min_height;
3416    }
3417    constraints
3418}
3419
3420#[cfg(test)]
3421#[path = "tests/layout_tests.rs"]
3422mod tests;