1use std::{cell::RefCell, rc::Rc};
15
16use arc_motion::{
17 BlurFallback, CubicBezier, arc_spring_with_options, is_reduced_motion, motion_now,
18 spring::SpringOptions,
19};
20use arc_tokens::{ActiveArcTheme as _, Radii, Spacing, Typography};
21use gpui_kit::{
22 AnyElement, App, ElementId, Hsla, IntoElement, ParentElement, RenderOnce, Role, SharedString,
23 Styled, Window, assets::IconName, base::StyledExt as _, div, prelude::*, px, svg,
24};
25
26type ActionBuilder = Rc<dyn Fn(&mut Window, &mut App) -> AnyElement>;
27
28pub fn empty_state_copy_sample(elapsed_secs: f64, reduced: bool) -> (f32, f32, f32, f32) {
30 if reduced {
31 let t = (elapsed_secs / 0.12).clamp(0.0, 1.0) as f32;
32 return ((1.0 - t).clamp(0.0, 1.0), 0.0, t, 0.0);
33 }
34 let out_t = (elapsed_secs / 0.16).clamp(0.0, 1.0) as f32;
35 let out_eased = CubicBezier::new(0.22, 1.0, 0.36, 1.0).sample(out_t);
36 let out_blur = BlurFallback::evaluate(px(out_eased * 2.0), false);
37 let old_alpha = ((1.0 - out_eased) * out_blur.opacity_multiplier).clamp(0.0, 1.0);
38 let old_y = -4.5 * out_eased;
39
40 let in_t = (elapsed_secs / 0.24).clamp(0.0, 1.0) as f32;
41 let in_eased = CubicBezier::new(0.16, 1.0, 0.3, 1.0).sample(in_t);
42 let in_blur = BlurFallback::evaluate(px((1.0 - in_eased) * 4.0), false);
43 let new_alpha = (in_eased * in_blur.opacity_multiplier).clamp(0.0, 1.0);
44 let new_y = 4.5 * (1.0 - in_eased);
45 (old_alpha, old_y, new_alpha, new_y)
46}
47
48pub fn empty_state_icon_sample(elapsed_secs: f64, reduced: bool) -> (f32, f32, f32) {
50 if reduced {
51 let t = (elapsed_secs / 0.12).clamp(0.0, 1.0) as f32;
52 return ((1.0 - t).clamp(0.0, 1.0), t, 1.0);
53 }
54 let out_t = (elapsed_secs / 0.16).clamp(0.0, 1.0) as f32;
55 let old_alpha = (1.0 - CubicBezier::new(0.22, 1.0, 0.36, 1.0).sample(out_t)).clamp(0.0, 1.0);
56
57 let in_t = (elapsed_secs / 0.26).clamp(0.0, 1.0) as f32;
58 let in_eased = CubicBezier::new(0.16, 1.0, 0.3, 1.0).sample(in_t);
59 let scale = 0.6 + 0.4 * in_eased;
60 (old_alpha, in_eased.clamp(0.0, 1.0), scale)
61}
62
63#[derive(Clone)]
64struct EmptyStateInternal {
65 last_title: SharedString,
66 prev_title: Option<SharedString>,
67 title_changed_at: f64,
68 last_desc: SharedString,
69 prev_desc: Option<SharedString>,
70 desc_changed_at: f64,
71 last_icon: IconName,
72 prev_icon: Option<IconName>,
73 icon_changed_at: f64,
74 measured_copy_h: Rc<RefCell<f32>>,
75 copy_changed_at: f64,
76}
77
78#[derive(IntoElement)]
80pub struct EmptyState {
81 id: ElementId,
82 title: SharedString,
83 description: SharedString,
84 icon: IconName,
85 action: Option<ActionBuilder>,
86 label: Option<SharedString>,
87}
88
89impl EmptyState {
90 pub fn new(
91 id: impl Into<ElementId>,
92 title: impl Into<SharedString>,
93 description: impl Into<SharedString>,
94 ) -> Self {
95 Self {
96 id: id.into(),
97 title: title.into(),
98 description: description.into(),
99 icon: IconName::Folder,
100 action: None,
101 label: None,
102 }
103 }
104
105 pub fn icon(mut self, icon: IconName) -> Self {
106 self.icon = icon;
107 self
108 }
109
110 pub fn action(
111 mut self,
112 builder: impl Fn(&mut Window, &mut App) -> AnyElement + 'static,
113 ) -> Self {
114 self.action = Some(Rc::new(builder));
115 self
116 }
117
118 pub fn label(mut self, label: impl Into<SharedString>) -> Self {
119 self.label = Some(label.into());
120 self
121 }
122}
123
124fn render_copy_slot(
125 current: SharedString,
126 previous: Option<SharedString>,
127 elapsed: f64,
128 text_size: gpui_kit::Pixels,
129 font_medium: bool,
130 color: Hsla,
131 reduced: bool,
132) -> AnyElement {
133 let (old_alpha, old_y, new_alpha, new_y) = if previous.is_some() {
134 empty_state_copy_sample(elapsed, reduced)
135 } else {
136 (0.0, 0.0, 1.0, 0.0)
137 };
138
139 let outgoing = previous.and_then(|prev| {
140 if old_alpha <= 0.01 {
141 None
142 } else {
143 let mut el = div()
144 .absolute()
145 .left_0()
146 .right_0()
147 .top(px(old_y))
148 .opacity(old_alpha)
149 .text_center()
150 .text_size(text_size)
151 .text_color(color)
152 .child(prev);
153 if font_medium {
154 el = el.font_medium();
155 }
156 Some(el)
157 }
158 });
159
160 let mut incoming = div()
161 .relative()
162 .top(px(new_y))
163 .opacity(new_alpha)
164 .w_full()
165 .text_center()
166 .text_size(text_size)
167 .text_color(color)
168 .child(current);
169 if font_medium {
170 incoming = incoming.font_medium();
171 }
172
173 div()
174 .relative()
175 .w_full()
176 .when_some(outgoing, |el, out| el.child(out))
177 .child(incoming)
178 .into_any_element()
179}
180
181impl RenderOnce for EmptyState {
182 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
183 let theme = cx.arc_theme().clone();
184 let colors = theme.colors;
185 let motion = theme.motion;
186 let reduced = is_reduced_motion(cx);
187 let now = motion_now(cx);
188
189 let state_cell = window
190 .use_keyed_state(
191 ElementId::NamedChild(self.id.clone().into(), SharedString::from("es-state")),
192 cx,
193 |_, _| {
194 Rc::new(RefCell::new(EmptyStateInternal {
195 last_title: self.title.clone(),
196 prev_title: None,
197 title_changed_at: now - 1.0,
198 last_desc: self.description.clone(),
199 prev_desc: None,
200 desc_changed_at: now - 1.0,
201 last_icon: self.icon,
202 prev_icon: None,
203 icon_changed_at: now - 1.0,
204 measured_copy_h: Rc::new(RefCell::new(0.0)),
205 copy_changed_at: now - 1.0,
206 }))
207 },
208 )
209 .read(cx)
210 .clone();
211
212 let (
213 prev_title,
214 title_elapsed,
215 prev_desc,
216 desc_elapsed,
217 prev_icon,
218 icon_elapsed,
219 copy_h_ref,
220 copy_recently_changed,
221 animating,
222 ) = {
223 let mut st = state_cell.borrow_mut();
224 if st.last_title != self.title {
225 st.prev_title = Some(st.last_title.clone());
226 st.last_title = self.title.clone();
227 st.title_changed_at = now;
228 st.copy_changed_at = now;
229 }
230 if st.last_desc != self.description {
231 st.prev_desc = Some(st.last_desc.clone());
232 st.last_desc = self.description.clone();
233 st.desc_changed_at = now;
234 st.copy_changed_at = now;
235 }
236 if st.last_icon != self.icon {
237 st.prev_icon = Some(st.last_icon);
238 st.last_icon = self.icon;
239 st.icon_changed_at = now;
240 }
241
242 let t_dt = (now - st.title_changed_at).max(0.0);
243 let d_dt = (now - st.desc_changed_at).max(0.0);
244 let i_dt = (now - st.icon_changed_at).max(0.0);
245 if st.prev_title.is_some() && t_dt >= 0.28 {
246 st.prev_title = None;
247 }
248 if st.prev_desc.is_some() && d_dt >= 0.28 {
249 st.prev_desc = None;
250 }
251 if st.prev_icon.is_some() && i_dt >= 0.30 {
252 st.prev_icon = None;
253 }
254 let anim = st.prev_title.is_some() || st.prev_desc.is_some() || st.prev_icon.is_some();
255 let recent = (now - st.copy_changed_at).max(0.0) < 0.6;
256 (
257 st.prev_title.clone(),
258 t_dt,
259 st.prev_desc.clone(),
260 d_dt,
261 st.prev_icon,
262 i_dt,
263 st.measured_copy_h.clone(),
264 recent,
265 anim,
266 )
267 };
268
269 if animating && !reduced {
270 window.request_animation_frame();
271 }
272
273 let (old_icon_alpha, new_icon_alpha, new_icon_scale) = if prev_icon.is_some() {
274 empty_state_icon_sample(icon_elapsed, reduced)
275 } else {
276 (0.0, 1.0, 1.0)
277 };
278
279 let icon_tile = div()
280 .relative()
281 .w(Spacing::S12)
282 .h(Spacing::S12)
283 .rounded(Radii::PANEL)
284 .border_1()
285 .border_color(colors.border)
286 .bg(colors.surface_muted)
287 .flex()
288 .items_center()
289 .justify_center()
290 .when_some(
291 prev_icon.filter(|_| old_icon_alpha > 0.01),
292 |el, prev_ic| {
293 el.child(
294 div()
295 .absolute()
296 .inset_0()
297 .opacity(old_icon_alpha)
298 .flex()
299 .items_center()
300 .justify_center()
301 .child(
302 svg()
303 .path(prev_ic.path())
304 .size(px(22.0))
305 .text_color(colors.text_secondary),
306 ),
307 )
308 },
309 )
310 .child(
311 div()
312 .opacity(new_icon_alpha)
313 .flex()
314 .items_center()
315 .justify_center()
316 .child(
317 svg()
318 .path(self.icon.path())
319 .size(px(24.0 * new_icon_scale))
320 .text_color(colors.text_secondary),
321 ),
322 );
323
324 let target_copy_h = *copy_h_ref.borrow();
325 let animated_copy_h = if !reduced && target_copy_h > 10.0 {
326 Some(
327 arc_spring_with_options(
328 ElementId::NamedChild(self.id.clone().into(), SharedString::from("copy-h")),
329 target_copy_h,
330 SpringOptions::new(motion.spring.smooth).with_initial_value(target_copy_h),
331 window,
332 cx,
333 )
334 .value,
335 )
336 } else {
337 None
338 };
339
340 let measure_copy_h = copy_h_ref.clone();
341 let copy_content = div()
342 .w_full()
343 .flex()
344 .flex_col()
345 .items_center()
346 .pt(Spacing::S5)
347 .child(render_copy_slot(
348 self.title,
349 prev_title,
350 title_elapsed,
351 Typography::TEXT_BASE,
352 true,
353 colors.foreground,
354 reduced,
355 ))
356 .child(
357 div()
358 .w_full()
359 .max_w(px(288.0))
360 .mt(Spacing::S2)
361 .child(render_copy_slot(
362 self.description,
363 prev_desc,
364 desc_elapsed,
365 Typography::TEXT_SM,
366 false,
367 colors.text_secondary,
368 reduced,
369 )),
370 );
371
372 let mut copy_frame = div()
373 .w_full()
374 .flex()
375 .flex_col()
376 .items_center()
377 .child(copy_content)
378 .on_children_prepainted(move |bounds, window, _| {
379 if let Some(b) = bounds.first() {
380 let h = f32::from(b.size.height);
381 let prev = *measure_copy_h.borrow();
382 if h > 10.0 && (prev - h).abs() > 0.5 {
383 *measure_copy_h.borrow_mut() = h;
384 window.request_animation_frame();
385 }
386 }
387 });
388
389 if let Some(h) = animated_copy_h
390 && copy_recently_changed
391 && (h - target_copy_h).abs() > 0.5
392 {
393 copy_frame = copy_frame.h(px(h.max(10.0))).overflow_hidden();
394 }
395
396 let action_el = self.action.map(|act| {
397 div()
398 .mt(Spacing::S5)
399 .flex()
400 .flex_row()
401 .flex_wrap()
402 .items_center()
403 .justify_center()
404 .gap(Spacing::S3)
405 .child(act(window, cx))
406 });
407
408 let _ = self.label;
409 div()
410 .id(self.id)
411 .role(Role::Region)
412 .w_full()
413 .px(Spacing::S5)
414 .py(Spacing::S10)
415 .flex()
416 .flex_col()
417 .items_center()
418 .child(icon_tile)
419 .child(copy_frame)
420 .when_some(action_el, |el, act| el.child(act))
421 .into_any_element()
422 }
423}
424
425#[cfg(test)]
426mod tests {
427 use super::*;
428
429 #[test]
430 fn empty_state_copy_and_icon_samples_converge() {
431 let (oa0, _, na0, ny0) = empty_state_copy_sample(0.0, false);
432 assert!(oa0 > 0.9 && na0 == 0.0 && (ny0 - 4.5).abs() < 0.01);
433
434 let (oa1, _, na1, ny1) = empty_state_copy_sample(0.30, false);
435 assert_eq!(oa1, 0.0);
436 assert_eq!(na1, 1.0);
437 assert_eq!(ny1, 0.0);
438
439 let (_, na_i, scale_i) = empty_state_icon_sample(0.30, false);
440 assert_eq!(na_i, 1.0);
441 assert_eq!(scale_i, 1.0);
442 }
443}