gpui_component/
spinner.rs1use crate::{Icon, IconName, Sizable, Size};
2use gpui::{
3 Animation, AnimationExt as _, App, Hsla, IntoElement, ParentElement, RenderOnce, Styled as _,
4 Transformation, Window, div, ease_in_out, percentage, prelude::FluentBuilder as _,
5};
6use instant::Duration;
7
8#[derive(IntoElement)]
10pub struct Spinner {
11 size: Size,
12 icon: Icon,
13 speed: Duration,
14 easing: Box<dyn Fn(f32) -> f32>,
15 color: Option<Hsla>,
16}
17
18impl Spinner {
19 pub fn new() -> Self {
21 Self {
22 size: Size::Medium,
23 speed: Duration::from_secs_f64(0.8),
24 easing: Box::new(ease_in_out),
25 icon: Icon::new(IconName::Loader),
26 color: None,
27 }
28 }
29
30 pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
36 self.icon = icon.into();
37 self
38 }
39
40 pub fn color(mut self, color: Hsla) -> Self {
42 self.color = Some(color);
43 self
44 }
45
46 pub fn ease(mut self, easing: impl Fn(f32) -> f32 + 'static) -> Self {
48 self.easing = Box::new(easing);
49 self
50 }
51}
52
53impl Sizable for Spinner {
54 fn with_size(mut self, size: impl Into<Size>) -> Self {
55 self.size = size.into();
56 self
57 }
58}
59
60impl RenderOnce for Spinner {
61 fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
62 div()
63 .child(
64 self.icon
65 .with_size(self.size)
66 .when_some(self.color, |this, color| this.text_color(color))
67 .with_animation(
68 "circle",
69 Animation::new(self.speed).repeat().with_easing(self.easing),
70 |this, delta| this.transform(Transformation::rotate(percentage(delta))),
71 ),
72 )
73 .into_element()
74 }
75}