label.rs

 1use crate::theme::theme;
 2use gpui2::elements::div;
 3use gpui2::style::StyleHelpers;
 4use gpui2::{Element, ViewContext};
 5use gpui2::{IntoElement, ParentElement};
 6
 7#[derive(Default, PartialEq, Copy, Clone)]
 8pub enum LabelColor {
 9    #[default]
10    Default,
11    Muted,
12    Created,
13    Modified,
14    Deleted,
15    Hidden,
16    Placeholder,
17}
18
19#[derive(Default, PartialEq, Copy, Clone)]
20pub enum LabelSize {
21    #[default]
22    Default,
23    Small,
24}
25
26#[derive(Element, Clone)]
27pub struct Label {
28    label: &'static str,
29    color: LabelColor,
30    size: LabelSize,
31}
32
33pub fn label(label: &'static str) -> Label {
34    Label {
35        label,
36        color: LabelColor::Default,
37        size: LabelSize::Default,
38    }
39}
40
41impl Label {
42    pub fn color(mut self, color: LabelColor) -> Self {
43        self.color = color;
44        self
45    }
46
47    pub fn size(mut self, size: LabelSize) -> Self {
48        self.size = size;
49        self
50    }
51
52    fn render<V: 'static>(&mut self, _: &mut V, cx: &mut ViewContext<V>) -> impl IntoElement<V> {
53        let theme = theme(cx);
54
55        let color = match self.color {
56            LabelColor::Default => theme.lowest.base.default.foreground,
57            LabelColor::Muted => theme.lowest.variant.default.foreground,
58            LabelColor::Created => theme.lowest.positive.default.foreground,
59            LabelColor::Modified => theme.lowest.warning.default.foreground,
60            LabelColor::Deleted => theme.lowest.negative.default.foreground,
61            LabelColor::Hidden => theme.lowest.variant.default.foreground,
62            LabelColor::Placeholder => theme.lowest.base.disabled.foreground,
63        };
64
65        let mut div = div();
66
67        if self.size == LabelSize::Small {
68            div = div.text_xs();
69        } else {
70            div = div.text_sm();
71        }
72
73        div.text_color(color).child(self.label.clone())
74    }
75}