Skip to main content

liora_components/
row.rs

1use crate::Col;
2use gpui::{App, Component, IntoElement, RenderOnce, Window, prelude::*};
3
4pub struct Row {
5    justify: Option<RowJustify>,
6    align: Option<RowAlign>,
7    children: Vec<gpui::AnyElement>,
8}
9
10#[derive(Clone, Copy)]
11pub enum RowJustify {
12    Start,
13    Center,
14    End,
15    SpaceBetween,
16    SpaceAround,
17}
18#[derive(Clone, Copy)]
19pub enum RowAlign {
20    Top,
21    Middle,
22    Bottom,
23}
24
25impl Row {
26    pub fn new() -> Self {
27        Self {
28            justify: None,
29            align: None,
30            children: vec![],
31        }
32    }
33    pub fn justify(mut self, j: RowJustify) -> Self {
34        self.justify = Some(j);
35        self
36    }
37    pub fn align(mut self, a: RowAlign) -> Self {
38        self.align = Some(a);
39        self
40    }
41    pub fn child(mut self, child: impl IntoElement) -> Self {
42        self.children.push(child.into_any_element());
43        self
44    }
45    /// Shorthand for adding a column.
46    pub fn column(mut self, col: Col) -> Self {
47        self.children.push(col.into_any_element());
48        self
49    }
50}
51
52impl RenderOnce for Row {
53    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
54        let mut row = gpui::div().flex().flex_row().flex_wrap().gap_2();
55        match self.justify {
56            Some(RowJustify::Start) => {
57                row = row.justify_start();
58            }
59            Some(RowJustify::Center) => {
60                row = row.justify_center();
61            }
62            Some(RowJustify::End) => {
63                row = row.justify_end();
64            }
65            Some(RowJustify::SpaceBetween) => {
66                row = row.justify_between();
67            }
68            Some(RowJustify::SpaceAround) => {
69                row = row.justify_around();
70            }
71            None => {}
72        }
73        match self.align {
74            Some(RowAlign::Top) => {
75                row = row.items_start();
76            }
77            Some(RowAlign::Middle) => {
78                row = row.items_center();
79            }
80            Some(RowAlign::Bottom) => {
81                row = row.items_end();
82            }
83            None => {}
84        }
85        row.children(self.children)
86    }
87}
88
89impl IntoElement for Row {
90    type Element = Component<Self>;
91    fn into_element(self) -> Self::Element {
92        Component::new(self)
93    }
94}