use std::{cell::RefCell, rc::Rc};
use arc_motion::{
BlurFallback, CubicBezier, arc_spring_with_options, is_reduced_motion, motion_now,
spring::SpringOptions,
};
use arc_tokens::{ActiveArcTheme as _, Radii, Spacing, Typography};
use gpui_kit::{
AnyElement, App, ElementId, Hsla, IntoElement, ParentElement, RenderOnce, Role, SharedString,
Styled, Window, assets::IconName, base::StyledExt as _, div, prelude::*, px, svg,
};
type ActionBuilder = Rc<dyn Fn(&mut Window, &mut App) -> AnyElement>;
pub fn empty_state_copy_sample(elapsed_secs: f64, reduced: bool) -> (f32, f32, f32, f32) {
if reduced {
let t = (elapsed_secs / 0.12).clamp(0.0, 1.0) as f32;
return ((1.0 - t).clamp(0.0, 1.0), 0.0, t, 0.0);
}
let out_t = (elapsed_secs / 0.16).clamp(0.0, 1.0) as f32;
let out_eased = CubicBezier::new(0.22, 1.0, 0.36, 1.0).sample(out_t);
let out_blur = BlurFallback::evaluate(px(out_eased * 2.0), false);
let old_alpha = ((1.0 - out_eased) * out_blur.opacity_multiplier).clamp(0.0, 1.0);
let old_y = -4.5 * out_eased;
let in_t = (elapsed_secs / 0.24).clamp(0.0, 1.0) as f32;
let in_eased = CubicBezier::new(0.16, 1.0, 0.3, 1.0).sample(in_t);
let in_blur = BlurFallback::evaluate(px((1.0 - in_eased) * 4.0), false);
let new_alpha = (in_eased * in_blur.opacity_multiplier).clamp(0.0, 1.0);
let new_y = 4.5 * (1.0 - in_eased);
(old_alpha, old_y, new_alpha, new_y)
}
pub fn empty_state_icon_sample(elapsed_secs: f64, reduced: bool) -> (f32, f32, f32) {
if reduced {
let t = (elapsed_secs / 0.12).clamp(0.0, 1.0) as f32;
return ((1.0 - t).clamp(0.0, 1.0), t, 1.0);
}
let out_t = (elapsed_secs / 0.16).clamp(0.0, 1.0) as f32;
let old_alpha = (1.0 - CubicBezier::new(0.22, 1.0, 0.36, 1.0).sample(out_t)).clamp(0.0, 1.0);
let in_t = (elapsed_secs / 0.26).clamp(0.0, 1.0) as f32;
let in_eased = CubicBezier::new(0.16, 1.0, 0.3, 1.0).sample(in_t);
let scale = 0.6 + 0.4 * in_eased;
(old_alpha, in_eased.clamp(0.0, 1.0), scale)
}
#[derive(Clone)]
struct EmptyStateInternal {
last_title: SharedString,
prev_title: Option<SharedString>,
title_changed_at: f64,
last_desc: SharedString,
prev_desc: Option<SharedString>,
desc_changed_at: f64,
last_icon: IconName,
prev_icon: Option<IconName>,
icon_changed_at: f64,
measured_copy_h: Rc<RefCell<f32>>,
copy_changed_at: f64,
}
#[derive(IntoElement)]
pub struct EmptyState {
id: ElementId,
title: SharedString,
description: SharedString,
icon: IconName,
action: Option<ActionBuilder>,
label: Option<SharedString>,
}
impl EmptyState {
pub fn new(
id: impl Into<ElementId>,
title: impl Into<SharedString>,
description: impl Into<SharedString>,
) -> Self {
Self {
id: id.into(),
title: title.into(),
description: description.into(),
icon: IconName::Folder,
action: None,
label: None,
}
}
pub fn icon(mut self, icon: IconName) -> Self {
self.icon = icon;
self
}
pub fn action(
mut self,
builder: impl Fn(&mut Window, &mut App) -> AnyElement + 'static,
) -> Self {
self.action = Some(Rc::new(builder));
self
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
}
fn render_copy_slot(
current: SharedString,
previous: Option<SharedString>,
elapsed: f64,
text_size: gpui_kit::Pixels,
font_medium: bool,
color: Hsla,
reduced: bool,
) -> AnyElement {
let (old_alpha, old_y, new_alpha, new_y) = if previous.is_some() {
empty_state_copy_sample(elapsed, reduced)
} else {
(0.0, 0.0, 1.0, 0.0)
};
let outgoing = previous.and_then(|prev| {
if old_alpha <= 0.01 {
None
} else {
let mut el = div()
.absolute()
.left_0()
.right_0()
.top(px(old_y))
.opacity(old_alpha)
.text_center()
.text_size(text_size)
.text_color(color)
.child(prev);
if font_medium {
el = el.font_medium();
}
Some(el)
}
});
let mut incoming = div()
.relative()
.top(px(new_y))
.opacity(new_alpha)
.w_full()
.text_center()
.text_size(text_size)
.text_color(color)
.child(current);
if font_medium {
incoming = incoming.font_medium();
}
div()
.relative()
.w_full()
.when_some(outgoing, |el, out| el.child(out))
.child(incoming)
.into_any_element()
}
impl RenderOnce for EmptyState {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.arc_theme().clone();
let colors = theme.colors;
let motion = theme.motion;
let reduced = is_reduced_motion(cx);
let now = motion_now(cx);
let state_cell = window
.use_keyed_state(
ElementId::NamedChild(self.id.clone().into(), SharedString::from("es-state")),
cx,
|_, _| {
Rc::new(RefCell::new(EmptyStateInternal {
last_title: self.title.clone(),
prev_title: None,
title_changed_at: now - 1.0,
last_desc: self.description.clone(),
prev_desc: None,
desc_changed_at: now - 1.0,
last_icon: self.icon,
prev_icon: None,
icon_changed_at: now - 1.0,
measured_copy_h: Rc::new(RefCell::new(0.0)),
copy_changed_at: now - 1.0,
}))
},
)
.read(cx)
.clone();
let (
prev_title,
title_elapsed,
prev_desc,
desc_elapsed,
prev_icon,
icon_elapsed,
copy_h_ref,
copy_recently_changed,
animating,
) = {
let mut st = state_cell.borrow_mut();
if st.last_title != self.title {
st.prev_title = Some(st.last_title.clone());
st.last_title = self.title.clone();
st.title_changed_at = now;
st.copy_changed_at = now;
}
if st.last_desc != self.description {
st.prev_desc = Some(st.last_desc.clone());
st.last_desc = self.description.clone();
st.desc_changed_at = now;
st.copy_changed_at = now;
}
if st.last_icon != self.icon {
st.prev_icon = Some(st.last_icon);
st.last_icon = self.icon;
st.icon_changed_at = now;
}
let t_dt = (now - st.title_changed_at).max(0.0);
let d_dt = (now - st.desc_changed_at).max(0.0);
let i_dt = (now - st.icon_changed_at).max(0.0);
if st.prev_title.is_some() && t_dt >= 0.28 {
st.prev_title = None;
}
if st.prev_desc.is_some() && d_dt >= 0.28 {
st.prev_desc = None;
}
if st.prev_icon.is_some() && i_dt >= 0.30 {
st.prev_icon = None;
}
let anim = st.prev_title.is_some() || st.prev_desc.is_some() || st.prev_icon.is_some();
let recent = (now - st.copy_changed_at).max(0.0) < 0.6;
(
st.prev_title.clone(),
t_dt,
st.prev_desc.clone(),
d_dt,
st.prev_icon,
i_dt,
st.measured_copy_h.clone(),
recent,
anim,
)
};
if animating && !reduced {
window.request_animation_frame();
}
let (old_icon_alpha, new_icon_alpha, new_icon_scale) = if prev_icon.is_some() {
empty_state_icon_sample(icon_elapsed, reduced)
} else {
(0.0, 1.0, 1.0)
};
let icon_tile = div()
.relative()
.w(Spacing::S12)
.h(Spacing::S12)
.rounded(Radii::PANEL)
.border_1()
.border_color(colors.border)
.bg(colors.surface_muted)
.flex()
.items_center()
.justify_center()
.when_some(
prev_icon.filter(|_| old_icon_alpha > 0.01),
|el, prev_ic| {
el.child(
div()
.absolute()
.inset_0()
.opacity(old_icon_alpha)
.flex()
.items_center()
.justify_center()
.child(
svg()
.path(prev_ic.path())
.size(px(22.0))
.text_color(colors.text_secondary),
),
)
},
)
.child(
div()
.opacity(new_icon_alpha)
.flex()
.items_center()
.justify_center()
.child(
svg()
.path(self.icon.path())
.size(px(24.0 * new_icon_scale))
.text_color(colors.text_secondary),
),
);
let target_copy_h = *copy_h_ref.borrow();
let animated_copy_h = if !reduced && target_copy_h > 10.0 {
Some(
arc_spring_with_options(
ElementId::NamedChild(self.id.clone().into(), SharedString::from("copy-h")),
target_copy_h,
SpringOptions::new(motion.spring.smooth).with_initial_value(target_copy_h),
window,
cx,
)
.value,
)
} else {
None
};
let measure_copy_h = copy_h_ref.clone();
let copy_content = div()
.w_full()
.flex()
.flex_col()
.items_center()
.pt(Spacing::S5)
.child(render_copy_slot(
self.title,
prev_title,
title_elapsed,
Typography::TEXT_BASE,
true,
colors.foreground,
reduced,
))
.child(
div()
.w_full()
.max_w(px(288.0))
.mt(Spacing::S2)
.child(render_copy_slot(
self.description,
prev_desc,
desc_elapsed,
Typography::TEXT_SM,
false,
colors.text_secondary,
reduced,
)),
);
let mut copy_frame = div()
.w_full()
.flex()
.flex_col()
.items_center()
.child(copy_content)
.on_children_prepainted(move |bounds, window, _| {
if let Some(b) = bounds.first() {
let h = f32::from(b.size.height);
let prev = *measure_copy_h.borrow();
if h > 10.0 && (prev - h).abs() > 0.5 {
*measure_copy_h.borrow_mut() = h;
window.request_animation_frame();
}
}
});
if let Some(h) = animated_copy_h
&& copy_recently_changed
&& (h - target_copy_h).abs() > 0.5
{
copy_frame = copy_frame.h(px(h.max(10.0))).overflow_hidden();
}
let action_el = self.action.map(|act| {
div()
.mt(Spacing::S5)
.flex()
.flex_row()
.flex_wrap()
.items_center()
.justify_center()
.gap(Spacing::S3)
.child(act(window, cx))
});
let _ = self.label;
div()
.id(self.id)
.role(Role::Region)
.w_full()
.px(Spacing::S5)
.py(Spacing::S10)
.flex()
.flex_col()
.items_center()
.child(icon_tile)
.child(copy_frame)
.when_some(action_el, |el, act| el.child(act))
.into_any_element()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_state_copy_and_icon_samples_converge() {
let (oa0, _, na0, ny0) = empty_state_copy_sample(0.0, false);
assert!(oa0 > 0.9 && na0 == 0.0 && (ny0 - 4.5).abs() < 0.01);
let (oa1, _, na1, ny1) = empty_state_copy_sample(0.30, false);
assert_eq!(oa1, 0.0);
assert_eq!(na1, 1.0);
assert_eq!(ny1, 0.0);
let (_, na_i, scale_i) = empty_state_icon_sample(0.30, false);
assert_eq!(na_i, 1.0);
assert_eq!(scale_i, 1.0);
}
}