S016: R1-10 cookie spinner in the tab's favicon spot

While a tab loads, a stroke runs along a scalloped cookie outline (draws
forward, retracts, keeps turning), as in the original's draw_spinner. When
the load ends it closes into the whole cookie and shrinks away as the
favicon grows back. Motion off shows a still three-quarter cookie.
chrome.txt shoots two spinner phases and the morph.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
astar76 2026-11-12 07:16:21 -06:00
commit 773fc7d778
3 changed files with 105 additions and 13 deletions

View file

@ -284,12 +284,28 @@ fn tab(ui: &mut Ui, f: &mut Frame, index: usize, rect: Rect, dragged: bool, drop
}
let cy = rect.center().y;
let icon_center = if pinned { pos2(rect.left() + PINNED / 2.0, cy) } else { pos2(rect.left() + theme::GAP_M + 8.0, cy) };
if loading {
spinner(ui, icon_center, 7.0, p.primary);
} else if let Some(texture) = favicon {
ui.painter().image(texture, Rect::from_center_size(icon_center, Vec2::splat(16.0)), Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), Color32::WHITE);
} else {
icon(ui, icon_center, if internal { icons::TAB } else { icons::PUBLIC }, 16.0, active, content);
// While loading, a cookie spinner takes the favicon's place. When the load ends the stroke closes into the whole
// cookie, which shrinks away as the favicon grows back in its place.
let load = theme::spring(&ctx, Id::new(("tab-loading", tab_id)), if loading { 1.0 } else { 0.0 }, theme::DEFAULT).clamp(0.0, 1.0);
if load > 0.0 {
let motion = theme::tokens(&ctx).motion;
let clock = (motion > 0.0).then(|| ui.input(|i| i.time) as f32 / motion);
let mut painter = ui.painter().clone();
painter.set_opacity(load);
crate::ui::paint::cookie_spinner(&painter, icon_center, 6.0 + 1.5 * load, clock, 1.0 - load, Stroke::new(2.0, p.primary));
if clock.is_some() {
ctx.request_repaint();
}
}
if load < 1.0 {
let size = 16.0 * (1.0 - 0.4 * load);
let mut painter = ui.painter().clone();
painter.set_opacity(1.0 - load);
if let Some(texture) = favicon {
painter.image(texture, Rect::from_center_size(icon_center, Vec2::splat(size)), Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), Color32::WHITE);
} else {
icon(ui, icon_center, if internal { icons::TAB } else { icons::PUBLIC }, size, active, content.gamma_multiply(1.0 - load));
}
}
let hovered = response.hovered() || ui.rect_contains_pointer(rect);
let mut right = rect.right() - theme::GAP_XS - INNER / 2.0;

View file

