Skip to main content

cranpose_ui/widgets/
row.rs

1//! Row widget implementation
2
3#![allow(non_snake_case)]
4
5use cranpose_core::NodeId;
6use cranpose_ui_layout::{LinearArrangement, VerticalAlignment};
7
8use super::layout::Layout;
9use crate::{composable, layout::policies::FlexMeasurePolicy, modifier::Modifier};
10
11/// Specification for Row layout behavior.
12#[derive(Clone, Copy, Debug, PartialEq)]
13pub struct RowSpec {
14    pub horizontal_arrangement: LinearArrangement,
15    pub vertical_alignment: VerticalAlignment,
16}
17
18impl RowSpec {
19    pub fn new() -> Self {
20        Self::default()
21    }
22
23    pub fn horizontal_arrangement(mut self, arrangement: LinearArrangement) -> Self {
24        self.horizontal_arrangement = arrangement;
25        self
26    }
27
28    pub fn vertical_alignment(mut self, alignment: VerticalAlignment) -> Self {
29        self.vertical_alignment = alignment;
30        self
31    }
32}
33
34impl Default for RowSpec {
35    fn default() -> Self {
36        Self {
37            horizontal_arrangement: LinearArrangement::Start,
38            vertical_alignment: VerticalAlignment::CenterVertically,
39        }
40    }
41}
42
43/// A layout composable that places its children in a horizontal sequence.
44///
45/// # When to use
46/// Use `Row` to arrange items side-by-side. For vertical arrangement, use [`Column`](crate::widgets::Column).
47///
48/// # Arguments
49///
50/// * `modifier` - Modifiers to apply to the row layout.
51/// * `spec` - Configuration for horizontal arrangement and vertical alignment.
52/// * `content` - The children composables to layout.
53///
54/// # Example
55///
56/// ```rust,ignore
57/// Row(
58///     Modifier::fill_max_width(),
59///     RowSpec::default().horizontal_arrangement(LinearArrangement::SpaceBetween),
60///     || {
61///         Text("Left", Modifier::empty());
62///         Text("Right", Modifier::empty());
63///     }
64/// );
65/// ```
66#[composable]
67pub fn Row<F>(modifier: Modifier, spec: RowSpec, content: F) -> NodeId
68where
69    F: FnMut() + 'static,
70{
71    let policy = FlexMeasurePolicy::row(spec.horizontal_arrangement, spec.vertical_alignment);
72    Layout(modifier, policy, content)
73}