Skip to main content

liora_components/
splitter.rs

1use gpui::{App, Component, IntoElement, RenderOnce, Window, prelude::*, px};
2
3pub struct Splitter {
4    left: Option<gpui::AnyElement>,
5    right: Option<gpui::AnyElement>,
6    height: Option<gpui::Pixels>,
7    bordered: bool,
8}
9
10impl Splitter {
11    pub fn new() -> Self {
12        Self {
13            left: None,
14            right: None,
15            height: None,
16            bordered: false,
17        }
18    }
19    pub fn left(mut self, el: impl IntoElement) -> Self {
20        self.left = Some(el.into_any_element());
21        self
22    }
23    pub fn right(mut self, el: impl IntoElement) -> Self {
24        self.right = Some(el.into_any_element());
25        self
26    }
27
28    pub fn height(mut self, height: impl Into<gpui::Pixels>) -> Self {
29        self.height = Some(height.into());
30        self
31    }
32
33    pub fn height_md(self) -> Self {
34        self.height(px(200.0))
35    }
36
37    pub fn bordered(mut self) -> Self {
38        self.bordered = true;
39        self
40    }
41}
42
43impl RenderOnce for Splitter {
44    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
45        let theme = &cx.global::<liora_core::Config>().theme;
46        let left = self.left.unwrap_or_else(|| gpui::div().into_any_element());
47        let right = self.right.unwrap_or_else(|| gpui::div().into_any_element());
48
49        gpui::div()
50            .flex()
51            .flex_row()
52            .size_full()
53            .when_some(self.height, |s, height| s.h(height))
54            .when(self.bordered, |s| {
55                s.border_1()
56                    .border_color(theme.neutral.border)
57                    .rounded(px(theme.radius.sm))
58            })
59            .child(gpui::div().flex_none().w(px(300.0)).h_full().child(left))
60            .child(
61                gpui::div()
62                    .flex_none()
63                    .w(px(4.0))
64                    .h_full()
65                    .bg(theme.neutral.border),
66            )
67            .child(gpui::div().flex_1().h_full().child(right))
68    }
69}
70
71impl IntoElement for Splitter {
72    type Element = Component<Self>;
73    fn into_element(self) -> Self::Element {
74        Component::new(self)
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[test]
83    fn splitter_presentation_helpers_track_state() {
84        let splitter = Splitter::new().height_md().bordered();
85
86        assert_eq!(splitter.height, Some(px(200.0)));
87        assert!(splitter.bordered);
88    }
89}