Skip to main content

cranpose_ui/widgets/
column.rs

1//! Column widget implementation
2
3use cranpose_core::NodeId;
4use cranpose_ui_layout::{HorizontalAlignment, LinearArrangement};
5
6use super::layout::compose_layout;
7use crate::{composable, layout::policies::FlexMeasurePolicy, modifier::Modifier};
8
9/// Specification for Column layout behavior.
10#[derive(Clone, Copy, Debug, PartialEq)]
11pub struct ColumnSpec {
12    pub vertical_arrangement: LinearArrangement,
13    pub horizontal_alignment: HorizontalAlignment,
14}
15
16impl ColumnSpec {
17    pub fn new() -> Self {
18        Self::default()
19    }
20
21    pub fn vertical_arrangement(mut self, arrangement: LinearArrangement) -> Self {
22        self.vertical_arrangement = arrangement;
23        self
24    }
25
26    pub fn horizontal_alignment(mut self, alignment: HorizontalAlignment) -> Self {
27        self.horizontal_alignment = alignment;
28        self
29    }
30}
31
32impl Default for ColumnSpec {
33    fn default() -> Self {
34        Self {
35            vertical_arrangement: LinearArrangement::Start,
36            horizontal_alignment: HorizontalAlignment::Start,
37        }
38    }
39}
40
41/// Places children from top to bottom. `spec` sets vertical space and horizontal alignment.
42/// Use [`Row`](crate::Row) for a horizontal sequence.
43///
44/// # Example
45///
46/// ```rust
47/// use cranpose_ui::*;
48///
49/// #[composable]
50/// fn Details() {
51///     Column(
52///         Modifier::empty().padding(16.0),
53///         ColumnSpec::default().vertical_arrangement(LinearArrangement::spaced_by(8.0)),
54///         || {
55///             Text("Title", Modifier::empty(), TextStyle::default());
56///             Text("Subtitle", Modifier::empty(), TextStyle::default());
57///         },
58///     );
59/// }
60/// ```
61#[composable]
62pub fn Column<F>(modifier: Modifier, spec: ColumnSpec, content: F) -> NodeId
63where
64    F: FnMut() + 'static,
65{
66    let density = crate::density::density();
67    let policy = FlexMeasurePolicy::column(
68        spec.vertical_arrangement,
69        spec.horizontal_alignment,
70        density.density(),
71    );
72    compose_layout(modifier, policy, density, content)
73}