From c605ac34f843f7a4ec39e39d6438cdc218fbefe0 Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Thu, 5 Nov 2026 01:04:36 -0600 Subject: [PATCH] fix(gui-plugins): drive the artifact viewer, image pan, video overlay and select preview through state machines (#61993) --- .../component-tests/input-surfaces.spec.ts | 26 + packages/app/e2e/regression/review.spec.ts | 34 +- .../src/file/artifact-view-machine.test.ts | 278 ++++++++++ .../src/file/artifact-view-machine.ts | 395 ++++++++++++++ .../gui-plugins/src/file/artifact-view.tsx | 493 +++++++++++------- packages/ui/src/forms/select/machine.test.ts | 105 ++++ packages/ui/src/forms/select/machine.ts | 81 +++ .../ui/src/forms/select/select.stories.tsx | 29 ++ packages/ui/src/forms/select/select.tsx | 76 ++- 9 files changed, 1289 insertions(+), 228 deletions(-) create mode 100644 packages/gui-plugins/src/file/artifact-view-machine.test.ts create mode 100644 packages/gui-plugins/src/file/artifact-view-machine.ts create mode 100644 packages/ui/src/forms/select/machine.test.ts create mode 100644 packages/ui/src/forms/select/machine.ts diff --git a/packages/app/component-tests/input-surfaces.spec.ts b/packages/app/component-tests/input-surfaces.spec.ts index fa2fde9473f..1cce08660c4 100644 --- a/packages/app/component-tests/input-surfaces.spec.ts +++ b/packages/app/component-tests/input-surfaces.spec.ts @@ -125,6 +125,32 @@ story("select fields come in the text input sizes and default to the inline trig await expect(fields.nth(3)).toHaveCSS("box-shadow", "none") }) +story( + "a select previews the highlighted option, reverts it on Escape, and keeps the chosen one", + async ({ mount, page }) => { + const root = await mount("ui-select--highlight-preview") + const trigger = root.locator('[data-component="select-v2"]') + const preview = root.locator('[data-slot="select-preview"]') + + await trigger.click() + await page.getByRole("option", { name: "Cherry", exact: true }).hover() + await expect(preview).toHaveText("Showing Cherry") + await page.keyboard.press("Escape") + await expect(page.getByRole("listbox")).toHaveCount(0) + await expect(preview).toHaveText("Showing Apple") + + await trigger.click() + const banana = page.getByRole("option", { name: "Banana", exact: true }) + await banana.hover() + await expect(preview).toHaveText("Showing Banana") + await banana.click() + await expect(page.getByRole("listbox")).toHaveCount(0) + await expect(trigger).toContainText("Banana") + // Choosing the previewed option must not run its undo afterwards. + await expect(preview).toHaveText("Showing Banana") + }, +) + story("compact checkboxes use the design system control states and focus ring", async ({ mount, page }) => { const root = await mount("ui-checkbox--compact") diff --git a/packages/app/e2e/regression/review.spec.ts b/packages/app/e2e/regression/review.spec.ts index d3bb97dfb76..bbbca8751ed 100644 --- a/packages/app/e2e/regression/review.spec.ts +++ b/packages/app/e2e/regression/review.spec.ts @@ -889,6 +889,7 @@ test("artifact actions copy and download content, switch source, and follow the await context.grantPermissions(["clipboard-read", "clipboard-write"]) const source = '' const table = 'name,role\n"Jensen, Oliver",Researcher\nIsabella,Support' + const broken = ' ({ @@ -935,7 +938,7 @@ test("artifact actions copy and download content, switch source, and follow the content: [ { type: "text", - text: "---\ndescription: Chat heading\n---\n\n[Vector](vector.svg) [Table](data.csv) [Notes](notes.md) [Binary](sample.bin)", + text: "---\ndescription: Chat heading\n---\n\n[Vector](vector.svg) [Table](data.csv) [Notes](notes.md) [Binary](sample.bin) [Broken](broken.svg)", }, ], time: { created: 1, completed: 2 }, @@ -1071,6 +1074,13 @@ test("artifact actions copy and download content, switch source, and follow the await expect(page.getByRole("menuitem", { name: "Download", exact: true })).toHaveCount(0) await page.getByRole("menuitem", { name: "Copy path", exact: true }).click() await expect.poll(() => page.evaluate(() => navigator.clipboard.readText())).toBe(`${workspace.directory}/sample.bin`) + + // A text SVG that fails to decode shows the binary placeholder but can still show its source. + await page.getByRole("link", { name: "Broken", exact: true }).click() + await expect(toolbar.getByText("broken.svg", { exact: true })).toBeVisible() + await expect(panel.getByText("Couldn't load this file.", { exact: true })).toBeVisible() + await toolbar.getByRole("button", { name: "View source", exact: true }).click() + await expect(panel.locator("diffs-container")).toContainText(broken) }) test("Markdown and DOCX sheets stay aligned when switching at wide and narrow preview widths", async ({ page }) => { @@ -1320,6 +1330,28 @@ test("image gestures anchor zoom, pan, and reset independently of the pane", asy await expect(stage).toHaveAttribute("data-zoom", "actual") expect((await image.boundingBox())!.x).toBeCloseTo(scrolled.x + 30, 1) expect((await image.boundingBox())!.y).toBeCloseTo(scrolled.y + 20, 1) + // A pan whose release the image never heard ends on the next move without the button, and pans nothing. + const touched = (await image.boundingBox())! + await page.mouse.down() + await expect(image).toHaveCSS("cursor", "grabbing") + await image.evaluate( + (element, point) => + element.dispatchEvent( + new PointerEvent("pointermove", { + bubbles: true, + pointerId: 1, + pointerType: "mouse", + isPrimary: true, + buttons: 0, + clientX: point.x, + clientY: point.y, + }), + ), + { x: anchor.x - 100, y: anchor.y - 100 }, + ) + await expect(image).toHaveCSS("cursor", "grab") + expect(await image.boundingBox()).toEqual(touched) + await page.mouse.up() // Keyboard-accessible fit restores the exact original geometry. await fit.focus() await page.keyboard.press("Enter") diff --git a/packages/gui-plugins/src/file/artifact-view-machine.test.ts b/packages/gui-plugins/src/file/artifact-view-machine.test.ts new file mode 100644 index 00000000000..36bc5442c40 --- /dev/null +++ b/packages/gui-plugins/src/file/artifact-view-machine.test.ts @@ -0,0 +1,278 @@ +import { describe, expect, test } from "bun:test" +import type { FileContent } from "@opencode/plugin/gui" +import { + createArtifactViewerState, + createVideoOverlayState, + INITIAL_IMAGE_STAGE_GESTURE_STATE, + selectArtifactInfo, + selectArtifactMode, + selectArtifactPreviewable, + selectArtifactReason, + selectArtifactShowingPreview, + selectArtifactStageKind, + selectStageClickSuppressed, + selectStageCursor, + selectVideoControlsVisible, + selectVideoLoading, + transitionArtifactViewer, + transitionImageStageGesture, + transitionVideoOverlay, + type ArtifactViewerEvent, + type ArtifactViewerState, + type ImageStageGestureEvent, + type ImageStageGestureState, + type ImageStageGestureTransition, + type VideoOverlayEvent, + type VideoOverlayState, + type VideoOverlayTransition, +} from "./artifact-view-machine" + +const svg = (content: string): FileContent => ({ type: "text", content }) + +describe("artifact viewer", () => { + const run = (state: ArtifactViewerState, content: FileContent, ...events: ArtifactViewerEvent[]) => + events.reduce((value, event) => transitionArtifactViewer(value, content, event), state) + + test("a broken SVG keeps its source action and can still show its source", () => { + // Before: a failed decode turned the kind into binary, which removed "View source" for a text file. + const content = svg(" { + const content = svg("") + + const reported = run(createArtifactViewerState(content), content, { + type: "info.reported", + content, + info: { width: 2, height: 3 }, + }) + + expect(selectArtifactInfo(reported, content)).toEqual({ width: 2, height: 3 }) + const failed = run(reported, content, { type: "decode.failed", content }) + expect(selectArtifactInfo(failed, content)).toEqual({}) + expect(run(failed, content, { type: "info.reported", content, info: { width: 2 } })).toBe(failed) + expect(run(failed, content, { type: "decode.failed", content })).toBe(failed) + }) + + test("binary files offer no source", () => { + const content: FileContent = { type: "binary", content: "AAEC", encoding: "base64" } + + expect(selectArtifactPreviewable("sample.bin", content)).toBe(false) + expect(selectArtifactShowingPreview(createArtifactViewerState(content), "sample.bin", content)).toBe(true) + expect(selectArtifactStageKind(createArtifactViewerState(content), "sample.bin", content)).toBe("binary") + }) + + test("results for an earlier content change nothing, and a reread starts from the preview", () => { + // Before: a late failure for the previous content was stamped onto the reread one. + const first = svg("") + const second = svg("") + const source = run(createArtifactViewerState(first), first, { type: "mode.changed", mode: "source" }) + const failed = run(source, first, { type: "decode.failed", content: first }) + + expect(selectArtifactMode(failed, second)).toBe("preview") + expect(selectArtifactStageKind(failed, "a.svg", second)).toBe("svg") + + const late = run( + failed, + second, + { type: "decode.failed", content: first, reason: "Failed to load" }, + { type: "info.reported", content: first, info: { width: 1, height: 1 } }, + ) + + expect(late.content).toBe(second) + expect(selectArtifactStageKind(late, "a.svg", second)).toBe("svg") + expect(selectArtifactInfo(late, second)).toEqual({}) + }) +}) + +describe("image stage gesture", () => { + const down = (pointerId = 1, zoomed = true): ImageStageGestureEvent => ({ + type: "pointer.down", + pointerId, + button: 0, + primary: true, + zoomed, + }) + + const run = (state: ImageStageGestureState, ...events: ImageStageGestureEvent[]) => + events.reduce((step, event) => transitionImageStageGesture(step.state, event), { + state, + effects: [], + }) + + test("a moved pan swallows its click, even after the browser releases capture", () => { + const released = run( + INITIAL_IMAGE_STAGE_GESTURE_STATE, + down(), + { type: "pointer.moved", pointerId: 1 }, + { type: "pointer.up", pointerId: 1 }, + { type: "pointer.cancel", pointerId: 1 }, + ) + + expect(selectStageClickSuppressed(released.state)).toBe(true) + const clicked = run(released.state, { type: "click" }) + expect(clicked.state).toEqual({ status: "idle" }) + expect(selectStageClickSuppressed(clicked.state)).toBe(false) + }) + + test("a cancelled pan, or a touch pan with no click, does not swallow the next click", () => { + // Before: the moved flag outlived a pointercancel and ate the next real click. + const cancelled = run( + INITIAL_IMAGE_STAGE_GESTURE_STATE, + down(), + { type: "pointer.moved", pointerId: 1 }, + { type: "pointer.cancel", pointerId: 1 }, + ) + + expect(cancelled.effects).toEqual([{ type: "pointer.release", pointerId: 1 }]) + expect(selectStageClickSuppressed(cancelled.state)).toBe(false) + + const touched = run( + INITIAL_IMAGE_STAGE_GESTURE_STATE, + down(), + { type: "pointer.moved", pointerId: 1 }, + { type: "pointer.up", pointerId: 1 }, + down(2, false), + ) + + expect(touched.state).toEqual({ status: "idle" }) + }) + + test("a press that never moved leaves clicks alone", () => { + const pressed = run(INITIAL_IMAGE_STAGE_GESTURE_STATE, down(), { type: "pointer.up", pointerId: 1 }) + + expect(selectStageClickSuppressed(pressed.state)).toBe(false) + }) + + test("zooming back to fit ends the pan and releases the pointer", () => { + // Before: a Ctrl+wheel zoom out or Fit mid-drag left the drag active at fit. + const panning = run(INITIAL_IMAGE_STAGE_GESTURE_STATE, down()) + const fitted = run(panning.state, { type: "zoom.changed", zoomed: false }) + + expect(fitted.state).toEqual({ status: "idle" }) + expect(fitted.effects).toEqual([{ type: "pointer.release", pointerId: 1 }]) + expect(selectStageCursor(panning.state, true)).toBe("grabbing") + expect(selectStageCursor(fitted.state, true)).toBe("grab") + expect(run(panning.state, { type: "zoom.changed", zoomed: true }).state).toBe(panning.state) + }) + + test("only a primary press on a zoomed image pans, and a second pointer leaves it alone", () => { + expect(run(INITIAL_IMAGE_STAGE_GESTURE_STATE, down(1, false)).state).toBe(INITIAL_IMAGE_STAGE_GESTURE_STATE) + const panning = run(INITIAL_IMAGE_STAGE_GESTURE_STATE, down()) + + expect(panning.effects).toEqual([{ type: "pointer.capture", pointerId: 1 }]) + expect(run(panning.state, down(2)).state).toBe(panning.state) + expect(run(panning.state, { type: "pointer.up", pointerId: 2 }).state).toBe(panning.state) + }) + + test("a pan that has moved keeps its state for further moves", () => { + const moved = run(INITIAL_IMAGE_STAGE_GESTURE_STATE, down(), { type: "pointer.moved", pointerId: 1 }) + + expect(run(moved.state, { type: "pointer.moved", pointerId: 1 }).state).toBe(moved.state) + }) + + test("disposal releases a pan and ignores everything after", () => { + const disposed = run(INITIAL_IMAGE_STAGE_GESTURE_STATE, down(), { type: "disposed" }) + + expect(disposed.effects).toEqual([{ type: "pointer.release", pointerId: 1 }]) + expect(run(disposed.state, down()).state).toBe(disposed.state) + }) +}) + +describe("video overlay", () => { + const run = (state: VideoOverlayState, ...events: VideoOverlayEvent[]) => + events.reduce((step, event) => transitionVideoOverlay(step.state, event), { + state, + effects: [], + }) + + const paused = run(createVideoOverlayState(), { type: "media.loaded", paused: true }).state + + test("loading hides the controls until the first frame", () => { + expect(selectVideoLoading(createVideoOverlayState())).toBe(true) + expect(selectVideoControlsVisible(createVideoOverlayState())).toBe(false) + expect(selectVideoLoading(paused)).toBe(false) + expect(selectVideoControlsVisible(paused)).toBe(true) + }) + + test("playing starts a fresh idle timer, so a timer armed while paused cannot hide the controls", () => { + // Before: a pointer move while paused armed a timer that hid the controls shortly after Play. + const moved = transitionVideoOverlay(paused, { type: "pointer.moved" }) + expect(moved.state).toBe(paused) + expect(moved.effects).toEqual([]) + + const playing = run(paused, { type: "playback.changed", paused: false }) + expect(playing.effects).toEqual([{ type: "idle.schedule", attempt: 1, delayMs: 2_500 }]) + expect(selectVideoControlsVisible(playing.state)).toBe(true) + expect(run(playing.state, { type: "idle.elapsed", attempt: 0 }).state).toBe(playing.state) + + const idle = run(playing.state, { type: "idle.elapsed", attempt: 1 }) + expect(selectVideoControlsVisible(idle.state)).toBe(false) + expect(run(idle.state, { type: "pointer.moved" }).effects).toEqual([ + { type: "idle.schedule", attempt: 2, delayMs: 2_500 }, + ]) + }) + + test("pausing or leaving clears the idle timer", () => { + const playing = run(paused, { type: "playback.changed", paused: false }).state + + expect(run(playing, { type: "playback.changed", paused: true }).effects).toEqual([{ type: "idle.clear" }]) + const left = run(playing, { type: "pointer.left" }) + expect(left.effects).toEqual([{ type: "idle.clear" }]) + expect(selectVideoControlsVisible(left.state)).toBe(false) + }) + + test("the controls stay hidden while the preview flies the video back", () => { + // Before: closing the preview showed the fullscreen button at once, over a stage the video had not returned to. + const expanded = run(paused, { type: "expand.requested", canExpand: true }) + expect(expanded.effects).toEqual([{ type: "preview.open", attempt: 1 }]) + expect(selectVideoControlsVisible(expanded.state)).toBe(false) + expect(run(expanded.state, { type: "expand.requested", canExpand: true }).state).toBe(expanded.state) + expect(run(expanded.state, { type: "pointer.moved" }).state).toBe(expanded.state) + + const playing = run(expanded.state, { type: "playback.changed", paused: false }) + expect(selectVideoControlsVisible(playing.state)).toBe(false) + expect(run(playing.state, { type: "preview.returned", attempt: 0 }).state).toBe(playing.state) + + const back = run(playing.state, { type: "preview.returned", attempt: 1 }) + expect(back.state).toEqual({ status: "playing-idle", issued: 1 }) + expect(back.effects).toEqual([{ type: "preview.release", attempt: 1 }]) + expect(run(back.state, { type: "preview.returned", attempt: 1 }).state).toBe(back.state) + }) + + test("a video without a frame, or still loading, does not expand", () => { + expect(run(paused, { type: "expand.requested", canExpand: false }).state).toBe(paused) + const loading = createVideoOverlayState() + expect(run(loading, { type: "expand.requested", canExpand: true }).state).toBe(loading) + }) + + test("unmounting closes an open preview and clears a running timer", () => { + // Before: a closed tab left its preview dialog open with no owner. + const expanded = run(paused, { type: "expand.requested", canExpand: true }).state + + expect(run(expanded, { type: "disposed" }).effects).toEqual([{ type: "preview.close", attempt: 1 }]) + const playing = run(paused, { type: "playback.changed", paused: false }).state + const disposed = run(playing, { type: "disposed" }) + expect(disposed.effects).toEqual([{ type: "idle.clear" }]) + expect(run(disposed.state, { type: "idle.elapsed", attempt: 1 }).state).toBe(disposed.state) + }) +}) diff --git a/packages/gui-plugins/src/file/artifact-view-machine.ts b/packages/gui-plugins/src/file/artifact-view-machine.ts new file mode 100644 index 00000000000..75ee77f7e48 --- /dev/null +++ b/packages/gui-plugins/src/file/artifact-view-machine.ts @@ -0,0 +1,395 @@ +import type { FileContent } from "@opencode/plugin/gui" +import { artifactKind, type ArtifactKind } from "@opencode/util/artifact" + +export type ArtifactMode = "preview" | "source" + +/** Facts a viewer learns from the decoded media, shown in the toolbar. */ +export type ArtifactInfo = { + readonly width?: number + readonly height?: number + readonly duration?: number + readonly rows?: number + readonly columns?: number + /** Facts a plugin's viewer reports, already localized. */ + readonly details?: readonly string[] +} + +/** + * The viewer of one loaded content: a reread file starts from the preview again. `undecodable` is a file its viewer + * could not decode; it shows the binary placeholder in preview and keeps the mode, so a broken SVG can still show its + * source. + */ +export type ArtifactViewerState = + | { readonly status: "preview" | "source"; readonly content: FileContent; readonly info: ArtifactInfo } + | { + readonly status: "undecodable" + readonly content: FileContent + readonly mode: ArtifactMode + /** Why the file shows as binary, when a viewer said. */ + readonly reason?: string + } + +/** Results name the content they decoded, so a late one from a file since reread changes nothing. */ +export type ArtifactViewerEvent = + | { readonly type: "mode.changed"; readonly mode: ArtifactMode } + | { readonly type: "info.reported"; readonly content: FileContent; readonly info: ArtifactInfo } + | { readonly type: "decode.failed"; readonly content: FileContent; readonly reason?: string } + +const EMPTY_INFO: ArtifactInfo = Object.freeze({}) + +/** Kinds that render a preview from their text and can toggle back to highlighted source. */ +const previewableKinds: readonly ArtifactKind[] = ["svg", "html", "markdown", "mermaid", "table"] + +export function createArtifactViewerState(content: FileContent): ArtifactViewerState { + return { status: "preview", content, info: EMPTY_INFO } +} + +/** `content` is the one shown now; state kept for an earlier content reads as a fresh preview. */ +export function transitionArtifactViewer( + state: ArtifactViewerState, + content: FileContent, + event: ArtifactViewerEvent, +): ArtifactViewerState { + const base = current(state, content) + + if (event.type === "mode.changed") { + if (selectArtifactMode(base, content) === event.mode) return base + + return base.status === "undecodable" + ? { ...base, mode: event.mode } + : { status: event.mode, content, info: base.info } + } + + if (event.content !== content) return base + + if (event.type === "info.reported") { + if (base.status === "undecodable") return base + + return { status: base.status, content, info: event.info } + } + + if (base.status === "undecodable" && base.reason === event.reason) return base + + // A file that fails also drops the facts its viewer reported, such as a page count. + return { status: "undecodable", content, mode: selectArtifactMode(base, content), reason: event.reason } +} + +/** Text of a kind that previews from it can switch to its source, also once its preview failed to decode. */ +export function selectArtifactPreviewable(path: string, content: FileContent) { + return content.type !== "binary" && previewableKinds.includes(artifactKind(path)) +} + +export function selectArtifactMode(state: ArtifactViewerState, content: FileContent): ArtifactMode { + const value = current(state, content) + + return value.status === "undecodable" ? value.mode : value.status +} + +export function selectArtifactShowingPreview(state: ArtifactViewerState, path: string, content: FileContent) { + return !selectArtifactPreviewable(path, content) || selectArtifactMode(state, content) === "preview" +} + +/** The viewer the stage renders; `binary` is the placeholder. */ +export function selectArtifactStageKind( + state: ArtifactViewerState, + path: string, + content: FileContent, +): ArtifactKind | "binary" { + if (content.type === "binary" && !content.mimeType) return "binary" + + if (current(state, content).status === "undecodable") return "binary" + + return artifactKind(path) +} + +export function selectArtifactInfo(state: ArtifactViewerState, content: FileContent) { + const value = current(state, content) + + return value.status === "undecodable" ? EMPTY_INFO : value.info +} + +export function selectArtifactReason(state: ArtifactViewerState, content: FileContent) { + const value = current(state, content) + + return value.status === "undecodable" ? value.reason : undefined +} + +function current(state: ArtifactViewerState, content: FileContent) { + return state.content === content ? state : createArtifactViewerState(content) +} + +/** How far a press moves before it pans rather than clicks. */ +export const STAGE_DRAG_THRESHOLD_PX = 3 + +/** + * The press on a zoomed image. `suppress-click` follows a pan that moved: the click that ends it neither fits nor + * zooms. It lasts until that click or the next press, since a touch pan produces no click. Pan offsets live in the + * driver and paint straight to the image; once a pan has moved, its pointer moves dispatch nothing at all. + */ +export type ImageStageGestureState = + | { readonly status: "idle" } + | { readonly status: "panning"; readonly pointerId: number; readonly moved: boolean } + | { readonly status: "suppress-click" } + | { readonly status: "disposed" } + +/** `zoomed` is whether the image shows above fit. */ +export type ImageStageGestureEvent = + | { + readonly type: "pointer.down" + readonly pointerId: number + readonly button: number + readonly primary: boolean + readonly zoomed: boolean + } + /** The pan pointer moved past the threshold; the driver sends it once per pan. */ + | { readonly type: "pointer.moved"; readonly pointerId: number } + | { readonly type: "pointer.up"; readonly pointerId: number } + /** `pointercancel`, `lostpointercapture`, or a mouse move with the primary button already up. */ + | { readonly type: "pointer.cancel"; readonly pointerId: number } + /** Fit, a click, a pinch or a wheel zoom changed whether the image shows above fit. */ + | { readonly type: "zoom.changed"; readonly zoomed: boolean } + | { readonly type: "click" } + | { readonly type: "disposed" } + +export type ImageStageGestureEffect = + | { readonly type: "pointer.capture"; readonly pointerId: number } + | { readonly type: "pointer.release"; readonly pointerId: number } + +export type ImageStageGestureTransition = { + readonly state: ImageStageGestureState + readonly effects: readonly ImageStageGestureEffect[] +} + +const STAGE_IDLE: ImageStageGestureState = Object.freeze({ status: "idle" }) + +const STAGE_SUPPRESS_CLICK: ImageStageGestureState = Object.freeze({ status: "suppress-click" }) + +const STAGE_DISPOSED: ImageStageGestureState = Object.freeze({ status: "disposed" }) + +const NO_STAGE_EFFECTS: readonly ImageStageGestureEffect[] = Object.freeze([]) + +export const INITIAL_IMAGE_STAGE_GESTURE_STATE: ImageStageGestureState = STAGE_IDLE + +export function transitionImageStageGesture( + state: ImageStageGestureState, + event: ImageStageGestureEvent, +): ImageStageGestureTransition { + if (state.status === "disposed") return unchangedStage(state) + const pan = state.status === "panning" ? state : undefined + + if (event.type === "disposed") return { state: STAGE_DISPOSED, effects: releaseStage(pan) } + + if (event.type === "pointer.down") { + // A second pointer leaves the pan alone; the same pointer pressing again means its release never arrived. + if (pan && pan.pointerId !== event.pointerId) return unchangedStage(state) + + if (event.primary && event.button === 0 && event.zoomed) { + return { + state: { status: "panning", pointerId: event.pointerId, moved: false }, + effects: [{ type: "pointer.capture", pointerId: event.pointerId }], + } + } + + return state.status === "suppress-click" ? unchangedStage(STAGE_IDLE) : unchangedStage(state) + } + + if (event.type === "pointer.moved") { + if (pan?.pointerId !== event.pointerId || pan.moved) return unchangedStage(state) + + return { state: { status: "panning", pointerId: pan.pointerId, moved: true }, effects: NO_STAGE_EFFECTS } + } + + if (event.type === "pointer.up") { + if (pan?.pointerId !== event.pointerId) return unchangedStage(state) + + return unchangedStage(pan.moved ? STAGE_SUPPRESS_CLICK : STAGE_IDLE) + } + + // The browser releases capture itself after pointerup; that lostpointercapture lands in suppress-click and changes + // nothing, so the click that follows is still swallowed. + if (event.type === "pointer.cancel") { + if (pan?.pointerId !== event.pointerId) return unchangedStage(state) + + return { state: STAGE_IDLE, effects: releaseStage(pan) } + } + + if (event.type === "zoom.changed") { + if (!pan || event.zoomed) return unchangedStage(state) + + return { state: STAGE_IDLE, effects: releaseStage(pan) } + } + + return state.status === "suppress-click" ? unchangedStage(STAGE_IDLE) : unchangedStage(state) +} + +/** Whether the pan under way follows `pointerId`. */ +export function selectStagePanPointer(state: ImageStageGestureState, pointerId: number) { + return state.status === "panning" && state.pointerId === pointerId +} + +/** Whether the pan under way has passed the threshold, so each move pans. */ +export function selectStagePanMoved(state: ImageStageGestureState) { + return state.status === "panning" && state.moved +} + +/** Whether the next click ends a moved pan, and so neither fits nor zooms. */ +export function selectStageClickSuppressed(state: ImageStageGestureState) { + return state.status === "suppress-click" +} + +export function selectStageCursor(state: ImageStageGestureState, zoomed: boolean) { + if (!zoomed) return undefined + + return state.status === "panning" ? "grabbing" : "grab" +} + +function releaseStage(pan: Extract | undefined) { + if (!pan) return NO_STAGE_EFFECTS + + return [{ type: "pointer.release", pointerId: pan.pointerId } as const] +} + +function unchangedStage(state: ImageStageGestureState): ImageStageGestureTransition { + return { state, effects: NO_STAGE_EFFECTS } +} + +/** How long playing video keeps its overlay controls after the pointer last moved. */ +export const VIDEO_IDLE_MS = 2_500 + +/** + * The inline video's overlay controls. They show while paused, and while playing until the pointer rests. `expanded` + * lasts from the preview opening until the borrowed video is back in the stage (or the preview is gone), so the + * controls never float over an empty stage while it flies back. `issued` numbers each idle timer and preview: in + * `playing-active` it is the running timer, in `expanded` the open preview. + */ +export type VideoOverlayState = + | { readonly status: "loading"; readonly paused: boolean; readonly issued: number } + | { readonly status: "paused" | "playing-active" | "playing-idle"; readonly issued: number } + | { readonly status: "expanded"; readonly issued: number; readonly paused: boolean } + | { readonly status: "disposed" } + +export type VideoOverlayEvent = + | { readonly type: "media.loaded"; readonly paused: boolean } + | { readonly type: "playback.changed"; readonly paused: boolean } + | { readonly type: "pointer.moved" } + | { readonly type: "pointer.left" } + | { readonly type: "idle.elapsed"; readonly attempt: number } + /** `canExpand`: the video has a frame and sits in the stage. */ + | { readonly type: "expand.requested"; readonly canExpand: boolean } + /** The video is back in the stage, or the preview closed or was disposed without taking it. */ + | { readonly type: "preview.returned"; readonly attempt: number } + | { readonly type: "disposed" } + +export type VideoOverlayEffect = + | { readonly type: "idle.schedule"; readonly attempt: number; readonly delayMs: number } + | { readonly type: "idle.clear" } + | { readonly type: "preview.open"; readonly attempt: number } + /** Stops watching for the video's return; the preview closes on its own. */ + | { readonly type: "preview.release"; readonly attempt: number } + /** The stage is going away: stop watching and close the preview. */ + | { readonly type: "preview.close"; readonly attempt: number } + +export type VideoOverlayTransition = { + readonly state: VideoOverlayState + readonly effects: readonly VideoOverlayEffect[] +} + +const VIDEO_DISPOSED: VideoOverlayState = Object.freeze({ status: "disposed" }) + +export function createVideoOverlayState(): VideoOverlayState { + return { status: "loading", paused: true, issued: 0 } +} + +export function transitionVideoOverlay(state: VideoOverlayState, event: VideoOverlayEvent): VideoOverlayTransition { + if (state.status === "disposed") return { state, effects: [] } + + if (event.type === "disposed") { + if (state.status === "playing-active") return { state: VIDEO_DISPOSED, effects: [{ type: "idle.clear" }] } + + if (state.status === "expanded") { + return { state: VIDEO_DISPOSED, effects: [{ type: "preview.close", attempt: state.issued }] } + } + + return { state: VIDEO_DISPOSED, effects: [] } + } + + if (event.type === "media.loaded") { + if (state.status !== "loading") return { state, effects: [] } + + return event.paused ? { state: { status: "paused", issued: state.issued }, effects: [] } : active(state.issued) + } + + if (event.type === "playback.changed") { + if (state.status === "loading" || state.status === "expanded") { + return state.paused === event.paused + ? { state, effects: [] } + : { state: { ...state, paused: event.paused }, effects: [] } + } + + if (event.paused === (state.status === "paused")) return { state, effects: [] } + + if (!event.paused) return active(state.issued) + + return { + state: { status: "paused", issued: state.issued }, + effects: state.status === "playing-active" ? [{ type: "idle.clear" }] : [], + } + } + + // Paused, loading or expanded video keeps its controls as they are, so pointer moves start no timers. + if (event.type === "pointer.moved") { + if (state.status !== "playing-active" && state.status !== "playing-idle") return { state, effects: [] } + + return active(state.issued) + } + + if (event.type === "pointer.left") { + if (state.status !== "playing-active") return { state, effects: [] } + + return { state: { status: "playing-idle", issued: state.issued }, effects: [{ type: "idle.clear" }] } + } + + if (event.type === "idle.elapsed") { + if (state.status !== "playing-active" || state.issued !== event.attempt) return { state, effects: [] } + + return { state: { status: "playing-idle", issued: state.issued }, effects: [] } + } + + if (event.type === "expand.requested") { + if (state.status === "loading" || state.status === "expanded" || !event.canExpand) return { state, effects: [] } + const attempt = state.issued + 1 + + return { + state: { status: "expanded", issued: attempt, paused: state.status === "paused" }, + effects: [ + ...(state.status === "playing-active" ? [{ type: "idle.clear" } as const] : []), + { type: "preview.open", attempt }, + ], + } + } + + if (state.status !== "expanded" || state.issued !== event.attempt) return { state, effects: [] } + + return { + state: { status: state.paused ? "paused" : "playing-idle", issued: state.issued }, + effects: [{ type: "preview.release", attempt: state.issued }], + } +} + +/** The loading indicator holds the stage until the first frame. */ +export function selectVideoLoading(state: VideoOverlayState) { + return state.status === "loading" +} + +export function selectVideoControlsVisible(state: VideoOverlayState) { + return state.status === "paused" || state.status === "playing-active" +} + +function active(issued: number): VideoOverlayTransition { + const attempt = issued + 1 + + return { + state: { status: "playing-active", issued: attempt }, + effects: [{ type: "idle.schedule", attempt, delayMs: VIDEO_IDLE_MS }], + } +} diff --git a/packages/gui-plugins/src/file/artifact-view.tsx b/packages/gui-plugins/src/file/artifact-view.tsx index 8590cd71eaf..2f3746c44ce 100644 --- a/packages/gui-plugins/src/file/artifact-view.tsx +++ b/packages/gui-plugins/src/file/artifact-view.tsx @@ -23,7 +23,7 @@ import { Loader } from "@opencode/ui/loader" import { showToast } from "@opencode/ui/toast" import { Markdown } from "@opencode/session-ui/markdown" import { MarkdownProvider, useMarkdown } from "@opencode/session-ui/context/markdown" -import { artifactKind, type ArtifactKind } from "@opencode/util/artifact" +import { artifactKind } from "@opencode/util/artifact" import { getDirectory, getFilename } from "@opencode/util/path" import { createKeyed, usePlugin, type FileContent, type MountedSession, type SessionScreen } from "@opencode/plugin/gui" import { @@ -36,40 +36,43 @@ import { type FrontmatterProperty, } from "./artifact" import { FileViewer } from "@opencode/plugin/gui/file" +import { uuid } from "@opencode/util/uuid" import { ArtifactToolbar } from "./artifact-toolbar" +import { + createArtifactViewerState, + createVideoOverlayState, + INITIAL_IMAGE_STAGE_GESTURE_STATE, + selectArtifactInfo, + selectArtifactMode, + selectArtifactPreviewable, + selectArtifactReason, + selectArtifactShowingPreview, + selectArtifactStageKind, + selectStageClickSuppressed, + selectStageCursor, + selectStagePanMoved, + selectStagePanPointer, + selectVideoControlsVisible, + selectVideoLoading, + STAGE_DRAG_THRESHOLD_PX, + transitionArtifactViewer, + transitionImageStageGesture, + transitionVideoOverlay, + type ArtifactInfo, + type ArtifactViewerEvent, + type ImageStageGestureEvent, + type VideoOverlayEvent, +} from "./artifact-view-machine" -type ArtifactMode = "preview" | "source" - -/** Facts a viewer learns from the decoded media, shown in the toolbar. */ -type ArtifactInfo = { - width?: number - height?: number - duration?: number - rows?: number - columns?: number - /** Facts a plugin's viewer reports, already localized. */ - details?: readonly string[] -} - -type ViewerState = { - readonly mode: ArtifactMode - readonly info: ArtifactInfo - readonly undecodable: boolean - /** Why the file shows as binary, when a viewer said. */ - readonly reason?: string -} - +/** Results name the content they came from, so one from a file since reread changes nothing. */ type MediaProps = { path: string content: FileContent - onInfo: (info: ArtifactInfo) => void + onInfo: (content: FileContent, info: ArtifactInfo) => void /** The browser could not decode the bytes; the host falls back to the binary placeholder. */ - onError: () => void + onError: (content: FileContent) => void } -/** Kinds that render a preview from their text and can toggle back to highlighted source. */ -const previewableKinds: readonly ArtifactKind[] = ["svg", "html", "markdown", "mermaid", "table"] - /** * Renders a loaded non-text file: media, documents, and data get a dedicated viewer with a toolbar; * previewable text kinds can switch to `source`, which the host supplies (its code view). @@ -84,32 +87,20 @@ export default function ArtifactView(props: { }) { const ctx = usePlugin() const locale = ctx.locale - // Media the browser could not decode falls back to the binary placeholder. - const initial: ViewerState = { mode: "preview", info: {}, undecodable: false } - // The viewer state belongs to one loaded content: a reloaded file starts from the preview again. - const [saved, setSaved] = createSignal({ ...initial, content: props.content }) + // The viewer state belongs to one loaded content: a reread file starts from the preview again. + const [viewerState, setViewerState] = createSignal(createArtifactViewerState(props.content)) - const state = () => { - const value = saved() + const dispatchViewer = (event: ArtifactViewerEvent) => { + const prev = untrack(viewerState) + const content = untrack(() => props.content) + const next = transitionArtifactViewer(prev, content, event) - return value.content === props.content ? value : initial + if (next !== prev) setViewerState(next) } - const change = (next: Partial) => setSaved({ ...state(), ...next, content: props.content }) - - const kind = createMemo(() => { - if (props.content.type === "binary" && !props.content.mimeType) return "binary" - - if (state().undecodable) return "binary" - - return artifactKind(props.path) - }) - - const previewable = createMemo(() => { - const value = kind() - - return value !== "binary" && previewableKinds.includes(value) - }) + // Media the browser could not decode falls back to the binary placeholder. + const kind = createMemo(() => selectArtifactStageKind(viewerState(), props.path, props.content)) + const previewable = createMemo(() => selectArtifactPreviewable(props.path, props.content)) const table = createMemo(() => kind() === "table" @@ -119,7 +110,10 @@ export default function ArtifactView(props: { const meta = createMemo(() => { const parsed = table() - const info: ArtifactInfo = parsed ? { rows: parsed.total, columns: parsed.columns } : state().info + + const info: ArtifactInfo = parsed + ? { rows: parsed.total, columns: parsed.columns } + : selectArtifactInfo(viewerState(), props.content) return [ info.duration ? formatDuration(info.duration) : undefined, @@ -131,10 +125,11 @@ export default function ArtifactView(props: { ].filter((item): item is string => !!item) }) - // A file that fails also drops the facts its viewer reported, such as a page count. - const fail = (reason?: string) => change({ undecodable: true, info: {}, reason }) - - const media = { onInfo: (info: ArtifactInfo) => change({ info }), onError: () => fail(ctx.t("view.loadFailed")) } + const media = { + onInfo: (content: FileContent, info: ArtifactInfo) => dispatchViewer({ type: "info.reported", content, info }), + onError: (content: FileContent) => + dispatchViewer({ type: "decode.failed", content, reason: ctx.t("view.loadFailed") }), + } const size = () => formatBytes(locale.locale(), contentBytes(props.content)) @@ -175,18 +170,22 @@ export default function ArtifactView(props: { session={props.session} screen={props.screen} path={props.path} - text={ - props.content.type !== "binary" && previewableKinds.includes(artifactKind(props.path)) - ? props.content.content - : undefined - } - mode={state().mode} - onModeChange={previewable() ? (mode) => change({ mode }) : undefined} + text={previewable() ? props.content.content : undefined} + mode={selectArtifactMode(viewerState(), props.content)} + onModeChange={previewable() ? (mode) => dispatchViewer({ type: "mode.changed", mode }) : undefined} meta={meta()} html={kind() === "html"} /> - - }> + + + } + > @@ -212,8 +211,8 @@ export default function ArtifactView(props: { path={props.path} size={size()} content={props.content} - onDetails={(details) => change({ info: { details } })} - onError={fail} + onDetails={(content, details) => dispatchViewer({ type: "info.reported", content, info: { details } })} + onError={(content, reason) => dispatchViewer({ type: "decode.failed", content, reason })} /> )} @@ -244,16 +243,24 @@ function formatDuration(seconds: number) { return `${minutes}:${String(total % 60).padStart(2, "0")}` } -function createMediaUrl(content: () => FileContent) { - return createMemo(() => { - const streamed = content().url +/** + * The URL media loads from, with the content it was made for. A reread that streams from the same URL keeps the + * source, so media already showing is not reloaded. + */ +function createMediaSource(content: () => FileContent) { + return createMemo( + () => { + const value = content() - if (streamed) return streamed - const value = blobUrlFromContent(content()) - onCleanup(() => URL.revokeObjectURL(value)) + if (value.url) return { content: value, src: value.url } + const src = blobUrlFromContent(value) + onCleanup(() => URL.revokeObjectURL(src)) - return value - }) + return { content: value, src } + }, + undefined, + { equals: (prev, next) => prev.src === next.src }, + ) } /** Holds the pane until the media's first frame, so a file still streaming never shows as blank. */ @@ -273,26 +280,25 @@ function MediaLoading() { /** Images and SVG previews: each decoded image owns its viewport. */ function ArtifactImage(props: MediaProps) { - const url = createMediaUrl(() => props.content) + const source = createMediaSource(() => props.content) return ( - - {(src) => } + + {(source) => } ) } function ImageStage(props: MediaProps & { src: string }) { - const [view, setView] = createStore({ - width: 0, - height: 0, - imageWidth: 0, - imageHeight: 0, - zoom: 1, - x: 0, - y: 0, - dragging: false, - }) + const [view, setView] = createStore({ width: 0, height: 0, imageWidth: 0, imageHeight: 0, zoom: 1 }) + const [gesture, setGesture] = createSignal(INITIAL_IMAGE_STAGE_GESTURE_STATE) + // The pan offsets paint straight to the image, so a pointer move writes no reactive state. + let panX = 0 + let panY = 0 + // Where the pan's pointer pressed, then where it was on its last move. + const origin = { x: 0, y: 0 } + let stage: HTMLDivElement | undefined + let image: HTMLImageElement | undefined const fit = () => Math.min( @@ -304,38 +310,44 @@ function ImageStage(props: MediaProps & { src: string }) { const minZoom = () => Math.min(0.25, 1 / fit()) const maxZoom = () => Math.max(8, 1 / fit()) - const bound = (offset: number, image: number, zoom: number) => { + const bound = (offset: number, size: number) => { // Keep the fitted footprint covered, including its letterboxing. Clamping to // the stage edges instead would move the anchor during small pinch increments. - const limit = Math.max(0, (image * fit() * (zoom - 1)) / 2) + const limit = Math.max(0, (size * fit() * (view.zoom - 1)) / 2) return Math.max(-limit, Math.min(limit, offset)) } - const x = () => bound(view.x, view.imageWidth, view.zoom) - const y = () => bound(view.y, view.imageHeight, view.zoom) - const reset = () => setView({ zoom: 1, x: 0, y: 0 }) - - const pan = (left: number, top: number) => - setView({ x: bound(left, view.imageWidth, view.zoom), y: bound(top, view.imageHeight, view.zoom) }) - - let stage: HTMLDivElement | undefined - let drag: { id: number; clientX: number; clientY: number; x: number; y: number } | undefined - let dragged = false - - const cursor = () => { - if (view.zoom <= 1) return undefined - - return view.dragging ? "grabbing" : "grab" + const paint = () => { + if (image) image.style.transform = `translate(-50%, -50%) translate(${panX}px, ${panY}px) scale(${view.zoom})` } - const endDrag = (event: PointerEvent) => { - if (event.pointerId !== drag?.id) return - - drag = undefined - setView("dragging", false) + const pan = (left: number, top: number) => { + panX = bound(left, view.imageWidth) + panY = bound(top, view.imageHeight) + paint() } + const dispatchGesture = (event: ImageStageGestureEvent) => { + const prev = untrack(gesture) + const next = transitionImageStageGesture(prev, event) + + if (next.state !== prev) setGesture(next.state) + next.effects.forEach((effect) => { + if (effect.type === "pointer.capture") return image?.setPointerCapture(effect.pointerId) + + if (image?.hasPointerCapture(effect.pointerId)) image.releasePointerCapture(effect.pointerId) + }) + } + + const setZoom = (zoom: number, left: number, top: number) => { + setView("zoom", zoom) + pan(left, top) + dispatchGesture({ type: "zoom.changed", zoomed: zoom > 1 }) + } + + const reset = () => setZoom(1, 0, 0) + const zoomAt = (zoom: number, clientX: number, clientY: number) => { if (!stage) return @@ -343,11 +355,7 @@ function ImageStage(props: MediaProps & { src: string }) { const next = Math.max(minZoom(), Math.min(maxZoom(), zoom)) const ratio = next / view.zoom const anchor = { x: clientX - rect.left - rect.width / 2, y: clientY - rect.top - rect.height / 2 } - setView({ - zoom: next, - x: bound(anchor.x - (anchor.x - x()) * ratio, view.imageWidth, next), - y: bound(anchor.y - (anchor.y - y()) * ratio, view.imageHeight, next), - }) + setZoom(next, anchor.x - (anchor.x - panX) * ratio, anchor.y - (anchor.y - panY) * ratio) } const zoomBy = (factor: number) => { @@ -357,16 +365,20 @@ function ImageStage(props: MediaProps & { src: string }) { zoomAt(view.zoom * factor, rect.left + rect.width / 2, rect.top + rect.height / 2) } + const cancel = (event: PointerEvent) => dispatchGesture({ type: "pointer.cancel", pointerId: event.pointerId }) + createResizeObserver( () => stage, () => { if (!stage) return setView({ width: stage.clientWidth, height: stage.clientHeight }) - setView({ x: x(), y: y() }) + pan(panX, panY) }, ) + onCleanup(() => dispatchGesture({ type: "disposed" })) + return (
{ + image = element + paint() + }} data-slot="artifact-media" src={props.src} alt={getFilename(props.path)} @@ -404,40 +420,54 @@ function ImageStage(props: MediaProps & { src: string }) { height: view.imageHeight ? `${view.imageHeight * fit()}px` : undefined, "max-width": "none", "max-height": "none", - transform: `translate(-50%, -50%) translate(${x()}px, ${y()}px) scale(${view.zoom})`, "touch-action": view.zoom > 1 ? "none" : "auto", - cursor: cursor(), + cursor: selectStageCursor(gesture(), view.zoom > 1), }} onPointerDown={(event) => { - if (!event.isPrimary || event.button !== 0) return + dispatchGesture({ + type: "pointer.down", + pointerId: event.pointerId, + button: event.button, + primary: event.isPrimary, + zoomed: view.zoom > 1, + }) - dragged = false - - if (view.zoom <= 1) return - - drag = { id: event.pointerId, clientX: event.clientX, clientY: event.clientY, x: x(), y: y() } - event.currentTarget.setPointerCapture(event.pointerId) - setView("dragging", true) + if (!selectStagePanPointer(gesture(), event.pointerId)) return + origin.x = event.clientX + origin.y = event.clientY }} onPointerMove={(event) => { - if (!drag || drag.id !== event.pointerId) return + const current = gesture() - const dx = event.clientX - drag.clientX - const dy = event.clientY - drag.clientY + if (!selectStagePanPointer(current, event.pointerId)) return - if (!dragged && Math.hypot(dx, dy) < 3) return + // The button came up where this element could not hear it. + if (event.pointerType === "mouse" && (event.buttons & 1) === 0) return cancel(event) + const dx = event.clientX - origin.x + const dy = event.clientY - origin.y - dragged = true - pan(drag.x + dx, drag.y + dy) + if (dx === 0 && dy === 0) return + + // Only the move that crosses the threshold changes the gesture; the rest dispatch and allocate nothing. + if (!selectStagePanMoved(current)) { + if (Math.hypot(dx, dy) < STAGE_DRAG_THRESHOLD_PX) return + + dispatchGesture({ type: "pointer.moved", pointerId: event.pointerId }) + } + + // Each move pans by its own delta, so reversing at a clamp wall moves the image at once. + origin.x = event.clientX + origin.y = event.clientY + pan(panX + dx, panY + dy) }} - onPointerUp={endDrag} - onPointerCancel={endDrag} - onLostPointerCapture={endDrag} - onError={() => props.onError()} + onPointerUp={(event) => dispatchGesture({ type: "pointer.up", pointerId: event.pointerId })} + onPointerCancel={cancel} + onLostPointerCapture={cancel} + onError={() => props.onError(props.content)} onLoad={(event) => { - const image = event.currentTarget - const width = image.naturalWidth || image.width || image.clientWidth || 300 - const height = image.naturalHeight || image.height || image.clientHeight || 150 + const element = event.currentTarget + const width = element.naturalWidth || element.width || element.clientWidth || 300 + const height = element.naturalHeight || element.height || element.clientHeight || 150 setView({ imageWidth: width, @@ -445,14 +475,15 @@ function ImageStage(props: MediaProps & { src: string }) { width: stage?.clientWidth ?? 0, height: stage?.clientHeight ?? 0, }) - props.onInfo({ width, height }) + pan(panX, panY) + props.onInfo(props.content, { width, height }) }} onClick={(event) => { - if (dragged) { - dragged = false + // The click that ends a moved pan neither fits nor zooms. + const suppressed = selectStageClickSuppressed(gesture()) + dispatchGesture({ type: "click" }) - return - } + if (suppressed) return if (view.zoom !== 1) return reset() @@ -475,44 +506,90 @@ function ImageStage(props: MediaProps & { src: string }) { } function ArtifactVideo(props: MediaProps) { + const source = createMediaSource(() => props.content) + + return ( + + {(source) => } + + ) +} + +function VideoStage(props: MediaProps & { src: string }) { const ctx = usePlugin() const dialog = useDialog() - const url = createMediaUrl(() => props.content) - const [loadedUrl, setLoadedUrl] = createSignal() - const [paused, setPaused] = createSignal(true) - const [active, setActive] = createSignal(false) - const [open, setOpen] = createSignal(false) + const [overlay, setOverlay] = createSignal(createVideoOverlayState()) const [box, setBox] = createStore({ left: 0, top: 0, ready: false }) let stage: HTMLDivElement | undefined + let frame: HTMLDivElement | undefined let video: HTMLVideoElement | undefined let idleTimer: ReturnType | undefined + let popoverFrame: number | undefined + let preview: { attempt: number; id: string; observer: MutationObserver } | undefined - const clearIdle = () => { - if (idleTimer !== undefined) clearTimeout(idleTimer) - idleTimer = undefined + const dispatch = (event: VideoOverlayEvent) => { + const prev = untrack(overlay) + const next = transitionVideoOverlay(prev, event) + + if (next.state !== prev) setOverlay(next.state) + next.effects.forEach((effect) => { + if (effect.type === "idle.schedule") { + clearTimeout(idleTimer) + idleTimer = setTimeout(() => dispatch({ type: "idle.elapsed", attempt: effect.attempt }), effect.delayMs) + + return + } + + if (effect.type === "idle.clear") { + clearTimeout(idleTimer) + idleTimer = undefined + + return + } + + if (effect.type === "preview.open") return open(effect.attempt) + + if (preview?.attempt !== effect.attempt) return + preview.observer.disconnect() + + if (effect.type === "preview.close") dialog.close(preview.id) + preview = undefined + }) } - const wake = () => { - setActive(true) - clearIdle() - idleTimer = setTimeout(() => setActive(false), 2500) - } - - onCleanup(clearIdle) - // The preview fills the window and borrows this