divider.rs

 1use gpui::{Div, Hsla, IntoElement};
 2
 3use crate::prelude::*;
 4
 5enum DividerDirection {
 6    Horizontal,
 7    Vertical,
 8}
 9
10#[derive(Default)]
11pub enum DividerColor {
12    Border,
13    #[default]
14    BorderVariant,
15}
16
17impl DividerColor {
18    pub fn hsla(self, cx: &WindowContext) -> Hsla {
19        match self {
20            DividerColor::Border => cx.theme().colors().border,
21            DividerColor::BorderVariant => cx.theme().colors().border_variant,
22        }
23    }
24}
25
26#[derive(IntoElement)]
27pub struct Divider {
28    direction: DividerDirection,
29    color: DividerColor,
30    inset: bool,
31}
32
33impl RenderOnce for Divider {
34    type Rendered = Div;
35
36    fn render(self, cx: &mut WindowContext) -> Self::Rendered {
37        div()
38            .map(|this| match self.direction {
39                DividerDirection::Horizontal => {
40                    this.h_px().w_full().when(self.inset, |this| this.mx_1p5())
41                }
42                DividerDirection::Vertical => {
43                    this.w_px().h_full().when(self.inset, |this| this.my_1p5())
44                }
45            })
46            .bg(self.color.hsla(cx))
47    }
48}
49
50impl Divider {
51    pub fn horizontal() -> Self {
52        Self {
53            direction: DividerDirection::Horizontal,
54            color: DividerColor::default(),
55            inset: false,
56        }
57    }
58
59    pub fn vertical() -> Self {
60        Self {
61            direction: DividerDirection::Vertical,
62            color: DividerColor::default(),
63            inset: false,
64        }
65    }
66
67    pub fn inset(mut self) -> Self {
68        self.inset = true;
69        self
70    }
71
72    pub fn color(mut self, color: DividerColor) -> Self {
73        self.color = color;
74        self
75    }
76}