Skip to content

The open source AI video agent: turns any coding agent into a video agent, with an editor to change anything it makes.

Studio: a ruler click moves the playhead; ⌘-click (Ctrl-click) refers it to the chat (#195)

riverisbuilding42c46e6ed5cd
diff --git a/.changeset/ruler-click-playhead.md b/.changeset/ruler-click-playhead.md
new file mode 100644
index 0000000..8e89b2e
--- /dev/null
+++ b/.changeset/ruler-click-playhead.md
@@ -0,0 +1,6 @@
+---
+"openfilm": patch
+"@openfilm/desktop": patch
+---
+
+Desktop: a click on the timeline's ruler moves the playhead and a drag scrubs, as in other editors. To refer a moment to the chat, ⌘-click the ruler (Ctrl-click on Windows), the key that draws a box on the picture; ⌘-drag refers a span. While the key is held the cursor turns into a crosshair and the moment under it is drawn.
diff --git a/packages/openfilm/studio/ruler-refer.test.mjs b/packages/openfilm/studio/ruler-refer.test.mjs
new file mode 100644
index 0000000..a92df0c
--- /dev/null
+++ b/packages/openfilm/studio/ruler-refer.test.mjs
@@ -0,0 +1,94 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { chromium } from 'playwright-core';
+import { filmHtml } from '../src/film-doc.mjs';
+import { openProject } from './server/projects.mjs';
+import { startStudio } from './server/server.mjs';
+
+test('with the app\'s chat, a ruler click moves the playhead and a ⌘-click (Ctrl-click) refers the moment', { timeout: 60_000 }, async () => {
+ const scratch = await mkdtemp(join(tmpdir(), 'of-ruler-refer-'));
+ const names = ['HOME', 'SHARED_HOME', 'LIBRARY', 'SAVED', 'EXPORTS'];
+ const saved = new Map(names.map((name) => [`OPENFILM_${name}`, process.env[`OPENFILM_${name}`]]));
+ for (const name of names) process.env[`OPENFILM_${name}`] = join(scratch, name.toLowerCase());
+ let studio, browser;
+ try {
+ const root = join(process.env.OPENFILM_LIBRARY, 'Ruler');
+ await mkdir(join(root, 'assets'), { recursive: true });
+ await writeFile(join(root, 'assets', 'card.svg'), '<svg xmlns="http://www.w3.org/2000/svg" width="320" height="180"><rect width="320" height="180" fill="#486b8e"/></svg>');
+ await writeFile(join(root, 'film.html'), filmHtml({
+ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ id: 'card', src: 'assets/card.svg', time: [0, 4] }] }],
+ }));
+ const project = await openProject(root);
+ studio = await startStudio({ port: 0, openBrowser: () => {} });
+ browser = await chromium.launch({ headless: true, args: ['--use-angle=swiftshader'] });
+ const page = await browser.newPage({ viewport: { width: 1280, height: 800 }, locale: 'en-US', reducedMotion: 'reduce' });
+ /* the app's chat, as far as the ruler sees it: what is referred to it */
+ await page.addInitScript(() => {
+ window.referred = [];
+ window.openfilmHost = { version: 1, refer: (ref) => window.referred.push(ref) };
+ });
+ await page.goto(`${studio.origin}/projects/${project.id}?key=${studio.key}`);
+ await page.locator('[data-block-id]').first().waitFor({ state: 'visible' });
+ const slider = page.locator('[data-timeline] [role="slider"]').first();
+ const ruler = await slider.boundingBox();
+ assert.ok(ruler);
+ const y = ruler.y + ruler.height / 2;
+ const playhead = async () => Number(await slider.getAttribute('aria-valuenow'));
+ const referred = () => page.evaluate(() => window.referred);
+ /* the key a box is drawn with on the picture: ⌘ on a Mac, Ctrl elsewhere */
+ const key = process.platform === 'darwin' ? 'Meta' : 'Control';
+ assert.equal(await playhead(), 0);
+
+ /* a plain click: the playhead goes there, nothing is referred. Just after the page opens, the preview may not take
+ a seek yet (the playhead is where the preview is), so the click is made again until it does */
+ for (let tries = 0; tries < 20 && await playhead() === 0; tries++) {
+ await page.mouse.click(ruler.x + 200, y);
+ await page.waitForTimeout(250);
+ }
+ const clicked = await playhead();
+ assert.ok(clicked > 0);
+ assert.deepEqual(await referred(), []);
+
+ /* a plain drag scrubs */
+ await page.mouse.move(ruler.x + 100, y);
+ await page.mouse.down();
+ await page.mouse.move(ruler.x + 300, y, { steps: 5 });
+ await page.mouse.up();
+ await page.waitForFunction((before) => Number(document.querySelector('[data-timeline] [role="slider"]')?.getAttribute('aria-valuenow')) > before, clicked);
+ const dragged = await playhead();
+ assert.deepEqual(await referred(), []);
+
+ /* the moment a click would refer is drawn only while the key is held, with a crosshair */
+ await page.mouse.move(ruler.x + 60, y);
+ assert.equal(await slider.evaluate((el) => el.style.cursor), 'col-resize');
+ await page.keyboard.down(key);
+ await page.waitForFunction(() => document.querySelector('[data-timeline] [role="slider"]')?.style.cursor === 'crosshair');
+ await page.keyboard.up(key);
+ await page.waitForFunction(() => document.querySelector('[data-timeline] [role="slider"]')?.style.cursor === 'col-resize');
+
+ /* a ⌘-click refers that moment, a ⌘-drag that span; the playhead stays */
+ await page.keyboard.down(key);
+ await page.mouse.click(ruler.x + 60, y);
+ await page.mouse.move(ruler.x + 40, y);
+ await page.mouse.down();
+ await page.mouse.move(ruler.x + 120, y, { steps: 5 });
+ await page.mouse.up();
+ await page.keyboard.up(key);
+ const refs = await referred();
+ assert.deepEqual(refs.map((r) => r.kind), ['time', 'range']);
+ assert.ok(refs[0].time > 0 && refs[0].time * 1000 < clicked);
+ assert.ok(refs[1].start < refs[1].end);
+ assert.equal(await playhead(), dragged);
+ } finally {
+ await browser?.close();
+ await studio?.close();
+ for (const [name, value] of saved) {
+ if (value === undefined) delete process.env[name];
+ else process.env[name] = value;
+ }
+ await rm(scratch, { recursive: true, force: true });
+ }
+});
diff --git a/packages/openfilm/studio/ui/src/components/ProjectView.tsx b/packages/openfilm/studio/ui/src/components/ProjectView.tsx
index 9886457..5c5f8ac 100644
--- a/packages/openfilm/studio/ui/src/components/ProjectView.tsx
+++ b/packages/openfilm/studio/ui/src/components/ProjectView.tsx
@@ -1832,7 +1832,7 @@ export function ProjectView({ projectId, onProject, onLeave }: {
for (const b of what.blocks) refer(refOfBlock(b));
if (what.range) refer(rangeRef(projectId, what.range, blocksRef.current));
}, [projectId]); // eslint-disable-line react-hooks/exhaustive-deps
- /* the ruler points at time for the chat: a click, that moment; a drag, that span */
+ /* the ruler points at time for the chat: a ⌘-click, that moment; a ⌘-drag, that span */
const referRuler = React.useCallback((what: { atMs: number } | { range: FilmRange }) => {
const refer = studioHost?.refer;
if (!refer) return;
diff --git a/packages/openfilm/studio/ui/src/components/Timeline.tsx b/packages/openfilm/studio/ui/src/components/Timeline.tsx
index ea7e429..06f0f0a 100644
--- a/packages/openfilm/studio/ui/src/components/Timeline.tsx
+++ b/packages/openfilm/studio/ui/src/components/Timeline.tsx
@@ -192,6 +192,11 @@ const PLAYHEAD_BLEED = 8;
const RIPPLE_WAIT_MS = 1000;
/** Within this many pixels either side of the playhead line, a press in the track area grabs the playhead. */
const PLAYHEAD_GRAB_PX = 4;
+/* the key the ruler refers time with, the one a box is drawn with on the picture (FilmStageSelect): ⌘ on a Mac (its
+ Ctrl-click is a right click), Ctrl elsewhere */
+const MAC = typeof navigator !== 'undefined' && /Mac|iPhone|iPad/.test(navigator.platform);
+const REFER_KEY = MAC ? 'Meta' : 'Control';
+const referKeyHeld = (e: { metaKey: boolean; ctrlKey: boolean }) => (MAC ? e.metaKey : e.ctrlKey);
/* the range bar's hit height (the visible bar is thinner, see .tl-range-thumb; 5 px could hardly be hit) */
const H_SCROLL_H = 10;
const RULER_H = 20;
@@ -576,9 +581,9 @@ export interface TimelineProps {
*/
onReferSelection?: (what: { blocks: readonly TimelineBlock[]; range: FilmRange | null; atMs: number | null }) => void;
/**
- * With the app's chat: the ruler points at time for it. A click there is that moment, a drag that span, each one
- * a pill in the chat; the playhead does not go there (it is dragged by its knob, or a click in the tracks' empty
- * space). Without it the ruler moves the playhead.
+ * With the app's chat: the ruler points at time for it with ⌘ (Ctrl on Windows) held, as a box is drawn on the
+ * picture. A ⌘-click there is that moment, a ⌘-drag that span, each one a pill in the chat. A plain click or drag
+ * moves the playhead, as without it.
*/
onReferRuler?: (what: { atMs: number } | { range: FilmRange }) => void;
/** The bar's Duplicate: copies of the clips right after them. */
@@ -1896,20 +1901,39 @@ export const Timeline = React.memo(function Timeline({
}, [playable, msAtClientX, laneAtY, contentY, tracks, selectOne, onScrubCommit, warmPlayheadAt, setSelected, withLinked, onEditOps, pxPerMs]);
/*
- * With the app's chat, the ruler points at time for it (onReferRuler): a click there is that moment, a drag that
- * span, drawn as it is pulled. A press on the playhead (its knob, ±4 px) still drags it.
+ * With the app's chat, the ruler points at time for it (onReferRuler) while ⌘ (Ctrl) is held: a ⌘-click there is
+ * that moment, a ⌘-drag that span, drawn as it is pulled. Without ⌘ it scrubs.
*/
const [referDraft, setReferDraft] = React.useState<FilmRange | null>(null);
- /* where the pointer is on the ruler (film ms), shown as the moment a click would refer; not near the playhead (its
- knob is dragged there) */
+ /* where the pointer is on the ruler (film ms) while ⌘ is held, shown as the moment a click would refer */
const [rulerHoverMs, setRulerHoverMs] = React.useState<number | null>(null);
+ /* the pointer's x over the ruler, so ⌘ pressed or let go there shows or hides that moment without a move */
+ const rulerPointerX = React.useRef<number | null>(null);
/* the ruler's own menu, open (rulerMenu) */
const [rulerMenuAt, setRulerMenuAt] = React.useState<{ x: number; y: number; items: ContextMenuEntry[] } | null>(null);
const hoverRuler = React.useCallback((e: React.PointerEvent) => {
if (!onReferRuler || !playable) return;
- /* on a marker's pin: the pin is what a click there is about */
- setRulerHoverMs(playheadGapPx(e.clientX, timeMsRef.current) > PLAYHEAD_GRAB_PX ? Math.max(0, Math.min(totalMs, msAtClientX(e.clientX))) : null);
- }, [onReferRuler, playable, playheadGapPx, totalMs, msAtClientX]);
+ rulerPointerX.current = e.clientX;
+ setRulerHoverMs(referKeyHeld(e) ? Math.max(0, Math.min(totalMs, msAtClientX(e.clientX))) : null);
+ }, [onReferRuler, playable, totalMs, msAtClientX]);
+ React.useEffect(() => {
+ if (!onReferRuler || !playable) return;
+ const key = (e: KeyboardEvent) => {
+ const x = rulerPointerX.current;
+ if (e.key !== REFER_KEY || x == null) return;
+ setRulerHoverMs(e.type === 'keydown' ? Math.max(0, Math.min(totalMs, msAtClientX(x))) : null);
+ };
+ /* ⌘ let go in another window (⌘-Tab) sends no keyup here */
+ const off = () => setRulerHoverMs(null);
+ window.addEventListener('keydown', key);
+ window.addEventListener('keyup', key);
+ window.addEventListener('blur', off);
+ return () => {
+ window.removeEventListener('keydown', key);
+ window.removeEventListener('keyup', key);
+ window.removeEventListener('blur', off);
+ };
+ }, [onReferRuler, playable, totalMs, msAtClientX]);
const startRefer = React.useCallback((e: React.PointerEvent) => {
if (e.button !== 0 || !playable || !onReferRuler) return;
e.preventDefault();
@@ -4074,11 +4098,11 @@ export const Timeline = React.memo(function Timeline({
aria-valuenow={Math.round(timeMs)}
aria-valuetext={`${formatTimecode(timeMs)} / ${formatTimecode(totalMs)}`}
onPointerDown={(e) => (e.shiftKey && onRangeChange ? startRange(e)
- : onReferRuler && playheadGapPx(e.clientX, timeMsRef.current) > PLAYHEAD_GRAB_PX ? startRefer(e)
+ : referKeyHeld(e) && onReferRuler ? startRefer(e)
: startScrub(e, true))}
onKeyDown={onKeyDown}
onPointerMove={hoverRuler}
- onPointerLeave={() => setRulerHoverMs(null)}
+ onPointerLeave={() => { rulerPointerX.current = null; setRulerHoverMs(null); }}
onContextMenu={rulerMenu ? (e) => {
e.preventDefault();
const items = rulerMenu(Math.max(0, Math.min(totalMs, msAtClientX(e.clientX))));
@@ -4087,8 +4111,8 @@ export const Timeline = React.memo(function Timeline({
/* the point picked on the ruler is let go with the timeline's focus */
onBlur={() => setBarFocus((f) => (f === 'point' ? null : f))}
className="relative h-full touch-none select-none outline-none"
- /* pointing at time for the chat: a hand, and the moment under it drawn (the playhead's knob keeps its own) */
- style={{ width: trackPx, cursor: onReferRuler ? (rulerHoverMs == null && !referDraft ? 'col-resize' : 'pointer') : 'col-resize' }}
+ /* pointing at time for the chat (⌘ held): a crosshair, as on the picture, and the moment under it drawn */
+ style={{ width: trackPx, cursor: onReferRuler ? (rulerHoverMs == null && !referDraft ? 'col-resize' : 'crosshair') : 'col-resize' }}
>
{tickMarks}
{previewBar?.length ? <PreviewBar ranges={previewBar} pxPerMs={pxPerMs} /> : null}
@@ -4881,7 +4905,7 @@ const chatClock = (ms: number): string => {
};
/**
- * What a click or a drag on the ruler gives the chat, drawn where it will be: under the pointer a line and a tag with
+ * What a ⌘-click or a ⌘-drag on the ruler gives the chat, drawn where it will be: under the pointer a line and a tag with
* the moment (the chat's mark and its time, as its pill will read), while dragged the span and its two ends. In the
* timeline's accent, as its switches; it takes no pointer.
*/