use std::time::Instant;
use arc_motion::{arc_transition, is_reduced_motion};
use arc_tokens::{ActiveArcTheme as _, ControlHeights, Radii, Spacing, oklab_mix};
use gpui_kit::{
AnyElement, App, ElementId, IntoElement, ParentElement, RenderOnce, SharedString, Styled,
Window, div, prelude::*, px,
};
const PULSE_SECS: f32 = 1.8;
const PULSE_CYCLES: f32 = 11.0;
const STAGGER_SECS: f32 = 0.09;
pub fn pulse_opacity(elapsed: f32, index: usize, ease: impl Fn(f32) -> f32) -> f32 {
let t = elapsed - index as f32 * STAGGER_SECS;
if t <= 0.0 || t >= PULSE_SECS * PULSE_CYCLES {
return 1.0;
}
let phase = (t % PULSE_SECS) / PULSE_SECS;
let half = if phase < 0.5 {
phase * 2.0
} else {
(1.0 - phase) * 2.0
};
1.0 - 0.48 * ease(half)
}
struct SkeletonInternal {
started: Instant,
}
#[derive(IntoElement)]
pub struct Skeleton {
id: ElementId,
label: SharedString,
lines: usize,
avatar: bool,
loading: bool,
content: Option<AnyElement>,
}
impl Skeleton {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
label: "Loading content".into(),
lines: 3,
avatar: false,
loading: true,
content: None,
}
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = label.into();
self
}
pub fn lines(mut self, lines: usize) -> Self {
self.lines = lines.clamp(1, 6);
self
}
pub fn avatar(mut self, avatar: bool) -> Self {
self.avatar = avatar;
self
}
pub fn loading(mut self, loading: bool) -> Self {
self.loading = loading;
self
}
pub fn content(mut self, content: impl IntoElement) -> Self {
self.content = Some(content.into_any_element());
self
}
}
impl RenderOnce for Skeleton {
fn render(mut 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 show_placeholder = self.content.is_none() || self.loading;
if !show_placeholder {
let p = arc_transition(
(self.id.clone(), "skeleton-content"),
1.0,
if reduced {
motion.duration.instant
} else {
motion.duration.standard
},
motion.ease.enter,
window,
cx,
);
let rise = if reduced { 0.0 } else { (1.0 - p) * 4.0 };
return div()
.id(self.id)
.w_full()
.mt(px(rise))
.opacity(p.clamp(0.0, 1.0))
.children(self.content.take())
.into_any_element();
}
let state_id = ElementId::NamedChild(self.id.clone().into(), "__arc-skeleton".into());
let internal = window.use_keyed_state(state_id, cx, |_, _| SkeletonInternal {
started: Instant::now(),
});
let elapsed = internal.read(cx).started.elapsed().as_secs_f32();
let animating = !reduced && elapsed < PULSE_SECS * PULSE_CYCLES + 1.0;
if animating {
window.request_animation_frame();
}
let easing = motion.ease.in_out.to_base_easing();
let opacity_for = |index: usize| {
if reduced {
1.0
} else {
pulse_opacity(elapsed, index, |t| easing.sample(t))
}
};
let block_color = oklab_mix(colors.border_strong, colors.surface, 0.38);
let offset = usize::from(self.avatar);
let widths = |i: usize, n: usize| -> f32 {
if i == 0 && n > 1 {
0.48
} else if i + 1 == n {
0.72
} else {
1.0
}
};
let mut lines = div()
.flex_1()
.min_w_0()
.pt(Spacing::S1)
.flex()
.flex_col()
.gap(Spacing::S3);
for i in 0..self.lines {
let h = if i == 0 && self.lines > 1 {
px(16.0)
} else {
px(12.0)
};
let frac = widths(i, self.lines);
lines = lines.child(
div()
.w(gpui_kit::relative(frac))
.h(h)
.rounded(Radii::CONTROL)
.bg(block_color)
.opacity(opacity_for(i + offset)),
);
}
div()
.id(self.id)
.w_full()
.min_w_0()
.flex()
.items_start()
.gap(Spacing::S4)
.when(self.avatar, |this| {
this.child(
div()
.flex_none()
.w(ControlHeights::MD)
.h(ControlHeights::MD)
.rounded(Radii::PILL)
.bg(block_color)
.opacity(opacity_for(0)),
)
})
.child(lines)
.into_any_element()
}
}
#[cfg(test)]
mod tests {
use arc_tokens::{Accent, ThemeMode, install_arc_theme};
use gpui_kit::{Context, Render, TestAppContext, div, px, test::TestWindowExt as _};
use super::*;
struct SkeletonHost {
loading: bool,
}
impl Render for SkeletonHost {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().w(px(360.0)).p(Spacing::S4).child(
Skeleton::new("sk")
.avatar(true)
.lines(3)
.loading(self.loading)
.content(div().child("Loaded content")),
)
}
}
#[test]
fn pulse_dims_then_recovers_and_rests() {
let lin = |t: f32| t;
assert_eq!(pulse_opacity(0.0, 0, lin), 1.0);
let mid = pulse_opacity(PULSE_SECS * 0.5, 0, lin);
assert!((mid - 0.52).abs() < 1e-4);
assert!(pulse_opacity(PULSE_SECS * 0.5 + 0.09, 1, lin) < 0.53);
assert_eq!(pulse_opacity(PULSE_SECS * PULSE_CYCLES + 1.0, 0, lin), 1.0);
assert_eq!(pulse_opacity(0.02, 3, lin), 1.0);
}
#[gpui_kit::test]
fn skeleton_swaps_placeholder_for_content(cx: &mut TestAppContext) {
cx.update(|cx| {
gpui_kit::init(cx);
install_arc_theme(ThemeMode::Light, Accent::Violet, cx);
});
let (view, cx) = cx.add_window_view(|_, _| SkeletonHost { loading: true });
cx.update(|window, cx| {
window.render_frame(cx);
view.update(cx, |h, cx| {
h.loading = false;
cx.notify();
});
window.render_frame(cx);
view.update(cx, |h, cx| {
h.loading = true;
cx.notify();
});
window.render_frame(cx);
});
}
}