Skip to main content

liora_components/
col.rs

1use crate::Row;
2use gpui::{App, Component, IntoElement, RenderOnce, Window, prelude::*};
3
4pub struct Col {
5    span: u8,
6    children: Vec<gpui::AnyElement>,
7}
8
9impl Col {
10    pub fn new(span: u8) -> Self {
11        Self {
12            span: span.min(24),
13            children: vec![],
14        }
15    }
16    pub fn child(mut self, child: impl IntoElement) -> Self {
17        self.children.push(child.into_any_element());
18        self
19    }
20    /// Add a nested row.
21    pub fn row(mut self, row: Row) -> Self {
22        self.children.push(row.into_any_element());
23        self
24    }
25    /// Add multiple nested rows.
26    pub fn rows(mut self, rows: Vec<Row>) -> Self {
27        self.children
28            .extend(rows.into_iter().map(|r| r.into_any_element()));
29        self
30    }
31}
32
33impl RenderOnce for Col {
34    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
35        let span = self.span as f32 / 24.0;
36        gpui::div()
37            .flex_none()
38            .w(gpui::relative(span))
39            .children(self.children)
40    }
41}
42
43impl IntoElement for Col {
44    type Element = Component<Self>;
45    fn into_element(self) -> Self::Element {
46        Component::new(self)
47    }
48}