use crate::{
motion,
t3::base::{spin_frame_now, stroke_arc},
theme::color,
tokens::ColorRole,
};
use gpui_kit::{
Animation, AnimationExt as _, App, ElementId, Hsla, IntoElement, ParentElement, RenderOnce,
SharedString, Styled, Window, canvas, div, linear_color_stop, linear_gradient, px, relative,
rems,
};
use std::{f32::consts::TAU, time::Duration};
pub const PANEL_PERIOD: Duration = Duration::from_millis(2400);
pub const SPIN_PERIOD: Duration = Duration::from_millis(690);
pub const PULSE_PERIOD: Duration = Duration::from_millis(1400);
pub const SWEEP_PERIOD: Duration = Duration::from_millis(1200);
pub fn slide(t: f32) -> f32 {
if t >= 0.4 {
1.2
} else {
-1. + 2.2 * motion::cubic_bezier_at(0., 0., 0.38, 0.9, t / 0.4)
}
}
pub fn fade(t: f32) -> f32 {
if t < 0.3 {
t / 0.3
} else if t <= 0.7 {
1.
} else {
(1. - t) / 0.3
}
}
pub fn pulse(t: f32) -> f32 {
let (from, to, local) = if t < 0.5 {
(1., 0.35, t / 0.5)
} else {
(0.35, 1., (t - 0.5) / 0.5)
};
from + (to - from) * motion::cubic_bezier_at(0.42, 0., 0.58, 1., local)
}
pub fn sweep(t: f32) -> f32 {
-1. + 2. * t
}
pub fn shimmer_clear(hover: Hsla) -> Hsla {
hover.opacity(0.)
}
fn ring(turn: f32, track: Hsla, arc: Hsla) -> impl IntoElement {
canvas(
|_, _, _| {},
move |bounds, _, window, _| {
let c = bounds.center();
stroke_arc(window, c, 7., 2., 0., TAU, track);
stroke_arc(
window,
c,
7.,
2.,
-0.75 * std::f32::consts::PI + turn * TAU,
TAU / 4.,
arc,
);
},
)
.size(px(16.))
}
#[derive(IntoElement)]
pub struct MotionSpecimen {
id: ElementId,
reduced: Option<bool>,
labels: [SharedString; 3],
captions: [SharedString; 6],
}
impl MotionSpecimen {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
reduced: None,
labels: ["Panel enter".into(), "Spinner".into(), "Shimmer".into()],
captions: [
"slide 240ms".into(),
"fade 70ms".into(),
"rotate 690ms".into(),
"pulse, no rotation".into(),
"sweep 1.2s".into(),
"static".into(),
],
}
}
pub fn reduced(mut self, reduced: bool) -> Self {
self.reduced = Some(reduced);
self
}
pub fn texts(mut self, labels: [SharedString; 3], captions: [SharedString; 6]) -> Self {
self.labels = labels;
self.captions = captions;
self
}
}
impl RenderOnce for MotionSpecimen {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let system = motion::reduced_motion(cx);
let r = self.reduced.unwrap_or(system);
let spin = spin_frame_now(SPIN_PERIOD, r || system, window);
let id = self.id.clone();
let interactive = color(ColorRole::Interactive);
let track_fill = color(ColorRole::Layer01);
let accent = color(ColorRole::LayerAccent01);
let hover = color(ColorRole::LayerHover01);
let clear = shimmer_clear(hover);
let [l_panel, l_spin, l_shimmer] = self.labels.clone();
let [c_slide, c_fade, c_rotate, c_pulse, c_sweep, c_static] = self.captions.clone();
let row = |label: SharedString, track: gpui_kit::AnyElement, caption: SharedString| {
div()
.flex()
.items_center()
.gap(rems(0.75))
.text_size(rems(0.75))
.line_height(rems(1.))
.child(
div()
.w(rems(6.))
.flex_none()
.text_color(color(ColorRole::TextPrimary))
.child(label),
)
.child(
div()
.relative()
.flex_1()
.min_w_0()
.h(rems(1.5))
.overflow_hidden()
.bg(track_fill)
.child(track),
)
.child(
div()
.w(rems(9.))
.flex_none()
.text_color(color(ColorRole::TextSecondary))
.child(caption),
)
};
let bar = div()
.absolute()
.top(px(4.))
.bottom(px(4.))
.w(relative(0.3))
.bg(interactive);
let panel = if system {
bar.left(relative(if r { 0.35 } else { 0. }))
.into_any_element()
} else if r {
bar.left(relative(0.35))
.with_animation(
(id.clone(), "fade"),
Animation::new(PANEL_PERIOD).repeat(),
|d, t| d.opacity(fade(t)),
)
.into_any_element()
} else {
bar.with_animation(
(id.clone(), "slide"),
Animation::new(PANEL_PERIOD).repeat(),
|d, t| d.left(relative(0.3 * slide(t))),
)
.into_any_element()
};
let spin_box = div().absolute().left(px(8.)).top(px(4.)).size(px(16.));
let spinner = spin_box
.opacity(spin.opacity)
.child(ring(spin.turn, accent, interactive))
.into_any_element();
let band = move |offset: f32| {
div()
.absolute()
.top_0()
.bottom_0()
.w_full()
.left(relative(offset))
.flex()
.child(div().flex_1().h_full().bg(linear_gradient(
90.,
linear_color_stop(clear, 0.),
linear_color_stop(hover, 1.),
)))
.child(div().flex_1().h_full().bg(linear_gradient(
90.,
linear_color_stop(hover, 0.),
linear_color_stop(clear, 1.),
)))
};
let shimmer = if r {
band(0.).opacity(0.6).into_any_element()
} else if system {
band(0.).into_any_element()
} else {
div()
.absolute()
.inset_0()
.with_animation(
(id.clone(), "sweep"),
Animation::new(SWEEP_PERIOD).repeat(),
move |d, t| d.child(band(sweep(t))),
)
.into_any_element()
};
div()
.flex()
.flex_col()
.gap(rems(0.75))
.child(row(l_panel, panel, if r { c_fade } else { c_slide }))
.child(row(l_spin, spinner, if r { c_pulse } else { c_rotate }))
.child(row(l_shimmer, shimmer, if r { c_static } else { c_sweep }))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn keyframes_match_ghost_css() {
assert!((slide(0.) + 1.).abs() < 1e-4);
assert!((slide(0.4) - 1.2).abs() < 1e-4);
assert_eq!(slide(0.9), 1.2);
assert_eq!(fade(0.), 0.);
assert!((fade(0.3) - 1.).abs() < 1e-6);
assert_eq!(fade(0.5), 1.);
assert!(fade(1.).abs() < 1e-6);
assert!((pulse(0.) - 1.).abs() < 1e-4);
assert!((pulse(0.5) - 0.35).abs() < 1e-4);
assert!((pulse(1.) - 1.).abs() < 1e-4);
assert_eq!(sweep(0.5), 0.);
assert_eq!(SPIN_PERIOD.as_millis(), 690);
}
#[test]
fn shimmer_fades_hover_without_darkening() {
use crate::{CarbonTheme, theme::color_in};
for theme in CarbonTheme::ALL {
let hover = color_in(theme, ColorRole::LayerHover01);
let clear = shimmer_clear(hover);
assert_eq!(clear.a, 0.);
let (h, c) = (hover.to_rgb(), clear.to_rgb());
for t in [0.25_f32, 0.5, 0.75] {
let mid = |x: f32, y: f32| x + (y - x) * t;
assert!((mid(c.r, h.r) - h.r).abs() < 1e-6, "{theme:?}");
assert!((mid(c.g, h.g) - h.g).abs() < 1e-6, "{theme:?}");
assert!((mid(c.b, h.b) - h.b).abs() < 1e-6, "{theme:?}");
}
}
}
}