use crate::{
icons::{CarbonIcon, Icon, IconSize},
motion::{self, cubic_bezier_at},
theme::{LayerLevel, color},
tokens::{ColorRole, TypeStyle},
typography::TypeStyled as _,
};
use gpui_kit::{
App, Bounds, ElementId, Hsla, InteractiveElement, IntoElement, ParentElement, PathBuilder,
Pixels, RenderOnce, SharedString, Styled, Window, anchored, canvas, deferred, div, point,
prelude::FluentBuilder as _, px, rems,
};
use std::f32::consts::{PI, TAU};
use std::time::{Duration, Instant};
pub const ROTATION_PERIOD: Duration = Duration::from_millis(690);
pub const STOP_PHASE: Duration = Duration::from_millis(700);
pub const DASH: f32 = 276.4608;
pub const OFFSET_LARGE: f32 = 52.527552;
#[allow(clippy::excessive_precision)]
pub const OFFSET_SMALL: f32 = 143.759616;
pub const OFFSET_BACKGROUND: f32 = -22.;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum SpinnerSize {
Sm,
#[default]
Md,
Inline,
}
impl SpinnerSize {
pub const ALL: [SpinnerSize; 3] = [Self::Sm, Self::Md, Self::Inline];
pub const fn name(self) -> &'static str {
match self {
Self::Sm => "sm",
Self::Md => "md",
Self::Inline => "inline",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum SpinnerStatus {
#[default]
Active,
Finished,
Error,
Inactive,
}
impl SpinnerStatus {
pub const ALL: [SpinnerStatus; 4] = [Self::Active, Self::Finished, Self::Error, Self::Inactive];
pub const fn name(self) -> &'static str {
match self {
Self::Active => "active",
Self::Finished => "finished",
Self::Error => "error",
Self::Inactive => "inactive",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Arc {
pub size: f32,
pub radius: f32,
pub stroke: f32,
pub offset: f32,
pub background: bool,
}
pub const fn arc(small: bool) -> Arc {
if small {
Arc {
size: 16.,
radius: 42.,
stroke: 16.,
offset: OFFSET_SMALL,
background: true,
}
} else {
Arc {
size: 88.,
radius: 44.,
stroke: 10.,
offset: OFFSET_LARGE,
background: false,
}
}
}
pub fn dash_span(radius: f32, offset: f32) -> (f32, f32) {
let circumference = TAU * radius;
let period = DASH * 2.;
let start = (period - offset.rem_euclid(period)) % period;
if start == 0. || start >= DASH {
let first = (DASH - offset.rem_euclid(period)).max(0.);
(0., first.min(circumference))
} else {
(start.min(circumference), (start + DASH).min(circumference))
}
}
pub fn stop_frame(elapsed: Duration, resting_offset: f32) -> Option<(f32, f32)> {
let phase = STOP_PHASE.as_secs_f32();
let t = elapsed.as_secs_f32();
let ease = |x: f32| cubic_bezier_at(0., 0., 0.25, 1., x);
if t < phase {
Some((360. * ease(t / phase), resting_offset))
} else if t < 2. * phase {
let k = ease((t - phase) / phase);
Some((-360. * k, resting_offset + (DASH - resting_offset) * k))
} else {
None
}
}
pub fn rotation_at(elapsed: Duration) -> f32 {
let period = ROTATION_PERIOD.as_secs_f32();
(elapsed.as_secs_f32() % period) / period * 360.
}
fn paint_ring(
bounds: Bounds<Pixels>,
arc: Arc,
offset: f32,
rotation: f32,
stroke: Hsla,
background: Option<Hsla>,
window: &mut Window,
) {
let scale = f32::from(bounds.size.width) / 100.;
let center = bounds.center();
let r = arc.radius * scale;
let width = px(arc.stroke * scale);
let mut ring = |from: f32, to: f32, color: Hsla| {
if to - from <= 0.01 {
return;
}
let (a0, a1) = (from / arc.radius, to / arc.radius);
let steps = ((a1 - a0) / (PI / 48.)).ceil().max(2.) as usize;
let mut path = PathBuilder::stroke(width);
for i in 0..=steps {
let a = a0 + (a1 - a0) * i as f32 / steps as f32 + rotation.to_radians();
let p = point(center.x + px(r * a.cos()), center.y + px(r * a.sin()));
if i == 0 {
path.move_to(p);
} else {
path.line_to(p);
}
}
if let Ok(path) = path.build() {
window.paint_path(path, color);
}
};
if let Some(bg) = background {
let (from, to) = dash_span(arc.radius, OFFSET_BACKGROUND);
ring(from, to, bg);
}
let (from, to) = dash_span(arc.radius, offset);
ring(from, to, stroke);
}
struct Clock {
started: Instant,
stopped: Option<Instant>,
}
#[derive(IntoElement)]
pub struct Spinner {
id: ElementId,
size: SpinnerSize,
status: SpinnerStatus,
description: Option<SharedString>,
label: SharedString,
overlay: bool,
level: LayerLevel,
}
impl Spinner {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
size: SpinnerSize::Md,
status: SpinnerStatus::Active,
description: None,
label: SharedString::default(),
overlay: false,
level: LayerLevel::One,
}
}
pub fn size(mut self, size: SpinnerSize) -> Self {
self.size = size;
self
}
pub fn status(mut self, status: SpinnerStatus) -> Self {
self.status = status;
self
}
pub fn active(self, active: bool) -> Self {
self.status(if active {
SpinnerStatus::Active
} else {
SpinnerStatus::Inactive
})
}
pub fn description(mut self, description: impl Into<SharedString>) -> Self {
self.description = Some(description.into());
self
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = label.into();
self
}
pub fn with_overlay(mut self, overlay: bool) -> Self {
self.overlay = overlay;
self
}
pub fn layer(mut self, level: LayerLevel) -> Self {
self.level = level;
self
}
pub fn is_inline(&self) -> bool {
self.description.is_some() || self.size == SpinnerSize::Inline
}
}
fn ring(
id: ElementId,
small: bool,
active: bool,
level: LayerLevel,
window: &mut Window,
cx: &mut App,
) -> impl IntoElement + use<> {
let geometry = arc(small);
let clock = window.use_keyed_state((id, "clock"), cx, |_, _| Clock {
started: Instant::now(),
stopped: None,
});
let now = Instant::now();
clock.update(cx, |clock, _| match (active, clock.stopped) {
(true, Some(_)) => {
clock.stopped = None;
clock.started = now;
}
(false, None) => clock.stopped = Some(now),
_ => {}
});
let (started, stopped) = {
let c = clock.read(cx);
(c.started, c.stopped)
};
let reduced = motion::reduced_motion(cx);
let (rotation, offset, opacity) = match stopped {
None if reduced => {
let t = (now - started).as_secs_f32() / (ROTATION_PERIOD.as_secs_f32() * 2.);
let phase = t.fract();
window.request_animation_frame();
(
0.,
geometry.offset,
0.4 + 0.6 * (1. - (phase * 2. - 1.).abs()),
)
}
None => {
window.request_animation_frame();
(rotation_at(now - started), geometry.offset, 1.)
}
Some(_) if reduced => (0., geometry.offset, 1.),
Some(at) => match stop_frame(now - at, geometry.offset) {
Some((rotation, offset)) => {
window.request_animation_frame();
(rotation, offset, 1.)
}
None => (0., DASH, 1.),
},
};
let background = (geometry.background && offset < DASH).then(|| color(level.layer_accent()));
let stroke = color(ColorRole::Interactive);
let size = rems(geometry.size / crate::tokens::REM_PX);
div().flex_none().size(size).opacity(opacity).child(
canvas(
|_, _, _| {},
move |bounds, _, window, _| {
paint_ring(
bounds, geometry, offset, rotation, stroke, background, window,
)
},
)
.size_full(),
)
}
impl RenderOnce for Spinner {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
if self.is_inline() {
let indicator = match self.status {
SpinnerStatus::Active | SpinnerStatus::Inactive => ring(
id.clone(),
true,
self.status == SpinnerStatus::Active,
self.level,
window,
cx,
)
.into_any_element(),
SpinnerStatus::Finished => Icon::carbon(CarbonIcon::CheckmarkFilled)
.size(IconSize::S16)
.color(color(ColorRole::SupportSuccess))
.into_any_element(),
SpinnerStatus::Error => Icon::carbon(CarbonIcon::ErrorFilled)
.size(IconSize::S16)
.color(color(ColorRole::SupportError))
.into_any_element(),
};
return div()
.flex()
.flex_row()
.items_center()
.min_h(rems(2.))
.text_color(color(ColorRole::TextSecondary))
.child(div().flex().flex_none().mr(rems(0.5)).child(indicator))
.when_some(self.description, |this, text| {
this.child(div().type_style(TypeStyle::Label02).child(text))
})
.into_any_element();
}
let small = self.size == SpinnerSize::Sm;
let active = self.status != SpinnerStatus::Inactive;
let spinner = ring(id, small, active, self.level, window, cx);
if self.overlay && active {
let viewport = window.viewport_size();
return deferred(
anchored().position(point(px(0.), px(0.))).child(
div()
.w(viewport.width)
.h(viewport.height)
.flex()
.items_center()
.justify_center()
.bg(color(ColorRole::Overlay))
.occlude()
.child(spinner),
),
)
.with_priority(6000)
.into_any_element();
}
spinner.into_any_element()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn arcs_follow_bundle_css() {
let large = arc(false);
let small = arc(true);
assert_eq!((large.size, large.radius, large.stroke), (88., 44., 10.));
assert_eq!((small.size, small.radius, small.stroke), (16., 42., 16.));
assert!(
(DASH - TAU * 44.).abs() < 1e-3,
"dasharray is the r=44 circumference"
);
let (from, to) = dash_span(44., OFFSET_LARGE);
assert_eq!(from, 0.);
assert!(((to - from) - (DASH - OFFSET_LARGE)).abs() < 1e-3);
let (from, to) = dash_span(42., OFFSET_SMALL);
assert!((to - from - 132.70118).abs() < 1e-3);
let (from, to) = dash_span(42., OFFSET_BACKGROUND);
assert!((from - 22.).abs() < 1e-3 && (to - TAU * 42.).abs() < 1e-3);
}
#[test]
fn rotation_and_stop_sequence() {
assert_eq!(ROTATION_PERIOD.as_millis(), 690);
assert!((rotation_at(Duration::from_millis(345)) - 180.).abs() < 1e-3);
let (rot, off) = stop_frame(Duration::from_millis(350), OFFSET_LARGE).unwrap();
assert!(rot > 180. && off == OFFSET_LARGE);
let (rot, off) = stop_frame(Duration::from_millis(1050), OFFSET_LARGE).unwrap();
assert!(rot < 0. && off > OFFSET_LARGE && off < DASH);
assert!(stop_frame(Duration::from_millis(1400), OFFSET_LARGE).is_none());
assert!(Spinner::new("s").description("Loading").is_inline());
assert!(!Spinner::new("s").size(SpinnerSize::Sm).is_inline());
}
}