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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 FernTech
//! `Spinner` — a shader-driven circular-arc loading indicator.
//!
//! Uses the same per-slot uniform-buffer pipeline as
//! [`ProgressBar::indeterminate`](crate::ProgressBar) (an
//! [`AnimatedQuadKind`] variant), so per-frame cost is one
//! `queue.write_buffer(64 B) + draw_indexed` — the widget's `paint()`
//! does not re-run between frames and there's no signal-dirty-mark
//! cascade.
//!
//! ```rust
//! # use teksilo_widgets::Spinner;
//! # use teksilo_tokens::TextRole;
//! # use teksilo_i18n::lit;
//! let _s = Spinner::new(24.0)
//! .color(TextRole::Secondary)
//! .label(lit!("Loading"));
//! ```
//!
//! Defaults match the typical CSS spinner: a quarter-circle (90°)
//! arc rotating clockwise from the top, completing one full
//! rotation every 900 ms.
//!
//! Honours `prefers-reduced-motion`: registers no animated quad and
//! falls back to a static three-quarter arc — the indicator is still
//! visible (so the user can tell the surface is busy) but doesn't
//! rotate.
use std::time::Duration;
use teksilo_canvas::{AnimatedQuadClass, Canvas, Path, Rect, Size, SizeProposal, StrokeStyle};
use teksilo_core::accessibility::AccessNodeBuilder;
use teksilo_core::animated_quad::{AnimatedQuadHandle, AnimatedQuadKind};
use teksilo_core::color_prop::ColorProp;
use teksilo_core::widget::{LayoutContext, PaintContext, Widget, WidgetPlacement};
use teksilo_core::widget_id::WidgetId;
use teksilo_i18n::LocalizedString;
use teksilo_tokens::TextRole;
const DEFAULT_SIZE: f32 = 20.0;
const DEFAULT_PERIOD: Duration = Duration::from_millis(900);
const DEFAULT_ARC_FRACTION: f32 = 0.25;
const DEFAULT_STROKE_FRACTION: f32 = 0.12;
/// A circular-arc loading indicator driven by a GPU shader quad.
///
/// Decorative — pair with [`.label`](Self::label) to give screen readers
/// context. Honours `prefers-reduced-motion` by falling back to a static
/// three-quarter arc.
pub struct Spinner {
size: f32,
period: Duration,
arc_fraction: f32,
stroke_fraction: f32,
color: ColorProp,
label: Option<LocalizedString>,
handle: Option<AnimatedQuadHandle>,
}
impl Spinner {
/// Construct a spinner of the given square edge length (logical
/// pixels). Use small sizes (16–24) for inline spinners and
/// larger (32–64) for full-content placeholders.
pub fn new(size: f32) -> Self {
Self {
size,
period: DEFAULT_PERIOD,
arc_fraction: DEFAULT_ARC_FRACTION,
stroke_fraction: DEFAULT_STROKE_FRACTION,
color: TextRole::Secondary.into(),
label: None,
handle: None,
}
}
/// Override the rotation period. Default: 900 ms (one full
/// rotation per period).
pub fn period(mut self, period: Duration) -> Self {
self.period = period;
self
}
/// Override the arc length as a fraction of the full circle.
/// Default: 0.25 (a quarter-circle "comet tail" arc).
pub fn arc_fraction(mut self, arc_fraction: f32) -> Self {
self.arc_fraction = arc_fraction.clamp(0.0, 1.0);
self
}
/// Override the stroke thickness as a fraction of the spinner's
/// edge length. Default: 0.12 (so a 24-px spinner has a ~3-px
/// stroke).
pub fn stroke_fraction(mut self, stroke_fraction: f32) -> Self {
self.stroke_fraction = stroke_fraction.clamp(0.0, 0.5);
self
}
/// Override the arc colour. Default: `TextRole::Secondary` so the
/// spinner picks up theme-aware text-tier styling.
pub fn color(mut self, color: impl Into<ColorProp>) -> Self {
self.color = color.into();
self
}
/// Accessible name (e.g. "Loading", "Uploading file"). Without
/// this, screen readers announce a bare "progress indicator"
/// with no context.
pub fn label(mut self, text: impl Into<LocalizedString>) -> Self {
let ls: LocalizedString = text.into();
self.label = Some(ls);
self
}
}
impl std::fmt::Debug for Spinner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Spinner")
.field("size", &self.size)
.field("period", &self.period)
.finish()
}
}
impl Widget for Spinner {
fn build(&mut self, ctx: &mut teksilo_core::build_context::BuildContext) -> Vec<WidgetId> {
// Skip the shader registration entirely under reduced motion
// — the static fallback in `paint()` doesn't need a slot, and
// the registry not having an entry stops the per-frame phase
// tick.
if ctx.prefers_reduced_motion() {
self.handle = None;
} else {
self.handle = Some(ctx.animated_quad(AnimatedQuadKind::SpinnerArc {
period: self.period,
arc_fraction: self.arc_fraction,
stroke_fraction: self.stroke_fraction,
color: self.color.clone(),
}));
}
vec![]
}
fn layout_response(
&self,
_proposal: SizeProposal,
_ctx: &LayoutContext,
) -> teksilo_core::widget::LayoutResponse {
Size::new(self.size, self.size).into()
}
fn place_children(
&self,
_bounds: Rect,
_proposal: SizeProposal,
_children: &mut [WidgetPlacement],
_ctx: &LayoutContext,
) {
}
fn paint(&self, bounds: Rect, canvas: &mut Canvas, ctx: &PaintContext) {
if let Some(handle) = self.handle {
// Animated path: emit a single AnimatedQuad. The shader
// computes the arc each frame from the per-slot phase.
canvas.draw_animated_quad(bounds, handle.slot(), AnimatedQuadClass::Procedural);
} else {
// Reduced-motion fallback: draw a static three-quarter
// arc, leading edge at the top. Communicates "busy"
// without rotating.
let color = self.color.resolve(ctx.theme, ctx.effective_enabled);
let extent = bounds.width.min(bounds.height);
let stroke_w = extent * self.stroke_fraction;
// Inscribe inside the bounds, leaving room for the stroke
// so it doesn't get clipped.
let inset = stroke_w * 0.5;
let inscribed = Rect::new(
bounds.x + inset,
bounds.y + inset,
bounds.width - inset * 2.0,
bounds.height - inset * 2.0,
);
let mut path = Path::new();
// Path::arc_to angles: 0° at 3 o'clock; offset -90° to
// start at the top. Sweep the arc clockwise.
path.arc_to(inscribed, -90.0, self.arc_fraction * 360.0);
canvas.stroke_path(&path, color, StrokeStyle::solid(stroke_w));
}
}
fn accessibility(&self, builder: &mut AccessNodeBuilder) {
builder.set_role(teksilo_core::accesskit::Role::ProgressIndicator);
// Indeterminate (no numeric value); polite live region so
// screen readers don't interrupt the user.
builder.set_live(teksilo_core::accesskit::Live::Polite);
if let Some(ref label) = self.label {
builder.set_name(label.clone());
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use teksilo_canvas::DrawCommand;
use teksilo_core::widget_tree::WidgetTree;
use teksilo_i18n::lit;
#[test]
fn spinner_size() {
let mut tree = WidgetTree::new().with_theme(teksilo_core::presets::intui::light());
let id = tree.add(Spinner::new(32.0));
// Pass `None` for both axes so the layout pass uses the
// spinner's natural (square) `size_that_fits` instead of
// forcing the proposal dimensions.
tree.layout(SizeProposal {
width: None,
height: None,
});
let b = tree.bounds(id);
assert!((b.width - 32.0).abs() < 0.01);
assert!((b.height - 32.0).abs() < 0.01);
}
#[test]
fn spinner_emits_one_animated_quad_when_motion_allowed() {
let mut tree = WidgetTree::new().with_theme(teksilo_core::presets::intui::light());
tree.add(Spinner::new(24.0));
tree.layout(SizeProposal::exact(64.0, 64.0));
let frame = tree.render();
assert_eq!(
frame.animated_quads.len(),
1,
"spinner with motion enabled should emit exactly one AnimatedQuad"
);
// No path commands — the shader does the rendering.
let path_count = frame
.draw_order
.iter()
.filter(|c| matches!(c, DrawCommand::Path(_)))
.count();
assert_eq!(path_count, 0);
}
#[test]
fn spinner_emits_static_path_under_reduced_motion() {
let mut tree = WidgetTree::new().with_theme(teksilo_core::presets::intui::light());
// high_contrast = false, reduced_motion = true, text_scale = 1.0
tree.set_accessibility_preferences(false, true, 1.0);
tree.add(Spinner::new(24.0));
tree.layout(SizeProposal::exact(64.0, 64.0));
let frame = tree.render();
assert_eq!(
frame.animated_quads.len(),
0,
"no animated quad should register when reduced-motion is on"
);
let path_count = frame
.draw_order
.iter()
.filter(|c| matches!(c, DrawCommand::Path(_)))
.count();
assert!(
path_count >= 1,
"reduced-motion fallback should emit at least one Path draw command"
);
}
#[test]
fn spinner_phase_advances_between_frames() {
// Same shape as ProgressBar's animation test: the per-slot
// phase must change between frames so the shader actually
// animates without paint() re-running.
let mut tree = WidgetTree::new().with_theme(teksilo_core::presets::intui::light());
tree.add(Spinner::new(24.0));
tree.layout(SizeProposal::exact(64.0, 64.0));
let frame1 = tree.render();
assert_eq!(frame1.animated_quads.len(), 1);
let phase1 = frame1.anim_params[frame1.animated_quads[0].slot as usize].phase;
std::thread::sleep(Duration::from_millis(100));
let frame2 = tree.render();
let phase2 = frame2.anim_params[frame2.animated_quads[0].slot as usize].phase;
assert_ne!(phase1, phase2, "spinner phase must advance between frames");
}
#[test]
fn accessibility_role_and_live_region() {
let mut tree = WidgetTree::new();
let id = tree.add(Spinner::new(24.0).label(lit!("Loading")));
tree.layout(SizeProposal::exact(64.0, 64.0));
let info = tree.accessibility_node(id);
assert_eq!(
info.role(),
teksilo_core::accesskit::Role::ProgressIndicator
);
assert_eq!(info.name(), Some("Loading"));
}
}