diff --git a/webviews/src/agent-session/acpmux/conversation/RevealedMarkdown.test.tsx b/webviews/src/agent-session/acpmux/conversation/RevealedMarkdown.test.tsx index f2b9fdd52d7b..c6e7d4f7adb6 100644 --- a/webviews/src/agent-session/acpmux/conversation/RevealedMarkdown.test.tsx +++ b/webviews/src/agent-session/acpmux/conversation/RevealedMarkdown.test.tsx @@ -226,6 +226,102 @@ test("the last word shows once text stops arriving, while the reply still stream expect(seen.at(-1)).toBe("Let me check the tests."); }); +/// A stand-in for `prefers-reduced-motion: reduce` that a test turns on and off, as the system +/// setting does while the app runs. +function reducedMotionQuery(initial: boolean) { + const listeners = new Set<() => void>(); + const query = { + matches: initial, + media: "(prefers-reduced-motion: reduce)", + addEventListener: (_type: string, listener: () => void) => listeners.add(listener), + removeEventListener: (_type: string, listener: () => void) => listeners.delete(listener), + }; + Object.assign(dom!.window, { matchMedia: () => query }); + return { + listeners, + set(on: boolean) { + query.matches = on; + act(() => { + for (const listener of listeners) listener(); + }); + }, + }; +} + +/// Display frames whose cancel takes the frame back, as `cancelAnimationFrame` does. `frame(now)` +/// runs the frames still pending and returns the reply's text. +function cancellableFrames() { + const pending = new Map void>(); + let handles = 0; + revealFrames.request = (callback) => { + handles += 1; + pending.set(handles, callback); + return handles; + }; + revealFrames.cancel = (handle) => void pending.delete(handle); + const frame = (now: number) => { + const due = [...pending.values()]; + pending.clear(); + act(() => { + for (const callback of due) callback(now); + }); + return container.querySelector(".cv-md")?.textContent ?? ""; + }; + return { pending, frame }; +} + +// Reduce Motion turned on while a reply streams: the stylesheet follows at once, and so does the +// reveal. The row shows all of the reply in that frame, its frame loop stops and no step is left +// fading; text that arrives later shows whole, as in a row that mounted with Reduce Motion on. +test("turning Reduce Motion on mid-reply shows the whole reply and stops the reveal", () => { + const motion = reducedMotionQuery(false); + const { pending, frame } = cancellableFrames(); + const reply = LONG_REPLY.trim(); + root = createRoot(container); + render("", true); + render(reply, true); + const paced = [frame(0), frame(16), frame(32)]; + expect(paced.at(-1)!.length).toBeGreaterThan(0); + expect(paced.at(-1)!.length).toBeLessThan(reply.length); + + motion.set(true); + expect(container.querySelector(".cv-md")?.textContent).toBe(reply); + expect(container.querySelectorAll(".cv-fresh").length).toBe(0); + expect(pending.size).toBe(0); + + const more = `${reply} The release went out.`; + render(more, true); + expect(frame(48)).toBe(more); + expect(container.querySelectorAll(".cv-fresh").length).toBe(0); + expect(pending.size).toBe(0); + + act(() => root!.unmount()); + root = undefined; + expect(motion.listeners.size).toBe(0); +}); + +// Text can render and have its pending frame cancelled before the reveal reads it. Turning Reduce +// Motion on there shows that text. Turning it off again must pace only what arrives after it, +// never re-reveal from the reveal's older length: what shows never shrinks. +test("Reduce Motion on then off before a frame keeps what shows when later text arrives", () => { + dom!.window.document.documentElement.dataset.streamWords = "off"; + const motion = reducedMotionQuery(false); + const { frame } = cancellableFrames(); + root = createRoot(container); + render("", true); + render("The checks on the merged head", true); + frame(0); + frame(16); + // Rendered, with its frame still pending when Reduce Motion turns on. + const shown = `${LONG_REPLY.trim()} and the notes are ready.`; + render(shown, true); + motion.set(true); + expect(container.querySelector(".cv-md")?.textContent).toBe(shown); + motion.set(false); + render(`${shown} The release went out.`, true); + for (let index = 0; index < 20; index++) expect(frame(32 + index * 16).length).toBeGreaterThanOrEqual(shown.length); +}); + test("wordEdge ends at a whole word, keeps what shows, and lets a long word through", () => { expect(wordEdge("alpha beta gam", 14, false, 0)).toBe(10); expect(wordEdge("alpha beta gam", 8, false, 0)).toBe(5); diff --git a/webviews/src/agent-session/acpmux/conversation/RevealedMarkdown.tsx b/webviews/src/agent-session/acpmux/conversation/RevealedMarkdown.tsx index bc3c203d0521..c8ea126ee456 100644 --- a/webviews/src/agent-session/acpmux/conversation/RevealedMarkdown.tsx +++ b/webviews/src/agent-session/acpmux/conversation/RevealedMarkdown.tsx @@ -159,6 +159,31 @@ export function useStreamReveal(text: string, streaming: boolean): { visible: st state.handle = 0; }; }, []); + // Reduce Motion can change while the row is mounted, and the stylesheet follows it at once. + // Turned on, the whole reply shows now: the pending frame is cancelled and no step is left + // fading; later text shows as it arrives. Turned off, later text is paced again. + useLayoutEffect(() => { + const query = typeof window === "undefined" ? undefined : window.matchMedia?.("(prefers-reduced-motion: reduce)"); + if (!query) return; + const changed = () => { + const on = query.matches; + if (reduce.current === on) return; + reduce.current = on; + reveal.current!.setReduceMotion(on, latest.current.text); + if (!on) return; + const state = loop.current; + revealFrames.cancel(state.handle); + state.handle = 0; + const { text: target, shown: before } = latest.current; + if (before.length === target.length && !before.fresh.length) return; + const next = { length: target.length, fresh: [], now: before.now }; + latest.current.shown = next; + setShown(next); + }; + changed(); + query.addEventListener("change", changed); + return () => query.removeEventListener("change", changed); + }, []); return { visible: text.slice(0, Math.min(shown.length, text.length)), fresh: shown.fresh, diff --git a/webviews/src/agent-session/acpmux/streamReveal.ts b/webviews/src/agent-session/acpmux/streamReveal.ts index 6854c34fa032..708f6c76b52c 100644 --- a/webviews/src/agent-session/acpmux/streamReveal.ts +++ b/webviews/src/agent-session/acpmux/streamReveal.ts @@ -46,7 +46,7 @@ export class StreamReveal { private gaps: number[] = []; private smoothedRate = 0; private drainUntil: number | undefined; - private readonly reduceMotion: boolean; + private reduceMotion: boolean; constructor(options: StreamRevealOptions = {}) { this.shown = options.initial?.length ?? 0; @@ -64,6 +64,16 @@ export class StreamReveal { this.shown = this.received; } + /// Reduce Motion changed while the row is mounted: on, all of `text` (the latest, which may not + /// have reached `advance` yet) shows now and later text shows as it arrives; off, only text after + /// it is paced. + setReduceMotion(on: boolean, text: string): void { + this.reduceMotion = on; + if (!on) return; + this.received = Math.max(this.received, text.length); + this.flush(); + } + /// The visible length of `text` for the frame at `now` (ms); `done` when the stream ended. advance(text: string, now: number, done: boolean): number { const elapsed =