Skip to main content

cranpose_ui/widgets/
layout.rs

1//! Generic Layout widget and SubcomposeLayout
2
3#![allow(non_snake_case)]
4
5use super::nodes::LayoutNode;
6use super::scopes::BoxWithConstraintsScopeImpl;
7use crate::composable;
8use crate::modifier::Modifier;
9use crate::subcompose_layout::{
10    Constraints, MeasurePolicy as SubcomposeMeasurePolicy, MeasureResult, SubcomposeLayoutNode,
11    SubcomposeLayoutScope, SubcomposeMeasureScope, SubcomposeMeasureScopeImpl,
12};
13use cranpose_core::{NodeId, SlotId};
14use cranpose_ui_graphics::Size;
15use cranpose_ui_layout::{MeasurePolicy, Placement};
16use std::cell::RefCell;
17use std::rc::Rc;
18
19struct RetainedMeasurePolicy<P> {
20    value: P,
21    policy: Rc<dyn MeasurePolicy>,
22}
23
24#[composable]
25pub fn Layout<F, P>(modifier: Modifier, measure_policy: P, mut content: F) -> NodeId
26where
27    F: FnMut() + 'static,
28    P: MeasurePolicy + Clone + PartialEq + 'static,
29{
30    let policy_holder = cranpose_core::remember({
31        let measure_policy = measure_policy.clone();
32        move || {
33            Rc::new(RefCell::new(RetainedMeasurePolicy {
34                value: measure_policy.clone(),
35                policy: Rc::new(measure_policy),
36            }))
37        }
38    })
39    .with(|holder| holder.clone());
40    let policy = {
41        let mut holder = policy_holder.borrow_mut();
42        if holder.value != measure_policy {
43            holder.value = measure_policy.clone();
44            holder.policy = Rc::new(measure_policy);
45        }
46        Rc::clone(&holder.policy)
47    };
48    let modifier_for_reset = modifier.clone();
49    let policy_for_reset = Rc::clone(&policy);
50    let id = cranpose_core::with_current_composer(|composer| {
51        composer.emit_recyclable_node(
52            || LayoutNode::new(modifier.clone(), Rc::clone(&policy)),
53            move |node| {
54                *node = LayoutNode::new(modifier_for_reset.clone(), Rc::clone(&policy_for_reset));
55            },
56        )
57    });
58    if let Err(err) = cranpose_core::with_node_mut(id, |node: &mut LayoutNode| {
59        node.set_modifier(modifier.clone());
60        node.set_measure_policy(Rc::clone(&policy));
61    }) {
62        debug_assert!(false, "failed to update Layout node: {err}");
63    }
64    cranpose_core::push_parent(id);
65    content();
66    cranpose_core::pop_parent();
67    id
68}
69
70#[composable]
71pub fn SubcomposeLayout(
72    modifier: Modifier,
73    measure_policy: impl for<'scope> Fn(&mut SubcomposeMeasureScopeImpl<'scope>, Constraints) -> MeasureResult
74        + 'static,
75) -> NodeId {
76    cranpose_core::debug_label_current_scope("SubcomposeLayout");
77    let policy_cell =
78        cranpose_core::remember(|| Rc::new(RefCell::new(None::<Rc<SubcomposeMeasurePolicy>>)))
79            .with(|cell| cell.clone());
80    let current_policy: Rc<SubcomposeMeasurePolicy> = Rc::new(measure_policy);
81    let policy_captures_changed = {
82        let mut policy_cell_ref = policy_cell.borrow_mut();
83        let changed = policy_cell_ref
84            .as_ref()
85            .is_none_or(|previous| !Rc::ptr_eq(previous, &current_policy));
86        *policy_cell_ref = Some(current_policy);
87        changed
88    };
89    let policy: Rc<SubcomposeMeasurePolicy> = cranpose_core::remember(move || {
90        let policy_cell = policy_cell.clone();
91        let policy: Rc<SubcomposeMeasurePolicy> =
92            Rc::new(
93                move |scope, constraints| match policy_cell.borrow().as_ref().cloned() {
94                    Some(current) => current(scope, constraints),
95                    None => empty_subcompose_measure_result(constraints),
96                },
97            );
98        policy
99    })
100    .with(|policy| policy.clone());
101    let id = cranpose_core::with_current_composer(|composer| {
102        composer.emit_node(|| SubcomposeLayoutNode::new(modifier.clone(), Rc::clone(&policy)))
103    });
104    if let Err(err) = cranpose_core::with_node_mut(id, |node: &mut SubcomposeLayoutNode| {
105        node.set_modifier(modifier.clone());
106        node.set_measure_policy(Rc::clone(&policy));
107        if policy_captures_changed {
108            node.request_measure_recompose();
109        }
110    }) {
111        debug_assert!(false, "failed to update SubcomposeLayout node: {err}");
112    }
113    id
114}
115
116fn empty_subcompose_measure_result(constraints: Constraints) -> MeasureResult {
117    let (width, height) = constraints.constrain(0.0, 0.0);
118    MeasureResult::new(Size { width, height }, Vec::new())
119}
120
121#[composable(no_skip)]
122pub fn BoxWithConstraints<F>(modifier: Modifier, content: F) -> NodeId
123where
124    F: FnMut(BoxWithConstraintsScopeImpl) + 'static,
125{
126    let content_ref: Rc<RefCell<F>> = Rc::new(RefCell::new(content));
127    SubcomposeLayout(modifier, move |scope, constraints| {
128        let scope_impl = BoxWithConstraintsScopeImpl::new(constraints);
129        let scope_for_content = scope_impl;
130        let measurables = {
131            let content_ref = Rc::clone(&content_ref);
132            scope.subcompose(SlotId::new(0), move || {
133                cranpose_core::debug_label_current_scope("BoxWithConstraints.slot(0)");
134                let mut content = content_ref.borrow_mut();
135                content(scope_for_content);
136            })
137        };
138        let child_constraints = Constraints {
139            min_width: 0.0,
140            max_width: constraints.max_width,
141            min_height: 0.0,
142            max_height: constraints.max_height,
143        };
144
145        let mut width = 0.0_f32;
146        let mut height = 0.0_f32;
147        let mut placements = Vec::with_capacity(measurables.len());
148
149        for measurable in measurables {
150            let placeable = scope.measure(measurable, child_constraints);
151            width = width.max(placeable.width());
152            height = height.max(placeable.height());
153            placeable.place(0.0, 0.0);
154            placements.push(Placement::new(placeable.node_id(), 0.0, 0.0, 0));
155        }
156
157        width = width.clamp(constraints.min_width, constraints.max_width);
158        height = height.clamp(constraints.min_height, constraints.max_height);
159        scope.layout(width, height, placements)
160    })
161}
162
163#[cfg(test)]
164mod tests {
165    use super::*;
166    use cranpose_core::{location_key, Composition, MemoryApplier, MutableState};
167    use std::cell::Cell;
168
169    #[test]
170    fn layout_recomposes_when_content_reads_state() {
171        let _app_context = crate::render_state::app_context_test_scope();
172        thread_local! {
173            static INVOCATIONS: Cell<usize> = const { Cell::new(0) };
174        }
175
176        let mut composition = Composition::new(MemoryApplier::new());
177        let runtime = composition.runtime_handle();
178        let state = MutableState::with_runtime(0_i32, runtime);
179
180        composition
181            .render(location_key(file!(), line!(), column!()), {
182                let observed_state = state;
183                move || {
184                    Layout(
185                        Modifier::empty(),
186                        crate::layout::policies::EmptyMeasurePolicy,
187                        {
188                            let observed_state = observed_state;
189                            move || {
190                                let _ = observed_state.value();
191                                INVOCATIONS.with(|calls| calls.set(calls.get() + 1));
192                            }
193                        },
194                    );
195                }
196            })
197            .expect("initial layout render");
198
199        INVOCATIONS.with(|calls| assert_eq!(calls.get(), 1));
200
201        state.set_value(1);
202        composition
203            .process_invalid_scopes()
204            .expect("layout content recomposition");
205
206        INVOCATIONS.with(|calls| assert_eq!(calls.get(), 2));
207    }
208
209    #[test]
210    fn subcompose_missing_policy_cell_measures_empty_layout() {
211        let result = empty_subcompose_measure_result(Constraints {
212            min_width: 12.0,
213            max_width: 120.0,
214            min_height: 8.0,
215            max_height: 90.0,
216        });
217
218        assert_eq!(result.size.width, 12.0);
219        assert_eq!(result.size.height, 8.0);
220        assert!(result.placements.is_empty());
221    }
222}