ailloli_ui_widgets/layout/
row.rs

1//! Declarative horizontal flex container.
2
3use super::flex::{layout_insets_only, layout_sizing_only, FlexWidget};
4use super::layout_ext::{finish_view_sized, FlexLayoutExt};
5use super::style_wrappers::apply_margin_padding;
6use ailloli_ui_core::style::{FlexStyle, LayoutSizeHint, LayoutStyle};
7use ailloli_ui_runtime::component::{IntoView, View};
8
9/// Horizontal flex container with ordered retained children.
10///
11/// Defaults to automatic sizing, zero gap, start alignment, and no children.
12///
13/// # Examples
14///
15/// ```
16/// use ailloli_ui_widgets::{layout::Row, text::Text};
17/// let row: Row<()> = Row::new().gap(4.0).child(Text::new("left")).child(Text::new("right"));
18/// let _ = row;
19/// ```
20pub struct Row<A = ()> {
21    /// Outer logical sizing policy.
22    pub(crate) layout: LayoutStyle,
23    /// Horizontal direction, gap, and cross-axis alignment.
24    pub(crate) flex: FlexStyle,
25    /// Parent-flex participation metadata.
26    pub(crate) flex_item: ailloli_ui_core::style::FlexItemStyle,
27    /// Ordered retained children laid out from left to right.
28    children: Vec<View<A>>,
29}
30
31crate::impl_layout_builders!(Row);
32crate::impl_flex_builders!(Row);
33
34/// Creates the same empty row as [`Row::new`].
35impl<A: 'static> Default for Row<A> {
36    fn default() -> Self {
37        Self::new()
38    }
39}
40
41impl<A: 'static> Row<A> {
42    /// Creates an empty horizontal flex container.
43    ///
44    /// # Examples
45    ///
46    /// ```
47    /// use ailloli_ui_widgets::layout::Row;
48    /// let row: Row<()> = Row::new();
49    /// let _ = row;
50    /// ```
51    pub fn new() -> Self {
52        Self {
53            layout: LayoutStyle::default(),
54            flex: FlexStyle::row(),
55            flex_item: ailloli_ui_core::style::FlexItemStyle::default(),
56            children: Vec::new(),
57        }
58    }
59
60    /// Appends one child after existing children.
61    ///
62    /// # Examples
63    ///
64    /// ```
65    /// use ailloli_ui_widgets::{layout::Row, text::Text};
66    /// let row: Row<()> = Row::new().child(Text::new("cell"));
67    /// let _ = row;
68    /// ```
69    pub fn child(mut self, child: impl IntoView<A>) -> Self {
70        self.children.push(child.into_view());
71        self
72    }
73}
74
75/// Converts declarative flex state and children into retained layout wrappers.
76impl<A: 'static> IntoView<A> for Row<A> {
77    fn into_view(self) -> View<A> {
78        let items: Vec<_> = self.children.iter().map(|c| c.flex_item).collect();
79        let child_hints: Vec<_> = self.children.iter().map(|c| c.size_hint).collect();
80        let widget = FlexWidget {
81            layout: layout_sizing_only(self.layout),
82            flex: self.flex,
83            items,
84            child_hints,
85        };
86        let content = View::node(widget, self.children);
87        let content = apply_margin_padding(content, layout_insets_only(self.layout));
88        finish_view_sized(
89            content,
90            self.flex_item,
91            LayoutSizeHint::from_layout(self.layout),
92        )
93    }
94}