divider.rs

 1use gpui::{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    fn render(self, cx: &mut WindowContext) -> impl IntoElement {
35        div()
36            .map(|this| match self.direction {
37                DividerDirection::Horizontal => {
38                    this.h_px().w_full().when(self.inset, |this| this.mx_1p5())
39                }
40                DividerDirection::Vertical => {
41                    this.w_px().h_full().when(self.inset, |this| this.my_1p5())
42                }
43            })
44            .bg(self.color.hsla(cx))
45    }
46}
47
48impl Divider {
49    pub fn horizontal() -> Self {
50        Self {
51            direction: DividerDirection::Horizontal,
52            color: DividerColor::default(),
53            inset: false,
54        }
55    }
56
57    pub fn vertical() -> Self {
58        Self {
59            direction: DividerDirection::Vertical,
60            color: DividerColor::default(),
61            inset: false,
62        }
63    }
64
65    pub fn inset(mut self) -> Self {
66        self.inset = true;
67        self
68    }
69
70    pub fn color(mut self, color: DividerColor) -> Self {
71        self.color = color;
72        self
73    }
74}