Skip to main content

cranpose_ui/layout/
mod.rs

1// WIP: Layout system infrastructure - many helper types not yet fully wired up
2
3pub mod coordinator;
4pub mod core;
5pub mod policies;
6
7use cranpose_core::collections::map::Entry;
8use cranpose_core::collections::map::HashMap;
9use std::{
10    cell::RefCell,
11    fmt,
12    rc::Rc,
13    sync::atomic::{AtomicU64, Ordering},
14};
15
16use cranpose_core::{
17    Applier, ApplierHost, Composer, ConcreteApplierHost, MemoryApplier, NodeError, NodeId, Phase,
18    RuntimeHandle, SlotBackend, SlotsHost, SnapshotStateObserver,
19};
20
21use self::coordinator::NodeCoordinator;
22use self::core::Measurable;
23use self::core::Placeable;
24#[cfg(test)]
25use self::core::{HorizontalAlignment, VerticalAlignment};
26use crate::modifier::{
27    collect_semantics_from_modifier, collect_slices_from_modifier, DimensionConstraint, EdgeInsets,
28    Modifier, ModifierNodeSlices, Point, Rect as GeometryRect, ResolvedModifiers, Size,
29};
30
31use crate::subcompose_layout::SubcomposeLayoutNode;
32use crate::widgets::nodes::{IntrinsicKind, LayoutNode, LayoutNodeCacheHandles};
33use cranpose_foundation::InvalidationKind;
34use cranpose_foundation::ModifierNodeContext;
35use cranpose_foundation::{NodeCapabilities, SemanticsConfiguration};
36use cranpose_ui_layout::{Constraints, MeasurePolicy, MeasureResult};
37
38/// Runtime context for modifier nodes during measurement.
39///
40/// Unlike `BasicModifierNodeContext`, this context accumulates invalidations
41/// that can be processed after measurement to set dirty flags on the LayoutNode.
42#[derive(Default)]
43pub(crate) struct LayoutNodeContext {
44    invalidations: Vec<InvalidationKind>,
45    update_requested: bool,
46    active_capabilities: Vec<NodeCapabilities>,
47}
48
49impl LayoutNodeContext {
50    pub(crate) fn new() -> Self {
51        Self::default()
52    }
53
54    pub(crate) fn take_invalidations(&mut self) -> Vec<InvalidationKind> {
55        std::mem::take(&mut self.invalidations)
56    }
57}
58
59impl ModifierNodeContext for LayoutNodeContext {
60    fn invalidate(&mut self, kind: InvalidationKind) {
61        if !self.invalidations.contains(&kind) {
62            self.invalidations.push(kind);
63        }
64    }
65
66    fn request_update(&mut self) {
67        self.update_requested = true;
68    }
69
70    fn push_active_capabilities(&mut self, capabilities: NodeCapabilities) {
71        self.active_capabilities.push(capabilities);
72    }
73
74    fn pop_active_capabilities(&mut self) {
75        self.active_capabilities.pop();
76    }
77}
78
79static NEXT_CACHE_EPOCH: AtomicU64 = AtomicU64::new(1);
80
81/// Forces all layout caches to be invalidated on the next measure by incrementing the epoch.
82///
83/// # ⚠️ Internal Use Only - NOT Public API
84///
85/// **This function is hidden from public documentation and MUST NOT be called by external code.**
86///
87/// Only `cranpose-app-shell` may call this for rare global events:
88/// - Window/viewport resize
89/// - Global font scale or density changes
90/// - Debug toggles that affect all layout
91///
92/// **This is O(entire app size) - extremely expensive!**
93///
94/// # For Local Changes
95///
96/// **Do NOT use this for scroll, single-node mutations, or any local layout change.**
97/// Instead, use the scoped repass mechanism:
98/// ```text
99/// cranpose_ui::schedule_layout_repass(node_id);
100/// ```
101///
102/// The scoped path bubbles dirty flags without invalidating all caches, giving you O(subtree) instead of O(app).
103#[doc(hidden)]
104pub fn invalidate_all_layout_caches() {
105    NEXT_CACHE_EPOCH.fetch_add(1, Ordering::Relaxed);
106}
107
108/// RAII guard that:
109/// - moves the current MemoryApplier into a ConcreteApplierHost
110/// - holds a shared handle to the SlotBackend used by LayoutBuilder
111/// - on Drop, always:
112///   * restores slots into the host from the shared handle
113///   * moves the original MemoryApplier back into the Composition
114///
115/// This makes `measure_layout` panic/Err-safe wrt both the applier and slots.
116/// The key invariant: guard and builder share the same `Rc<RefCell<SlotBackend>>`,
117/// so the guard never loses access to the authoritative slots even on panic.
118struct ApplierSlotGuard<'a> {
119    /// The `MemoryApplier` inside the Composition::applier that we must restore into.
120    target: &'a mut MemoryApplier,
121    /// Host that owns the original MemoryApplier while layout is running.
122    host: Rc<ConcreteApplierHost<MemoryApplier>>,
123    /// Shared handle to the slot table. Both the guard and the builder hold a clone.
124    /// On Drop, we write whatever is in this handle back into the applier.
125    slots: Rc<RefCell<SlotBackend>>,
126}
127
128impl<'a> ApplierSlotGuard<'a> {
129    /// Creates a new guard:
130    /// - moves the current MemoryApplier out of `target` into a host
131    /// - takes the current slots out of the host and wraps them in a shared handle
132    fn new(target: &'a mut MemoryApplier) -> Self {
133        // Move the original applier into a host; leave `target` with a fresh one
134        let original_applier = std::mem::replace(target, MemoryApplier::new());
135        let host = Rc::new(ConcreteApplierHost::new(original_applier));
136
137        // Take slots from the host into a shared handle
138        let slots = {
139            let mut applier_ref = host.borrow_typed();
140            std::mem::take(applier_ref.slots())
141        };
142        let slots = Rc::new(RefCell::new(slots));
143
144        Self {
145            target,
146            host,
147            slots,
148        }
149    }
150
151    /// Rc to pass into LayoutBuilder::new_with_epoch
152    fn host(&self) -> Rc<ConcreteApplierHost<MemoryApplier>> {
153        Rc::clone(&self.host)
154    }
155
156    /// Returns the shared handle to slots for the builder to use.
157    /// The builder clones this Rc, so both guard and builder share the same slots.
158    fn slots_handle(&self) -> Rc<RefCell<SlotBackend>> {
159        Rc::clone(&self.slots)
160    }
161}
162
163impl Drop for ApplierSlotGuard<'_> {
164    fn drop(&mut self) {
165        // 1) Restore slots into the host's MemoryApplier from the shared handle.
166        // This works correctly whether we're on the success path or panic/error path,
167        // because we always have the shared handle.
168        {
169            let mut applier_ref = self.host.borrow_typed();
170            *applier_ref.slots() = std::mem::take(&mut *self.slots.borrow_mut());
171        }
172
173        // 2) Move the original MemoryApplier (with restored/updated slots) back into `target`
174        {
175            let mut applier_ref = self.host.borrow_typed();
176            let original_applier = std::mem::take(&mut *applier_ref);
177            let _ = std::mem::replace(self.target, original_applier);
178        }
179        // No Rc::try_unwrap in Drop → no "panic during panic" risk.
180    }
181}
182
183/// Result of measuring through the modifier node chain.
184struct ModifierChainMeasurement {
185    result: MeasureResult,
186    /// Content offset for scroll/inner transforms - NOT padding semantics
187    content_offset: Point,
188    /// Node's own offset (from OffsetNode, affects position in parent)
189    offset: Point,
190}
191
192/// Discrete event callback reference produced during semantics extraction.
193#[derive(Clone, Debug, PartialEq, Eq)]
194pub struct SemanticsCallback {
195    node_id: NodeId,
196}
197
198impl SemanticsCallback {
199    pub fn new(node_id: NodeId) -> Self {
200        Self { node_id }
201    }
202
203    pub fn node_id(&self) -> NodeId {
204        self.node_id
205    }
206}
207
208/// Semantics action exposed to the input system.
209#[derive(Clone, Debug, PartialEq, Eq)]
210pub enum SemanticsAction {
211    Click { handler: SemanticsCallback },
212}
213
214/// Semantic role describing how a node should participate in accessibility and hit testing.
215/// Roles are now derived from SemanticsConfiguration rather than widget types.
216#[derive(Clone, Debug, PartialEq, Eq)]
217pub enum SemanticsRole {
218    /// Generic container or layout node
219    Layout,
220    /// Subcomposition boundary
221    Subcompose,
222    /// Text content (derived from TextNode for backward compatibility)
223    Text { value: String },
224    /// Spacer (non-interactive)
225    Spacer,
226    /// Button (derived from is_button semantics flag)
227    Button,
228    /// Unknown or unspecified role
229    Unknown,
230}
231
232/// A single node within the semantics tree.
233#[derive(Clone, Debug)]
234pub struct SemanticsNode {
235    pub node_id: NodeId,
236    pub role: SemanticsRole,
237    pub actions: Vec<SemanticsAction>,
238    pub children: Vec<SemanticsNode>,
239    pub description: Option<String>,
240}
241
242impl SemanticsNode {
243    fn new(
244        node_id: NodeId,
245        role: SemanticsRole,
246        actions: Vec<SemanticsAction>,
247        children: Vec<SemanticsNode>,
248        description: Option<String>,
249    ) -> Self {
250        Self {
251            node_id,
252            role,
253            actions,
254            children,
255            description,
256        }
257    }
258}
259
260/// Rooted semantics tree extracted after layout.
261#[derive(Clone, Debug)]
262pub struct SemanticsTree {
263    root: SemanticsNode,
264}
265
266impl SemanticsTree {
267    fn new(root: SemanticsNode) -> Self {
268        Self { root }
269    }
270
271    pub fn root(&self) -> &SemanticsNode {
272        &self.root
273    }
274}
275
276/// Caches semantics configurations for layout nodes, similar to Jetpack Compose's SemanticsOwner.
277/// This enables lazy semantics tree construction and efficient invalidation.
278#[derive(Default)]
279pub struct SemanticsOwner {
280    configurations: RefCell<HashMap<NodeId, Option<SemanticsConfiguration>>>,
281}
282
283impl SemanticsOwner {
284    pub fn new() -> Self {
285        Self {
286            configurations: RefCell::new(HashMap::default()),
287        }
288    }
289
290    /// Returns the cached configuration for the given node, computing it if necessary.
291    pub fn get_or_compute(
292        &self,
293        node_id: NodeId,
294        applier: &mut MemoryApplier,
295    ) -> Option<SemanticsConfiguration> {
296        // Check cache first
297        if let Some(cached) = self.configurations.borrow().get(&node_id) {
298            return cached.clone();
299        }
300
301        // Compute and cache
302        let config = compute_semantics_for_node(applier, node_id);
303        self.configurations
304            .borrow_mut()
305            .insert(node_id, config.clone());
306        config
307    }
308}
309
310/// Result of running layout for a Compose tree.
311#[derive(Debug, Clone)]
312pub struct LayoutTree {
313    root: LayoutBox,
314}
315
316impl LayoutTree {
317    pub fn new(root: LayoutBox) -> Self {
318        Self { root }
319    }
320
321    pub fn root(&self) -> &LayoutBox {
322        &self.root
323    }
324
325    pub fn root_mut(&mut self) -> &mut LayoutBox {
326        &mut self.root
327    }
328
329    pub fn into_root(self) -> LayoutBox {
330        self.root
331    }
332}
333
334/// Layout information for a single node.
335#[derive(Debug, Clone)]
336pub struct LayoutBox {
337    pub node_id: NodeId,
338    pub rect: GeometryRect,
339    /// Content offset for scroll/inner transforms (applies to children, NOT this node's position)
340    pub content_offset: Point,
341    pub node_data: LayoutNodeData,
342    pub children: Vec<LayoutBox>,
343}
344
345impl LayoutBox {
346    pub fn new(
347        node_id: NodeId,
348        rect: GeometryRect,
349        content_offset: Point,
350        node_data: LayoutNodeData,
351        children: Vec<LayoutBox>,
352    ) -> Self {
353        Self {
354            node_id,
355            rect,
356            content_offset,
357            node_data,
358            children,
359        }
360    }
361}
362
363/// Snapshot of the data required to render a layout node.
364#[derive(Debug, Clone)]
365pub struct LayoutNodeData {
366    pub modifier: Modifier,
367    pub resolved_modifiers: ResolvedModifiers,
368    pub modifier_slices: Rc<ModifierNodeSlices>,
369    pub kind: LayoutNodeKind,
370}
371
372impl LayoutNodeData {
373    pub fn new(
374        modifier: Modifier,
375        resolved_modifiers: ResolvedModifiers,
376        modifier_slices: Rc<ModifierNodeSlices>,
377        kind: LayoutNodeKind,
378    ) -> Self {
379        Self {
380            modifier,
381            resolved_modifiers,
382            modifier_slices,
383            kind,
384        }
385    }
386
387    pub fn resolved_modifiers(&self) -> ResolvedModifiers {
388        self.resolved_modifiers
389    }
390
391    pub fn modifier_slices(&self) -> &ModifierNodeSlices {
392        &self.modifier_slices
393    }
394}
395
396/// Classification of the node captured inside a [`LayoutBox`].
397///
398/// Note: Text content is no longer represented as a distinct LayoutNodeKind.
399/// Text nodes now use `LayoutNodeKind::Layout` with their content stored in
400/// `modifier_slices.text_content()` via TextModifierNode, following Jetpack
401/// Compose's pattern where text is a modifier node capability.
402#[derive(Clone)]
403pub enum LayoutNodeKind {
404    Layout,
405    Subcompose,
406    Spacer,
407    Button { on_click: Rc<RefCell<dyn FnMut()>> },
408    Unknown,
409}
410
411impl fmt::Debug for LayoutNodeKind {
412    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
413        match self {
414            LayoutNodeKind::Layout => f.write_str("Layout"),
415            LayoutNodeKind::Subcompose => f.write_str("Subcompose"),
416            LayoutNodeKind::Spacer => f.write_str("Spacer"),
417            LayoutNodeKind::Button { .. } => f.write_str("Button"),
418            LayoutNodeKind::Unknown => f.write_str("Unknown"),
419        }
420    }
421}
422
423/// Extension trait that equips `MemoryApplier` with layout computation.
424pub trait LayoutEngine {
425    fn compute_layout(&mut self, root: NodeId, max_size: Size) -> Result<LayoutTree, NodeError>;
426}
427
428impl LayoutEngine for MemoryApplier {
429    fn compute_layout(&mut self, root: NodeId, max_size: Size) -> Result<LayoutTree, NodeError> {
430        let measurements = measure_layout(self, root, max_size)?;
431        Ok(measurements.into_layout_tree())
432    }
433}
434
435/// Result of running the measure pass for a Compose layout tree.
436#[derive(Debug, Clone)]
437pub struct LayoutMeasurements {
438    root: Rc<MeasuredNode>,
439    semantics: SemanticsTree,
440    layout_tree: LayoutTree,
441}
442
443impl LayoutMeasurements {
444    fn new(root: Rc<MeasuredNode>, semantics: SemanticsTree, layout_tree: LayoutTree) -> Self {
445        Self {
446            root,
447            semantics,
448            layout_tree,
449        }
450    }
451
452    /// Returns the measured size of the root node.
453    pub fn root_size(&self) -> Size {
454        self.root.size
455    }
456
457    pub fn semantics_tree(&self) -> &SemanticsTree {
458        &self.semantics
459    }
460
461    /// Consumes the measurements and produces a [`LayoutTree`].
462    pub fn into_layout_tree(self) -> LayoutTree {
463        self.layout_tree
464    }
465
466    /// Returns a borrowed [`LayoutTree`] for rendering.
467    pub fn layout_tree(&self) -> LayoutTree {
468        self.layout_tree.clone()
469    }
470}
471
472/// Check if a node or any of its descendants needs measure (selective measure optimization).
473/// This can be used by the app shell to skip layout when the tree is clean.
474///
475/// O(1) check - just looks at root's dirty flag.
476/// Works because all mutation paths bubble dirty flags to root via composer commands.
477///
478/// Returns Result to force caller to handle errors explicitly. No more unwrap_or(true) safety net.
479pub fn tree_needs_layout(applier: &mut dyn Applier, root: NodeId) -> Result<bool, NodeError> {
480    // Just check root - bubbling ensures it's dirty if any descendant is dirty
481    let node = applier.get_mut(root)?;
482    let layout_node =
483        node.as_any_mut()
484            .downcast_mut::<LayoutNode>()
485            .ok_or(NodeError::TypeMismatch {
486                id: root,
487                expected: std::any::type_name::<LayoutNode>(),
488            })?;
489    Ok(layout_node.needs_layout())
490}
491
492/// Test helper: bubbles layout dirty flag to root.
493#[cfg(test)]
494pub(crate) fn bubble_layout_dirty(applier: &mut MemoryApplier, node_id: NodeId) {
495    cranpose_core::bubble_layout_dirty(applier as &mut dyn Applier, node_id);
496}
497
498/// Runs the measure phase for the subtree rooted at `root`.
499pub fn measure_layout(
500    applier: &mut MemoryApplier,
501    root: NodeId,
502    max_size: Size,
503) -> Result<LayoutMeasurements, NodeError> {
504    let constraints = Constraints {
505        min_width: 0.0,
506        max_width: max_size.width,
507        min_height: 0.0,
508        max_height: max_size.height,
509    };
510
511    // Selective measure: only increment epoch if something needs MEASURING (not just layout)
512    // O(1) check - just look at root's dirty flag (bubbling ensures correctness)
513    //
514    // CRITICAL: We check needs_MEASURE, not needs_LAYOUT!
515    // - needs_measure: size may change, caches must be invalidated
516    // - needs_layout: position may change but size is cached (e.g., scroll)
517    //
518    // Scroll operations bubble needs_layout to ancestors, but NOT needs_measure.
519    // Using needs_layout here would wipe ALL caches on every scroll frame, causing
520    // O(N) full remeasurement instead of O(changed nodes).
521    let (needs_remeasure, _needs_semantics, cached_epoch) = match applier
522        .with_node::<LayoutNode, _>(root, |node| {
523            (
524                node.needs_measure(), // CORRECT: check needs_measure, not needs_layout
525                node.needs_semantics(),
526                node.cache_handles().epoch(),
527            )
528        }) {
529        Ok(tuple) => tuple,
530        Err(NodeError::TypeMismatch { .. }) => {
531            let node = applier.get_mut(root)?;
532            // For non-LayoutNode roots, check needs_layout as fallback
533            let measure_dirty = node.needs_layout();
534            let semantics_dirty = node.needs_semantics();
535            (measure_dirty, semantics_dirty, 0)
536        }
537        Err(err) => return Err(err),
538    };
539
540    let epoch = if needs_remeasure {
541        NEXT_CACHE_EPOCH.fetch_add(1, Ordering::Relaxed)
542    } else if cached_epoch != 0 {
543        cached_epoch
544    } else {
545        // Fallback when caller root isn't a LayoutNode (e.g. tests using Spacer directly).
546        NEXT_CACHE_EPOCH.load(Ordering::Relaxed)
547    };
548
549    // Move the current applier into a host and set up a guard that will
550    // ALWAYS restore:
551    // - the MemoryApplier back into `applier`
552    // - the SlotBackend back into that MemoryApplier
553    //
554    // IMPORTANT: Declare the guard *before* the builder so the builder
555    // is dropped first (both on Ok and on unwind).
556    let guard = ApplierSlotGuard::new(applier);
557    let applier_host = guard.host();
558    let slots_handle = guard.slots_handle();
559
560    // Give the builder the shared slots handle - both guard and builder
561    // now share access to the same SlotBackend via Rc<RefCell<_>>.
562    let mut builder =
563        LayoutBuilder::new_with_epoch(Rc::clone(&applier_host), epoch, Rc::clone(&slots_handle));
564
565    // ---- Measurement -------------------------------------------------------
566    // If measurement fails, the guard will restore slots from the shared handle
567    // on drop - this is safe because the handle always contains valid slots.
568
569    let measured = builder.measure_node(root, normalize_constraints(constraints))?;
570
571    // Root node has no parent to place it, so we must explicitly place it at (0,0).
572    // This ensures is_placed=true, allowing the renderer to traverse the tree.
573    // Handle both LayoutNode and SubcomposeLayoutNode as potential roots.
574    if let Ok(mut applier) = applier_host.try_borrow_typed() {
575        if applier
576            .with_node::<LayoutNode, _>(root, |node| {
577                node.set_position(Point::default());
578            })
579            .is_err()
580        {
581            let _ = applier.with_node::<SubcomposeLayoutNode, _>(root, |node| {
582                node.set_position(Point::default());
583            });
584        }
585    }
586
587    // ---- Metadata ----------------------------------------------------------
588    let metadata = {
589        let mut applier_ref = applier_host.borrow_typed();
590        collect_runtime_metadata(&mut applier_ref, &measured)?
591    };
592
593    // ---- Semantics snapshot ------------------------------------------------
594    let semantics_snapshot = {
595        let mut applier_ref = applier_host.borrow_typed();
596        collect_semantics_snapshot(&mut applier_ref, &measured)?
597    };
598
599    // Drop builder before guard - slots are already in the shared handle.
600    // Guard's Drop will write them back to the applier.
601    drop(builder);
602
603    // DO NOT manually unwrap `applier_host` or replace `applier` here.
604    // `ApplierSlotGuard::drop` will restore everything when this function returns.
605
606    // Build semantics and layout trees from `measured` + metadata + snapshot
607    let semantics_root = build_semantics_node(&measured, &metadata, &semantics_snapshot);
608    let semantics = SemanticsTree::new(semantics_root);
609    let layout_tree = build_layout_tree_from_metadata(&measured, &metadata);
610
611    Ok(LayoutMeasurements::new(measured, semantics, layout_tree))
612}
613
614struct LayoutBuilder {
615    state: Rc<RefCell<LayoutBuilderState>>,
616}
617
618impl LayoutBuilder {
619    fn new_with_epoch(
620        applier: Rc<ConcreteApplierHost<MemoryApplier>>,
621        epoch: u64,
622        slots: Rc<RefCell<SlotBackend>>,
623    ) -> Self {
624        Self {
625            state: Rc::new(RefCell::new(LayoutBuilderState::new_with_epoch(
626                applier, epoch, slots,
627            ))),
628        }
629    }
630
631    fn measure_node(
632        &mut self,
633        node_id: NodeId,
634        constraints: Constraints,
635    ) -> Result<Rc<MeasuredNode>, NodeError> {
636        LayoutBuilderState::measure_node(Rc::clone(&self.state), node_id, constraints)
637    }
638
639    fn set_runtime_handle(&mut self, handle: Option<RuntimeHandle>) {
640        self.state.borrow_mut().runtime_handle = handle;
641    }
642}
643
644struct LayoutBuilderState {
645    applier: Rc<ConcreteApplierHost<MemoryApplier>>,
646    runtime_handle: Option<RuntimeHandle>,
647    /// Shared handle to the slot table. This is shared with ApplierSlotGuard
648    /// to ensure panic-safety: even if we panic, the guard can restore slots.
649    slots: Rc<RefCell<SlotBackend>>,
650    cache_epoch: u64,
651    tmp_measurables: Vec<Box<dyn Measurable>>,
652    tmp_records: Vec<(NodeId, ChildRecord)>,
653}
654
655impl LayoutBuilderState {
656    fn new_with_epoch(
657        applier: Rc<ConcreteApplierHost<MemoryApplier>>,
658        epoch: u64,
659        slots: Rc<RefCell<SlotBackend>>,
660    ) -> Self {
661        let runtime_handle = applier.borrow_typed().runtime_handle();
662
663        Self {
664            applier,
665            runtime_handle,
666            slots,
667            cache_epoch: epoch,
668            tmp_measurables: Vec::new(),
669            tmp_records: Vec::new(),
670        }
671    }
672
673    fn try_with_applier_result<R>(
674        state_rc: &Rc<RefCell<Self>>,
675        f: impl FnOnce(&mut MemoryApplier) -> Result<R, NodeError>,
676    ) -> Option<Result<R, NodeError>> {
677        let host = {
678            let state = state_rc.borrow();
679            Rc::clone(&state.applier)
680        };
681
682        // Try to borrow - if already borrowed (nested call), return None
683        let Ok(mut applier) = host.try_borrow_typed() else {
684            return None;
685        };
686
687        Some(f(&mut applier))
688    }
689
690    fn with_applier_result<R>(
691        state_rc: &Rc<RefCell<Self>>,
692        f: impl FnOnce(&mut MemoryApplier) -> Result<R, NodeError>,
693    ) -> Result<R, NodeError> {
694        Self::try_with_applier_result(state_rc, f).unwrap_or_else(|| {
695            Err(NodeError::MissingContext {
696                id: NodeId::default(),
697                reason: "applier already borrowed",
698            })
699        })
700    }
701
702    /// Clears the is_placed flag for a node at the start of measurement.
703    /// This ensures nodes that drop out of placement won't render with stale geometry.
704    fn clear_node_placed(state_rc: &Rc<RefCell<Self>>, node_id: NodeId) {
705        let host = {
706            let state = state_rc.borrow();
707            Rc::clone(&state.applier)
708        };
709        let Ok(mut applier) = host.try_borrow_typed() else {
710            return;
711        };
712        // Try LayoutNode first, then SubcomposeLayoutNode
713        if applier
714            .with_node::<LayoutNode, _>(node_id, |node| {
715                node.clear_placed();
716            })
717            .is_err()
718        {
719            let _ = applier.with_node::<SubcomposeLayoutNode, _>(node_id, |node| {
720                node.clear_placed();
721            });
722        }
723    }
724
725    fn measure_node(
726        state_rc: Rc<RefCell<Self>>,
727        node_id: NodeId,
728        constraints: Constraints,
729    ) -> Result<Rc<MeasuredNode>, NodeError> {
730        // Clear is_placed at the start of measurement.
731        // Nodes that are placed will have is_placed set to true via Placeable::place().
732        // Nodes that drop out of placement (not placed this pass) will remain is_placed=false.
733        Self::clear_node_placed(&state_rc, node_id);
734
735        // Try SubcomposeLayoutNode first
736        if let Some(subcompose) =
737            Self::try_measure_subcompose(Rc::clone(&state_rc), node_id, constraints)?
738        {
739            return Ok(subcompose);
740        }
741
742        // Try LayoutNode (the primary modern path)
743        if let Some(result) = Self::try_with_applier_result(&state_rc, |applier| {
744            match applier.with_node::<LayoutNode, _>(node_id, |layout_node| {
745                LayoutNodeSnapshot::from_layout_node(layout_node)
746            }) {
747                Ok(snapshot) => Ok(Some(snapshot)),
748                Err(NodeError::TypeMismatch { .. }) | Err(NodeError::Missing { .. }) => Ok(None),
749                Err(err) => Err(err),
750            }
751        }) {
752            // Applier was available, process the result
753            if let Some(snapshot) = result? {
754                return Self::measure_layout_node(
755                    Rc::clone(&state_rc),
756                    node_id,
757                    snapshot,
758                    constraints,
759                );
760            }
761        }
762        // If applier was busy (None) or snapshot was None, fall through to fallback
763
764        // No legacy fallbacks - all widgets now use LayoutNode or SubcomposeLayoutNode
765        // If we reach here, it's an unknown node type (shouldn't happen in normal use)
766        Ok(Rc::new(MeasuredNode::new(
767            node_id,
768            Size::default(),
769            Point { x: 0.0, y: 0.0 },
770            Point::default(), // No content offset for fallback nodes
771            Vec::new(),
772        )))
773    }
774
775    fn try_measure_subcompose(
776        state_rc: Rc<RefCell<Self>>,
777        node_id: NodeId,
778        constraints: Constraints,
779    ) -> Result<Option<Rc<MeasuredNode>>, NodeError> {
780        let applier_host = {
781            let state = state_rc.borrow();
782            Rc::clone(&state.applier)
783        };
784
785        let (node_handle, resolved_modifiers) = {
786            // Try to borrow - if already borrowed (nested measurement), return None
787            let Ok(mut applier) = applier_host.try_borrow_typed() else {
788                return Ok(None);
789            };
790            let node = match applier.get_mut(node_id) {
791                Ok(node) => node,
792                Err(NodeError::Missing { .. }) => return Ok(None),
793                Err(err) => return Err(err),
794            };
795            let any = node.as_any_mut();
796            if let Some(subcompose) =
797                any.downcast_mut::<crate::subcompose_layout::SubcomposeLayoutNode>()
798            {
799                let handle = subcompose.handle();
800                let resolved_modifiers = handle.resolved_modifiers();
801                (handle, resolved_modifiers)
802            } else {
803                return Ok(None);
804            }
805        };
806
807        let runtime_handle = {
808            let mut state = state_rc.borrow_mut();
809            if state.runtime_handle.is_none() {
810                // Try to borrow - if already borrowed, we can't get runtime handle
811                if let Ok(applier) = applier_host.try_borrow_typed() {
812                    state.runtime_handle = applier.runtime_handle();
813                }
814            }
815            state
816                .runtime_handle
817                .clone()
818                .ok_or(NodeError::MissingContext {
819                    id: node_id,
820                    reason: "runtime handle required for subcomposition",
821                })?
822        };
823
824        let props = resolved_modifiers.layout_properties();
825        let padding = resolved_modifiers.padding();
826        let offset = resolved_modifiers.offset();
827        let mut inner_constraints = normalize_constraints(subtract_padding(constraints, padding));
828
829        if let DimensionConstraint::Points(width) = props.width() {
830            let constrained_width = width - padding.horizontal_sum();
831            inner_constraints.max_width = inner_constraints.max_width.min(constrained_width);
832            inner_constraints.min_width = inner_constraints.min_width.min(constrained_width);
833        }
834        if let DimensionConstraint::Points(height) = props.height() {
835            let constrained_height = height - padding.vertical_sum();
836            inner_constraints.max_height = inner_constraints.max_height.min(constrained_height);
837            inner_constraints.min_height = inner_constraints.min_height.min(constrained_height);
838        }
839
840        let mut slots_guard = SlotsGuard::take(Rc::clone(&state_rc));
841        let slots_host = slots_guard.host();
842        let applier_host_dyn: Rc<dyn ApplierHost> = applier_host.clone();
843        let observer = SnapshotStateObserver::new(|callback| callback());
844        let composer = Composer::new(
845            Rc::clone(&slots_host),
846            applier_host_dyn,
847            runtime_handle.clone(),
848            observer,
849            Some(node_id),
850        );
851        composer.enter_phase(Phase::Measure);
852
853        let state_rc_clone = Rc::clone(&state_rc);
854        let measure_error: Rc<RefCell<Option<NodeError>>> = Rc::new(RefCell::new(None));
855        let error_for_measurer = Rc::clone(&measure_error);
856        let measurer = Box::new(
857            move |child_id: NodeId, child_constraints: Constraints| -> Size {
858                match Self::measure_node(Rc::clone(&state_rc_clone), child_id, child_constraints) {
859                    Ok(measured) => measured.size,
860                    Err(err) => {
861                        let mut slot = error_for_measurer.borrow_mut();
862                        if slot.is_none() {
863                            *slot = Some(err);
864                        }
865                        Size::default()
866                    }
867                }
868            },
869        );
870
871        let measure_result = node_handle.measure(
872            &composer,
873            node_id,
874            inner_constraints,
875            measurer,
876            Rc::clone(&measure_error),
877        )?;
878
879        slots_guard.restore(slots_host.take());
880
881        if let Some(err) = measure_error.borrow_mut().take() {
882            return Err(err);
883        }
884
885        // NOTE: Children are now managed by the composer via insert_child commands
886        // (from parent_stack initialization with root). set_active_children is no longer used.
887
888        let mut width = measure_result.size.width + padding.horizontal_sum();
889        let mut height = measure_result.size.height + padding.vertical_sum();
890
891        width = resolve_dimension(
892            width,
893            props.width(),
894            props.min_width(),
895            props.max_width(),
896            constraints.min_width,
897            constraints.max_width,
898        );
899        height = resolve_dimension(
900            height,
901            props.height(),
902            props.min_height(),
903            props.max_height(),
904            constraints.min_height,
905            constraints.max_height,
906        );
907
908        let mut children = Vec::new();
909
910        // Update the SubcomposeLayoutNode's size (position will be set by parent's placement)
911        if let Ok(mut applier) = applier_host.try_borrow_typed() {
912            let _ = applier.with_node::<SubcomposeLayoutNode, _>(node_id, |parent_node| {
913                parent_node.set_measured_size(Size { width, height });
914            });
915        }
916
917        for placement in measure_result.placements {
918            let child =
919                Self::measure_node(Rc::clone(&state_rc), placement.node_id, inner_constraints)?;
920            let position = Point {
921                x: padding.left + placement.x,
922                y: padding.top + placement.y,
923            };
924
925            // Critical: Update the child LayoutNode's retained state.
926            // Standard layouts do this via Placeable::place(), but SubcomposeLayout logic
927            // bypasses Placeables and returns raw Placements.
928            if let Ok(mut applier) = applier_host.try_borrow_typed() {
929                let _ = applier.with_node::<LayoutNode, _>(placement.node_id, |node| {
930                    node.set_position(position);
931                });
932            }
933
934            children.push(MeasuredChild {
935                node: child,
936                offset: position,
937            });
938        }
939
940        // Update the SubcomposeLayoutNode's active children for rendering
941        node_handle.set_active_children(children.iter().map(|c| c.node.node_id));
942
943        Ok(Some(Rc::new(MeasuredNode::new(
944            node_id,
945            Size { width, height },
946            offset,
947            Point::default(), // Subcompose nodes: content_offset handled by child layout
948            children,
949        ))))
950    }
951    /// Measures through the layout modifier coordinator chain using reconciled modifier nodes.
952    /// Iterates through LayoutModifierNode instances from the ModifierNodeChain and calls
953    /// their measure() methods, mirroring Jetpack Compose's LayoutModifierNodeCoordinator pattern.
954    ///
955    /// Always succeeds, building a coordinator chain (possibly just InnerCoordinator) to measure.
956    ///
957    fn measure_through_modifier_chain(
958        state_rc: &Rc<RefCell<Self>>,
959        node_id: NodeId,
960        measurables: &[Box<dyn Measurable>],
961        measure_policy: &Rc<dyn MeasurePolicy>,
962        constraints: Constraints,
963    ) -> ModifierChainMeasurement {
964        use cranpose_foundation::NodeCapabilities;
965
966        // Collect layout node information from the modifier chain
967        #[allow(clippy::type_complexity)]
968        // Tuple of (index, boxed trait object) is reasonable for modifier nodes
969        let mut layout_node_data: Vec<(
970            usize,
971            Rc<RefCell<Box<dyn cranpose_foundation::ModifierNode>>>,
972        )> = Vec::new();
973        let mut offset = Point::default();
974
975        {
976            let state = state_rc.borrow();
977            let mut applier = state.applier.borrow_typed();
978
979            let _ = applier.with_node::<LayoutNode, _>(node_id, |layout_node| {
980                let chain_handle = layout_node.modifier_chain();
981
982                if !chain_handle.has_layout_nodes() {
983                    return;
984                }
985
986                // Collect indices and node Rc clones for layout modifier nodes
987                chain_handle.chain().for_each_forward_matching(
988                    NodeCapabilities::LAYOUT,
989                    |node_ref| {
990                        if let Some(index) = node_ref.entry_index() {
991                            // Get the Rc clone for this node
992                            if let Some(node_rc) = chain_handle.chain().get_node_rc(index) {
993                                layout_node_data.push((index, Rc::clone(&node_rc)));
994                            }
995
996                            // Extract offset from OffsetNode for the node's own position
997                            // The coordinator chain handles placement_offset (for children),
998                            // but the node's offset affects where IT is positioned in the parent
999                            node_ref.with_node(|node| {
1000                                if let Some(offset_node) =
1001                                    node.as_any()
1002                                        .downcast_ref::<crate::modifier_nodes::OffsetNode>()
1003                                {
1004                                    let delta = offset_node.offset();
1005                                    offset.x += delta.x;
1006                                    offset.y += delta.y;
1007                                }
1008                            });
1009                        }
1010                    },
1011                );
1012            });
1013        }
1014
1015        // Fast path: if there are no layout modifiers, measure directly without coordinator chain.
1016        // This saves 3 allocations (shared_context, policy_result, InnerCoordinator box).
1017        if layout_node_data.is_empty() {
1018            let result = measure_policy.measure(measurables, constraints);
1019            let final_size = result.size;
1020            let placements = result.placements;
1021
1022            return ModifierChainMeasurement {
1023                result: MeasureResult {
1024                    size: final_size,
1025                    placements,
1026                },
1027                content_offset: Point::default(),
1028                offset,
1029            };
1030        }
1031
1032        // Slow path: build coordinator chain for layout modifiers
1033        // Reverse order: rightmost modifier is measured first (innermost), leftmost is outer
1034        layout_node_data.reverse();
1035
1036        // Create a shared context for this measurement pass to track invalidations
1037        let shared_context = Rc::new(RefCell::new(LayoutNodeContext::new()));
1038
1039        // Create the inner coordinator that wraps the measure policy
1040        let policy_result = Rc::new(RefCell::new(None));
1041        let inner_coordinator: Box<dyn NodeCoordinator + '_> =
1042            Box::new(coordinator::InnerCoordinator::new(
1043                Rc::clone(measure_policy),
1044                measurables,
1045                Rc::clone(&policy_result),
1046            ));
1047
1048        // Wrap each layout modifier node in a coordinator, building the chain
1049        let mut current_coordinator = inner_coordinator;
1050        for (_, node_rc) in layout_node_data {
1051            current_coordinator = Box::new(coordinator::LayoutModifierCoordinator::new(
1052                node_rc,
1053                current_coordinator,
1054                Rc::clone(&shared_context),
1055            ));
1056        }
1057
1058        // Measure through the complete coordinator chain
1059        let placeable = current_coordinator.measure(constraints);
1060        let final_size = Size {
1061            width: placeable.width(),
1062            height: placeable.height(),
1063        };
1064
1065        // Get accumulated content offset from the placeable (computed during measure)
1066        let content_offset = placeable.content_offset();
1067        let all_placement_offset = Point {
1068            x: content_offset.0,
1069            y: content_offset.1,
1070        };
1071
1072        // The content_offset for scroll/inner transforms is the accumulated placement offset
1073        // MINUS the node's own offset (which affects its position in the parent, not content position).
1074        // This properly separates: node position (offset) vs inner content position (content_offset).
1075        let content_offset = Point {
1076            x: all_placement_offset.x - offset.x,
1077            y: all_placement_offset.y - offset.y,
1078        };
1079
1080        // offset was already extracted from OffsetNode above
1081
1082        let placements = policy_result
1083            .borrow_mut()
1084            .take()
1085            .map(|result| result.placements)
1086            .unwrap_or_default();
1087
1088        // Process any invalidations requested during measurement
1089        let invalidations = shared_context.borrow_mut().take_invalidations();
1090        if !invalidations.is_empty() {
1091            // Mark the LayoutNode as needing the appropriate passes
1092            Self::with_applier_result(state_rc, |applier| {
1093                applier.with_node::<LayoutNode, _>(node_id, |layout_node| {
1094                    for kind in invalidations {
1095                        match kind {
1096                            InvalidationKind::Layout => layout_node.mark_needs_measure(),
1097                            InvalidationKind::Draw => layout_node.mark_needs_redraw(),
1098                            InvalidationKind::Semantics => layout_node.mark_needs_semantics(),
1099                            InvalidationKind::PointerInput => layout_node.mark_needs_pointer_pass(),
1100                            InvalidationKind::Focus => layout_node.mark_needs_focus_sync(),
1101                        }
1102                    }
1103                })
1104            })
1105            .ok();
1106        }
1107
1108        ModifierChainMeasurement {
1109            result: MeasureResult {
1110                size: final_size,
1111                placements,
1112            },
1113            content_offset,
1114            offset,
1115        }
1116    }
1117
1118    fn measure_layout_node(
1119        state_rc: Rc<RefCell<Self>>,
1120        node_id: NodeId,
1121        snapshot: LayoutNodeSnapshot,
1122        constraints: Constraints,
1123    ) -> Result<Rc<MeasuredNode>, NodeError> {
1124        let cache_epoch = {
1125            let state = state_rc.borrow();
1126            state.cache_epoch
1127        };
1128        let LayoutNodeSnapshot {
1129            resolved_modifiers,
1130            measure_policy,
1131            children,
1132            cache,
1133            needs_measure,
1134        } = snapshot;
1135        cache.activate(cache_epoch);
1136        let layout_props = resolved_modifiers.layout_properties();
1137
1138        if needs_measure {
1139            // Node has needs_measure=true
1140        }
1141
1142        // Only check cache if not marked as needing measure.
1143        // When needs_measure=true, we MUST re-run measure() even if constraints match,
1144        // because something else changed (e.g., scroll offset, modifier state).
1145        if !needs_measure {
1146            // Check cache for current constraints
1147            if let Some(cached) = cache.get_measurement(constraints) {
1148                // Clear dirty flag after successful cache hit
1149                Self::with_applier_result(&state_rc, |applier| {
1150                    applier.with_node::<LayoutNode, _>(node_id, |node| {
1151                        node.clear_needs_measure();
1152                        node.clear_needs_layout();
1153                    })
1154                })
1155                .ok();
1156                return Ok(cached);
1157            }
1158        }
1159
1160        let (runtime_handle, applier_host) = {
1161            let state = state_rc.borrow();
1162            (state.runtime_handle.clone(), Rc::clone(&state.applier))
1163        };
1164
1165        let measure_handle = LayoutMeasureHandle::new(Rc::clone(&state_rc));
1166        let error = Rc::new(RefCell::new(None));
1167        let mut pools = VecPools::acquire(Rc::clone(&state_rc));
1168        let (measurables, records) = pools.parts();
1169
1170        for &child_id in children.iter() {
1171            let measured = Rc::new(RefCell::new(None));
1172            let position = Rc::new(RefCell::new(None));
1173
1174            let data = {
1175                let mut applier = applier_host.borrow_typed();
1176                match applier.with_node::<LayoutNode, _>(child_id, |n| {
1177                    (n.cache_handles(), n.layout_state_handle())
1178                }) {
1179                    Ok((cache, state)) => Some((cache, Some(state))),
1180                    Err(NodeError::TypeMismatch { .. }) => {
1181                        Some((LayoutNodeCacheHandles::default(), None))
1182                    }
1183                    Err(NodeError::Missing { .. }) => None,
1184                    Err(err) => return Err(err),
1185                }
1186            };
1187
1188            let Some((cache_handles, layout_state)) = data else {
1189                continue;
1190            };
1191
1192            cache_handles.activate(cache_epoch);
1193
1194            records.push((
1195                child_id,
1196                ChildRecord {
1197                    measured: Rc::clone(&measured),
1198                    last_position: Rc::clone(&position),
1199                },
1200            ));
1201            measurables.push(Box::new(LayoutChildMeasurable::new(
1202                Rc::clone(&applier_host),
1203                child_id,
1204                measured,
1205                position,
1206                Rc::clone(&error),
1207                runtime_handle.clone(),
1208                cache_handles,
1209                cache_epoch,
1210                Some(measure_handle.clone()),
1211                layout_state,
1212            )));
1213        }
1214
1215        // Try to measure through the modifier node chain first.
1216        let chain_constraints = Constraints {
1217            min_width: constraints.min_width,
1218            max_width: if matches!(layout_props.width(), DimensionConstraint::Unspecified) {
1219                f32::INFINITY
1220            } else {
1221                constraints.max_width
1222            },
1223            min_height: constraints.min_height,
1224            max_height: if matches!(layout_props.height(), DimensionConstraint::Unspecified) {
1225                f32::INFINITY
1226            } else {
1227                constraints.max_height
1228            },
1229        };
1230
1231        let mut modifier_chain_result = Self::measure_through_modifier_chain(
1232            &state_rc,
1233            node_id,
1234            measurables.as_slice(),
1235            &measure_policy,
1236            chain_constraints,
1237        );
1238
1239        if (chain_constraints.max_width != constraints.max_width
1240            || chain_constraints.max_height != constraints.max_height)
1241            && ((constraints.max_width.is_finite()
1242                && modifier_chain_result.result.size.width > constraints.max_width)
1243                || (constraints.max_height.is_finite()
1244                    && modifier_chain_result.result.size.height > constraints.max_height))
1245        {
1246            modifier_chain_result = Self::measure_through_modifier_chain(
1247                &state_rc,
1248                node_id,
1249                measurables.as_slice(),
1250                &measure_policy,
1251                constraints,
1252            );
1253        }
1254
1255        // Modifier chain always succeeds - use the node-driven measurement.
1256        let (width, height, policy_result, content_offset, offset) = {
1257            let result = modifier_chain_result;
1258            // The size is already correct from the modifier chain (modifiers like SizeNode
1259            // have already enforced their constraints), so we use it directly.
1260            if let Some(err) = error.borrow_mut().take() {
1261                return Err(err);
1262            }
1263
1264            (
1265                result.result.size.width,
1266                result.result.size.height,
1267                result.result,
1268                result.content_offset,
1269                result.offset,
1270            )
1271        };
1272
1273        let mut measured_children = Vec::new();
1274        for &child_id in children.iter() {
1275            if let Some((_, record)) = records.iter().find(|(id, _)| *id == child_id) {
1276                if let Some(measured) = record.measured.borrow_mut().take() {
1277                    let base_position = policy_result
1278                        .placements
1279                        .iter()
1280                        .find(|placement| placement.node_id == child_id)
1281                        .map(|placement| Point {
1282                            x: placement.x,
1283                            y: placement.y,
1284                        })
1285                        .or_else(|| record.last_position.borrow().as_ref().copied())
1286                        .unwrap_or(Point { x: 0.0, y: 0.0 });
1287                    // Apply content_offset (from scroll/transforms) to child positioning
1288                    let position = Point {
1289                        x: content_offset.x + base_position.x,
1290                        y: content_offset.y + base_position.y,
1291                    };
1292                    measured_children.push(MeasuredChild {
1293                        node: measured,
1294                        offset: position,
1295                    });
1296                }
1297            }
1298        }
1299
1300        let measured = Rc::new(MeasuredNode::new(
1301            node_id,
1302            Size { width, height },
1303            offset,
1304            content_offset,
1305            measured_children,
1306        ));
1307
1308        cache.store_measurement(constraints, Rc::clone(&measured));
1309
1310        // Clear dirty flags and update derived state
1311        Self::with_applier_result(&state_rc, |applier| {
1312            applier.with_node::<LayoutNode, _>(node_id, |node| {
1313                node.clear_needs_measure();
1314                node.clear_needs_layout();
1315                node.set_measured_size(Size { width, height });
1316                node.set_content_offset(content_offset);
1317            })
1318        })
1319        .ok();
1320
1321        Ok(measured)
1322    }
1323}
1324
1325/// Snapshot of a LayoutNode's data for measuring.
1326/// This is a temporary copy used during the measure phase, not a live node.
1327///
1328/// Note: We capture `needs_measure` here because it's checked during measure to enable
1329/// selective measure optimization at the individual node level. Even if the tree is partially
1330/// dirty (some nodes changed), clean nodes can skip measure and use cached results.
1331struct LayoutNodeSnapshot {
1332    resolved_modifiers: ResolvedModifiers,
1333    measure_policy: Rc<dyn MeasurePolicy>,
1334    children: Vec<NodeId>,
1335    cache: LayoutNodeCacheHandles,
1336    /// Whether this specific node needs to be measured (vs using cached measurement)
1337    needs_measure: bool,
1338}
1339
1340impl LayoutNodeSnapshot {
1341    fn from_layout_node(node: &LayoutNode) -> Self {
1342        Self {
1343            resolved_modifiers: node.resolved_modifiers(),
1344            measure_policy: Rc::clone(&node.measure_policy),
1345            children: node.children.iter().copied().collect(),
1346            cache: node.cache_handles(),
1347            needs_measure: node.needs_measure(),
1348        }
1349    }
1350}
1351
1352// Helper types for accessing subsets of LayoutBuilderState
1353struct VecPools {
1354    state: Rc<RefCell<LayoutBuilderState>>,
1355    measurables: Option<Vec<Box<dyn Measurable>>>,
1356    records: Option<Vec<(NodeId, ChildRecord)>>,
1357}
1358
1359impl VecPools {
1360    fn acquire(state: Rc<RefCell<LayoutBuilderState>>) -> Self {
1361        let measurables = {
1362            let mut state_mut = state.borrow_mut();
1363            std::mem::take(&mut state_mut.tmp_measurables)
1364        };
1365        let records = {
1366            let mut state_mut = state.borrow_mut();
1367            std::mem::take(&mut state_mut.tmp_records)
1368        };
1369        Self {
1370            state,
1371            measurables: Some(measurables),
1372            records: Some(records),
1373        }
1374    }
1375
1376    #[allow(clippy::type_complexity)] // Returns internal Vec references for layout operations
1377    fn parts(
1378        &mut self,
1379    ) -> (
1380        &mut Vec<Box<dyn Measurable>>,
1381        &mut Vec<(NodeId, ChildRecord)>,
1382    ) {
1383        let measurables = self
1384            .measurables
1385            .as_mut()
1386            .expect("measurables already returned");
1387        let records = self.records.as_mut().expect("records already returned");
1388        (measurables, records)
1389    }
1390}
1391
1392impl Drop for VecPools {
1393    fn drop(&mut self) {
1394        let mut state = self.state.borrow_mut();
1395        if let Some(mut measurables) = self.measurables.take() {
1396            measurables.clear();
1397            state.tmp_measurables = measurables;
1398        }
1399        if let Some(mut records) = self.records.take() {
1400            records.clear();
1401            state.tmp_records = records;
1402        }
1403    }
1404}
1405
1406struct SlotsGuard {
1407    state: Rc<RefCell<LayoutBuilderState>>,
1408    slots: Option<SlotBackend>,
1409}
1410
1411impl SlotsGuard {
1412    fn take(state: Rc<RefCell<LayoutBuilderState>>) -> Self {
1413        let slots = {
1414            let state_ref = state.borrow();
1415            let mut slots_ref = state_ref.slots.borrow_mut();
1416            std::mem::take(&mut *slots_ref)
1417        };
1418        Self {
1419            state,
1420            slots: Some(slots),
1421        }
1422    }
1423
1424    fn host(&mut self) -> Rc<SlotsHost> {
1425        let slots = self.slots.take().unwrap_or_default();
1426        Rc::new(SlotsHost::new(slots))
1427    }
1428
1429    fn restore(&mut self, slots: SlotBackend) {
1430        debug_assert!(self.slots.is_none());
1431        self.slots = Some(slots);
1432    }
1433}
1434
1435impl Drop for SlotsGuard {
1436    fn drop(&mut self) {
1437        if let Some(slots) = self.slots.take() {
1438            let state_ref = self.state.borrow();
1439            *state_ref.slots.borrow_mut() = slots;
1440        }
1441    }
1442}
1443
1444#[derive(Clone)]
1445struct LayoutMeasureHandle {
1446    state: Rc<RefCell<LayoutBuilderState>>,
1447}
1448
1449impl LayoutMeasureHandle {
1450    fn new(state: Rc<RefCell<LayoutBuilderState>>) -> Self {
1451        Self { state }
1452    }
1453
1454    fn measure(
1455        &self,
1456        node_id: NodeId,
1457        constraints: Constraints,
1458    ) -> Result<Rc<MeasuredNode>, NodeError> {
1459        LayoutBuilderState::measure_node(Rc::clone(&self.state), node_id, constraints)
1460    }
1461}
1462
1463#[derive(Debug, Clone)]
1464pub(crate) struct MeasuredNode {
1465    node_id: NodeId,
1466    size: Size,
1467    /// Node's position offset relative to parent (from OffsetNode etc.)
1468    offset: Point,
1469    /// Content offset for scroll/inner transforms (NOT node position)
1470    content_offset: Point,
1471    children: Vec<MeasuredChild>,
1472}
1473
1474impl MeasuredNode {
1475    fn new(
1476        node_id: NodeId,
1477        size: Size,
1478        offset: Point,
1479        content_offset: Point,
1480        children: Vec<MeasuredChild>,
1481    ) -> Self {
1482        Self {
1483            node_id,
1484            size,
1485            offset,
1486            content_offset,
1487            children,
1488        }
1489    }
1490}
1491
1492#[derive(Debug, Clone)]
1493struct MeasuredChild {
1494    node: Rc<MeasuredNode>,
1495    offset: Point,
1496}
1497
1498struct ChildRecord {
1499    measured: Rc<RefCell<Option<Rc<MeasuredNode>>>>,
1500    last_position: Rc<RefCell<Option<Point>>>,
1501}
1502
1503struct LayoutChildMeasurable {
1504    applier: Rc<ConcreteApplierHost<MemoryApplier>>,
1505    node_id: NodeId,
1506    measured: Rc<RefCell<Option<Rc<MeasuredNode>>>>,
1507    last_position: Rc<RefCell<Option<Point>>>,
1508    error: Rc<RefCell<Option<NodeError>>>,
1509    runtime_handle: Option<RuntimeHandle>,
1510    cache: LayoutNodeCacheHandles,
1511    cache_epoch: u64,
1512    measure_handle: Option<LayoutMeasureHandle>,
1513    layout_state: Option<Rc<RefCell<crate::widgets::nodes::layout_node::LayoutState>>>,
1514}
1515
1516impl LayoutChildMeasurable {
1517    #[allow(clippy::too_many_arguments)] // Constructor needs all layout state for child measurement
1518    fn new(
1519        applier: Rc<ConcreteApplierHost<MemoryApplier>>,
1520        node_id: NodeId,
1521        measured: Rc<RefCell<Option<Rc<MeasuredNode>>>>,
1522        last_position: Rc<RefCell<Option<Point>>>,
1523        error: Rc<RefCell<Option<NodeError>>>,
1524        runtime_handle: Option<RuntimeHandle>,
1525        cache: LayoutNodeCacheHandles,
1526        cache_epoch: u64,
1527        measure_handle: Option<LayoutMeasureHandle>,
1528        layout_state: Option<Rc<RefCell<crate::widgets::nodes::layout_node::LayoutState>>>,
1529    ) -> Self {
1530        cache.activate(cache_epoch);
1531        Self {
1532            applier,
1533            node_id,
1534            measured,
1535            last_position,
1536            error,
1537            runtime_handle,
1538            cache,
1539            cache_epoch,
1540            measure_handle,
1541            layout_state,
1542        }
1543    }
1544
1545    fn record_error(&self, err: NodeError) {
1546        let mut slot = self.error.borrow_mut();
1547        if slot.is_none() {
1548            *slot = Some(err);
1549        }
1550    }
1551
1552    fn perform_measure(&self, constraints: Constraints) -> Result<Rc<MeasuredNode>, NodeError> {
1553        if let Some(handle) = &self.measure_handle {
1554            handle.measure(self.node_id, constraints)
1555        } else {
1556            measure_node_with_host(
1557                Rc::clone(&self.applier),
1558                self.runtime_handle.clone(),
1559                self.node_id,
1560                constraints,
1561                self.cache_epoch,
1562            )
1563        }
1564    }
1565
1566    fn intrinsic_measure(&self, constraints: Constraints) -> Option<Rc<MeasuredNode>> {
1567        self.cache.activate(self.cache_epoch);
1568        if let Some(cached) = self.cache.get_measurement(constraints) {
1569            return Some(cached);
1570        }
1571
1572        match self.perform_measure(constraints) {
1573            Ok(measured) => {
1574                self.cache
1575                    .store_measurement(constraints, Rc::clone(&measured));
1576                Some(measured)
1577            }
1578            Err(err) => {
1579                self.record_error(err);
1580                None
1581            }
1582        }
1583    }
1584}
1585
1586impl Measurable for LayoutChildMeasurable {
1587    fn measure(&self, constraints: Constraints) -> Box<dyn Placeable> {
1588        self.cache.activate(self.cache_epoch);
1589        let measured_size;
1590        if let Some(cached) = self.cache.get_measurement(constraints) {
1591            measured_size = cached.size;
1592            *self.measured.borrow_mut() = Some(Rc::clone(&cached));
1593        } else {
1594            match self.perform_measure(constraints) {
1595                Ok(measured) => {
1596                    measured_size = measured.size;
1597                    self.cache
1598                        .store_measurement(constraints, Rc::clone(&measured));
1599                    *self.measured.borrow_mut() = Some(measured);
1600                }
1601                Err(err) => {
1602                    self.record_error(err);
1603                    self.measured.borrow_mut().take();
1604                    measured_size = Size {
1605                        width: 0.0,
1606                        height: 0.0,
1607                    };
1608                }
1609            }
1610        }
1611
1612        // Update retained LayoutNode state with measured size (new architecture).
1613        // PRIORITIZE direct handle to avoid Applier borrow conflicts during layout!
1614        if let Some(state) = &self.layout_state {
1615            let mut state = state.borrow_mut();
1616            state.size = measured_size;
1617            state.measurement_constraints = constraints;
1618        } else if let Ok(mut applier) = self.applier.try_borrow_typed() {
1619            let _ = applier.with_node::<LayoutNode, _>(self.node_id, |node| {
1620                node.set_measured_size(measured_size);
1621                node.set_measurement_constraints(constraints);
1622            });
1623        }
1624
1625        Box::new(LayoutChildPlaceable::new(
1626            Rc::clone(&self.applier),
1627            self.node_id,
1628            Rc::clone(&self.measured),
1629            Rc::clone(&self.last_position),
1630            self.layout_state.clone(),
1631        ))
1632    }
1633
1634    fn min_intrinsic_width(&self, height: f32) -> f32 {
1635        let kind = IntrinsicKind::MinWidth(height);
1636        self.cache.activate(self.cache_epoch);
1637        if let Some(value) = self.cache.get_intrinsic(&kind) {
1638            return value;
1639        }
1640        let constraints = Constraints {
1641            min_width: 0.0,
1642            max_width: f32::INFINITY,
1643            min_height: height,
1644            max_height: height,
1645        };
1646        if let Some(node) = self.intrinsic_measure(constraints) {
1647            let value = node.size.width;
1648            self.cache.store_intrinsic(kind, value);
1649            value
1650        } else {
1651            0.0
1652        }
1653    }
1654
1655    fn max_intrinsic_width(&self, height: f32) -> f32 {
1656        let kind = IntrinsicKind::MaxWidth(height);
1657        self.cache.activate(self.cache_epoch);
1658        if let Some(value) = self.cache.get_intrinsic(&kind) {
1659            return value;
1660        }
1661        let constraints = Constraints {
1662            min_width: 0.0,
1663            max_width: f32::INFINITY,
1664            min_height: 0.0,
1665            max_height: height,
1666        };
1667        if let Some(node) = self.intrinsic_measure(constraints) {
1668            let value = node.size.width;
1669            self.cache.store_intrinsic(kind, value);
1670            value
1671        } else {
1672            0.0
1673        }
1674    }
1675
1676    fn min_intrinsic_height(&self, width: f32) -> f32 {
1677        let kind = IntrinsicKind::MinHeight(width);
1678        self.cache.activate(self.cache_epoch);
1679        if let Some(value) = self.cache.get_intrinsic(&kind) {
1680            return value;
1681        }
1682        let constraints = Constraints {
1683            min_width: width,
1684            max_width: width,
1685            min_height: 0.0,
1686            max_height: f32::INFINITY,
1687        };
1688        if let Some(node) = self.intrinsic_measure(constraints) {
1689            let value = node.size.height;
1690            self.cache.store_intrinsic(kind, value);
1691            value
1692        } else {
1693            0.0
1694        }
1695    }
1696
1697    fn max_intrinsic_height(&self, width: f32) -> f32 {
1698        let kind = IntrinsicKind::MaxHeight(width);
1699        self.cache.activate(self.cache_epoch);
1700        if let Some(value) = self.cache.get_intrinsic(&kind) {
1701            return value;
1702        }
1703        let constraints = Constraints {
1704            min_width: 0.0,
1705            max_width: width,
1706            min_height: 0.0,
1707            max_height: f32::INFINITY,
1708        };
1709        if let Some(node) = self.intrinsic_measure(constraints) {
1710            let value = node.size.height;
1711            self.cache.store_intrinsic(kind, value);
1712            value
1713        } else {
1714            0.0
1715        }
1716    }
1717
1718    fn flex_parent_data(&self) -> Option<cranpose_ui_layout::FlexParentData> {
1719        // Try to borrow the applier - if it's already borrowed (nested measurement), return None.
1720        // This is safe because parent data doesn't change during measurement.
1721        let Ok(mut applier) = self.applier.try_borrow_typed() else {
1722            return None;
1723        };
1724
1725        applier
1726            .with_node::<LayoutNode, _>(self.node_id, |layout_node| {
1727                let props = layout_node.resolved_modifiers().layout_properties();
1728                props.weight().map(|weight_data| {
1729                    cranpose_ui_layout::FlexParentData::new(weight_data.weight, weight_data.fill)
1730                })
1731            })
1732            .ok()
1733            .flatten()
1734    }
1735}
1736
1737struct LayoutChildPlaceable {
1738    applier: Rc<ConcreteApplierHost<MemoryApplier>>,
1739    node_id: NodeId,
1740    measured: Rc<RefCell<Option<Rc<MeasuredNode>>>>,
1741    last_position: Rc<RefCell<Option<Point>>>,
1742    layout_state: Option<Rc<RefCell<crate::widgets::nodes::layout_node::LayoutState>>>,
1743}
1744
1745impl LayoutChildPlaceable {
1746    fn new(
1747        applier: Rc<ConcreteApplierHost<MemoryApplier>>,
1748        node_id: NodeId,
1749        measured: Rc<RefCell<Option<Rc<MeasuredNode>>>>,
1750        last_position: Rc<RefCell<Option<Point>>>,
1751        layout_state: Option<Rc<RefCell<crate::widgets::nodes::layout_node::LayoutState>>>,
1752    ) -> Self {
1753        Self {
1754            applier,
1755            node_id,
1756            measured,
1757            last_position,
1758            layout_state,
1759        }
1760    }
1761}
1762
1763impl Placeable for LayoutChildPlaceable {
1764    fn place(&self, x: f32, y: f32) {
1765        // Retrieve the node's own offset (from modifiers like offset(), padding(), etc.)
1766        // This must be added to the placement position (x, y) provided by the parent.
1767        let internal_offset = self
1768            .measured
1769            .borrow()
1770            .as_ref()
1771            .map(|m| m.offset)
1772            .unwrap_or_default();
1773
1774        let position = Point {
1775            x: x + internal_offset.x,
1776            y: y + internal_offset.y,
1777        };
1778        // Update transient storage (for backwards compatibility during transition)
1779        *self.last_position.borrow_mut() = Some(position);
1780
1781        // Update retained LayoutNode state (the new architecture)
1782        // PRIORITIZE direct handle to avoid Applier borrow conflicts during layout!
1783        if let Some(state) = &self.layout_state {
1784            let mut state = state.borrow_mut();
1785            state.position = position;
1786            state.is_placed = true;
1787        } else if let Ok(mut applier) = self.applier.try_borrow_typed() {
1788            // Try LayoutNode first, then SubcomposeLayoutNode
1789            if applier
1790                .with_node::<LayoutNode, _>(self.node_id, |node| {
1791                    node.set_position(position);
1792                })
1793                .is_err()
1794            {
1795                let _ = applier.with_node::<SubcomposeLayoutNode, _>(self.node_id, |node| {
1796                    node.set_position(position);
1797                });
1798            }
1799        }
1800    }
1801
1802    fn width(&self) -> f32 {
1803        self.measured
1804            .borrow()
1805            .as_ref()
1806            .map(|node| node.size.width)
1807            .unwrap_or(0.0)
1808    }
1809
1810    fn height(&self) -> f32 {
1811        self.measured
1812            .borrow()
1813            .as_ref()
1814            .map(|node| node.size.height)
1815            .unwrap_or(0.0)
1816    }
1817
1818    fn node_id(&self) -> NodeId {
1819        self.node_id
1820    }
1821}
1822
1823fn measure_node_with_host(
1824    applier: Rc<ConcreteApplierHost<MemoryApplier>>,
1825    runtime_handle: Option<RuntimeHandle>,
1826    node_id: NodeId,
1827    constraints: Constraints,
1828    epoch: u64,
1829) -> Result<Rc<MeasuredNode>, NodeError> {
1830    let runtime_handle = match runtime_handle {
1831        Some(handle) => Some(handle),
1832        None => applier.borrow_typed().runtime_handle(),
1833    };
1834    let mut builder = LayoutBuilder::new_with_epoch(
1835        applier,
1836        epoch,
1837        Rc::new(RefCell::new(SlotBackend::default())),
1838    );
1839    builder.set_runtime_handle(runtime_handle);
1840    builder.measure_node(node_id, constraints)
1841}
1842
1843#[derive(Clone)]
1844struct RuntimeNodeMetadata {
1845    modifier: Modifier,
1846    resolved_modifiers: ResolvedModifiers,
1847    modifier_slices: Rc<ModifierNodeSlices>,
1848    role: SemanticsRole,
1849    button_handler: Option<Rc<RefCell<dyn FnMut()>>>,
1850}
1851
1852impl Default for RuntimeNodeMetadata {
1853    fn default() -> Self {
1854        Self {
1855            modifier: Modifier::empty(),
1856            resolved_modifiers: ResolvedModifiers::default(),
1857            modifier_slices: Rc::default(),
1858            role: SemanticsRole::Unknown,
1859            button_handler: None,
1860        }
1861    }
1862}
1863
1864fn collect_runtime_metadata(
1865    applier: &mut MemoryApplier,
1866    node: &MeasuredNode,
1867) -> Result<HashMap<NodeId, RuntimeNodeMetadata>, NodeError> {
1868    let mut map = HashMap::default();
1869    collect_runtime_metadata_inner(applier, node, &mut map)?;
1870    Ok(map)
1871}
1872
1873/// Collects semantics configurations for all nodes in the measured tree using the SemanticsOwner cache.
1874fn collect_semantics_with_owner(
1875    applier: &mut MemoryApplier,
1876    node: &MeasuredNode,
1877    owner: &SemanticsOwner,
1878) -> Result<(), NodeError> {
1879    // Compute and cache configuration for this node
1880    owner.get_or_compute(node.node_id, applier);
1881
1882    // Recurse to children
1883    for child in &node.children {
1884        collect_semantics_with_owner(applier, &child.node, owner)?;
1885    }
1886    Ok(())
1887}
1888
1889fn collect_semantics_snapshot(
1890    applier: &mut MemoryApplier,
1891    node: &MeasuredNode,
1892) -> Result<HashMap<NodeId, Option<SemanticsConfiguration>>, NodeError> {
1893    let owner = SemanticsOwner::new();
1894    collect_semantics_with_owner(applier, node, &owner)?;
1895
1896    // Extract all cached configurations into a map
1897    let mut map = HashMap::default();
1898    extract_configurations_recursive(node, &owner, &mut map);
1899    Ok(map)
1900}
1901
1902fn extract_configurations_recursive(
1903    node: &MeasuredNode,
1904    owner: &SemanticsOwner,
1905    map: &mut HashMap<NodeId, Option<SemanticsConfiguration>>,
1906) {
1907    if let Some(config) = owner.configurations.borrow().get(&node.node_id) {
1908        map.insert(node.node_id, config.clone());
1909    }
1910    for child in &node.children {
1911        extract_configurations_recursive(&child.node, owner, map);
1912    }
1913}
1914
1915fn collect_runtime_metadata_inner(
1916    applier: &mut MemoryApplier,
1917    node: &MeasuredNode,
1918    map: &mut HashMap<NodeId, RuntimeNodeMetadata>,
1919) -> Result<(), NodeError> {
1920    if let Entry::Vacant(entry) = map.entry(node.node_id) {
1921        let meta = runtime_metadata_for(applier, node.node_id)?;
1922        entry.insert(meta);
1923    }
1924    for child in &node.children {
1925        collect_runtime_metadata_inner(applier, &child.node, map)?;
1926    }
1927    Ok(())
1928}
1929
1930/// Extracts text content from a LayoutNode's modifier chain.
1931///
1932/// Searches the modifier chain for a TextModifierNode and returns its text content.
1933/// This replaces the old approach of checking measure_policy.text_content().
1934///
1935/// We extract text from the semantics configuration, which TextModifierNode
1936/// populates via its SemanticsNode implementation.
1937fn extract_text_from_layout_node(layout: &LayoutNode) -> Option<String> {
1938    // Use the semantics configuration which collects data from all SemanticsNode instances
1939    // in the modifier chain, including TextModifierNode
1940    layout
1941        .semantics_configuration()
1942        .and_then(|config| config.content_description)
1943}
1944
1945fn runtime_metadata_for(
1946    applier: &mut MemoryApplier,
1947    node_id: NodeId,
1948) -> Result<RuntimeNodeMetadata, NodeError> {
1949    // Try LayoutNode (the primary modern path)
1950    // IMPORTANT: We use with_node (reference) instead of try_clone because cloning
1951    // LayoutNode creates a NEW ModifierChainHandle with NEW nodes and NEW handlers,
1952    // which would lose gesture state like press_position.
1953    if let Ok(meta) = applier.with_node::<LayoutNode, _>(node_id, |layout| {
1954        // Extract text content from the modifier chain instead of measure policy
1955        let role = if let Some(text) = extract_text_from_layout_node(layout) {
1956            SemanticsRole::Text { value: text }
1957        } else {
1958            SemanticsRole::Layout
1959        };
1960
1961        RuntimeNodeMetadata {
1962            modifier: layout.modifier.clone(),
1963            resolved_modifiers: layout.resolved_modifiers(),
1964            modifier_slices: layout.modifier_slices_snapshot(),
1965            role,
1966            button_handler: None,
1967        }
1968    }) {
1969        return Ok(meta);
1970    }
1971
1972    // Try SubcomposeLayoutNode
1973    if let Ok((modifier, resolved_modifiers)) = applier
1974        .with_node::<SubcomposeLayoutNode, _>(node_id, |node| {
1975            (node.modifier(), node.resolved_modifiers())
1976        })
1977    {
1978        // SubcomposeLayoutNode doesn't cache slices yet, so we still allocate here.
1979        // TODO: Optimize SubcomposeLayoutNode to cache slices too.
1980        let modifier_slices = Rc::new(collect_slices_from_modifier(&modifier));
1981        return Ok(RuntimeNodeMetadata {
1982            modifier,
1983            resolved_modifiers,
1984            modifier_slices,
1985            role: SemanticsRole::Subcompose,
1986            button_handler: None,
1987        });
1988    }
1989    Ok(RuntimeNodeMetadata::default())
1990}
1991
1992/// Computes semantics configuration for a node by reading from its modifier chain.
1993/// This is the primary entry point for extracting semantics from nodes, replacing
1994/// the widget-specific fallbacks with pure modifier-node traversal.
1995fn compute_semantics_for_node(
1996    applier: &mut MemoryApplier,
1997    node_id: NodeId,
1998) -> Option<SemanticsConfiguration> {
1999    // Try LayoutNode (the primary modern path)
2000    match applier.with_node::<LayoutNode, _>(node_id, |layout| {
2001        let config = layout.semantics_configuration();
2002        layout.clear_needs_semantics();
2003        config
2004    }) {
2005        Ok(config) => return config,
2006        Err(NodeError::TypeMismatch { .. }) | Err(NodeError::Missing { .. }) => {}
2007        Err(_) => return None,
2008    }
2009
2010    // Try SubcomposeLayoutNode
2011    if let Ok(modifier) =
2012        applier.with_node::<SubcomposeLayoutNode, _>(node_id, |node| node.modifier())
2013    {
2014        return collect_semantics_from_modifier(&modifier);
2015    }
2016
2017    None
2018}
2019
2020/// Builds a semantics node from measured tree data and semantics configurations.
2021/// Roles and actions are now derived entirely from SemanticsConfiguration, with
2022/// metadata consulted only for legacy widget type information.
2023fn build_semantics_node(
2024    node: &MeasuredNode,
2025    metadata: &HashMap<NodeId, RuntimeNodeMetadata>,
2026    semantics: &HashMap<NodeId, Option<SemanticsConfiguration>>,
2027) -> SemanticsNode {
2028    let info = metadata.get(&node.node_id).cloned().unwrap_or_default();
2029
2030    // Start with the widget-derived role as a fallback
2031    let mut role = info.role.clone();
2032    let mut actions = Vec::new();
2033    let mut description = None;
2034
2035    // Override with semantics configuration if present
2036    if let Some(config) = semantics.get(&node.node_id).cloned().flatten() {
2037        // Role synthesis: prefer semantics flags over widget type
2038        if config.is_button {
2039            role = SemanticsRole::Button;
2040        }
2041
2042        // Action synthesis: create click action if node is clickable
2043        if config.is_clickable {
2044            actions.push(SemanticsAction::Click {
2045                handler: SemanticsCallback::new(node.node_id),
2046            });
2047        }
2048
2049        // Description from configuration
2050        if let Some(desc) = config.content_description {
2051            description = Some(desc);
2052        }
2053    }
2054
2055    let children = node
2056        .children
2057        .iter()
2058        .map(|child| build_semantics_node(&child.node, metadata, semantics))
2059        .collect();
2060
2061    SemanticsNode::new(node.node_id, role, actions, children, description)
2062}
2063
2064fn build_layout_tree_from_metadata(
2065    node: &MeasuredNode,
2066    metadata: &HashMap<NodeId, RuntimeNodeMetadata>,
2067) -> LayoutTree {
2068    fn place(
2069        node: &MeasuredNode,
2070        origin: Point,
2071        metadata: &HashMap<NodeId, RuntimeNodeMetadata>,
2072    ) -> LayoutBox {
2073        // Include the node's own offset (from OffsetNode) in its position
2074        let top_left = Point {
2075            x: origin.x + node.offset.x,
2076            y: origin.y + node.offset.y,
2077        };
2078        let rect = GeometryRect {
2079            x: top_left.x,
2080            y: top_left.y,
2081            width: node.size.width,
2082            height: node.size.height,
2083        };
2084        let info = metadata.get(&node.node_id).cloned().unwrap_or_default();
2085        let kind = layout_kind_from_metadata(node.node_id, &info);
2086        let data = LayoutNodeData::new(
2087            info.modifier.clone(),
2088            info.resolved_modifiers,
2089            info.modifier_slices.clone(),
2090            kind,
2091        );
2092        let children = node
2093            .children
2094            .iter()
2095            .map(|child| {
2096                let child_origin = Point {
2097                    x: top_left.x + child.offset.x,
2098                    y: top_left.y + child.offset.y,
2099                };
2100                place(&child.node, child_origin, metadata)
2101            })
2102            .collect();
2103        LayoutBox::new(node.node_id, rect, node.content_offset, data, children)
2104    }
2105
2106    LayoutTree::new(place(node, Point { x: 0.0, y: 0.0 }, metadata))
2107}
2108
2109fn layout_kind_from_metadata(_node_id: NodeId, info: &RuntimeNodeMetadata) -> LayoutNodeKind {
2110    match &info.role {
2111        SemanticsRole::Layout => LayoutNodeKind::Layout,
2112        SemanticsRole::Subcompose => LayoutNodeKind::Subcompose,
2113        SemanticsRole::Text { .. } => {
2114            // Text content is now handled via TextModifierNode in the modifier chain
2115            // and collected in modifier_slices.text_content(). LayoutNodeKind should
2116            // reflect the layout policy (EmptyMeasurePolicy), not the content type.
2117            LayoutNodeKind::Layout
2118        }
2119        SemanticsRole::Spacer => LayoutNodeKind::Spacer,
2120        SemanticsRole::Button => {
2121            let handler = info
2122                .button_handler
2123                .as_ref()
2124                .cloned()
2125                .unwrap_or_else(|| Rc::new(RefCell::new(|| {})));
2126            LayoutNodeKind::Button { on_click: handler }
2127        }
2128        SemanticsRole::Unknown => LayoutNodeKind::Unknown,
2129    }
2130}
2131
2132fn subtract_padding(constraints: Constraints, padding: EdgeInsets) -> Constraints {
2133    let horizontal = padding.horizontal_sum();
2134    let vertical = padding.vertical_sum();
2135    let min_width = (constraints.min_width - horizontal).max(0.0);
2136    let mut max_width = constraints.max_width;
2137    if max_width.is_finite() {
2138        max_width = (max_width - horizontal).max(0.0);
2139    }
2140    let min_height = (constraints.min_height - vertical).max(0.0);
2141    let mut max_height = constraints.max_height;
2142    if max_height.is_finite() {
2143        max_height = (max_height - vertical).max(0.0);
2144    }
2145    normalize_constraints(Constraints {
2146        min_width,
2147        max_width,
2148        min_height,
2149        max_height,
2150    })
2151}
2152
2153#[cfg(test)]
2154pub(crate) fn align_horizontal(alignment: HorizontalAlignment, available: f32, child: f32) -> f32 {
2155    match alignment {
2156        HorizontalAlignment::Start => 0.0,
2157        HorizontalAlignment::CenterHorizontally => ((available - child) / 2.0).max(0.0),
2158        HorizontalAlignment::End => (available - child).max(0.0),
2159    }
2160}
2161
2162#[cfg(test)]
2163pub(crate) fn align_vertical(alignment: VerticalAlignment, available: f32, child: f32) -> f32 {
2164    match alignment {
2165        VerticalAlignment::Top => 0.0,
2166        VerticalAlignment::CenterVertically => ((available - child) / 2.0).max(0.0),
2167        VerticalAlignment::Bottom => (available - child).max(0.0),
2168    }
2169}
2170
2171fn resolve_dimension(
2172    base: f32,
2173    explicit: DimensionConstraint,
2174    min_override: Option<f32>,
2175    max_override: Option<f32>,
2176    min_limit: f32,
2177    max_limit: f32,
2178) -> f32 {
2179    let mut min_bound = min_limit;
2180    if let Some(min_value) = min_override {
2181        min_bound = min_bound.max(min_value);
2182    }
2183
2184    let mut max_bound = if max_limit.is_finite() {
2185        max_limit
2186    } else {
2187        max_override.unwrap_or(max_limit)
2188    };
2189    if let Some(max_value) = max_override {
2190        if max_bound.is_finite() {
2191            max_bound = max_bound.min(max_value);
2192        } else {
2193            max_bound = max_value;
2194        }
2195    }
2196    if max_bound < min_bound {
2197        max_bound = min_bound;
2198    }
2199
2200    let mut size = match explicit {
2201        DimensionConstraint::Points(points) => points,
2202        DimensionConstraint::Fraction(fraction) => {
2203            if max_limit.is_finite() {
2204                max_limit * fraction.clamp(0.0, 1.0)
2205            } else {
2206                base
2207            }
2208        }
2209        DimensionConstraint::Unspecified => base,
2210        // Intrinsic sizing is resolved at a higher level where we have access to children.
2211        // At this point we just use the base size as a fallback.
2212        DimensionConstraint::Intrinsic(_) => base,
2213    };
2214
2215    size = clamp_dimension(size, min_bound, max_bound);
2216    size = clamp_dimension(size, min_limit, max_limit);
2217    size.max(0.0)
2218}
2219
2220fn clamp_dimension(value: f32, min: f32, max: f32) -> f32 {
2221    let mut result = value.max(min);
2222    if max.is_finite() {
2223        result = result.min(max);
2224    }
2225    result
2226}
2227
2228fn normalize_constraints(mut constraints: Constraints) -> Constraints {
2229    if constraints.max_width < constraints.min_width {
2230        constraints.max_width = constraints.min_width;
2231    }
2232    if constraints.max_height < constraints.min_height {
2233        constraints.max_height = constraints.min_height;
2234    }
2235    constraints
2236}
2237
2238#[cfg(test)]
2239#[path = "tests/layout_tests.rs"]
2240mod tests;