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::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::CenterVertically,
37        }
38    }
39}
40
41/// A layout composable that places its children in a horizontal sequence.
42///
43/// # When to use
44/// Use `Row` to arrange items side-by-side. For vertical arrangement, use [`Column`](crate::widgets::Column).
45///
46/// # Arguments
47///
48/// * `modifier` - Modifiers to apply to the row layout.
49/// * `spec` - Configuration for horizontal arrangement and vertical alignment.
50/// * `content` - The children composables to layout.
51///
52/// # Example
53///
54/// ```rust,ignore
55/// Row(
56///     Modifier::fill_max_width(),
57///     RowSpec::default().horizontal_arrangement(LinearArrangement::SpaceBetween),
58///     || {
59///         Text("Left", Modifier::empty());
60///         Text("Right", Modifier::empty());
61///     }
62/// );
63/// ```
64#[composable]
65pub fn Row<F>(modifier: Modifier, spec: RowSpec, content: F) -> NodeId
66where
67    F: FnMut() + 'static,
68{
69    let policy = FlexMeasurePolicy::row(spec.horizontal_arrangement, spec.vertical_alignment);
70    Layout(modifier, policy, content)
71}