@ -11,13 +11,57 @@ use crate::theme::Palette;
/// deep, turned by `rotation` radians.
pub fn cookie_points(center: Pos2, radius: f32, lobes: u32, depth: f32, rotation: f32) -> Vec<Pos2> {
let count = (lobes * 16).clamp(48, 192);
(0..count)
.map(|i| {
let a = i as f32 / count as f32 * TAU;
let r = radius * (1.0 - depth * (1.0 - (lobes as f32 * (a - rotation)).cos()) / 2.0);
center + vec2(a.cos(), a.sin()) * r
})
.collect()
(0..count).map(|i| cookie_point(center, radius, lobes, depth, rotation, i as f32 / count as f32 * TAU)).collect()
}
/// The point of a cookie outline at angle `a` (radians, clockwise from 3 o'clock in screen coordinates).
fn cookie_point(center: Pos2, radius: f32, lobes: u32, depth: f32, rotation: f32, a: f32) -> Pos2 {
let r = radius * (1.0 - depth * (1.0 - (lobes as f32 * (a - rotation)).cos()) / 2.0);
center + vec2(a.cos(), a.sin()) * r
}
/// Points along part of a cookie outline, from angle `start` for `sweep` radians.
pub fn cookie_arc_points(center: Pos2, radius: f32, lobes: u32, depth: f32, rotation: f32, start: f32, sweep: f32) -> Vec<Pos2> {
let steps = ((sweep.abs() * radius).ceil() as usize).max(8);
(0..=steps).map(|i| cookie_point(center, radius, lobes, depth, rotation, start + sweep * i as f32 / steps as f32)).collect()
}
/// Lobes and scallop depth of the loading cookie: deeper than `COOKIE_DEPTH` so the scallops read at favicon size.
const SPINNER_LOBES: u32 = 7;
const SPINNER_DEPTH: f32 = 0.18;
/// Head angle and sweep of the indeterminate cookie spinner at `t` seconds (M3 Expressive: the stroke draws forward
/// round the outline, then its tail catches up, while the whole thing keeps turning).
pub fn spinner_arc(t: f32) -> (f32, f32) {
let cycle = 0.5 - 0.5 * (t * 2.6).cos();
let sweep = (20.0 + 250.0 * cycle).to_radians();
let start = (t * 300.0 + 125.0 * (1.0 - (t * 2.6).cos())).to_radians();
(start, sweep)
}
/// The tab loading indicator: a stroke along a scalloped cookie outline. `t` is the animation clock in seconds
/// (`None` = Motion off: a still three-quarter cookie). `closed` (0..1) closes the stroke into the whole cookie,
/// which is how it turns back into the favicon when a load ends. The outline itself turns slowly with `t`.
pub fn cookie_spinner(painter: &Painter, center: Pos2, radius: f32, t: Option<f32>, closed: f32, stroke: Stroke) {
let (start, sweep, rotation) = match t {
Some(t) => {
let (start, sweep) = spinner_arc(t);
(start, sweep, t * 0.9)
},
None => (-std::f32::consts::FRAC_PI_2, 0.75 * TAU, 0.0),
};
let closed = closed.clamp(0.0, 1.0);
let sweep = sweep + (TAU - sweep) * closed;
let points = cookie_arc_points(center, radius, SPINNER_LOBES, SPINNER_DEPTH, rotation, start, sweep);
if closed >= 0.999 {
painter.add(Shape::closed_line(points, stroke));
return;
}
let ends = [points[0], points[points.len() - 1]];
painter.add(Shape::line(points, stroke));
for p in ends {
painter.circle_filled(p, stroke.width / 2.0, stroke.color);
}
}
/// A filled cookie with an optional outline. The fill is a fan from the centre (cookies are star-shaped around it,
@ -112,4 +156,28 @@ mod tests {
assert!((45.0 - 1e-3..=50.0 + 1e-3).contains(&r), "radius {r}");
}
}
#[test]
fn spinner_arc_grows_retracts_and_turns() {
let sweeps: Vec<f32> = (0..200).map(|i| spinner_arc(i as f32 * 0.02).1).collect();
let (min, max) = sweeps.iter().fold((f32::MAX, f32::MIN), |(a, b), &s| (a.min(s), b.max(s)));
assert!((min - 20f32.to_radians()).abs() < 0.05, "min sweep {min}");
assert!((max - 270f32.to_radians()).abs() < 0.05, "max sweep {max}");
// Over a whole cycle the arc moves on (it spins), it doesn't just breathe in place.
let period = TAU / 2.6;
assert!(spinner_arc(period).0 - spinner_arc(0.0).0 > 1.0);
}
#[test]
fn cookie_arc_follows_the_outline() {
let c = pos2(0.0, 0.0);
let points = cookie_arc_points(c, 8.0, 7, 0.18, 0.4, 1.0, 3.0);
assert!(points.len() >= 9);
for p in &points {
let r = (*p - c).length();
assert!((8.0 * 0.82 - 1e-3..=8.0 + 1e-3).contains(&r), "radius {r}");
}
let first = points[0] - c;
assert!((first.y.atan2(first.x) - 1.0).abs() < 1e-4);
}
}

View file

@ -81,7 +81,15 @@ shot loading-hover-dark
hover none
wait 0.8
shot loading-again-dark
# R1-10: the tab's favicon spot shows a cookie spinner while loading, at two points of its cycle, then the favicon.
fakeload 2
wait 0.3
shot tab-spinner-a-dark
wait 0.45
shot tab-spinner-b-dark
fakeload done
wait 0.04
shot tab-spinner-morph-dark
wait 1
shot loaded-dark
set /theme/mode "light"