divider.rs

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