Skip to main content

fc_ui/capabilities/effects/
spotlight.rs

1use gpui::{prelude::FluentBuilder as _, *};
2
3pub struct SpotlightState {
4    mouse_pos: Option<Point<Pixels>>,
5    bounds: Option<Bounds<Pixels>>,
6}
7
8impl SpotlightState {
9    pub fn new() -> Self {
10        Self {
11            mouse_pos: None,
12            bounds: None,
13        }
14    }
15}
16
17impl Default for SpotlightState {
18    fn default() -> Self {
19        Self::new()
20    }
21}
22
23#[derive(IntoElement)]
24pub struct Spotlight {
25    id: ElementId,
26    state: Entity<SpotlightState>,
27    color: Option<Hsla>,
28    spot_size: Pixels,
29    intensity: f32,
30    children: Vec<AnyElement>,
31    style: StyleRefinement,
32}
33
34impl Spotlight {
35    pub fn new(id: impl Into<ElementId>, state: Entity<SpotlightState>) -> Self {
36        Self {
37            id: id.into(),
38            state,
39            color: None,
40            spot_size: px(300.0),
41            intensity: 0.15,
42            children: Vec::new(),
43            style: StyleRefinement::default(),
44        }
45    }
46
47    pub fn color(mut self, color: Hsla) -> Self {
48        self.color = Some(color);
49        self
50    }
51
52    pub fn size(mut self, size: Pixels) -> Self {
53        self.spot_size = size;
54        self
55    }
56
57    pub fn intensity(mut self, intensity: f32) -> Self {
58        self.intensity = intensity.clamp(0.0, 1.0);
59        self
60    }
61}
62
63impl Styled for Spotlight {
64    fn style(&mut self) -> &mut StyleRefinement {
65        &mut self.style
66    }
67}
68
69impl ParentElement for Spotlight {
70    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
71        self.children.extend(elements);
72    }
73}
74
75impl RenderOnce for Spotlight {
76    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
77        let spot_color = self.color.unwrap_or(hsla(0.6, 0.8, 0.7, 1.0));
78        let spot_size = self.spot_size;
79        let intensity = self.intensity;
80        let mouse_pos = self.state.read(cx).mouse_pos;
81        let half_size = px(f32::from(spot_size) / 2.0);
82        let user_style = self.style;
83
84        let state_bounds = self.state.clone();
85        let state_for_move = self.state.clone();
86        let state_for_hover = self.state.clone();
87
88        let mut container = div()
89            .id(self.id)
90            .relative()
91            .overflow_hidden()
92            .child(
93                canvas(
94                    move |bounds, _window, cx| {
95                        state_bounds.update(cx, |s, _| s.bounds = Some(bounds));
96                    },
97                    |_bounds, _, _window, _cx| {},
98                )
99                .absolute()
100                .inset_0()
101                .size_full(),
102            )
103            .on_mouse_move(move |event: &MouseMoveEvent, _, cx| {
104                state_for_move.update(cx, |s, cx| {
105                    if let Some(bounds) = s.bounds {
106                        s.mouse_pos = Some(point(
107                            event.position.x - bounds.left(),
108                            event.position.y - bounds.top(),
109                        ));
110                        cx.notify();
111                    }
112                });
113            })
114            .on_hover(move |hovered: &bool, _window, cx| {
115                if !*hovered {
116                    state_for_hover.update(cx, |s, cx| {
117                        s.mouse_pos = None;
118                        cx.notify();
119                    });
120                }
121            })
122            .map(|mut el| {
123                el.style().refine(&user_style);
124                el
125            });
126
127        for child in self.children {
128            container = container.child(child);
129        }
130
131        if let Some(pos) = mouse_pos {
132            let glow_color = Hsla {
133                a: intensity,
134                ..spot_color
135            };
136
137            let glow = div()
138                .absolute()
139                .left(pos.x - half_size)
140                .top(pos.y - half_size)
141                .w(spot_size)
142                .h(spot_size)
143                .rounded(spot_size)
144                .bg(glow_color);
145
146            container = container.child(glow);
147        }
148
149        container
150    }
151}