Skip to content

Commit 2d924ef

Browse files
committed
Keep a resting kite sharp across zoom, and never draw a Bot as OpenBot's own kite
From review of this branch: - A resting kite asks for no frames, so a zoom or a move to another screen left it at the old resolution. It now redraws when devicePixelRatio changes. - One Bot in 64 landed on violet quarters, OpenBot's own kite. That Bot takes the next sail and keeps its colour; none of the examples' Bots change. - Kites draw in a layout effect, so a newly mounted one is never shown as an empty frame. - A kite at 32 px and under draws at most about thirty frames a second while its square moves. - The changelog said every Bot keeps its colour. It keeps its place in the palette; the palette is the brand's, so the teal slot becomes graphite and the sky slot teal.
1 parent 8fc8c72 commit 2d924ef

5 files changed

Lines changed: 113 additions & 12 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.
1515
question, an approval or a stalled hand-off for you, red with a failed task, dimmed with no face
1616
while it is paused, and green with a reply to read. A conversation's kite works while the Bot has
1717
the turn, and a busy conversation in the sidebar shows it in the kite rather than three dots.
18-
Each Bot keeps the colour its orb had, and gets a sail pattern from the same seed. OpenBot's own
19-
mark (sign-in, onboarding, the New Bot sheet, the skins' frames, OpenBot for Business) is the
20-
violet kite. A kite holds still under reduced motion and at 32 px and under, where only its
18+
Each Bot keeps its orb's place in the palette, now the brand's eight colours, so most stay in
19+
their colour family (the teal slot becomes graphite, the sky slot teal), and gets a sail pattern
20+
from the same seed. OpenBot's own mark (sign-in, onboarding, the New Bot sheet, the skins'
21+
frames, OpenBot for Business) is the violet kite. A kite holds still under reduced motion and at 32 px and under, where only its
2122
square moves, shows no face under 22 px, and uses each colour's lighter twin in dark mode. A skin
2223
that set `botLook: "orb"` sets `botLook: "kite"`.
2324
- OpenBot Desktop's menu bar and tray no longer have an item that opens the back end's own

