cranpose_ui/widgets/
box_widget.rs1use cranpose_core::NodeId;
4use cranpose_ui_layout::Alignment;
5
6use super::layout::{LayoutEnvironment, compose_layout};
7use crate::{composable, layout::policies::BoxMeasurePolicy, modifier::Modifier};
8
9#[derive(Clone, Copy, Debug, PartialEq)]
11pub struct BoxSpec {
12 pub content_alignment: Alignment,
14 pub propagate_min_constraints: bool,
16}
17
18impl BoxSpec {
19 pub fn new() -> Self {
21 Self::default()
22 }
23
24 pub fn content_alignment(mut self, alignment: Alignment) -> Self {
26 self.content_alignment = alignment;
27 self
28 }
29
30 pub fn propagate_min_constraints(mut self, propagate: bool) -> Self {
32 self.propagate_min_constraints = propagate;
33 self
34 }
35}
36
37impl Default for BoxSpec {
38 fn default() -> Self {
39 Self {
40 content_alignment: Alignment::TOP_START,
41 propagate_min_constraints: false,
42 }
43 }
44}
45
46#[composable]
68pub fn Box<F>(modifier: Modifier, spec: BoxSpec, content: F) -> NodeId
69where
70 F: FnMut() + 'static,
71{
72 let environment = LayoutEnvironment::current();
73 let mut policy = BoxMeasurePolicy::new(spec.content_alignment, spec.propagate_min_constraints);
74 policy.layout_direction = environment.direction;
75 compose_layout(modifier, policy, environment, content)
76}