Skip to main content

fc_ui/capabilities/effects/
pulse_indicator.rs

1use crate::capabilities::motion::animations::easings;
2use gpui::*;
3use std::time::Duration;
4
5#[derive(IntoElement)]
6pub struct PulseIndicator {
7    id: ElementId,
8    base: Stateful<Div>,
9    color: Hsla,
10    dot_size: Pixels,
11    speed: Duration,
12}
13
14impl PulseIndicator {
15    pub fn new(id: impl Into<ElementId>) -> Self {
16        let id = id.into();
17        Self {
18            base: div().id(id.clone()),
19            id,
20            color: hsla(142.0 / 360.0, 0.71, 0.45, 1.0),
21            dot_size: px(8.0),
22            speed: Duration::from_secs(2),
23        }
24    }
25
26    pub fn color(mut self, color: Hsla) -> Self {
27        self.color = color;
28        self
29    }
30
31    pub fn size(mut self, size: Pixels) -> Self {
32        self.dot_size = size;
33        self
34    }
35
36    pub fn speed(mut self, speed: Duration) -> Self {
37        self.speed = speed;
38        self
39    }
40}
41
42impl RenderOnce for PulseIndicator {
43    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
44        let color = self.color;
45        let dot = self.dot_size;
46        let ring_max = dot * 2.5;
47        let speed = self.speed;
48
49        self.base
50            .flex()
51            .items_center()
52            .justify_center()
53            .size(ring_max)
54            .child(
55                div()
56                    .absolute()
57                    .rounded_full()
58                    .bg(color.opacity(0.6))
59                    .size(dot)
60                    .with_animation(
61                        ElementId::Name(format!("{}-ring", self.id).into()),
62                        Animation::new(speed)
63                            .repeat()
64                            .with_easing(easings::ease_out_cubic),
65                        move |this, delta| {
66                            let current_size = dot + (ring_max - dot) * delta;
67                            let current_opacity = 0.6 * (1.0 - delta);
68                            this.size(current_size).opacity(current_opacity)
69                        },
70                    ),
71            )
72            .child(div().absolute().rounded_full().bg(color).size(dot))
73    }
74}
75
76impl Styled for PulseIndicator {
77    fn style(&mut self) -> &mut StyleRefinement {
78        self.base.style()
79    }
80}
81
82impl InteractiveElement for PulseIndicator {
83    fn interactivity(&mut self) -> &mut Interactivity {
84        self.base.interactivity()
85    }
86}
87
88impl StatefulInteractiveElement for PulseIndicator {}
89
90impl ParentElement for PulseIndicator {
91    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
92        self.base.extend(elements)
93    }
94}