Skip to main content

cranpose_ui/widgets/
row.rs

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