From 7691a5a7e1c285e9223e6f564a0e061ad0730536 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Sat, 21 Nov 2026 13:58:27 +0700 Subject: [PATCH] refactor: simplify spinner to icon-only --- crates/ui/src/components/button.rs | 2 - crates/ui/src/components/dropdown_menu.rs | 1 - crates/ui/src/components/speech.rs | 3 +- crates/ui/src/components/spinner.rs | 203 ++-------------------- 4 files changed, 13 insertions(+), 196 deletions(-) diff --git a/crates/ui/src/components/button.rs b/crates/ui/src/components/button.rs index 50c70da..555b368 100644 --- a/crates/ui/src/components/button.rs +++ b/crates/ui/src/components/button.rs @@ -25,7 +25,6 @@ use crate::components::common::{ }; use crate::components::spinner::Spinner; use crate::icons::Icon; -use crate::spinner::SpinnerVariant; use crate::theme::{ActiveTheme as _, DISABLED_OPACITY, Size}; /// The single activation handler, shared by pointer, Enter, and Space. @@ -382,7 +381,6 @@ impl RenderOnce for Button { h_flex().absolute().inset_0().justify_center().child( Spinner::new((self.id.clone(), "spinner")) .with_size(self.size) - .variant(SpinnerVariant::Icon) .color(foreground), ), ) diff --git a/crates/ui/src/components/dropdown_menu.rs b/crates/ui/src/components/dropdown_menu.rs index 7d98d12..eaf59ef 100644 --- a/crates/ui/src/components/dropdown_menu.rs +++ b/crates/ui/src/components/dropdown_menu.rs @@ -285,7 +285,6 @@ impl RenderOnce for DropdownMenu { h_flex().absolute().inset_0().justify_center().child( Spinner::new((self.id.clone(), "spinner")) .with_size(self.size) - .variant(crate::components::spinner::SpinnerVariant::Icon) .color(foreground), ), ) diff --git a/crates/ui/src/components/speech.rs b/crates/ui/src/components/speech.rs index d56b112..d603382 100644 --- a/crates/ui/src/components/speech.rs +++ b/crates/ui/src/components/speech.rs @@ -36,7 +36,7 @@ use gpui::{ }; use crate::components::common::{ControlSizing, Sizable, StyledExt as _, focus_ring, h_flex}; -use crate::components::spinner::{Spinner, SpinnerVariant}; +use crate::components::spinner::Spinner; use crate::components::tooltip::Tooltip; use crate::icons::{IconRole, icon}; use crate::theme::{ActiveTheme as _, DISABLED_OPACITY, SPEECH_WAVEFORM_WIDTH, Size}; @@ -807,7 +807,6 @@ impl RenderOnce for SpeechButton { .child(if stopping { Spinner::new(("speech-spinner", self.speech.entity_id())) .with_size(self.size) - .variant(SpinnerVariant::Icon) .color(foreground) .into_any_element() } else if capturing { diff --git a/crates/ui/src/components/spinner.rs b/crates/ui/src/components/spinner.rs index 68f8a2b..4e3fbb5 100644 --- a/crates/ui/src/components/spinner.rs +++ b/crates/ui/src/components/spinner.rs @@ -1,54 +1,28 @@ -//! Renders a loading ring that spins for unknown work, or holds a determinate -//! arc for known progress. +//! Renders a loader icon that spins for unknown work. //! //! # Examples //! //! ```no_run -//! use ui::spinner::{Spinner, SpinnerVariant}; +//! use ui::spinner::Spinner; //! //! let loading = Spinner::new("loading"); -//! let progress = Spinner::new("progress").value(0.5); -//! let glyph = Spinner::new("glyph").variant(SpinnerVariant::Icon); //! ``` -use std::cell::Cell; -use std::f32::consts::{PI, TAU}; -use std::rc::Rc; - use gpui::prelude::FluentBuilder as _; use gpui::{ - Animation, AnimationElement, AnimationExt as _, App, Canvas, ElementId, Hsla, - InteractiveElement as _, IntoElement, ParentElement as _, Path, PathBuilder, Pixels, Point, - RenderOnce, Role, SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled as _, - Window, canvas, div, point, px, + App, ElementId, Hsla, InteractiveElement as _, IntoElement, ParentElement as _, RenderOnce, + Role, SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled as _, Window, div, }; use crate::components::common::{Sizable, StyledExt as _}; -use crate::icons::{Icon, IconRole, icon}; +use crate::icons::{IconRole, icon}; use crate::theme::{ActiveTheme as _, SPINNER_PERIOD, Size}; -/// The share of the circle the spinning arc paints. -const SPIN_SWEEP: f32 = 0.75; -/// The ring stroke as a share of the side. -const RING_STROKE: f32 = 0.125; - -/// The visual language of a [`Spinner`]. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub enum SpinnerVariant { - /// The painted arc ring. - #[default] - Ring, - /// The platform loader glyph, rotated. - Icon, -} - -/// A loading ring that spins for unknown work or paints a determinate arc. +/// A loader icon that spins for unknown work. #[derive(IntoElement)] #[must_use] pub struct Spinner { id: ElementId, - value: Option, - variant: SpinnerVariant, size: Size, color: Option, a11y_label: Option, @@ -56,15 +30,13 @@ pub struct Spinner { } impl Spinner { - /// Creates a spinning ring. + /// Creates a spinning loader icon. pub fn new(id: I) -> Self where I: Into, { Self { id: id.into(), - value: None, - variant: SpinnerVariant::default(), size: Size::default(), color: None, a11y_label: None, @@ -72,24 +44,12 @@ impl Spinner { } } - /// Overrides the arc color, for accent-filled surfaces. + /// Overrides the icon color, for accent-filled surfaces. pub fn color(mut self, color: Hsla) -> Self { self.color = Some(color); self } - /// Sets the spinner's visual language. - pub fn variant(mut self, variant: SpinnerVariant) -> Self { - self.variant = variant; - self - } - - /// Paints a determinate arc at `value` (`0..=1`) instead of spinning. - pub fn value(mut self, value: f32) -> Self { - self.value = Some(value); - self - } - /// Sets the accessibility label. pub fn accessibility_label(mut self, label: L) -> Self where @@ -116,10 +76,7 @@ impl RenderOnce for Spinner { fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { let theme = cx.theme(); let side = self.size.icon_rems() * window.rem_size(); - let stroke = side * RING_STROKE; - let track = theme.colors.border; let accent = self.color.unwrap_or(theme.ramps.primary.base); - let value = self.value.map(|value| value.clamp(0.0, 1.0)); div() .id(self.id) @@ -128,147 +85,11 @@ impl RenderOnce for Spinner { .refine_style(&self.style) .role(Role::ProgressIndicator) .when_some(self.a11y_label, |spinner, label| spinner.aria_label(label)) - .when_some(value, |spinner, value| { - spinner - .aria_numeric_value(f64::from(value)) - .aria_min_numeric_value(0.0) - .aria_max_numeric_value(1.0) - }) - .child(match (self.variant, value) { - (SpinnerVariant::Ring, Some(value)) => { - determinate_ring(side, stroke, track, accent, value).into_any_element() - } - (SpinnerVariant::Ring, None) => { - spinning_ring(side, stroke, accent).into_any_element() - } - (SpinnerVariant::Icon, Some(_)) => icon(IconRole::Loader) + .child( + icon(IconRole::Loader) .size(side) .color(accent) - .into_any_element(), - (SpinnerVariant::Icon, None) => spinning_icon(side, accent).into_any_element(), - }) + .with_rotation_animation(SPINNER_PERIOD), + ) } } - -fn determinate_ring( - side: Pixels, - stroke: Pixels, - track: Hsla, - accent: Hsla, - value: f32, -) -> Canvas<()> { - canvas( - |_, _, _| (), - move |bounds, (), window, _| { - let center = bounds.center(); - let radius = bounds.size.width / 2.0 - stroke / 2.0; - paint_circle(center, radius, stroke, track, window); - if value >= 1.0 { - paint_circle(center, radius, stroke, accent, window); - } else if value > 0.0 - && let Some(path) = arc_path(center, radius, stroke, 0.0, value * TAU) - { - window.paint_path(path, accent); - } - }, - ) - .size(side) -} - -fn spinning_ring(side: Pixels, stroke: Pixels, accent: Hsla) -> AnimationElement> { - let angle = Rc::new(Cell::new(0.0)); - let spun = angle.clone(); - canvas( - |_, _, _| (), - move |bounds, (), window, _| { - let radius = bounds.size.width / 2.0 - stroke / 2.0; - if let Some(path) = arc_path( - bounds.center(), - radius, - stroke, - spun.get(), - SPIN_SWEEP * TAU, - ) { - window.paint_path(path, accent); - } - }, - ) - .size(side) - .with_animation( - "spin", - Animation::new(SPINNER_PERIOD).repeat().with_max_fps(60.), - move |ring, delta| { - angle.set(delta * TAU); - ring - }, - ) -} - -fn spinning_icon(side: Pixels, accent: Hsla) -> Icon { - icon(IconRole::Loader) - .size(side) - .color(accent) - .with_rotation_animation(SPINNER_PERIOD) -} - -/// Paints a stroked circle into the window. -fn paint_circle( - center: Point, - radius: Pixels, - stroke: Pixels, - color: Hsla, - window: &mut Window, -) { - if let Some(path) = circle_path(center, radius, stroke) { - window.paint_path(path, color); - } -} - -/// A point on the ring at `angle`, clockwise from 12 o'clock. -fn ring_point(center: Point, radius: Pixels, angle: f32) -> Point { - point( - center.x + radius * angle.sin(), - center.y - radius * angle.cos(), - ) -} - -/// A stroked arc from `start`, sweeping clockwise by `sweep` radians. -fn arc_path( - center: Point, - radius: Pixels, - stroke: Pixels, - start: f32, - sweep: f32, -) -> Option { - let mut builder = PathBuilder::stroke(stroke); - builder.move_to(ring_point(center, radius, start)); - builder.arc_to( - point(radius, radius), - px(0.), - sweep > PI, - true, - ring_point(center, radius, start + sweep), - ); - builder.build().ok() -} - -/// A stroked full circle; `None` on degenerate geometry. -fn circle_path(center: Point, radius: Pixels, stroke: Pixels) -> Option { - let mut builder = PathBuilder::stroke(stroke); - builder.move_to(ring_point(center, radius, 0.0)); - builder.arc_to( - point(radius, radius), - px(0.), - false, - true, - ring_point(center, radius, PI), - ); - builder.arc_to( - point(radius, radius), - px(0.), - false, - true, - ring_point(center, radius, TAU), - ); - builder.build().ok() -}