use crate::{
motion::{self, cubic_bezier_at},
theme::color,
tokens::ColorRole,
};
use gpui_kit::{
Animation, AnimationExt as _, App, Bounds, ElementId, Entity, Hsla, InteractiveElement as _,
IntoElement, ParentElement, PathBuilder, Pixels, RenderOnce, SharedString, Styled, Task,
Window, canvas, div, point, px,
};
use std::f32::consts::TAU;
use std::time::{Duration, Instant};
pub fn fmt_elapsed(secs: u64) -> String {
let (h, m, s) = (secs / 3600, (secs % 3600) / 60, secs % 60);
if h > 0 {
format!("{h}h {m}m")
} else if m > 0 {
format!("{m}m {s}s")
} else {
format!("{s}s")
}
}
pub fn fmt_elapsed_opt(secs: Option<u64>) -> String {
secs.map(fmt_elapsed).unwrap_or_default()
}
struct TickerState {
start: u64,
since: Instant,
value: u64,
running: bool,
_task: Option<Task<()>>,
}
impl TickerState {
fn now(&self) -> u64 {
if self.running {
self.value + self.since.elapsed().as_secs()
} else {
self.value
}
}
}
pub fn use_ticker(
id: impl Into<ElementId>,
start: u64,
running: bool,
window: &mut Window,
cx: &mut App,
) -> u64 {
let id: ElementId = id.into();
let state = window.use_keyed_state((id, "ticker"), cx, |_, _| TickerState {
start,
since: Instant::now(),
value: start,
running,
_task: None,
});
let value = state.update(cx, |s, _| {
if s.start != start {
s.start = start;
s.value = start;
s.since = Instant::now();
}
if s.running != running {
s.value = s.now();
s.since = Instant::now();
s.running = running;
}
s.now()
});
if running && state.read(cx)._task.is_none() {
let elapsed = state.read(cx).since.elapsed();
let next = Duration::from_secs(elapsed.as_secs() + 1).saturating_sub(elapsed);
let weak = state.downgrade();
let task = window.spawn(cx, async move |cx| {
cx.background_executor().timer(next).await;
let _ = cx.update(|_, cx| {
if let Some(state) = weak.upgrade() {
state.update(cx, |s, cx| {
s._task = None;
cx.notify();
});
}
});
});
state.update(cx, |s, _| s._task = Some(task));
} else if !running {
state.update(cx, |s, _| s._task = None);
}
value
}
struct FlashState {
on: bool,
generation: u64,
_task: Option<Task<()>>,
}
#[derive(Clone)]
pub struct Flash {
pub on: bool,
state: Entity<FlashState>,
duration: Duration,
}
impl Flash {
pub fn arm(&self, cx: &mut App) {
let duration = self.duration;
self.state.update(cx, |s, cx| {
s.on = true;
s.generation += 1;
let generation = s.generation;
s._task = Some(cx.spawn(async move |this, cx| {
cx.background_executor().timer(duration).await;
let _ = this.update(cx, |s, cx| {
if s.generation == generation {
s.on = false;
cx.notify();
}
});
}));
cx.notify();
});
}
}
pub fn use_flash(
id: impl Into<ElementId>,
duration: Duration,
window: &mut Window,
cx: &mut App,
) -> Flash {
let id: ElementId = id.into();
let state = window.use_keyed_state((id, "flash"), cx, |_, _| FlashState {
on: false,
generation: 0,
_task: None,
});
Flash {
on: state.read(cx).on,
state,
duration,
}
}
pub const FLASH_DURATION: Duration = Duration::from_millis(1500);
pub fn radio_step(key: &str, len: usize, current: Option<usize>) -> Option<usize> {
if len == 0 {
return None;
}
let i = current.map(|c| c as isize).unwrap_or(-1);
let n = len as isize;
match key {
"right" | "down" => Some(((i + 1).rem_euclid(n)) as usize),
"left" | "up" => Some(((i - 1 + n).rem_euclid(n)) as usize),
"home" => Some(0),
"end" => Some(len - 1),
_ => None,
}
}
pub fn active_after_close(active: usize, closed: usize, len: usize) -> usize {
let remaining = len.saturating_sub(1);
let next = if closed < active {
active - 1
} else if closed == active && closed + 1 >= len {
closed.saturating_sub(1)
} else {
active
};
next.min(remaining.saturating_sub(1))
}
pub fn visually_hidden(text: impl Into<SharedString>) -> gpui_kit::Div {
div()
.absolute()
.size(px(1.))
.overflow_hidden()
.opacity(0.)
.child(text.into())
}
pub fn warning_glyph(size: f32) -> gpui_kit::Div {
use super::craft::rem;
let k = size / 16.;
div()
.relative()
.flex_none()
.size(rem(size))
.child(
div()
.absolute()
.left(rem(7. * k))
.top(rem(6. * k))
.w(rem(2. * k))
.h(rem(7.5 * k))
.bg(ink()),
)
.child(
crate::icons::Icon::carbon(crate::icons::CarbonIcon::WarningAltFilled)
.size_length(rem(size))
.color(color(ColorRole::SupportWarning))
.absolute()
.top_0()
.left_0(),
)
}
pub fn ink() -> Hsla {
crate::theme::color_in(crate::tokens::CarbonTheme::White, ColorRole::TextPrimary)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Tone {
Interactive,
Warning,
Success,
Error,
Neutral,
}
impl Tone {
pub const fn name(self) -> &'static str {
match self {
Self::Interactive => "interactive",
Self::Warning => "warning",
Self::Success => "success",
Self::Error => "error",
Self::Neutral => "neutral",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Status {
Working,
NeedsYou,
Done,
Failed,
#[default]
Idle,
Queued,
Waiting,
Stopped,
}
impl Status {
pub const ALL: [Status; 8] = [
Self::Working,
Self::NeedsYou,
Self::Done,
Self::Failed,
Self::Idle,
Self::Queued,
Self::Waiting,
Self::Stopped,
];
pub const fn name(self) -> &'static str {
match self {
Self::Working => "working",
Self::NeedsYou => "needs-you",
Self::Done => "done",
Self::Failed => "failed",
Self::Idle => "idle",
Self::Queued => "queued",
Self::Waiting => "waiting",
Self::Stopped => "stopped",
}
}
pub fn from_name(name: &str) -> Self {
Self::ALL
.into_iter()
.find(|s| s.name() == name)
.unwrap_or_default()
}
pub const fn label(self) -> &'static str {
match self {
Self::Working => "Working",
Self::NeedsYou => "Needs you",
Self::Done => "Done",
Self::Failed => "Failed",
Self::Idle => "Idle",
Self::Queued => "Queued",
Self::Waiting => "Waiting",
Self::Stopped => "Stopped",
}
}
pub const fn tone(self) -> Tone {
match self {
Self::Working => Tone::Interactive,
Self::NeedsYou | Self::Waiting => Tone::Warning,
Self::Done => Tone::Success,
Self::Failed => Tone::Error,
Self::Idle | Self::Queued | Self::Stopped => Tone::Neutral,
}
}
}
pub const PULSE_PERIOD: Duration = Duration::from_millis(1200);
pub const PULSE_LOW: f32 = 0.35;
pub const EASE_IN_OUT: [f32; 4] = [0.42, 0., 0.58, 1.];
pub const EASE: [f32; 4] = [0.25, 0.1, 0.25, 1.];
pub fn pulse_opacity(t: f32, curve: [f32; 4]) -> f32 {
let [x1, y1, x2, y2] = curve;
let t = t.rem_euclid(1.);
let (from, to, local) = if t < 0.5 {
(1., PULSE_LOW, t * 2.)
} else {
(PULSE_LOW, 1., (t - 0.5) * 2.)
};
from + (to - from) * cubic_bezier_at(x1, y1, x2, y2, local)
}
pub fn pulse<E: IntoElement + Styled + 'static>(
element: E,
id: impl Into<ElementId>,
curve: [f32; 4],
) -> impl IntoElement {
element.with_animation(id, Animation::new(PULSE_PERIOD).repeat(), move |el, t| {
el.opacity(pulse_opacity(t, curve))
})
}
pub const SPIN_PERIOD: Duration = Duration::from_millis(690);
#[deprecated(note = "ghost.css overrides it: `.t3-spin svg` pulses under reduced motion")]
pub const SPIN_PERIOD_REDUCED: Duration = Duration::from_millis(2000);
pub const REDUCED_PULSE_PERIOD: Duration = Duration::from_millis(1400);
pub fn reduced_pulse_opacity(t: f32) -> f32 {
pulse_opacity(t, EASE_IN_OUT)
}
pub fn loop_phase(elapsed: Duration, period: Duration) -> f32 {
let period = period.as_nanos();
if period == 0 {
return 0.;
}
(elapsed.as_nanos() % period) as f32 / period as f32
}
fn loop_epoch() -> Instant {
static EPOCH: std::sync::OnceLock<Instant> = std::sync::OnceLock::new();
*EPOCH.get_or_init(Instant::now)
}
pub fn endless_phase(period: Duration, window: &mut Window) -> f32 {
window.request_animation_frame();
loop_phase(loop_epoch().elapsed(), period)
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SpinFrame {
pub turn: f32,
pub opacity: f32,
}
pub fn spin_frame(elapsed: Duration, period: Duration, reduced: bool) -> SpinFrame {
if reduced {
SpinFrame {
turn: 0.,
opacity: reduced_pulse_opacity(loop_phase(elapsed, REDUCED_PULSE_PERIOD)),
}
} else {
SpinFrame {
turn: loop_phase(elapsed, period),
opacity: 1.,
}
}
}
pub fn spin_frame_now(period: Duration, reduced: bool, window: &mut Window) -> SpinFrame {
window.request_animation_frame();
spin_frame(loop_epoch().elapsed(), period, reduced)
}
pub mod spin_geometry {
pub const RADIUS: f32 = 6.5;
pub const STROKE: f32 = 2.25;
pub const DASH: f32 = 40.8;
pub const OFFSET: f32 = 28.;
pub const ARC_LENGTH: f32 = DASH - OFFSET;
pub const SWEEP: f32 = ARC_LENGTH / RADIUS;
}
pub(crate) fn stroke_arc(
window: &mut Window,
center: gpui_kit::Point<Pixels>,
radius: f32,
width: f32,
start: f32,
sweep: f32,
color: Hsla,
) {
if sweep <= 0.001 || color.a == 0. {
return;
}
let steps = ((sweep / (TAU / 96.)).ceil() as usize).max(2);
let mut path = PathBuilder::stroke(px(width));
for i in 0..=steps {
let a = start + sweep * i as f32 / steps as f32;
let p = point(
center.x + px(radius * a.cos()),
center.y + px(radius * 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);
}
}
fn paint_spin(bounds: Bounds<Pixels>, turn: f32, arc: Hsla, track: Hsla, window: &mut Window) {
use spin_geometry::*;
let scale = f32::from(bounds.size.width) / 16.;
let center = bounds.center();
let (r, w) = (RADIUS * scale, STROKE * scale);
stroke_arc(window, center, r, w, 0., TAU, track);
stroke_arc(window, center, r, w, turn * TAU, SWEEP, arc);
}
#[derive(IntoElement)]
pub struct Spin {
id: ElementId,
size: f32,
label: Option<SharedString>,
color: Option<Hsla>,
track: Option<Hsla>,
}
impl Spin {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
size: 16.,
label: None,
color: None,
track: None,
}
}
pub fn size(mut self, size: f32) -> Self {
self.size = size;
self
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
pub fn color(mut self, color: Hsla) -> Self {
self.color = Some(color);
self
}
pub fn track(mut self, track: Hsla) -> Self {
self.track = Some(track);
self
}
}
impl RenderOnce for Spin {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let arc = self.color.unwrap_or_else(|| color(ColorRole::Interactive));
let track = self
.track
.unwrap_or_else(|| color(ColorRole::LayerAccent01));
let frame = spin_frame_now(SPIN_PERIOD, motion::reduced_motion(cx), window);
let size = super::craft::rem(self.size);
div()
.id(self.id)
.relative()
.flex_none()
.size(size)
.children(self.label.map(visually_hidden))
.child(
div().size_full().opacity(frame.opacity).child(
canvas(
|_, _, _| {},
move |bounds, _, window, _| {
paint_spin(bounds, frame.turn, arc, track, window)
},
)
.size_full(),
),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn elapsed_follows_fmt_elapsed() {
assert_eq!(fmt_elapsed(74), "1m 14s");
assert_eq!(fmt_elapsed(9), "9s");
assert_eq!(fmt_elapsed(0), "0s");
assert_eq!(fmt_elapsed(3600), "1h 0m");
assert_eq!(fmt_elapsed(3725), "1h 2m");
assert_eq!(fmt_elapsed_opt(None), "");
}
#[test]
fn status_vocabulary_follows_base_jsx() {
assert_eq!(Status::from_name("needs-you"), Status::NeedsYou);
assert_eq!(Status::from_name("bogus"), Status::Idle);
assert_eq!(Status::NeedsYou.label(), "Needs you");
assert_eq!(Status::Waiting.tone(), Tone::Warning);
assert_eq!(Status::Working.tone(), Tone::Interactive);
assert_eq!(Status::Queued.tone(), Tone::Neutral);
assert_eq!(Status::Failed.tone().name(), "error");
}
#[test]
fn radio_keys_wrap_and_jump() {
assert_eq!(radio_step("right", 3, Some(2)), Some(0));
assert_eq!(radio_step("up", 3, Some(0)), Some(2));
assert_eq!(radio_step("home", 3, Some(1)), Some(0));
assert_eq!(radio_step("end", 3, Some(0)), Some(2));
assert_eq!(radio_step("down", 3, None), Some(0));
assert_eq!(radio_step("a", 3, Some(0)), None);
assert_eq!(radio_step("down", 0, None), None);
}
#[test]
fn closing_a_tab_moves_the_active_index_like_a_browser() {
assert_eq!(active_after_close(1, 1, 2), 0);
assert_eq!(active_after_close(1, 1, 3), 1);
assert_eq!(active_after_close(0, 0, 3), 0);
assert_eq!(active_after_close(2, 0, 3), 1);
assert_eq!(active_after_close(1, 0, 2), 0);
assert_eq!(active_after_close(0, 1, 2), 0);
assert_eq!(active_after_close(1, 2, 4), 1);
assert_eq!(active_after_close(0, 0, 1), 0);
assert_eq!(active_after_close(5, 0, 3), 1);
}
#[test]
fn pulse_dips_to_35_percent_halfway() {
assert!((pulse_opacity(0., EASE_IN_OUT) - 1.).abs() < 1e-4);
assert!((pulse_opacity(0.5, EASE_IN_OUT) - 0.35).abs() < 1e-4);
assert!((pulse_opacity(0.999, EASE) - 1.).abs() < 1e-2);
assert!(pulse_opacity(0.25, EASE_IN_OUT) < 1.);
}
#[test]
fn spin_arc_follows_t3_css() {
assert!((spin_geometry::ARC_LENGTH - 12.8).abs() < 1e-4);
assert!((spin_geometry::SWEEP.to_degrees() - 112.8).abs() < 0.1);
assert_eq!(SPIN_PERIOD.as_millis(), 690);
}
#[test]
fn loop_phase_wraps_each_period() {
let ms = Duration::from_millis;
assert_eq!(loop_phase(ms(0), SPIN_PERIOD), 0.);
assert!((loop_phase(ms(345), SPIN_PERIOD) - 0.5).abs() < 1e-4);
assert!(loop_phase(ms(690), SPIN_PERIOD).abs() < 1e-4);
assert!((loop_phase(ms(690 * 1000 + 69), SPIN_PERIOD) - 0.1).abs() < 1e-3);
assert_eq!(loop_phase(ms(5), Duration::ZERO), 0.);
}
#[test]
fn spinner_turns_at_full_motion() {
let ms = Duration::from_millis;
let quarter = spin_frame(ms(172) + Duration::from_micros(500), SPIN_PERIOD, false);
assert!((quarter.turn - 0.25).abs() < 1e-3);
assert_eq!(quarter.opacity, 1.);
let half = spin_frame(ms(400), Duration::from_millis(800), false);
assert!((half.turn - 0.5).abs() < 1e-4);
}
#[test]
fn spinner_pulses_under_reduced_motion() {
let ms = Duration::from_millis;
assert_eq!(REDUCED_PULSE_PERIOD.as_millis(), 1400);
let start = spin_frame(ms(0), SPIN_PERIOD, true);
assert_eq!(start.turn, 0.);
assert!((start.opacity - 1.).abs() < 1e-4);
let low = spin_frame(ms(700), SPIN_PERIOD, true);
assert_eq!(low.turn, 0.);
assert!((low.opacity - 0.35).abs() < 1e-4);
let again = spin_frame(ms(1400 + 700), SPIN_PERIOD, true);
assert!((again.opacity - 0.35).abs() < 1e-3);
let between = spin_frame(ms(350), SPIN_PERIOD, true).opacity;
assert!(between > 0.35 && between < 1.);
assert!((reduced_pulse_opacity(0.25) - between).abs() < 1e-4);
}
}