diff --git a/packages/session-ui/package.json b/packages/session-ui/package.json index fbc8079e551..b8667b64b9a 100644 --- a/packages/session-ui/package.json +++ b/packages/session-ui/package.json @@ -47,7 +47,6 @@ "./v2/*": "./src/v2/components/*.tsx" }, "scripts": { - "generate:progress-indicator": "bun script/generate-session-progress-indicator.ts", "typecheck": "tsc -b", "test": "bun test src --only-failures", "test:components": "playwright test --config playwright.components.config.ts", diff --git a/packages/session-ui/src/v2/components/session-progress-indicator-v2-1x.png b/packages/session-ui/src/v2/components/session-progress-indicator-v2-1x.png deleted file mode 100644 index fc158c80f04..00000000000 Binary files a/packages/session-ui/src/v2/components/session-progress-indicator-v2-1x.png and /dev/null differ diff --git a/packages/session-ui/src/v2/components/session-progress-indicator-v2.css b/packages/session-ui/src/v2/components/session-progress-indicator-v2.css index 8d053fb8a20..7cebec8cfd2 100644 --- a/packages/session-ui/src/v2/components/session-progress-indicator-v2.css +++ b/packages/session-ui/src/v2/components/session-progress-indicator-v2.css @@ -2,30 +2,4 @@ display: block; flex-shrink: 0; color: var(--v2-icon-icon-muted, #808080); - background-color: currentColor; - /* The animated frames carry each dot's opacity; the grid keeps the gaps between dots. */ - mask-image: - url("./session-progress-indicator-v2-1x.png"), - url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M1 1h2v2h-2zM4 1h2v2h-2zM7 1h2v2h-2zM10 1h2v2h-2zM13 1h2v2h-2zM1 4h2v2h-2zM4 4h2v2h-2zM7 4h2v2h-2zM10 4h2v2h-2zM13 4h2v2h-2zM1 7h2v2h-2zM4 7h2v2h-2zM7 7h2v2h-2zM10 7h2v2h-2zM13 7h2v2h-2zM1 10h2v2h-2zM4 10h2v2h-2zM7 10h2v2h-2zM10 10h2v2h-2zM13 10h2v2h-2zM1 13h2v2h-2zM4 13h2v2h-2zM7 13h2v2h-2zM10 13h2v2h-2zM13 13h2v2h-2z'/%3E%3C/svg%3E"); - mask-size: 100% 100%; - mask-repeat: no-repeat; - mask-composite: intersect; - image-rendering: pixelated; - image-rendering: crisp-edges; -} - -[data-component="session-progress-indicator-v2"] [data-reduced-motion] { - display: none; - fill: currentColor; -} - -:root[data-motion="reduce"] { - [data-component="session-progress-indicator-v2"] { - background-color: transparent; - mask-image: none; - } - - [data-component="session-progress-indicator-v2"] [data-reduced-motion] { - display: block; - } } diff --git a/packages/session-ui/src/v2/components/session-progress-indicator-v2.tsx b/packages/session-ui/src/v2/components/session-progress-indicator-v2.tsx index c1a1a6cdec3..6b18b9132bc 100644 --- a/packages/session-ui/src/v2/components/session-progress-indicator-v2.tsx +++ b/packages/session-ui/src/v2/components/session-progress-indicator-v2.tsx @@ -1,25 +1,16 @@ -import { splitProps, type ComponentProps } from "solid-js" +import { type ComponentProps } from "solid-js" +import { Spinner } from "@opencode/ui/spinner" import "./session-progress-indicator-v2.css" -// The frames paint through a CSS mask (see the stylesheet), not an SVG : Chromium restarts an animated image -// shown by or for every instance whenever another one mounts, while CSS images join the running loop. +// The app's one activity spinner, so the activity chip, subagent rows, task cards and loading tabs follow its choice. export function SessionProgressIndicatorV2(props: ComponentProps<"svg">) { - const [local, rest] = splitProps(props, ["class", "classList", "width", "height"]) - return ( - - - + aria-hidden={props["aria-hidden"] ?? "true"} + /> ) } diff --git a/packages/ui/package.json b/packages/ui/package.json index f5a25241bc9..1fd5692b246 100644 --- a/packages/ui/package.json +++ b/packages/ui/package.json @@ -102,6 +102,7 @@ "test": "bun test src --only-failures", "dev": "vite", "bench:motion": "bun script/motion-bench.ts", + "generate:spinner": "bun script/generate-spinner.ts", "generate:tailwind": "bun run script/tailwind.ts", "generate:v2-oc2": "bun run script/build-oc2-v2-overrides.ts" }, diff --git a/packages/session-ui/script/generate-session-progress-indicator.ts b/packages/ui/script/generate-spinner.ts similarity index 97% rename from packages/session-ui/script/generate-session-progress-indicator.ts rename to packages/ui/script/generate-spinner.ts index 218ca94f56b..e27f2fb892b 100644 --- a/packages/session-ui/script/generate-session-progress-indicator.ts +++ b/packages/ui/script/generate-spinner.ts @@ -26,7 +26,7 @@ const crcTable = Array.from({ length: 256 }, (_, value) => Array.from({ length: 8 }).reduce((crc) => ((crc & 1) !== 0 ? 0xedb88320 ^ (crc >>> 1) : crc >>> 1), value), ) -await Bun.write(new URL("../src/v2/components/session-progress-indicator-v2-1x.png", import.meta.url), apng(1)) +await Bun.write(new URL("../src/components/spinner.png", import.meta.url), apng(1)) function apng(scale: number) { const frameSize = size * scale diff --git a/packages/session-ui/src/v2/components/session-progress-indicator-v2.test.ts b/packages/ui/src/components/spinner-png.test.ts similarity index 85% rename from packages/session-ui/src/v2/components/session-progress-indicator-v2.test.ts rename to packages/ui/src/components/spinner-png.test.ts index 1e325dc15c2..6998562d9a4 100644 --- a/packages/session-ui/src/v2/components/session-progress-indicator-v2.test.ts +++ b/packages/ui/src/components/spinner-png.test.ts @@ -1,9 +1,7 @@ import { expect, test } from "bun:test" -test("progress indicator PNG loops 36 frames at 30 fps in a 16x16 canvas", async () => { - const png = Buffer.from( - await Bun.file(new URL("./session-progress-indicator-v2-1x.png", import.meta.url)).arrayBuffer(), - ) +test("Today spinner PNG loops 36 frames at 30 fps in a 16x16 canvas", async () => { + const png = Buffer.from(await Bun.file(new URL("./spinner.png", import.meta.url)).arrayBuffer()) expect(png.subarray(0, 8)).toEqual(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10])) diff --git a/packages/ui/src/components/spinner-variants.test.ts b/packages/ui/src/components/spinner-variants.test.ts index 3915cc85da6..b62fbe3edac 100644 --- a/packages/ui/src/components/spinner-variants.test.ts +++ b/packages/ui/src/components/spinner-variants.test.ts @@ -26,7 +26,60 @@ test("a border snake rests its middle dots and visits each edge dot once", () => }) test("keyframes run from the dim rest to fully lit", () => { - expect(spinnerKeyframes("spinner-radar", spinnerVariants.radar)).toBe( - "@keyframes spinner-radar{0%{opacity:0.2;animation-timing-function:cubic-bezier(.45,0,.2,1)}3%{opacity:1;animation-timing-function:cubic-bezier(.45,0,.2,1)}51%{opacity:0.2;animation-timing-function:cubic-bezier(.45,0,.2,1)}100%{opacity:0.2;animation-timing-function:cubic-bezier(.45,0,.2,1)}}", + expect(spinnerKeyframes("spinner-ripple", spinnerVariants.ripple)).toBe( + "@keyframes spinner-ripple-0{0%{opacity:0.2;animation-timing-function:cubic-bezier(.45,0,.2,1)}3%{opacity:1;animation-timing-function:cubic-bezier(.45,0,.2,1)}37%{opacity:0.2;animation-timing-function:cubic-bezier(.45,0,.2,1)}100%{opacity:0.2;animation-timing-function:cubic-bezier(.45,0,.2,1)}}", ) }) + +test.each(["radar", "radarSmooth"] as const)( + "%s's brightest dot is never more than half a dot from its head", + (name) => { + const variant = spinnerVariants[name] + const css = spinnerKeyframes(name, variant) + // Head keyframes are linear, so each dot's opacity is a straight line between stops. + expect(css).not.toContain("cubic-bezier") + const shapes = [...css.matchAll(/@keyframes [\w-]+\{(.*?)\}\}/g)].map((match) => + [...match[1]!.matchAll(/([\d.]+)%\{opacity:([\d.]+)/g)].map( + (stop) => [Number(stop[1]), Number(stop[2])] as const, + ), + ) + const opacity = (shape: number, percent: number) => { + const stops = shapes[shape]! + const next = stops.findIndex(([at]) => at >= percent) + if (next <= 0) return stops[0]![1] + const [p0, o0] = stops[next - 1]! + const [p1, o1] = stops[next]! + return o0 + ((o1 - o0) * (percent - p0)) / (p1 - p0) + } + const dots = spinnerDots(variant) + const centre = dots.find((dot) => dot.hold === "lit")! + const ring = dots + .filter((dot) => !dot.hold) + .map((dot) => ({ + ...dot, + ring: Math.round(Math.max(Math.abs(dot.x + dot.size / 2 - 8), Math.abs(dot.y + dot.size / 2 - 8))), + })) + const wrap = (turns: number) => turns - Math.round(turns) + + Array.from({ length: 360 }, (_, step) => step / 360).forEach((head) => { + const lit = ring.map((dot) => opacity(dot.shape, ((((head - dot.at) % 1) + 1) % 1) * 100)) + const top = Math.max(...lit) + const brightest = ring.filter((_, index) => lit[index]! > top - 1e-9) + + expect(top).toBeGreaterThan(centre.still) + brightest.forEach((dot) => { + const offset = wrap(dot.at - head) + // The gap to the neighbour on the head's side, on the dot's own ring. + const gap = Math.min( + ...ring + .filter( + (other) => + other.ring === dot.ring && other !== dot && Math.sign(wrap(other.at - dot.at)) === -Math.sign(offset), + ) + .map((other) => Math.abs(wrap(other.at - dot.at))), + ) + expect(Math.abs(offset)).toBeLessThanOrEqual(gap / 2 + 0.003) + }) + }) + }, +) diff --git a/packages/ui/src/components/spinner-variants.ts b/packages/ui/src/components/spinner-variants.ts index 0a2021f3923..9738171665b 100644 --- a/packages/ui/src/components/spinner-variants.ts +++ b/packages/ui/src/components/spinner-variants.ts @@ -1,4 +1,5 @@ -// Dot-matrix variations of the activity spinner, tried from Knobs → Experiments → "Activity spinner". Each variation +// Dot-matrix variations of the activity spinner, Soft Radar by default and others from Knobs → Experiments → "Activity +// spinner". Each variation // is plain data: a dot layout, a keyframe shape, and a pattern that turns each dot's place (angle, distance, path // index…) into its animation delay. Animation is CSS only; every dot shares the variation's loop, so pinning start // times with syncSpinPhase keeps every spinner on screen in phase. Paste replacements from the prototype's @@ -33,8 +34,20 @@ export type SpinnerVariant = { steady?: boolean /** Resting opacity of an unlit dot. */ dim: number - /** The keyframe shape: [percent of the loop, level from 0 (dim) to 1 (lit)]. */ + /** The keyframe shape: [percent of the loop, level from 0 (dim) to 1 (lit)]. With a head, the trail it leaves. */ stops: readonly (readonly [number, number])[] + /** + * Sweeps on a grid: the head is the brightest dot and `stops` only fade behind it. "step" lights the dot nearest the + * head on each square ring; "glide" shares the head between the two dots either side of it, so it moves continuously. + */ + head?: "step" | "glide" +} + +// Soft Radar's trail: its ease-in-out fade over 48% of the loop, sampled every 4%, from a level below the head's. +const fade = [1, 0.987, 0.934, 0.812, 0.599, 0.384, 0.238, 0.143, 0.081, 0.041, 0.017, 0.004, 0] + +function trail(from: number) { + return [...fade.map((level, index) => [index * 4, round(from * level)] as const), [100, 0] as const] } export const spinnerVariants = { @@ -48,12 +61,8 @@ export const spinnerVariants = { pattern: "sweep", steady: true, dim: 0.2, - stops: [ - [0, 0], - [3, 1], - [51, 0], - [100, 0], - ], + head: "step", + stops: trail(0.55), }, orbRadar: { label: "Orb Radar", @@ -140,6 +149,21 @@ export const spinnerVariants = { [100, 0], ], }, + // Soft Radar's sweep without the steps: the head lights the two dots either side of it on each ring in proportion + // to how close it is, so it glides between them. Last, because Knobs saves a choice by its index. + radarSmooth: { + label: "Soft Radar (smooth)", + layout: "grid", + span: 5, + dot: 2, + gap: 1, + loop: 1.8, + pattern: "sweep", + steady: true, + dim: 0.2, + head: "glide", + stops: trail(0.35), + }, } satisfies Record export type SpinnerDot = { @@ -154,12 +178,34 @@ export type SpinnerDot = { hold?: "lit" | "rest" /** Opacity for reduced motion: the pattern frozen a fifth of the way into its loop. */ still: number + /** Which of the variation's keyframes the dot runs: spinnerKeyframes names them `name-0`, `name-1`… */ + shape: number } type Cell = { c: number; r: number; gx: number; gy: number } /** The dots of a variation, positioned in a 16-unit viewBox. */ -export function spinnerDots(variant: SpinnerVariant): SpinnerDot[] { +export function spinnerDots(variant: SpinnerVariant) { + return layout(variant).dots +} + +/** The variation's keyframes, one per shape its dots run, named `name-0`, `name-1`… */ +export function spinnerKeyframes(name: string, variant: SpinnerVariant) { + return layout(variant) + .shapes.map((stops, index) => { + const frames = stops + .map( + ([percent, level]) => + `${round(percent)}%{opacity:${round(variant.dim + (1 - variant.dim) * level)};animation-timing-function:${variant.head ? "linear" : "cubic-bezier(.45,0,.2,1)"}}`, + ) + .join("") + + return `@keyframes ${name}-${index}{${frames}}` + }) + .join("") +} + +function layout(variant: SpinnerVariant) { const cells = variant.layout === "orb" ? orb() : grid(variant.span) const extent = variant.span * variant.dot + (variant.span - 1) * variant.gap const fit = Math.min(1, 15 / extent) @@ -170,7 +216,7 @@ export function spinnerDots(variant: SpinnerVariant): SpinnerDot[] { const random = seeded(4242) const offsets = Array.from({ length: variant.span }, () => random()) - return cells.map((cell, index) => { + const placed = cells.map((cell) => { const centre = cell.gx === 0 && cell.gy === 0 const twinkle = @@ -204,35 +250,88 @@ export function spinnerDots(variant: SpinnerVariant): SpinnerDot[] { const hold = variant.steady && centre - ? "lit" + ? ("lit" as const) : variant.pattern === "border" && !ring.includes(cell.r * variant.span + cell.c) - ? "rest" + ? ("rest" as const) : undefined - const level = hold === "lit" ? 1 : hold === "rest" ? 0 : levelAt(variant.stops, frac(0.22 - at) * 100) + return { cell, at, hold, scale: twinkle?.scale ?? 1 } + }) + + // With a head, each dot's shape depends on how far it sits from its neighbours on its square ring, in loop fractions. + const keys = placed.map((dot) => { + if (!variant.head || dot.hold) return "" + const ringOf = (cell: Cell) => Math.max(Math.abs(cell.gx), Math.abs(cell.gy)) + const others = placed.filter((other) => other !== dot && !other.hold && ringOf(other.cell) === ringOf(dot.cell)) + const behind = Math.min(...others.map((other) => frac(dot.at - other.at))) + const ahead = Math.min(...others.map((other) => frac(other.at - dot.at))) + return `${round(behind)} ${round(ahead)}` + }) + const unique = [...new Set(keys.filter((key) => key !== ""))] + const shapes = variant.head + ? unique.map((key) => { + const [behind, ahead] = key.split(" ").map(Number) + return headStops(variant, behind! * 100, ahead! * 100) + }) + : [variant.stops] + + const dots = placed.map((dot, index): SpinnerDot => { + const shape = Math.max(0, unique.indexOf(keys[index]!)) + // A steady centre glows like the fresh trail, so the head stays the brightest dot. + const lit = variant.head ? variant.stops[0]![1] : 1 + const level = + dot.hold === "lit" ? lit : dot.hold === "rest" ? 0 : levelAt(shapes[shape]!, frac(0.22 - dot.at) * 100) return { - x: round(8 + cell.gx * pitch - size / 2), - y: round(8 + cell.gy * pitch - size / 2), + x: round(8 + dot.cell.gx * pitch - size / 2), + y: round(8 + dot.cell.gy * pitch - size / 2), size: round(size), - at: round(at), - scale: twinkle?.scale ?? 1, - hold, + at: round(dot.at), + scale: dot.scale, + hold: dot.hold, still: round(variant.dim + (1 - variant.dim) * level), + shape, } }) + + return { dots, shapes } } -/** The variation's keyframes, named `name`. */ -export function spinnerKeyframes(name: string, variant: SpinnerVariant) { - const frames = variant.stops - .map( - ([percent, level]) => - `${round(percent)}%{opacity:${round(variant.dim + (1 - variant.dim) * level)};animation-timing-function:cubic-bezier(.45,0,.2,1)}`, - ) - .join("") +// One dot's level through the loop, 0% being the moment the head passes it, with `behind` and `ahead` the percent of +// the loop to its neighbours on its ring. "step" lights it while it's the dot nearest the head; "glide" rises linearly +// from the neighbour behind and falls linearly toward the one ahead. Then the trail takes over, never above the head. +function headStops(variant: SpinnerVariant, behind: number, ahead: number): (readonly [number, number])[] { + const edge = 1.5 + const trailAt = (percent: number) => levelAt(variant.stops, percent) + const tail = (from: number) => + variant.stops + .filter(([percent]) => percent > from && percent < 100) + .map(([percent, level]) => [percent, level] as const) - return `@keyframes ${name}{${frames}}` + if (variant.head === "step") { + const off = ahead / 2 + edge + const on = 100 - behind / 2 + return [ + [0, 1], + [ahead / 2, 1], + [off, trailAt(off)], + ...tail(off).filter(([percent]) => percent < on - edge), + [on - edge, 0], + [on, 1], + [100, 1], + ] + } + + // Where the falling edge meets the trail. + const meet = Array.from({ length: 40 }).reduce<[number, number]>( + ([lo, hi]) => { + const mid = (lo + hi) / 2 + return 1 - mid / ahead > trailAt(mid) ? [mid, hi] : [lo, mid] + }, + [0, ahead], + )[0] + const on = 100 - behind + return [[0, 1], [meet, trailAt(meet)], ...tail(meet).filter(([percent]) => percent < on), [on, 0], [100, 1]] } function grid(span: number): Cell[] { diff --git a/packages/ui/src/components/spinner.stories.tsx b/packages/ui/src/components/spinner.stories.tsx index c3a32d24b73..aaa7e317652 100644 --- a/packages/ui/src/components/spinner.stories.tsx +++ b/packages/ui/src/components/spinner.stories.tsx @@ -6,8 +6,9 @@ Animated loading indicator for inline or page-level loading states. Tabs, \`Load titlebar update button all render it. ### Variants and states -- Today: the 5×5 radar sweep from an animated PNG mask. -- Dot-matrix variations to try from Knobs → Experiments → Activity spinner; a \`variant\` prop pins one. +- Soft Radar by default; Soft Radar (smooth) glides its sweep head between dots instead of stepping. +- Today (the 5×5 PNG radar) and the other dot-matrix variations from Knobs → Experiments → Activity spinner; a + \`variant\` prop pins one. ### Behavior - CSS animation only. Every dot of a variation shares one loop, and \`syncSpinPhase\` pins every instance to the @@ -25,7 +26,7 @@ export default { id: "components-spinner", component: Spinner, tags: ["autodocs"], - args: { variant: "today", speed: 1, size: 16 }, + args: { variant: "radar", speed: 1, size: 16 }, argTypes: { variant: { control: "select", options: ["today", ...Object.keys(spinnerVariants)] }, speed: { control: { type: "range", min: 0.25, max: 2, step: 0.05 } }, @@ -50,6 +51,8 @@ export const Today = { args: { variant: "today" } } export const SoftRadar = { args: { variant: "radar" } } +export const SoftRadarSmooth = { args: { variant: "radarSmooth" } } + export const OrbRadar = { args: { variant: "orbRadar" } } export const CentreRipple = { args: { variant: "ripple" } } diff --git a/packages/ui/src/components/spinner.tsx b/packages/ui/src/components/spinner.tsx index 07e04cc1de3..c839ce2e203 100644 --- a/packages/ui/src/components/spinner.tsx +++ b/packages/ui/src/components/spinner.tsx @@ -5,12 +5,12 @@ import { spinnerDots, spinnerKeyframes, spinnerVariants, type SpinnerVariant } f export type SpinnerVariantName = "today" | keyof typeof spinnerVariants -// Experiment: dot-matrix variations to try in place of today's spinner, everywhere it shows (tabs, loaders, the -// update button), before one ships. +// Every activity spinner (tabs, loaders, the activity chip, subagents, the update button) shows Soft Radar; Knobs → +// Experiments tries the other dot-matrix variations, and Today's PNG, in its place. const choice = motionTuning.choice("spinner.variant", { label: "Activity spinner", group: "Experiments", - value: "today", + value: "radar", // SAFETY: built from spinnerVariants' own keys, plus "today". labels: Object.fromEntries([ ["today", "Today"], @@ -83,10 +83,10 @@ export function Spinner(props: ComponentProps<"svg"> & { "data-component"?: stri height={dot.size} rx={dot.size * 0.24} style={{ - "--still": dot.hold === "lit" ? 1 : dot.hold === "rest" ? current.dim : dot.still, + "--still": dot.still, animation: dot.hold ? undefined - : `${keyframes} calc(${loop}s * var(--spinner-time, 1)) linear calc(${-loop * (1 - dot.at)}s * var(--spinner-time, 1)) infinite`, + : `${keyframes}-${dot.shape} calc(${loop}s * var(--spinner-time, 1)) linear calc(${-loop * (1 - dot.at)}s * var(--spinner-time, 1)) infinite`, }} /> )