‎app/src/components/agents/kite/kite.tsx‎

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useReducedMotion } from "motion/react";
2-
import { useEffect, useRef, useSyncExternalStore } from "react";
2+
import { useLayoutEffect, useRef, useSyncExternalStore } from "react";
33
import { cn } from "@/lib/utils";
44
import { subscribeFrame } from "./frame-clock";
55
import {
@@ -12,13 +12,19 @@ import {
1212
OPENBOT_PATTERN,
1313
OPENBOT_SLOT,
1414
poseAt,
15+
STILL_MAX,
1516
} from "./kite-draw";
1617
import { botLivery } from "./livery";
1718

1819
/** Backing pixels per CSS pixel, at most: sharp on a 3× screen without a huge canvas. */
1920
const MAX_DPR = 3;
2021
/** Where a kite's clock starts its first state: long enough ago that it is already settled. */
2122
const SETTLED = -9;
23+
/**
24+
* The least time between two frames of a kite at 32 px and under, in ms. Only its square moves, a
25+
* few px, so about thirty a second is plenty where a fast screen would draw every one 120 times.
26+
*/
27+
const SMALL_FRAME_MS = 30;
2228

2329
// ---------- dark mode ----------
2430

@@ -55,6 +61,38 @@ function useDarkMode(): boolean {
5561
return useSyncExternalStore(subscribeDark, isDark, () => false);
5662
}
5763

64+
// ---------- screen resolution ----------
65+
66+
/*
67+
* Page zoom and moving a window to another screen change `devicePixelRatio`. A resting kite asks
68+
* for no frames, so without this it would keep the old resolution and go soft. The media query
69+
* matches only the current ratio, so its change event is the moment the ratio changes; it is then
70+
* replaced by one for the new ratio.
71+
*/
72+
function subscribeResolution(onChange: () => void) {
73+
let query: MediaQueryList | null = null;
74+
function listen() {
75+
query?.removeEventListener("change", changed);
76+
query =
77+
window.matchMedia?.(
78+
`(resolution: ${window.devicePixelRatio || 1}dppx)`,
79+
) ?? null;
80+
query?.addEventListener("change", changed);
81+
}
82+
function changed() {
83+
listen();
84+
onChange();
85+
}
86+
listen();
87+
return () => query?.removeEventListener("change", changed);
88+
}
89+
90+
const resolution = () => Math.min(MAX_DPR, window.devicePixelRatio || 1);
91+
92+
function useResolution(): number {
93+
return useSyncExternalStore(subscribeResolution, resolution, () => 1);
94+
}
95+
5896
// ---------- the kite ----------
5997

6098
export type KiteProps = {
@@ -103,6 +141,7 @@ export function Kite({
103141
const canvasRef = useRef<HTMLCanvasElement>(null);
104142
const reduced = useReducedMotion() === true;
105143
const dark = useDarkMode();
144+
const dpr = useResolution();
106145
// The kite's own clock and the states it has been through. Refs, never state: a frame of the
107146
// animation never re-renders React.
108147
const clock = useRef<{ start: number; timeline: KiteTimeline } | null>(null);
@@ -114,7 +153,8 @@ export function Kite({
114153
}
115154
const bleed = kiteBleed(size);
116155

117-
useEffect(() => {
156+
// Before the browser paints, so a kite that mounts is never shown as an empty frame.
157+
useLayoutEffect(() => {
118158
const canvas = canvasRef.current;
119159
const kite = clock.current;
120160
if (!canvas || !kite) return;
@@ -128,7 +168,6 @@ export function Kite({
128168
const draw = (t: number): boolean => {
129169
const ctx = canvas.getContext("2d");
130170
if (!ctx) return false;
131-
const dpr = Math.min(MAX_DPR, window.devicePixelRatio || 1);
132171
const box = size + 2 * bleed;
133172
const backing = Math.round(box * dpr);
134173
if (canvas.width !== backing || canvas.height !== backing) {
@@ -157,7 +196,10 @@ export function Kite({
157196
const t0 = now();
158197
if (!draw(t0) || !busy(t0)) return;
159198
let stop: (() => void) | null = null;
160-
stop = subscribeFrame(() => {
199+
let drewAt = Number.NEGATIVE_INFINITY;
200+
stop = subscribeFrame((frame) => {
201+
if (size <= STILL_MAX && frame - drewAt < SMALL_FRAME_MS) return;
202+
drewAt = frame;
161203
const t = now();
162204
draw(t);
163205
if (!busy(t)) {
@@ -166,7 +208,7 @@ export function Kite({
166208
}
167209
});
168210
return () => stop?.();
169-
}, [state, size, slot, pattern, seed, dark, reduced, still, bleed]);
211+
}, [state, size, slot, pattern, seed, dark, dpr, reduced, still, bleed]);
170212

171213
// The canvas is laid out in shares of the slot, so a parent that resizes the slot scales the
172214
// drawing with it rather than cropping it.

‎app/src/components/agents/kite/livery.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { KITE_PATTERNS, type KitePattern } from "./kite-draw";
1+
import {
2+
KITE_PATTERNS,
3+
type KitePattern,
4+
OPENBOT_PATTERN,
5+
OPENBOT_SLOT,
6+
} from "./kite-draw";
27

38
/** How one Bot is told apart from the others: its colour, its sail pattern and its blink. */
49
export type BotLivery = {
@@ -32,9 +37,15 @@ export function botLivery(seed: string): BotLivery {
3237
hash = Math.imul(hash, FNV_PRIME);
3338
}
3439
const h = hash >>> 0;
40+
const slot = h % 8;
41+
let pick = (h >>> 16) % KITE_PATTERNS.length;
42+
// Violet quarters is OpenBot's own kite. One Bot in 64 would land on it and read as the product,
43+
// so that Bot takes the next sail instead, keeping its colour.
44+
if (slot === OPENBOT_SLOT && KITE_PATTERNS[pick] === OPENBOT_PATTERN)
45+
pick = (pick + 1) % KITE_PATTERNS.length;
3546
return {
36-
slot: h % 8,
37-
pattern: KITE_PATTERNS[(h >>> 16) % KITE_PATTERNS.length],
47+
slot,
48+
pattern: KITE_PATTERNS[pick],
3849
seed: (h % 9973) + 1,
3950
};
4051
}

‎app/tests/kite-livery.test.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { describe, expect, test } from "bun:test";
2-
import { KITE_PATTERNS } from "@/components/agents/kite/kite-draw";
2+
import {
3+
KITE_PATTERNS,
4+
OPENBOT_PATTERN,
5+
OPENBOT_SLOT,
6+
} from "@/components/agents/kite/kite-draw";
37
import { botLivery } from "@/components/agents/kite/livery";
48
import { kiteStateOf } from "@/components/bot-profile/attention";
59
import type { BotAttention } from "@/lib/bot-lifecycle/queries";
@@ -82,6 +86,15 @@ describe("botLivery", () => {
8286
expect(new Set(liveries).size).toBe(shipped.length);
8387
});
8488

89+
test("no Bot wears OpenBot's own kite, violet quarters", () => {
90+
for (let i = 0; i < 20000; i++) {
91+
const { slot, pattern } = botLivery(
92+
`agent_${i.toString(36)}-${i * 7919}`,
93+
);
94+
expect(slot === OPENBOT_SLOT && pattern === OPENBOT_PATTERN).toBe(false);
95+
}
96+
});
97+
8598
test("no seed has no identity: the first colour and a plain sail", () => {
8699
expect(botLivery("")).toEqual({ slot: 0, pattern: "solid", seed: 1 });
87100
});

‎app/tests/kite.test.tsx‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,37 @@ test("a kite that mounts done after working plays its landing", () => {
190190
render(<OpenBotKite enterFrom="thinking" size={88} state="done" />);
191191
expect(frames).toHaveLength(1);
192192
});
193+
194+
test("a zoom or a move to another screen redraws a resting kite at the new resolution", async () => {
195+
let changed: (() => void) | null = null;
196+
spyOn(window, "matchMedia").mockImplementation(
197+
() =>
198+
({
199+
addEventListener: (_: string, listener: () => void) => {
200+
changed = listener;
201+
},
202+
removeEventListener: () => {},
203+
}) as unknown as MediaQueryList,
204+
);
205+
let ratio = 1;
206+
const original = Object.getOwnPropertyDescriptor(window, "devicePixelRatio");
207+
Object.defineProperty(window, "devicePixelRatio", {
208+
configurable: true,
209+
get: () => ratio,
210+
});
211+
try {
212+
const view = render(<OpenBotKite size={24} />);
213+
const canvas = view.container.querySelector("canvas") as HTMLCanvasElement;
214+
const before = canvas.width;
215+
ratio = 2;
216+
await act(async () => {
217+
changed?.();
218+
await Promise.resolve();
219+
});
220+
expect(canvas.width).toBe(before * 2);
221+
expect(frames).toHaveLength(0);
222+
} finally {
223+
if (original) Object.defineProperty(window, "devicePixelRatio", original);
224+
else Reflect.deleteProperty(window, "devicePixelRatio");
225+
}
226+
});

0 commit comments

Comments
 (0)