divider.rs

 1#![allow(missing_docs)]
 2use gpui::{Hsla, IntoElement};
 3
 4use crate::prelude::*;
 5
 6enum DividerDirection {
 7    Horizontal,
 8    Vertical,
 9}
10
11/// The color of a [`Divider`].
12#[derive(Default)]
13pub enum DividerColor {
14    Border,
15    #[default]
16    BorderVariant,
17}
18
19impl DividerColor {
20    pub fn hsla(self, cx: &WindowContext) -> Hsla {
21        match self {
22            DividerColor::Border => cx.theme().colors().border,
23            DividerColor::BorderVariant => cx.theme().colors().border_variant,
24        }
25    }
26}
27
28#[derive(IntoElement)]
29pub struct Divider {
30    direction: DividerDirection,
31    color: DividerColor,
32    inset: bool,
33}
34
35impl RenderOnce for Divider {
36    fn render(self, cx: &mut WindowContext) -> impl IntoElement {
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}