refactor: simplify spinner to icon-only
This commit is contained in:
@@ -25,7 +25,6 @@ use crate::components::common::{
|
|||||||
};
|
};
|
||||||
use crate::components::spinner::Spinner;
|
use crate::components::spinner::Spinner;
|
||||||
use crate::icons::Icon;
|
use crate::icons::Icon;
|
||||||
use crate::spinner::SpinnerVariant;
|
|
||||||
use crate::theme::{ActiveTheme as _, DISABLED_OPACITY, Size};
|
use crate::theme::{ActiveTheme as _, DISABLED_OPACITY, Size};
|
||||||
|
|
||||||
/// The single activation handler, shared by pointer, Enter, and Space.
|
/// 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(
|
h_flex().absolute().inset_0().justify_center().child(
|
||||||
Spinner::new((self.id.clone(), "spinner"))
|
Spinner::new((self.id.clone(), "spinner"))
|
||||||
.with_size(self.size)
|
.with_size(self.size)
|
||||||
.variant(SpinnerVariant::Icon)
|
|
||||||
.color(foreground),
|
.color(foreground),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -285,7 +285,6 @@ impl RenderOnce for DropdownMenu {
|
|||||||
h_flex().absolute().inset_0().justify_center().child(
|
h_flex().absolute().inset_0().justify_center().child(
|
||||||
Spinner::new((self.id.clone(), "spinner"))
|
Spinner::new((self.id.clone(), "spinner"))
|
||||||
.with_size(self.size)
|
.with_size(self.size)
|
||||||
.variant(crate::components::spinner::SpinnerVariant::Icon)
|
|
||||||
.color(foreground),
|
.color(foreground),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ use gpui::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::components::common::{ControlSizing, Sizable, StyledExt as _, focus_ring, h_flex};
|
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::components::tooltip::Tooltip;
|
||||||
use crate::icons::{IconRole, icon};
|
use crate::icons::{IconRole, icon};
|
||||||
use crate::theme::{ActiveTheme as _, DISABLED_OPACITY, SPEECH_WAVEFORM_WIDTH, Size};
|
use crate::theme::{ActiveTheme as _, DISABLED_OPACITY, SPEECH_WAVEFORM_WIDTH, Size};
|
||||||
@@ -807,7 +807,6 @@ impl RenderOnce for SpeechButton {
|
|||||||
.child(if stopping {
|
.child(if stopping {
|
||||||
Spinner::new(("speech-spinner", self.speech.entity_id()))
|
Spinner::new(("speech-spinner", self.speech.entity_id()))
|
||||||
.with_size(self.size)
|
.with_size(self.size)
|
||||||
.variant(SpinnerVariant::Icon)
|
|
||||||
.color(foreground)
|
.color(foreground)
|
||||||
.into_any_element()
|
.into_any_element()
|
||||||
} else if capturing {
|
} else if capturing {
|
||||||
|
|||||||
@@ -1,54 +1,28 @@
|
|||||||
//! Renders a loading ring that spins for unknown work, or holds a determinate
|
//! Renders a loader icon that spins for unknown work.
|
||||||
//! arc for known progress.
|
|
||||||
//!
|
//!
|
||||||
//! # Examples
|
//! # Examples
|
||||||
//!
|
//!
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! use ui::spinner::{Spinner, SpinnerVariant};
|
//! use ui::spinner::Spinner;
|
||||||
//!
|
//!
|
||||||
//! let loading = Spinner::new("loading");
|
//! 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::prelude::FluentBuilder as _;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
Animation, AnimationElement, AnimationExt as _, App, Canvas, ElementId, Hsla,
|
App, ElementId, Hsla, InteractiveElement as _, IntoElement, ParentElement as _, RenderOnce,
|
||||||
InteractiveElement as _, IntoElement, ParentElement as _, Path, PathBuilder, Pixels, Point,
|
Role, SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled as _, Window, div,
|
||||||
RenderOnce, Role, SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled as _,
|
|
||||||
Window, canvas, div, point, px,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::components::common::{Sizable, StyledExt as _};
|
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};
|
use crate::theme::{ActiveTheme as _, SPINNER_PERIOD, Size};
|
||||||
|
|
||||||
/// The share of the circle the spinning arc paints.
|
/// A loader icon that spins for unknown work.
|
||||||
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.
|
|
||||||
#[derive(IntoElement)]
|
#[derive(IntoElement)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub struct Spinner {
|
pub struct Spinner {
|
||||||
id: ElementId,
|
id: ElementId,
|
||||||
value: Option<f32>,
|
|
||||||
variant: SpinnerVariant,
|
|
||||||
size: Size,
|
size: Size,
|
||||||
color: Option<Hsla>,
|
color: Option<Hsla>,
|
||||||
a11y_label: Option<SharedString>,
|
a11y_label: Option<SharedString>,
|
||||||
@@ -56,15 +30,13 @@ pub struct Spinner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Spinner {
|
impl Spinner {
|
||||||
/// Creates a spinning ring.
|
/// Creates a spinning loader icon.
|
||||||
pub fn new<I>(id: I) -> Self
|
pub fn new<I>(id: I) -> Self
|
||||||
where
|
where
|
||||||
I: Into<ElementId>,
|
I: Into<ElementId>,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
id: id.into(),
|
id: id.into(),
|
||||||
value: None,
|
|
||||||
variant: SpinnerVariant::default(),
|
|
||||||
size: Size::default(),
|
size: Size::default(),
|
||||||
color: None,
|
color: None,
|
||||||
a11y_label: 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 {
|
pub fn color(mut self, color: Hsla) -> Self {
|
||||||
self.color = Some(color);
|
self.color = Some(color);
|
||||||
self
|
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.
|
/// Sets the accessibility label.
|
||||||
pub fn accessibility_label<L>(mut self, label: L) -> Self
|
pub fn accessibility_label<L>(mut self, label: L) -> Self
|
||||||
where
|
where
|
||||||
@@ -116,10 +76,7 @@ impl RenderOnce for Spinner {
|
|||||||
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
|
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
|
||||||
let theme = cx.theme();
|
let theme = cx.theme();
|
||||||
let side = self.size.icon_rems() * window.rem_size();
|
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 accent = self.color.unwrap_or(theme.ramps.primary.base);
|
||||||
let value = self.value.map(|value| value.clamp(0.0, 1.0));
|
|
||||||
|
|
||||||
div()
|
div()
|
||||||
.id(self.id)
|
.id(self.id)
|
||||||
@@ -128,147 +85,11 @@ impl RenderOnce for Spinner {
|
|||||||
.refine_style(&self.style)
|
.refine_style(&self.style)
|
||||||
.role(Role::ProgressIndicator)
|
.role(Role::ProgressIndicator)
|
||||||
.when_some(self.a11y_label, |spinner, label| spinner.aria_label(label))
|
.when_some(self.a11y_label, |spinner, label| spinner.aria_label(label))
|
||||||
.when_some(value, |spinner, value| {
|
.child(
|
||||||
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)
|
|
||||||
.size(side)
|
|
||||||
.color(accent)
|
|
||||||
.into_any_element(),
|
|
||||||
(SpinnerVariant::Icon, None) => spinning_icon(side, accent).into_any_element(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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<Canvas<()>> {
|
|
||||||
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)
|
icon(IconRole::Loader)
|
||||||
.size(side)
|
.size(side)
|
||||||
.color(accent)
|
.color(accent)
|
||||||
.with_rotation_animation(SPINNER_PERIOD)
|
.with_rotation_animation(SPINNER_PERIOD),
|
||||||
}
|
)
|
||||||
|
|
||||||
/// Paints a stroked circle into the window.
|
|
||||||
fn paint_circle(
|
|
||||||
center: Point<Pixels>,
|
|
||||||
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<Pixels>, radius: Pixels, angle: f32) -> Point<Pixels> {
|
|
||||||
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<Pixels>,
|
|
||||||
radius: Pixels,
|
|
||||||
stroke: Pixels,
|
|
||||||
start: f32,
|
|
||||||
sweep: f32,
|
|
||||||
) -> Option<Path> {
|
|
||||||
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<Pixels>, radius: Pixels, stroke: Pixels) -> Option<Path> {
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user