1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! Spinner widget
//!
//! A loading spinner for indicating ongoing operations.
//! Purely visual, not focusable.
//!
//! # Example
//!
//! ```ignore
//! use ccf_gpui_widgets::widgets::{Spinner, SpinnerSize};
//!
//! // Small inline spinner
//! let small = cx.new(|_cx| {
//! Spinner::new()
//! .size(SpinnerSize::Small)
//! });
//!
//! // Medium spinner with label
//! let loading = cx.new(|_cx| {
//! Spinner::new()
//! .size(SpinnerSize::Medium)
//! .label("Loading...")
//! });
//!
//! // Large centered spinner
//! let large = cx.new(|_cx| {
//! Spinner::new()
//! .size(SpinnerSize::Large)
//! });
//! ```
use std::f32::consts::PI;
use std::time::Duration;
use gpui::prelude::*;
use gpui::*;
use crate::theme::{get_theme_or, Theme};
/// Spinner size presets
#[derive(Clone, Copy, Debug, Default)]
pub enum SpinnerSize {
/// Small (16px)
Small,
/// Medium (24px, default)
#[default]
Medium,
/// Large (32px)
Large,
/// Custom size in pixels
Custom(f32),
}
impl SpinnerSize {
/// Get the size in pixels
pub fn pixels(&self) -> f32 {
match self {
SpinnerSize::Small => 16.0,
SpinnerSize::Medium => 24.0,
SpinnerSize::Large => 32.0,
SpinnerSize::Custom(px) => *px,
}
}
}
/// Spinner widget
pub struct Spinner {
size: SpinnerSize,
custom_theme: Option<Theme>,
label: Option<SharedString>,
}
impl Spinner {
/// Create a new spinner
pub fn new() -> Self {
Self {
size: SpinnerSize::default(),
custom_theme: None,
label: None,
}
}
/// Set spinner size (builder pattern)
#[must_use]
pub fn size(mut self, size: SpinnerSize) -> Self {
self.size = size;
self
}
/// Set label text (builder pattern)
#[must_use]
pub fn label(mut self, text: impl Into<SharedString>) -> Self {
self.label = Some(text.into());
self
}
/// Set custom theme (builder pattern)
#[must_use]
pub fn theme(mut self, theme: Theme) -> Self {
self.custom_theme = Some(theme);
self
}
}
impl Default for Spinner {
fn default() -> Self {
Self::new()
}
}
impl Render for Spinner {
fn render(&mut self, _window: &mut Window, cx: &mut Context<'_, Self>) -> impl IntoElement {
let theme = get_theme_or(cx, self.custom_theme.as_ref());
let size = self.size.pixels();
let label = self.label.clone();
// Number of dots in the spinner
let dot_count = 8;
let dot_size = size * 0.15;
let radius = (size - dot_size) / 2.0;
div()
.id("ccf_spinner")
.flex()
.flex_row()
.gap_2()
.items_center()
// Spinner container
.child(
div()
.relative()
.w(px(size))
.h(px(size))
.children((0..dot_count).map(|i| {
// Calculate position for each dot
let angle = (i as f32 / dot_count as f32) * 2.0 * PI;
let x = radius * angle.cos() + (size - dot_size) / 2.0;
let y = radius * angle.sin() + (size - dot_size) / 2.0;
// Base opacity for static appearance
let base_opacity = 0.2 + (i as f32 / dot_count as f32) * 0.8;
let dot_index = i;
div()
.absolute()
.left(px(x))
.top(px(y))
.w(px(dot_size))
.h(px(dot_size))
.rounded_full()
.bg(rgb(theme.primary))
.with_animation(
ElementId::Name(format!("spinner_dot_{}", i).into()),
Animation::new(Duration::from_millis(1000))
.repeat(),
move |el, delta| {
// Create a "chasing" effect by offsetting each dot's animation phase
let phase = (delta + (dot_index as f32 / dot_count as f32)) % 1.0;
// Opacity varies: high when "active", low otherwise
let opacity = if phase < 0.125 {
1.0
} else {
base_opacity * (1.0 - phase * 0.5)
};
el.opacity(opacity)
},
)
}))
)
// Optional label
.when_some(label, |d, text| {
d.child(
div()
.text_sm()
.text_color(rgb(theme.text_muted))
.child(text)
)
})
}
}