AI media harness for planning, generating, and editing locally
TypeScript
Add shot video popout playback
Bitti3cb42a1e89b6
diff --git a/src/components/workspace/GeneratorView.tsx b/src/components/workspace/GeneratorView.tsxindex 2f8a9a4..fe0d3c8 100644--- a/src/components/workspace/GeneratorView.tsx+++ b/src/components/workspace/GeneratorView.tsx@@ -538,7 +538,7 @@ export function GeneratorView({ config, folderPath, runtime = null, generationCo/></CommandBar>- <div className="generator-board">+ <div className="generator-board-viewport"><div className="generator-board">{jobs.length > 0? <SceneBoardassets={config.assets}@@ -583,7 +583,7 @@ export function GeneratorView({ config, folderPath, runtime = null, generationCodescription="Add a scene, then write what happens in each of its shots."action={<button type="button" className="secondary-button" onClick={newScene}>Add a scene</button>}/>}- </div>+ </div></div><ProjectStatus label="Generator status"><span role="status">{boardSummary(active.length, queued.length, jobs.length)}</span>diff --git a/src/components/workspace/ShotPopout.tsx b/src/components/workspace/ShotPopout.tsxnew file mode 100644index 0000000..7fb2dd4--- /dev/null+++ b/src/components/workspace/ShotPopout.tsx@@ -0,0 +1,96 @@+import { useEffect, useId, useRef, useState, type CSSProperties } from "react";+import { readMediaFileUrl } from "../../lib/persistence";+import { Dismiss16, Pause20, Play20Filled } from "../ui/icons";++export const SHOT_PLAY_EVENT = "slopus:play-generated-shot";++/** A persistent shot player: reaching the cut leaves the frame open for replay. */+export function ShotPopout({ folderPath, relativePath, start, end, poster, title, onClose }: {+ folderPath: string; relativePath: string; start: number; end: number; poster: string; title: string; onClose: () => void;+}) {+ const id = useId();+ const frame = useRef<HTMLDivElement>(null);+ const video = useRef<HTMLVideoElement>(null);+ const close = useRef(onClose); close.current = onClose;+ const active = useRef(false);+ const epoch = useRef(0);+ const timer = useRef<number>();+ const finished = useRef(false);+ const [url, setUrl] = useState<string | null>(null);+ const [playing, setPlaying] = useState(false);+ const [error, setError] = useState<string | null>(null);+ const [aspect, setAspect] = useState(16 / 9);+ const clearTimer = () => { window.clearTimeout(timer.current); timer.current = undefined; };+ const pause = (ended = false) => {+ epoch.current++; clearTimer(); video.current?.pause(); finished.current = ended; setPlaying(false);+ };+ const play = async () => {+ const media = video.current;+ if (!media || !url || !active.current) return;+ const cut = Math.min(end, Number.isFinite(media.duration) ? media.duration : end);+ if (start >= cut) { setError("This shot is outside the available video."); return; }+ if (finished.current || media.currentTime < start || media.currentTime >= cut - 0.02) media.currentTime = start;+ finished.current = false; setError(null); clearTimer();+ const request = ++epoch.current;+ try {+ await media.play();+ if (!active.current || request !== epoch.current) return;+ setPlaying(true);+ timer.current = window.setTimeout(() => pause(true), Math.max(0, (cut - media.currentTime) * 1000 / (media.playbackRate || 1)));+ } catch {+ if (active.current && request === epoch.current) { setPlaying(false); setError("Could not play this shot. Try replaying it."); }+ }+ };+ useEffect(() => {+ active.current = true;+ let owned: string | null = null;+ let disposed = false;+ const focused = document.activeElement as HTMLElement | null;+ const board = frame.current?.closest(".generator-board-viewport")?.querySelector<HTMLElement>(".generator-board");+ const wasInert = board?.hasAttribute("inert");+ board?.setAttribute("inert", "");+ frame.current?.focus();+ window.dispatchEvent(new CustomEvent<string>(SHOT_PLAY_EVENT, { detail: id }));+ const outside = (event: PointerEvent) => { if (!frame.current?.contains(event.target as Node)) close.current(); };+ const escape = (event: KeyboardEvent) => { if (event.key === "Escape") { event.preventDefault(); event.stopPropagation(); close.current(); } };+ const other = (event: Event) => { if ((event as CustomEvent<string>).detail !== id) close.current(); };+ document.addEventListener("pointerdown", outside, true);+ document.addEventListener("keydown", escape, true);+ window.addEventListener(SHOT_PLAY_EVENT, other);+ void readMediaFileUrl(folderPath, { relativePath }, "video/mp4").then(value => {+ if (disposed) { if (value) URL.revokeObjectURL(value); return; }+ owned = value; setUrl(value);+ if (!value) setError("This shot's video file is unavailable.");+ }).catch(() => { if (!disposed) setError("Could not open this shot's video file."); });+ const media = video.current;+ return () => {+ disposed = true; active.current = false; epoch.current++; clearTimer();+ media?.pause();+ document.removeEventListener("pointerdown", outside, true);+ document.removeEventListener("keydown", escape, true);+ window.removeEventListener(SHOT_PLAY_EVENT, other);+ if (board && !wasInert) board.removeAttribute("inert");+ if (owned) URL.revokeObjectURL(owned);+ if (focused?.isConnected) focused.focus();+ };+ }, [folderPath, relativePath, id]);+ return <div className="shot-popout" onClick={event => { event.stopPropagation(); if (event.target === event.currentTarget) onClose(); }}>+ <div ref={frame} className="shot-popout__frame" role="dialog" aria-label={`${title} preview`} tabIndex={-1}+ style={{ "--shot-aspect": aspect } as CSSProperties}>+ <video ref={video} src={url ?? undefined} poster={poster} playsInline+ onLoadedMetadata={event => {+ if (!url) return;+ const media = event.currentTarget;+ if (media.videoWidth > 0 && media.videoHeight > 0) setAspect(media.videoWidth / media.videoHeight);+ media.currentTime = start;+ void play();+ }}+ onTimeUpdate={event => { if (event.currentTarget.currentTime >= end - 0.02) pause(true); }}+ onEnded={() => pause(true)} onError={() => { if (url) { pause(); setError("This shot's video could not be played."); } }} />+ <button type="button" className="shot-popout__close" aria-label="Close shot preview" onClick={onClose}><Dismiss16 /></button>+ <button type="button" className="shot-popout__play" disabled={!url} aria-label={`${playing ? "Pause" : "Replay"} ${title}`}+ onClick={() => { if (playing) pause(); else void play(); }}>{playing ? <Pause20 /> : <Play20Filled />}</button>+ {error ? <p className="shot-popout__message" role="alert">{error}</p> : !url && <p className="shot-popout__message" role="status">Opening video…</p>}+ </div>+ </div>;+}diff --git a/src/components/workspace/ShotThumbnail.test.tsx b/src/components/workspace/ShotThumbnail.test.tsxindex 0df7479..e14575d 100644--- a/src/components/workspace/ShotThumbnail.test.tsx+++ b/src/components/workspace/ShotThumbnail.test.tsx@@ -1,6 +1,6 @@// @vitest-environment jsdom-import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";+import { cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react";import { afterEach, describe, expect, it, vi } from "vitest";import { createProjectConfig, parseProjectConfig, sceneShots, type GenerationJob } from "../../lib/project";import { ShotThumbnail } from "./ShotThumbnail";@@ -100,6 +100,80 @@ const rendered = (folder: string): GenerationJob => {};describe("a shot's picture", () => {+ it("pops a shot into the board, respects its source offset and cut, and replays without reopening the file", async () => {+ const folder = "C:\\Popout shot replay", job = rendered(folder);+ await withFakeFile({ seconds: 20 }, async seen => {+ const { container } = render(<div className="generator-board-viewport"><div className="generator-board">+ <ShotThumbnail folderPath={folder} job={job} seconds={1} endSeconds={2} sourceOffsetSeconds={5} shotNumber={1} />+ </div></div>);+ const button = await screen.findByRole("button", { name: "Pop out shot 1 of First scene" });+ button.focus(); fireEvent.click(button);+ const dialog = await screen.findByRole("dialog", { name: "shot 1 of First scene preview" });+ expect(dialog.closest(".generator-board-viewport")).toBe(container.firstChild);+ expect(container.querySelector(".generator-board")?.hasAttribute("inert")).toBe(true);+ await waitFor(() => expect(within(dialog).getByRole("button", { name: "Pause shot 1 of First scene" })).not.toBeNull());+ const video = dialog.querySelector("video")!;+ expect(seen.seeks).toContain(6);+ fireEvent.pointerDown(video); fireEvent.click(video);+ expect(screen.getByRole("dialog")).toBe(dialog);+ video.currentTime = 7; fireEvent.timeUpdate(video);+ const reads = seen.reads();+ fireEvent.click(within(dialog).getByRole("button", { name: "Replay shot 1 of First scene" }));+ await waitFor(() => expect(within(dialog).getByRole("button", { name: "Pause shot 1 of First scene" })).not.toBeNull());+ expect(video.currentTime).toBe(6);+ expect(seen.reads()).toBe(reads);+ fireEvent.click(dialog.parentElement!);+ expect(screen.queryByRole("dialog")).toBeNull();+ expect(container.querySelector(".generator-board")?.hasAttribute("inert")).toBe(false);+ expect(document.activeElement).toBe(button);+ });+ });++ it("stops inline playback when popping out and dismisses with Escape or an outside pointer press", async () => {+ const folder = "C:\\Popout dismissal", job = rendered(folder);+ await withFakeFile({ seconds: 9 }, async () => {+ render(<div className="generator-board-viewport"><div className="generator-board">+ <ShotThumbnail folderPath={folder} job={job} seconds={0} endSeconds={4.5} shotNumber={1} />+ </div></div>);+ fireEvent.click(await screen.findByRole("button", { name: "Play shot 1 of First scene" }));+ await waitFor(() => expect(screen.getByRole("button", { name: "Pause shot 1 of First scene" })).not.toBeNull());+ const popout = screen.getByRole("button", { name: "Pop out shot 1 of First scene" });+ fireEvent.click(popout);+ await screen.findByRole("dialog");+ expect(document.querySelector(".shot-thumb video")).toBeNull();+ fireEvent.keyDown(document, { key: "Escape" });+ expect(screen.queryByRole("dialog")).toBeNull();+ fireEvent.click(popout); await screen.findByRole("dialog");+ fireEvent.pointerDown(document.body);+ expect(screen.queryByRole("dialog")).toBeNull();+ });+ });++ it("reports popout file errors and releases a file that finishes loading after dismissal", async () => {+ const folder = "C:\\Popout errors", job = rendered(folder);+ const { invoke } = await import("@tauri-apps/api/core");+ await withFakeFile({ seconds: 9 }, async () => {+ render(<div className="generator-board-viewport"><div className="generator-board">+ <ShotThumbnail folderPath={folder} job={job} seconds={0} shotNumber={1} />+ </div></div>);+ const popout = await screen.findByRole("button", { name: "Pop out shot 1 of First scene" });+ vi.mocked(invoke).mockRejectedValueOnce(new Error("Missing file"));+ fireEvent.click(popout);+ expect((await screen.findByRole("alert")).textContent).toContain("Could not open");+ fireEvent.click(screen.getByRole("button", { name: "Close shot preview" }));+ let finish!: (value: ArrayBuffer) => void;+ vi.mocked(invoke).mockImplementationOnce(() => new Promise(resolve => { finish = resolve; }));+ const revoke = vi.spyOn(URL, "revokeObjectURL");+ fireEvent.click(popout);+ await waitFor(() => expect(finish).toBeDefined());+ fireEvent.keyDown(document, { key: "Escape" });+ finish(new ArrayBuffer(8));+ await waitFor(() => expect(revoke).toHaveBeenCalledWith("blob:slopus-test"));+ expect(screen.queryByRole("dialog")).toBeNull();+ revoke.mockRestore();+ });+ });+it("previews the new scene inside its joined file and stops at the offset cut", async () => {const folder = "C:\\Joined scene playback";const job = rendered(folder);diff --git a/src/components/workspace/ShotThumbnail.tsx b/src/components/workspace/ShotThumbnail.tsxindex f0ab707..f1f1ab1 100644--- a/src/components/workspace/ShotThumbnail.tsx+++ b/src/components/workspace/ShotThumbnail.tsx@@ -1,5 +1,7 @@-import { Clock18, Film18, Pause20, Play20Filled, Prohibited18, Spinner18, Spinner20, Warning18, Warning20 } from "../ui/icons";+import { Clock18, Film18, OpenExternal16, Pause20, Play20Filled, Prohibited18, Spinner18, Spinner20, Warning18, Warning20 } from "../ui/icons";import { useEffect, useRef, useState, type ReactNode } from "react";+import { createPortal } from "react-dom";+import { ShotPopout, SHOT_PLAY_EVENT } from "./ShotPopout";import { readMediaFileUrl } from "../../lib/persistence";import { sceneDurationSeconds, type GenerationJob } from "../../lib/project";@@ -33,7 +35,7 @@ const SEEK_LIMIT_MS = 4_000;* its own startSeconds. *//** Small JPEGs, but not free. Bounded like MediaThumbnail's own cache. */const CACHE_LIMIT = 96;-const PLAY_EVENT = "slopus:play-generated-shot";+const PLAY_EVENT = SHOT_PLAY_EVENT;/** Drawn frames, keyed by file and time. */const POSTERS = new Map<string, string>();@@ -288,6 +290,8 @@ export function ShotThumbnail({ folderPath, job, seconds, endSeconds = sceneDuraconst [loadingPlayback, setLoadingPlayback] = useState(false);const [playing, setPlaying] = useState(false);const [playFailed, setPlayFailed] = useState(false);+ const [popoutHost, setPopoutHost] = useState<HTMLElement | null>(null);+ const popoutButton = useRef<HTMLButtonElement>(null);const [estimateNow, setEstimateNow] = useState(() => Date.now());/* Bumped when a poster's shape is first measured, to re-render with it. */const [, setShapeSeen] = useState(0);@@ -309,8 +313,10 @@ export function ShotThumbnail({ folderPath, job, seconds, endSeconds = sceneDuraconst playToCut = async (video: HTMLVideoElement) => {clearCutTimer();+ const request = requestRef.current;try {await video.play();+ if (request !== requestRef.current || videoRef.current !== video) return;setPlaying(true);const rate = video.playbackRate > 0 ? video.playbackRate : 1;cutTimerRef.current = window.setTimeout(finishPlayback, Math.max(0, (endAt - video.currentTime) * 1_000 / rate));@@ -319,6 +325,10 @@ export function ShotThumbnail({ folderPath, job, seconds, endSeconds = sceneDura}};+ useEffect(() => {+ setPopoutHost(null);+ }, [folderPath, relativePath, at, endAt]);+useEffect(() => {if (!relativePath) {setResult(NOTHING);@@ -403,7 +413,7 @@ export function ShotThumbnail({ folderPath, job, seconds, endSeconds = sceneDuraconst poster = result.poster;const shape = POSTER_SHAPES.get(poster);const action = playFailed ? "Playback unavailable" : loadingPlayback ? "Opening shot" : playing ? "Pause" : "Play";- return <button+ return <div className="shot-thumb-frame" style={shape ? { aspectRatio: shape } : undefined}><buttontype="button"className="shot-thumb shot-thumb--poster shot-thumb--playable"/* The tile takes the rendered frame's own shape, not a fixed 16:9. */@@ -433,7 +443,16 @@ export function ShotThumbnail({ folderPath, job, seconds, endSeconds = sceneDura<span className={`shot-thumb__play ${loadingPlayback ? "shot-thumb__play--loading" : ""}`} aria-hidden="true">{loadingPlayback ? <Spinner20 className="spin" /> : playing ? <Pause20 /> : playFailed ? <Warning20 /> : <Play20Filled />}</span>- </button>;+ </button>+ <button ref={popoutButton} type="button" className="shot-thumb__popout" aria-label={`Pop out shot ${shotNumber} of ${job.title}`}+ data-tooltip="Pop out shot" onClick={event => {+ event.stopPropagation();+ const host = event.currentTarget.closest<HTMLElement>(".generator-board-viewport");+ if (host) { onPlay?.(); setPopoutHost(host); }+ }}><OpenExternal16 /></button>+ {popoutHost && relativePath && createPortal(<ShotPopout folderPath={folderPath} relativePath={relativePath} start={at} end={endAt}+ poster={poster} title={`shot ${shotNumber} of ${job.title}`} onClose={() => { setPopoutHost(null); popoutButton.current?.focus(); }} />, popoutHost)}+ </div>;}const { icon, word } = placeholder(job, result.failed, cancelling);const estimateLine = job.status === "generating" && !cancellingdiff --git a/src/styles/generator.css b/src/styles/generator.cssindex 235f005..118e616 100644--- a/src/styles/generator.css+++ b/src/styles/generator.css@@ -72,6 +72,17 @@/* The board is where the shots are laid out, so it sits in the well; thescene headers across it are chrome, edge to edge. */+.generator-board-viewport {+ position: relative;+ flex: 1 1 auto;+ min-height: 0;+ min-width: 0;+ display: flex;+ flex-direction: column;+ overflow: hidden;+ isolation: isolate;+}+.generator-board {flex: 1 1 auto;min-height: 0;@@ -562,6 +573,75 @@background-color: var(--panel-3);}+.shot-thumb-frame {+ position: relative;+ width: 100%;+ aspect-ratio: 16 / 9;+}++.shot-thumb-frame > .shot-thumb { height: 100%; }++.shot-thumb__popout {+ position: absolute;+ top: 50%;+ left: calc(50% + var(--icon-button) / 2 + var(--space-1));+ transform: translateY(-50%);+ width: var(--icon-button);+ height: var(--icon-button);+ display: grid;+ place-items: center;+ padding: 0;+ border: 0;+ border-radius: var(--radius-pill);+ background: var(--scrim);+ color: var(--on-accent);+ opacity: 0;+ transition: opacity var(--motion-fast) linear;+}++.shot-thumb-frame:hover .shot-thumb__popout,+.shot-thumb-frame:focus-within .shot-thumb__popout,+.shot-thumb-frame:focus-within .shot-thumb__play { opacity: 1; }++.shot-popout {+ position: absolute;+ inset: 0;+ z-index: 10;+ display: grid;+ place-items: center;+ padding: var(--space-5);+ container-type: size;+ background: color-mix(in srgb, var(--surface-chrome) 75%, transparent);+ backdrop-filter: grayscale(1) brightness(.55);+}++.shot-popout__frame {+ position: relative;+ width: min(100cqw, calc(100cqh * var(--shot-aspect)));+ max-width: 100%;+ aspect-ratio: var(--shot-aspect);+ overflow: hidden;+ border-radius: var(--radius-lg);+ background: var(--stage-bg);+}++.shot-popout__frame video { display: block; width: 100%; height: 100%; object-fit: contain; }+.shot-popout__close, .shot-popout__play {+ position: absolute;+ display: grid;+ place-items: center;+ width: var(--control-md);+ height: var(--control-md);+ padding: 0;+ border: 0;+ border-radius: var(--radius-pill);+ color: var(--on-accent);+ background: var(--scrim);+}+.shot-popout__close { top: var(--space-2); right: var(--space-2); }+.shot-popout__play { left: 50%; bottom: var(--space-3); transform: translateX(-50%); }+.shot-popout__message { position: absolute; inset: var(--space-3) var(--space-5) auto; margin: 0; padding: var(--space-2); text-align: center; color: var(--on-accent); background: var(--scrim); }+.shot-thumb img {width: 100%;height: 100%;