fc_ui/capabilities/effects/
meteors.rs1use gpui::prelude::FluentBuilder as _;
4use gpui::*;
5use std::time::Duration;
6
7pub struct MeteorState {
8 version: usize,
9}
10
11impl MeteorState {
12 pub fn new(cx: &mut Context<Self>) -> Self {
13 cx.spawn(async move |this, cx| loop {
14 cx.background_executor()
15 .timer(Duration::from_millis(100))
16 .await;
17 let result = this.update(cx, |state, cx| {
18 state.version = state.version.wrapping_add(1);
19 cx.notify();
20 });
21 if result.is_err() {
22 break;
23 }
24 })
25 .detach();
26
27 Self { version: 0 }
28 }
29}
30
31fn meteor_hash(seed: u32) -> f32 {
32 let mut h = seed;
33 h ^= h >> 16;
34 h = h.wrapping_mul(0x45d9f3b);
35 h ^= h >> 16;
36 h = h.wrapping_mul(0x45d9f3b);
37 h ^= h >> 16;
38 (h & 0xFFFF) as f32 / 65535.0
39}
40
41#[derive(IntoElement)]
42pub struct Meteors {
43 id: ElementId,
44 state: Entity<MeteorState>,
45 count: usize,
46 speed: f32,
47 angle: f32,
48 color: Option<Hsla>,
49 trail_length: Pixels,
50 children: Vec<AnyElement>,
51 style: StyleRefinement,
52}
53
54impl Meteors {
55 pub fn new(id: impl Into<ElementId>, state: Entity<MeteorState>) -> Self {
56 Self {
57 id: id.into(),
58 state,
59 count: 8,
60 speed: 1.0,
61 angle: 215.0,
62 color: None,
63 trail_length: px(120.0),
64 children: Vec::new(),
65 style: StyleRefinement::default(),
66 }
67 }
68
69 pub fn count(mut self, count: usize) -> Self {
70 self.count = count.clamp(1, 50);
71 self
72 }
73
74 pub fn speed(mut self, speed: f32) -> Self {
75 self.speed = speed.max(0.1);
76 self
77 }
78
79 pub fn angle(mut self, angle: f32) -> Self {
80 self.angle = angle;
81 self
82 }
83
84 pub fn color(mut self, color: Hsla) -> Self {
85 self.color = Some(color);
86 self
87 }
88
89 pub fn trail_length(mut self, length: Pixels) -> Self {
90 self.trail_length = length;
91 self
92 }
93}
94
95impl Styled for Meteors {
96 fn style(&mut self) -> &mut StyleRefinement {
97 &mut self.style
98 }
99}
100
101impl ParentElement for Meteors {
102 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
103 self.children.extend(elements);
104 }
105}
106
107impl RenderOnce for Meteors {
108 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
109 let user_style = self.style;
110 let version = self.state.read(cx).version;
111 let count = self.count;
112 let speed = self.speed;
113 let color = self.color.unwrap_or(hsla(0.0, 0.0, 1.0, 0.6));
114 let trail_length = self.trail_length;
115
116 let angle_rad = self.angle.to_radians();
117 let cos_a = angle_rad.cos();
118 let sin_a = angle_rad.sin();
119
120 let mut container = div()
121 .id(self.id)
122 .relative()
123 .w_full()
124 .h_full()
125 .overflow_hidden();
126
127 for idx in 0..count {
128 let base_seed = (idx as u32).wrapping_mul(31337);
129 let start_x_pct = meteor_hash(base_seed);
130 let delay_pct = meteor_hash(base_seed.wrapping_add(1));
131 let speed_var = 0.5 + meteor_hash(base_seed.wrapping_add(2));
132
133 let base_dur = (3000.0 / speed / speed_var) as u64;
134 let duration = Duration::from_millis(base_dur.max(400));
135
136 let trail_f32 = trail_length / px(1.0);
137 let meteor_color = color;
138
139 let time_offset = version as f32 * 0.1 * speed * speed_var;
140 let cycle_secs = duration.as_secs_f32();
141 let phase = ((time_offset + delay_pct * cycle_secs) % cycle_secs) / cycle_secs;
142
143 let travel = 1.5 + trail_f32 / 500.0;
144 let pos = -0.3 + phase * travel;
145
146 let meteor_x = start_x_pct + pos * cos_a;
147 let meteor_y = -0.1 + pos * (-sin_a);
148
149 let horizontal_out_of_bounds = !meteor_x.is_nan() && !(-0.5..=1.5).contains(&meteor_x);
153 if meteor_y > 1.3 || horizontal_out_of_bounds {
154 continue;
155 }
156
157 let brightness = if phase < 0.1 {
158 phase / 0.1
159 } else if phase > 0.8 {
160 (1.0 - phase) / 0.2
161 } else {
162 1.0
163 };
164
165 let segments = 6;
166 for seg in 0..segments {
167 let seg_pct = seg as f32 / segments as f32;
168 let seg_len = trail_f32 / segments as f32;
169 let offset = seg_pct * trail_f32 / 500.0;
170
171 let seg_x = meteor_x - offset * cos_a;
172 let seg_y = meteor_y + offset * sin_a;
173 let seg_alpha = meteor_color.a * brightness * (1.0 - seg_pct * 0.8);
174 let seg_width = 2.0 - seg_pct * 1.2;
175
176 let seg_color = Hsla {
177 a: seg_alpha.max(0.0),
178 ..meteor_color
179 };
180
181 container = container.child(
182 div()
183 .absolute()
184 .left(relative(seg_x))
185 .top(relative(seg_y))
186 .w(px(seg_len))
187 .h(px(seg_width.max(0.5)))
188 .rounded(px(seg_width))
189 .bg(seg_color),
190 );
191 }
192 }
193
194 container
195 .child(div().relative().size_full().children(self.children))
196 .map(|this| {
197 let mut el = this;
198 el.style().refine(&user_style);
199 el
200 })
201 }
202}