fc_ui/capabilities/primitives/
spinner.rs1use crate::capabilities::foundation::theme::use_theme;
2use gpui::{prelude::FluentBuilder as _, *};
3
4#[derive(Copy, Clone, Debug, PartialEq, Eq)]
5pub enum SpinnerSize {
6 Xs,
7 Sm,
8 Md,
9 Lg,
10 Xl,
11}
12
13impl SpinnerSize {
14 fn to_pixels(self) -> Pixels {
15 match self {
16 SpinnerSize::Xs => px(16.0),
17 SpinnerSize::Sm => px(20.0),
18 SpinnerSize::Md => px(24.0),
19 SpinnerSize::Lg => px(32.0),
20 SpinnerSize::Xl => px(48.0),
21 }
22 }
23}
24
25#[derive(Copy, Clone, Debug, PartialEq, Eq)]
26pub enum SpinnerVariant {
27 Default,
28 Primary,
29 Secondary,
30 Muted,
31}
32
33#[derive(IntoElement)]
34pub struct Spinner {
35 size: SpinnerSize,
36 variant: SpinnerVariant,
37 label: Option<SharedString>,
38 style: StyleRefinement,
39}
40
41impl Spinner {
42 pub fn new() -> Self {
43 Self {
44 size: SpinnerSize::Md,
45 variant: SpinnerVariant::Default,
46 label: None,
47 style: StyleRefinement::default(),
48 }
49 }
50
51 pub fn size(mut self, size: SpinnerSize) -> Self {
52 self.size = size;
53 self
54 }
55
56 pub fn variant(mut self, variant: SpinnerVariant) -> Self {
57 self.variant = variant;
58 self
59 }
60
61 pub fn label(mut self, label: impl Into<SharedString>) -> Self {
62 self.label = Some(label.into());
63 self
64 }
65}
66
67impl Default for Spinner {
68 fn default() -> Self {
69 Self::new()
70 }
71}
72
73impl Styled for Spinner {
74 fn style(&mut self) -> &mut StyleRefinement {
75 &mut self.style
76 }
77}
78
79impl RenderOnce for Spinner {
80 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
81 let theme = use_theme(cx);
82 let user_style = self.style;
83 let size_px = self.size.to_pixels();
84 let stroke_width = size_px * 0.15;
85
86 let color = match self.variant {
87 SpinnerVariant::Default => theme.tokens.foreground,
88 SpinnerVariant::Primary => theme.tokens.primary,
89 SpinnerVariant::Secondary => theme.tokens.secondary,
90 SpinnerVariant::Muted => theme.tokens.muted_foreground,
91 };
92
93 let center = size_px * 0.5;
94 let path_radius = (size_px - stroke_width) * 0.5;
95 let dot_size = stroke_width * 1.2;
96 let dot_offset = dot_size * 0.5;
97
98 div()
99 .flex()
100 .flex_col()
101 .items_center()
102 .gap(px(8.0))
103 .map(|mut this| {
104 this.style().refine(&user_style);
105 this
106 })
107 .child(
108 div()
109 .size(size_px)
110 .relative()
111 .child(
112 div()
113 .absolute()
114 .inset_0()
115 .border(stroke_width)
116 .border_color(theme.tokens.muted)
117 .rounded(px(9999.0)),
118 )
119 .child(
120 div()
121 .absolute()
122 .size(dot_size)
123 .rounded(px(9999.0))
124 .bg(color)
125 .with_animation(
126 "spinner-orbit",
127 Animation::new(std::time::Duration::from_millis(700))
128 .repeat()
129 .with_easing(
130 crate::capabilities::motion::animations::easings::linear,
131 ),
132 move |dot, delta| {
133 let angle = delta * std::f32::consts::TAU;
134 let x = center + path_radius * angle.cos() - dot_offset;
135 let y = center + path_radius * angle.sin() - dot_offset;
136 dot.left(x).top(y)
137 },
138 ),
139 ),
140 )
141 .when_some(self.label, |d, label| {
142 d.child(
143 div()
144 .text_size(px(12.0))
145 .text_color(theme.tokens.muted_foreground)
146 .font_family(theme.tokens.font_family.clone())
147 .child(label),
148 )
149 })
150 }
151}