gpui-component/crates/ui/src/spinner.rs
Jason Lee 4dc10165fb
spinner: Rename Indicator to Spinner. (#1526)
## Break Change

- Renamed `Indicator` to `Spinner`.

```diff
- use gpui_component::indicator::Indicator;
+ use gpui_component::spinner::Spinner;
```
2025-11-06 11:36:39 +08:00

68 lines
1.8 KiB
Rust

use std::time::Duration;
use crate::{Icon, IconName, Sizable, Size};
use gpui::{
div, ease_in_out, percentage, prelude::FluentBuilder as _, Animation, AnimationExt as _, App,
Hsla, IntoElement, ParentElement, RenderOnce, Styled as _, Transformation, Window,
};
/// A cycling loading spinner.
#[derive(IntoElement)]
pub struct Spinner {
size: Size,
icon: Icon,
speed: Duration,
color: Option<Hsla>,
}
impl Spinner {
/// Create a new loading spinner.
pub fn new() -> Self {
Self {
size: Size::Medium,
speed: Duration::from_secs_f64(0.8),
icon: Icon::new(IconName::Loader),
color: None,
}
}
/// Set specified icon for the spinner.
///
/// Default is [`IconName::Loader`].
///
/// Please ensure the icon used is suitable for a loading spinner.
pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
self.icon = icon.into();
self
}
/// Set the icon color.
pub fn color(mut self, color: Hsla) -> Self {
self.color = Some(color);
self
}
}
impl Sizable for Spinner {
fn with_size(mut self, size: impl Into<Size>) -> Self {
self.size = size.into();
self
}
}
impl RenderOnce for Spinner {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
div()
.child(
self.icon
.with_size(self.size)
.when_some(self.color, |this, color| this.text_color(color))
.with_animation(
"circle",
Animation::new(self.speed).repeat().with_easing(ease_in_out),
|this, delta| this.transform(Transformation::rotate(percentage(delta))),
),
)
.into_element()
}
}