indicator.rs

 1use gpui::Transformation;
 2
 3use crate::{prelude::*, AnyIcon};
 4
 5#[derive(Default)]
 6pub enum IndicatorStyle {
 7    #[default]
 8    Dot,
 9    Bar,
10    Icon(AnyIcon),
11}
12
13#[derive(IntoElement)]
14pub struct Indicator {
15    style: IndicatorStyle,
16    pub color: Color,
17}
18
19impl Indicator {
20    pub fn dot() -> Self {
21        Self {
22            style: IndicatorStyle::Dot,
23            color: Color::Default,
24        }
25    }
26
27    pub fn bar() -> Self {
28        Self {
29            style: IndicatorStyle::Dot,
30            color: Color::Default,
31        }
32    }
33
34    pub fn icon(icon: impl Into<AnyIcon>) -> Self {
35        Self {
36            style: IndicatorStyle::Icon(icon.into()),
37            color: Color::Default,
38        }
39    }
40
41    pub fn color(mut self, color: Color) -> Self {
42        self.color = color;
43        self
44    }
45}
46
47impl RenderOnce for Indicator {
48    fn render(self, cx: &mut WindowContext) -> impl IntoElement {
49        let container = div().flex_none();
50
51        match self.style {
52            IndicatorStyle::Icon(icon) => container
53                .child(icon.map(|icon| icon.custom_size(rems_from_px(8.)).color(self.color))),
54            IndicatorStyle::Dot => container
55                .w_1p5()
56                .h_1p5()
57                .rounded_full()
58                .bg(self.color.color(cx)),
59            IndicatorStyle::Bar => container
60                .w_full()
61                .h_1p5()
62                .rounded_t_md()
63                .bg(self.color.color(cx)),
64        }
65    }
66}
67
68#[derive(IntoElement)]
69pub struct IndicatorIcon {
70    icon: Icon,
71    transformation: Option<Transformation>,
72}
73
74impl IndicatorIcon {
75    pub fn new(icon: Icon) -> Self {
76        Self {
77            icon,
78            transformation: None,
79        }
80    }
81
82    pub fn transformation(mut self, transformation: Transformation) -> Self {
83        self.transformation = Some(transformation);
84        self
85    }
86}
87
88impl RenderOnce for IndicatorIcon {
89    fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
90        self.icon
91            .custom_size(rems_from_px(8.))
92            .when_some(self.transformation, |this, transformation| {
93                this.transform(transformation)
94            })
95    }
96}