Skip to content

The VS Code of video editing, built for agents.

TypeScript

Refactor timeline layers and enhance drag-and-drop functionality

konstantin-paulusea608254a2a6
diff --git a/apps/web/index.html b/apps/web/index.html
index a0a0a095..1ca29bdd 100644
--- a/apps/web/index.html
+++ b/apps/web/index.html
@@ -52,19 +52,8 @@
}
#boot-splash svg {
- width: 72px;
- height: auto;
- animation: boot-splash-breathe 1.8s ease-in-out infinite;
- }
-
- @keyframes boot-splash-breathe {
- 0%,
- 100% {
- opacity: 0.3;
- }
- 50% {
- opacity: 0.8;
- }
+ width: 90px;
+ height: 90px;
}
</style>
<link href="/src/index.css" rel="stylesheet">
@@ -84,8 +73,8 @@
</head>
<body>
<div id="boot-splash" aria-hidden="true">
- <svg viewBox="0 0 331 353" fill="none" xmlns="http://www.w3.org/2000/svg">
- <path d="M118.548 12.4799C147.369 -4.15995 182.878 -4.15995 211.699 12.4799L283.672 54.0333C312.493 70.6731 330.248 101.425 330.248 134.705V217.812C330.248 251.091 312.493 281.843 283.672 298.483L211.699 340.036C182.878 356.676 147.369 356.676 118.548 340.036L46.5756 298.483C17.7545 281.843 0 251.091 0 217.812V134.705C2.67104e-05 101.425 17.7545 70.6731 46.5756 54.0333L118.548 12.4799ZM188.897 64.0464C174.486 55.7265 156.732 55.7265 142.321 64.0464L79.6548 100.227C65.2443 108.547 56.367 123.923 56.367 140.563V212.924C56.367 229.564 65.2442 244.94 79.6548 253.26L142.321 289.44C156.732 297.76 174.486 297.76 188.897 289.44L251.563 253.26C265.974 244.94 274.851 229.564 274.851 212.924V140.563C274.851 123.923 265.974 108.547 251.563 100.227L188.897 64.0464Z" fill="white"/>
+ <svg viewBox="-20.62 -6 422.3 422.3" fill="none" xmlns="http://www.w3.org/2000/svg">
+ <path d="M142.525 12.8615C172.228 -4.28717 208.823 -4.28717 238.525 12.8615L333.051 67.4357C362.753 84.5845 381.051 116.277 381.051 150.574V259.723C381.051 294.02 362.753 325.713 333.051 342.862L238.525 397.436C208.823 414.585 172.228 414.585 142.525 397.436L48 342.862C18.2975 325.713 0 294.02 0 259.723V150.574C2.76439e-05 116.277 18.2975 84.5845 48 67.4357L142.525 12.8615ZM214.025 68.0051C199.174 59.4308 180.877 59.4308 166.025 68.0051L83.6885 115.542C68.8373 124.117 59.6885 139.963 59.6885 157.112V252.186C59.6885 269.334 68.8373 285.181 83.6885 293.755L166.025 341.292C180.877 349.867 199.174 349.867 214.025 341.292L296.362 293.755C311.213 285.181 320.362 269.334 320.362 252.186V157.112C320.362 139.963 311.214 124.117 296.362 115.542L214.025 68.0051Z" fill="white"/>
</svg>
</div>
<div id="root"></div>
diff --git a/apps/web/src/components/timeline/layers/context.tsx b/apps/web/src/components/timeline/layers/context.tsx
index f0422419..83e017d7 100644
--- a/apps/web/src/components/timeline/layers/context.tsx
+++ b/apps/web/src/components/timeline/layers/context.tsx
@@ -4,26 +4,32 @@
import { createContext, createSignal, useContext, type JSX, type Signal } from "solid-js";
+import { assert } from "@/utils";
+
+import type { Entity } from "koota";
+
export type DropPosition = 'above' | 'below' | 'inside';
export type LayerDragState = {
- targetEid: number | null;
- position: DropPosition | null;
+ target: Entity;
+ position: DropPosition;
};
type LayerContextValue = {
- resizedId: Signal<number | null>;
+ /** The row whose height is being dragged, if any. */
+ resized: Signal<Entity | null>;
+ /** Where the rows being dragged would land, if they were dropped now. */
dragState: Signal<LayerDragState | null>;
}
const LayerContext = createContext<LayerContextValue>();
export function LayerContextProvider(props: { children: JSX.Element }) {
- const resizedId = createSignal<number | null>(null);
+ const resized = createSignal<Entity | null>(null);
const dragState = createSignal<LayerDragState | null>(null);
return (
- <LayerContext.Provider value={{ resizedId, dragState }}>
+ <LayerContext.Provider value={{ resized, dragState }}>
{props.children}
</LayerContext.Provider>
);
@@ -31,8 +37,6 @@ export function LayerContextProvider(props: { children: JSX.Element }) {
export function useLayerContext() {
const context = useContext(LayerContext);
- if (!context) {
- throw new Error('useLayerContext must be used within a LayerContextProvider');
- }
+ assert(context, 'useLayerContext must be used within a LayerContextProvider');
return context;
}
diff --git a/apps/web/src/components/timeline/layers/hover.ts b/apps/web/src/components/timeline/layers/hover.ts
new file mode 100644
index 00000000..b0ab76d6
--- /dev/null
+++ b/apps/web/src/components/timeline/layers/hover.ts
@@ -0,0 +1,24 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { Hovering } from '@diffusionstudio/runtime';
+
+import type { Entity, World } from 'koota';
+
+/**
+ * Points the hover at one row, or at none. Only ever one row is hovered, so
+ * this clears whatever held it rather than each row having to undo its own —
+ * the pointer can leave a row without a leave event ever arriving (a row that
+ * scrolls out from under it, or one that is removed).
+ *
+ * Hover is not authored: it says where the pointer is, which the file has
+ * nothing to say about, so it is written straight to the trait.
+ */
+export function setRowHover(world: World, entity: Entity | null): void {
+ for (const hovered of world.query(Hovering)) {
+ if (hovered !== entity) hovered.remove(Hovering);
+ }
+
+ if (entity?.isAlive() && !entity.has(Hovering)) entity.add(Hovering);
+}
diff --git a/apps/web/src/components/timeline/layers/keyframe.tsx b/apps/web/src/components/timeline/layers/keyframe.tsx
index 4f3897e5..97314903 100644
--- a/apps/web/src/components/timeline/layers/keyframe.tsx
+++ b/apps/web/src/components/timeline/layers/keyframe.tsx
@@ -3,97 +3,81 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { createMemo, Show } from 'solid-js';
+import { useTag, useTrait, useWorld } from '@diffusionstudio/koota-solid';
+import {
+ Cache,
+ Computed,
+ Hovering,
+ Keyframe as KeyframeTrait,
+ KeyframeTrack,
+ Selected,
+ findClosestParentGeometry,
+ getActiveEntity,
+ setPlayhead,
+} from '@diffusionstudio/runtime';
import { Button } from '@/components/ui/button';
import { Icon } from '@/components/ui/icon';
+import { Keyframe } from '@/components/ui/keyframe';
import { Tooltip, TooltipContent, TooltipPortal, TooltipTrigger } from '@/components/ui/tooltip';
-import { Keyframe } from '@/components/ui/keyframe-bitecs';
-import { useEngine } from '@/context/engine';
-import { useECS } from '@/context/ecs';
-import { KEYFRAME_TRACK_HEIGHT } from '@/components/engine/timeline/config';
+import { useDerived } from '@/engine/hooks';
+import { KEYFRAME_TRACK_HEIGHT } from '@/engine/timeline';
import { NESTED_INDENT_PX } from './config';
-
-import { addComponent, removeComponent, useEntityTag, getParentEntity, type PropertyPath, type TimelineNode } from '@/components/engine';
-import { findClosestParentGeometry } from '@/components/engine/api/utils';
-
-type KeyframeLayerProps = {
- depth: number;
- layer: TimelineNode;
- ancestorSelected: boolean;
-}
-
-export function KeyframeLayer(props: KeyframeLayerProps) {
- const { world } = useEngine();
- const { now } = useECS();
- const c = world.components;
-
- const eid = () => props.layer.eid;
-
- const property = createMemo(() => c.KeyframeTrack.property[eid()]);
- const targetEid = createMemo(() => getParentEntity(world, eid()));
- const clipEid = createMemo(() => findClosestParentGeometry(world, eid()));
- const hovering = useEntityTag(c.Hovering, eid);
- const selected = useEntityTag(c.Selected, eid);
-
- const hasPrev = createMemo(() => {
- const cid = clipEid();
- if (cid === null) return false;
- const delay = c.Computed.delay[cid];
- const localNowFrame = now() - delay;
- return (c.Cache.keyframes[props.layer.eid] ?? []).some(kf => c.Keyframe.time[kf] < localNowFrame);
+import { setRowHover } from './hover';
+
+import type { AnimatableProperty } from '@diffusionstudio/jsx';
+import type { Entity } from 'koota';
+import type { LayerRowProps } from './layer';
+
+/**
+ * A row for one animated property, with a way to step between its keyframes
+ * and to put one where the playhead is.
+ */
+export function KeyframeLayer(props: LayerRowProps) {
+ const world = useWorld();
+
+ const entity = () => props.layer.entity;
+
+ const track = useTrait(entity, KeyframeTrack);
+ const property = () => (track()?.property ?? '') as AnimatableProperty;
+ const target = () => track()?.target ?? null;
+
+ const hovering = useTag(entity, Hovering);
+ const selected = useTag(entity, Selected);
+
+ /**
+ * The track's keyframes as scene frames, in order. They are authored in the
+ * clip's own time — which is what makes them move with it, and run at its
+ * speed — so each is put back on the scene's the way `getNodeLocalFrame`
+ * takes them off it.
+ */
+ const frames = useDerived(
+ () => {
+ const clip = findClosestParentGeometry(entity());
+ const computed = clip?.get(Computed);
+ if (!computed) return [];
+
+ const rate = computed.playbackRate || 1;
+
+ return (entity().get(Cache)?.keyframes ?? [])
+ .map((keyframe) => computed.origin + (keyframe.get(KeyframeTrait)?.time ?? 0) / rate)
+ .sort((a, b) => a - b);
+ },
+ (prev, next) => prev.length === next.length && prev.every((frame, i) => frame === next[i]),
+ );
+
+ const now = useDerived(() => {
+ const scene = getActiveEntity(world);
+ return scene === null ? 0 : (scene.get(Computed)?.localTime ?? 0);
});
- const handlePrev = () => {
- const sceneEid = world.selection.scene;
- const fps = world.frameRate;
- const cid = clipEid();
- if (sceneEid === null || cid === null) return;
- const delay = c.Computed.delay[cid];
- const nowFrame = c.Computed.localTime[sceneEid];
- const frames = (c.Cache.keyframes[props.layer.eid] ?? [])
- .map(kf => c.Keyframe.time[kf])
- .sort((a: number, b: number) => b - a);
+ const previous = createMemo(() => [...frames()].reverse().find((frame) => frame < now()));
+ const next = createMemo(() => frames().find((frame) => frame > now()));
- for (const frame of frames) {
- const absoluteFrame = delay + frame;
- if (absoluteFrame < nowFrame) {
- c.Computed.localTimeInSeconds[sceneEid] = absoluteFrame / fps;
- c.Computed.localTime[sceneEid] = absoluteFrame;
- break;
- }
- }
- }
-
- const hasNext = createMemo(() => {
- const cid = clipEid();
- if (cid === null) return false;
- const delay = c.Computed.delay[cid];
- const localNowFrame = now() - delay;
- return (c.Cache.keyframes[props.layer.eid] ?? []).some(kf => c.Keyframe.time[kf] > localNowFrame);
- });
-
- const handleNext = () => {
- const sceneEid = world.selection.scene;
- const fps = world.frameRate;
- const cid = clipEid();
- if (sceneEid === null || cid === null) return;
- const delay = c.Computed.delay[cid];
- const nowFrame = c.Computed.localTime[sceneEid];
- const frames = (c.Cache.keyframes[props.layer.eid] ?? [])
- .map(kf => c.Keyframe.time[kf])
- .sort((a: number, b: number) => a - b);
-
- for (const frame of frames) {
- const absoluteFrame = delay + frame;
- if (absoluteFrame > nowFrame) {
- c.Computed.localTimeInSeconds[sceneEid] = absoluteFrame / fps;
- c.Computed.localTime[sceneEid] = absoluteFrame;
- break;
- }
- }
- }
-
- const handlePointerEnter = () => addComponent(world, props.layer.eid, c.Hovering, false);
- const handlePointerLeave = () => removeComponent(world, props.layer.eid, c.Hovering, false);
+ const goTo = (frame: number | undefined) => {
+ const scene = getActiveEntity(world);
+ if (scene === null || frame === undefined) return;
+ setPlayhead(world, scene, frame);
+ };
return (
<div
@@ -103,8 +87,8 @@ export function KeyframeLayer(props: KeyframeLayerProps) {
'bg-accent/70': !selected() && hovering(),
'bg-accent/40': !selected() && !hovering() && props.ancestorSelected,
}}
- onPointerEnter={handlePointerEnter}
- onPointerLeave={handlePointerLeave}
+ onPointerEnter={() => setRowHover(world, entity())}
+ onPointerLeave={() => setRowHover(world, null)}
style={{ height: KEYFRAME_TRACK_HEIGHT + 'px' }}
>
<div data-layer-label class="flex-1 min-w-0 overflow-hidden">
@@ -119,24 +103,36 @@ export function KeyframeLayer(props: KeyframeLayerProps) {
<Icon name="keyframe-indicator-default" class="size-6" />
</div>
<span class="text-xs px-0.5 shrink-0 whitespace-nowrap text-foreground">
- {formatProperty(c.KeyframeTrack.property[props.layer.eid])}
+ {formatProperty(property())}
</span>
</div>
</div>
<div class="flex items-center gap-0.5 shrink-0">
<Tooltip placement="top">
- <TooltipTrigger as={Button} variant="ghost" size="icon" disabled={!hasPrev()} onClick={handlePrev}>
+ <TooltipTrigger
+ as={Button}
+ variant="ghost"
+ size="icon"
+ disabled={previous() === undefined}
+ onClick={() => goTo(previous())}
+ >
<Icon name="caret-left" class="size-6" />
</TooltipTrigger>
<TooltipPortal>
<TooltipContent>Previous keyframe</TooltipContent>
</TooltipPortal>
</Tooltip>
- <Show when={targetEid()}>
- {(target) => <Keyframe property={property() as PropertyPath} target={target()} />}
+ <Show when={target()}>
+ {(entity) => <Keyframe property={property()} target={entity() as Entity} />}
</Show>
<Tooltip placement="top">
- <TooltipTrigger as={Button} variant="ghost" size="icon" disabled={!hasNext()} onClick={handleNext}>
+ <TooltipTrigger
+ as={Button}
+ variant="ghost"
+ size="icon"
+ disabled={next() === undefined}
+ onClick={() => goTo(next())}
+ >
<Icon name="caret-right" class="size-6" />
</TooltipTrigger>
<TooltipPortal>
@@ -148,39 +144,16 @@ export function KeyframeLayer(props: KeyframeLayerProps) {
)
}
-const PATH_NAMES: Record<PropertyPath, string> = {
- 'position.x': 'Position X',
- 'position.y': 'Position Y',
- 'offset.x': 'Offset X',
- 'offset.y': 'Offset Y',
- 'scale.x': 'Scale X',
- 'scale.y': 'Scale Y',
- 'scale': 'Scale',
- 'rotation': 'Rotation',
- 'skew.x': 'Skew X',
- 'skew.y': 'Skew Y',
- 'opacity': 'Opacity',
- 'color': 'Color',
- 'blur': 'Blur',
- 'volume': 'Volume',
- 'effect.value': 'Value',
- 'width': 'Width',
- 'height': 'Height',
- 'stroke.width': 'Stroke Width',
- 'vertexRadius': 'Radius',
- 'mixedVertexRadius.topLeft': 'Radius TL',
- 'mixedVertexRadius.topRight': 'Radius TR',
- 'mixedVertexRadius.bottomRight': 'Radius BR',
- 'mixedVertexRadius.bottomLeft': 'Radius BL',
- 'stop.offset': 'Offset',
- 'stop.color': 'Color',
- 'stop.opacity': 'Opacity',
- 'chars': 'Text',
-};
-
+/**
+ * A property's name as the row shows it. The file spells properties the way
+ * JSX does (`offsetX`, `cornerRadiusTopLeft`); this is the same name with the
+ * words separated, so the row and the inspector agree on what to call it.
+ */
export function formatProperty(property: string): string {
- return PATH_NAMES[property as PropertyPath]
- ?.split('.')
- ?.map(s => s.charAt(0).toUpperCase() + s.slice(1))
- ?.join(' ') ?? '';
+ if (!property) return '';
+
+ return property
+ .replace(/([A-Z])/g, ' $1')
+ .replace(/^./, (first) => first.toUpperCase())
+ .trim();
}
diff --git a/apps/web/src/components/timeline/layers/layer.tsx b/apps/web/src/components/timeline/layers/layer.tsx
index c5ea2aa4..7b622883 100644
--- a/apps/web/src/components/timeline/layers/layer.tsx
+++ b/apps/web/src/components/timeline/layers/layer.tsx
@@ -4,14 +4,14 @@
import { Index } from 'solid-js';
import { Dynamic } from 'solid-js/web';
-import { useEngine } from '@/context/engine';
-import { useEntityTag } from '@/components/engine';
+import { useTag } from '@diffusionstudio/koota-solid';
+import { Selected } from '@diffusionstudio/runtime';
import { KeyframeLayer } from './keyframe';
import { NodeLayer } from './node';
import { SubItemLayer } from './sub-item';
import type { Component } from 'solid-js';
-import type { TimelineNode, TimelineNodeKind } from '@/components/engine/api/timeline-index';
+import type { TimelineNode, TimelineNodeKind } from '@diffusionstudio/runtime';
type LayerProps = {
layer: TimelineNode;
@@ -19,7 +19,7 @@ type LayerProps = {
ancestorSelected?: boolean;
}
-type LayerRowProps = {
+export type LayerRowProps = {
layer: TimelineNode;
depth: number;
expanded: boolean;
@@ -32,15 +32,14 @@ const LAYER_ROWS: Record<TimelineNodeKind, Component<LayerRowProps>> = {
'keyframe-track': KeyframeLayer,
};
+/**
+ * One row and everything expanded under it. A row's own kind decides what it
+ * looks like; the nesting is the same whatever that is.
+ */
export function Layer(props: LayerProps) {
- const { world } = useEngine();
-
- const c = world.components;
- const eid = () => props.layer.eid;
-
const depth = () => props.depth ?? 0;
const ancestorSelected = () => props.ancestorSelected ?? false;
- const selected = useEntityTag(c.Selected, eid);
+ const selected = useTag(() => props.layer.entity, Selected);
return (
<>
@@ -52,7 +51,13 @@ export function Layer(props: LayerProps) {
ancestorSelected={ancestorSelected()}
/>
<Index each={props.layer.children}>
- {(child) => <Layer layer={child()} depth={depth() + 1} ancestorSelected={ancestorSelected() || selected()} />}
+ {(child) => (
+ <Layer
+ layer={child()}
+ depth={depth() + 1}
+ ancestorSelected={ancestorSelected() || selected()}
+ />
+ )}
</Index>
</>
)
diff --git a/apps/web/src/components/timeline/layers/layers.tsx b/apps/web/src/components/timeline/layers/layers.tsx
index d5a36c51..0f631117 100644
--- a/apps/web/src/components/timeline/layers/layers.tsx
+++ b/apps/web/src/components/timeline/layers/layers.tsx
@@ -2,14 +2,21 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-import { createEffect, createMemo, createSignal, Index, onCleanup, onMount, Show } from 'solid-js';
+import { createEffect, createMemo, Index, onCleanup, onMount, Show } from 'solid-js';
+import { useTrait, useWorld } from '@diffusionstudio/koota-solid';
+import {
+ ClipHeight,
+ Computed,
+ FrameRate,
+ Playback,
+ togglePlayback,
+} from '@diffusionstudio/runtime';
import { Button } from '@/components/ui/button';
import { Icon } from '@/components/ui/icon';
import {
DropdownMenu,
DropdownMenuCheckboxItem,
DropdownMenuContent,
- DropdownMenuItem,
DropdownMenuPortal,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
@@ -20,88 +27,92 @@ import {
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { Tooltip, TooltipContent, TooltipPortal, TooltipTrigger } from '@/components/ui/tooltip';
-import { DEFAULT_CLIP_HEIGHT, RULER_HEIGHT } from '@/components/engine/timeline/config';
+import { DEFAULT_CLIP_HEIGHT, RULER_HEIGHT } from '@/engine/timeline';
+import { splitAtPlayhead } from '@/engine/split';
+import { useDerived, useEditor, useTimelineIndex } from '@/engine/hooks';
import { useTimeline } from '@/context/timeline';
-import { useEngine } from '@/context/engine';
import { useLayout } from '@/context/layout';
-import { togglePlayback, splitAtPlayhead, setComponent, clearComponent } from '@/components/engine';
-import { useECS } from '@/context/ecs';
import { store } from '@/init';
import { createStoredSignal } from '@/lib/store';
import { Layer } from './layer';
import { LayerContextProvider } from './context';
import { formatFrames, TIME_FORMAT_OPTIONS, type TimeFormat } from '../time-format';
-import { createEntity, addComponent, getNextName, appendChild, resizeEntity, reorderEntity } from '@/components/engine';
-import { DEFAULT_DURATION_FRAMES } from '@/components/engine/constants';
-import type { EngineWorld } from '@/components/engine';
-import type { TimelineNode } from '@/components/engine';
+import type { TimelineNode } from '@diffusionstudio/runtime';
+import type { Entity } from 'koota';
+
+/** The row heights the height menu offers, tightest first. */
+const HEIGHT_PRESETS = [
+ { label: 'Tight', height: 28 },
+ { label: 'Snug', height: 32 },
+ { label: 'Normal', height: 40 },
+ { label: 'Relaxed', height: 64 },
+ { label: 'Loose', height: 116 },
+];
export function Layers() {
+ const world = useWorld();
+ const editor = useEditor();
const timeline = useTimeline();
- const { world } = useEngine();
- const { now, playing, looping } = useECS();
+ const index = useTimelineIndex();
const { timelineMinimized, toggleTimeline } = useLayout();
- const c = world.components;
- const timelineIndex = world.timelineIndex;
+ const layers = createMemo(() => index().layers);
+ const scene = createMemo(() => index().root);
+
+ const frameRate = useTrait(world, FrameRate);
+ const playback = useTrait(scene, Playback);
+ const now = useDerived(() => scene()?.get(Computed)?.localTime ?? 0);
- const layers = createMemo(() => timelineIndex().layers);
- const [commonLayerHeight, setCommonLayerHeight] = createSignal(DEFAULT_CLIP_HEIGHT);
const [timeFormat, setTimeFormat] = createStoredSignal(
store.define<TimeFormat>('timeline.timeFormat', 'standard'),
);
- const clock = createMemo(() => formatFrames(now(), world.frameRate, timeFormat()));
-
- createEffect(() => {
- let sum = 0;
- let count = 0;
+ const clock = createMemo(() => formatFrames(now(), frameRate()?.value ?? 30, timeFormat()));
- forEachGeometryLayer(layers(), (eid) => {
- sum += (c.ClipHeight[eid] ?? DEFAULT_CLIP_HEIGHT);
- count++;
- });
+ // What the height menu ticks: the height the rows share, or none of them if
+ // they differ.
+ const commonHeight = useDerived(() => {
+ let common: number | null = null;
- if (count === 0) return;
+ for (const entity of geometryEntities(layers())) {
+ const height = entity.get(ClipHeight)?.value ?? DEFAULT_CLIP_HEIGHT;
+ if (common === null) common = height;
+ else if (common !== height) return null;
+ }
- setCommonLayerHeight(sum / count);
+ return common;
});
- const handleCommonClipHeightChange = (height: number) => {
- world.history.transaction('Set layer height', () => {
- forEachGeometryLayer(layers(), (eid) => {
- setComponent(world, eid, c.ClipHeight, height);
- });
- });
-
- setCommonLayerHeight(height);
- }
-
- const addAdjustmentLayer = () => createAdjustmentLayer(world);
+ const setCommonHeight = (height: number) => {
+ for (const entity of geometryEntities(layers())) {
+ editor.editProperty(entity, 'clipHeight', height);
+ }
+ };
const toggleLooping = () => {
- const sceneEid = timelineIndex().root;
- if (sceneEid === null) return;
+ const entity = scene();
+ if (!entity) return;
+ entity.set(Playback, { loop: !playback()?.loop });
+ };
- // Will toggle between 0 and 1
- const currentLoop = c.Playback.loop[sceneEid] ?? 0;
- setComponent(world, sceneEid, c.Playback, { loop: 1 - currentLoop }, false);
- }
+ const handlePlay = () => {
+ const entity = scene();
+ if (entity) togglePlayback(world, entity);
+ };
+ // A press on the empty space below the rows, not on one of them.
const handlePointerDown = (e: PointerEvent) => {
if (e.button !== 0 || e.target !== e.currentTarget) return;
- clearComponent(world, c.Selected, false);
- }
+ editor.clearSelection();
+ };
const handleHeaderDoubleClick = (e: MouseEvent) => {
if ((e.target as HTMLElement).closest('button')) return;
toggleTimeline();
- }
+ };
- createEffect(() => {
- timeline.setMinimized(timelineMinimized());
- });
+ createEffect(() => timeline.setMinimized(timelineMinimized()));
onMount(timeline.mount);
onCleanup(timeline.unmount);
@@ -121,8 +132,8 @@ export function Layers() {
<Tooltip placement="top">
<TooltipTrigger<typeof Button>
as={(triggerProps) => (
- <Button {...triggerProps} variant="ghost" size="icon" onClick={() => togglePlayback(world)}>
- <Show when={playing()} fallback={<Icon name="play" class="size-6" />}>
+ <Button {...triggerProps} variant="ghost" size="icon" onClick={handlePlay}>
+ <Show when={playback()?.playing} fallback={<Icon name="play" class="size-6" />}>
<Icon name="pause" class="size-6" />
</Show>
</Button>
@@ -130,7 +141,7 @@ export function Layers() {
/>
<TooltipPortal>
<TooltipContent shortcut="Space">
- {playing() ? 'Pause' : 'Play'}
+ {playback()?.playing ? 'Pause' : 'Play'}
</TooltipContent>
</TooltipPortal>
</Tooltip>
@@ -138,7 +149,7 @@ export function Layers() {
<TooltipTrigger<typeof Button>
as={(triggerProps) => (
<Button {...triggerProps} variant="ghost" size="icon" onClick={toggleLooping}>
- <Show when={looping() === 1} fallback={<Icon name="controls-no-loop" />}>
+ <Show when={playback()?.loop} fallback={<Icon name="controls-no-loop" />}>
<Icon name="controls-loop" />
</Show>
</Button>
@@ -146,7 +157,7 @@ export function Layers() {
/>
<TooltipPortal>
<TooltipContent>
- {looping() === 1 ? 'Disable loop' : 'Enable loop'}
+ {playback()?.loop ? 'Disable loop' : 'Enable loop'}
</TooltipContent>
</TooltipPortal>
</Tooltip>
@@ -187,41 +198,17 @@ export function Layers() {
<DropdownMenuSubTrigger>Layer height</DropdownMenuSubTrigger>
<DropdownMenuPortal>
<DropdownMenuSubContent class="w-[140px]">
- <DropdownMenuCheckboxItem
- onSelect={() => handleCommonClipHeightChange(28)}
- checked={commonLayerHeight() === 28}
- >
- Tight
- <span class="text-xxs text-muted-foreground ml-auto">28</span>
- </DropdownMenuCheckboxItem>
- <DropdownMenuCheckboxItem
- onSelect={() => handleCommonClipHeightChange(32)}
- checked={commonLayerHeight() === 32}
- >
- Snug
- <span class="text-xxs text-muted-foreground ml-auto">32</span>
- </DropdownMenuCheckboxItem>
- <DropdownMenuCheckboxItem
- onSelect={() => handleCommonClipHeightChange(40)}
- checked={commonLayerHeight() === 40}
- >
- Normal
- <span class="text-xxs text-muted-foreground ml-auto">40</span>
- </DropdownMenuCheckboxItem>
- <DropdownMenuCheckboxItem
- onSelect={() => handleCommonClipHeightChange(64)}
- checked={commonLayerHeight() === 64}
- >
- Relaxed
- <span class="text-xxs text-muted-foreground ml-auto">64</span>
- </DropdownMenuCheckboxItem>
- <DropdownMenuCheckboxItem
- onSelect={() => handleCommonClipHeightChange(116)}
- checked={commonLayerHeight() === 116}
- >
- Loose
- <span class="text-xxs text-muted-foreground ml-auto">116</span>
- </DropdownMenuCheckboxItem>
+ <Index each={HEIGHT_PRESETS}>
+ {(preset) => (
+ <DropdownMenuCheckboxItem
+ onSelect={() => setCommonHeight(preset().height)}
+ checked={commonHeight() === preset().height}
+ >
+ {preset().label}
+ <span class="text-xxs text-muted-foreground ml-auto">{preset().height}</span>
+ </DropdownMenuCheckboxItem>
+ )}
+ </Index>
</DropdownMenuSubContent>
</DropdownMenuPortal>
</DropdownMenuSub>
@@ -248,10 +235,6 @@ export function Layers() {
</DropdownMenuSubContent>
</DropdownMenuPortal>
</DropdownMenuSub>
- <DropdownMenuSeparator />
- <DropdownMenuItem onSelect={addAdjustmentLayer}>
- Add adjustment layer
- </DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenuPortal>
</DropdownMenu>
@@ -278,38 +261,10 @@ export function Layers() {
);
}
-function forEachGeometryLayer(nodes: TimelineNode[], fn: (eid: number) => void) {
+/** Every clip row of the tree, expanded ones included. */
+function* geometryEntities(nodes: TimelineNode[]): Generator<Entity> {
for (const node of nodes) {
- if (node.kind === 'geometry') {
- fn(node.eid);
- }
- forEachGeometryLayer(node.children, fn);
+ if (node.kind === 'geometry') yield node.entity;
+ yield* geometryEntities(node.children);
}
-};
-
-function createAdjustmentLayer(world: EngineWorld): number | null {
- const c = world.components;
- const sceneEid = world.selection.scene;
- if (sceneEid === null) return null;
-
- const width = Math.max(1, Math.round(c.Computed.width[sceneEid] ?? 0));
- const height = Math.max(1, Math.round(c.Computed.height[sceneEid] ?? 0));
-
- return world.history.transaction('Add adjustment layer', () => {
- const eid = createEntity(world);
- addComponent(world, eid, c.AdjustmentLayer);
- setComponent(world, eid, c.Name, getNextName(world, 'Adjustment Layer'));
- setComponent(world, eid, c.Position, { x: 0, y: 0 });
-
- setComponent(world, eid, c.Trim, { start: 0, end: DEFAULT_DURATION_FRAMES });
- appendChild(world, eid, sceneEid);
- resizeEntity(world, eid, { width, height });
-
- reorderEntity(world, eid, 'front');
-
- clearComponent(world, c.Selected, false);
- addComponent(world, eid, c.Selected, false);
-
- return eid;
- });
}
diff --git a/apps/web/src/components/timeline/layers/node.tsx b/apps/web/src/components/timeline/layers/node.tsx
index b25aa1ee..859f3fa3 100644
--- a/apps/web/src/components/timeline/layers/node.tsx
+++ b/apps/web/src/components/timeline/layers/node.tsx
@@ -3,7 +3,27 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { createMemo, createSignal, Show } from 'solid-js';
-import { hasComponent, Not, Or, query } from 'bitecs';
+import { useTag, useTrait, useWorld } from '@diffusionstudio/koota-solid';
+import {
+ ClipHeight,
+ Expanded,
+ Hidden,
+ Hovering,
+ Muted,
+ Name,
+ Selected,
+ Soloed,
+ findGeometryAsset,
+ getEntityChildren,
+ getParentEntity,
+ isAdjustmentLayer,
+ isCaption,
+ isGroup,
+ isMask,
+ isScene,
+ isSequence,
+ isText,
+} from '@diffusionstudio/runtime';
import { Button } from '@/components/ui/button';
import { Icon } from '@/components/ui/icon';
import {
@@ -16,230 +36,178 @@ import {
ContextMenuTrigger,
} from '@/components/ui/context-menu';
import { Tooltip, TooltipContent, TooltipPortal, TooltipTrigger } from '@/components/ui/tooltip';
-import { DEFAULT_CLIP_HEIGHT, MAX_CLIP_HEIGHT, MIN_CLIP_HEIGHT } from '@/components/engine/timeline/config';
-import { useEngine } from '@/context/engine';
-import {
- useEntityState,
- useEntityTag,
- deleteEntity,
- addComponent,
- removeComponent,
- setComponent,
- reorderEntity,
- getParentEntity,
- getEntityTree,
- appendChild,
- removeChild,
- sortByItemIndex,
- ChildOf,
- type EngineWorld,
- isScene,
- isGroup,
- findGeometryAsset,
- clearComponent,
- isMask,
- isCaption,
- isText,
- isAdjustmentLayer,
- isSequence,
-} from '@/components/engine';
+import { useEditor } from '@/engine/hooks';
+import { DEFAULT_CLIP_HEIGHT, MAX_CLIP_HEIGHT, MIN_CLIP_HEIGHT } from '@/engine/timeline';
import { NESTED_INDENT_PX } from './config';
-import { useLayerContext, type DropPosition, type LayerDragState } from './context';
+import { useLayerContext } from './context';
+import { dropSelection, findDropTarget } from './reparent';
+import { setRowHover } from './hover';
-import type { TimelineNode } from '@/components/engine/api/timeline-index';
-
-type NodeLayerProps = {
- layer: TimelineNode;
- depth?: number;
- expanded: boolean;
- ancestorSelected: boolean;
-}
+import type { Entity, World } from 'koota';
+import type { TimelineNode } from '@diffusionstudio/runtime';
+import type { LayerRowProps } from './layer';
+/** How far the pointer must move before a press on a row becomes a drag. */
const DRAG_THRESHOLD_PX = 4;
-export function NodeLayer(props: NodeLayerProps) {
- const { world } = useEngine();
-
- const c = world.components;
-
- const depth = () => props.depth ?? 0;
+export function NodeLayer(props: LayerRowProps) {
+ const world = useWorld();
+ const editor = useEditor();
- const eid = () => props.layer.eid;
+ const entity = () => props.layer.entity;
+ const clipHeight = useTrait(entity, ClipHeight);
+ const height = () => clipHeight()?.value ?? DEFAULT_CLIP_HEIGHT;
- const height = useEntityState(c.ClipHeight, eid, DEFAULT_CLIP_HEIGHT);
-
- const muted = useEntityTag(c.Muted, eid);
- const soloed = useEntityTag(c.Soloed, eid);
- const hidden = useEntityTag(c.Hidden, eid);
- const hovering = useEntityTag(c.Hovering, eid);
- const selected = useEntityTag(c.Selected, eid);
+ const muted = useTag(entity, Muted);
+ const soloed = useTag(entity, Soloed);
+ const hidden = useTag(entity, Hidden);
+ const hovering = useTag(entity, Hovering);
+ const selected = useTag(entity, Selected);
const controlsVisible = createMemo(() => hovering() || muted() || soloed() || hidden());
- const [resizedId, setResizedId] = useLayerContext().resizedId;
+ const [resized, setResized] = useLayerContext().resized;
const [dragState, setDragState] = useLayerContext().dragState;
const [editing, setEditing] = createSignal(false);
let originalName = '';
- const canExpand = () => props.layer.expandable;
- const nameState = useEntityState(c.Name, eid);
- const name = createMemo(() => nameState() ?? `Layer ${eid()}`);
+ const nameTrait = useTrait(entity, Name);
+ const name = createMemo(() => nameTrait()?.value ?? 'Layer');
const icon = createMemo(() => getLayerIcon(world, props.layer));
const dropPosition = createMemo(() => {
- if (dragState()?.targetEid === props.layer.eid) {
- return dragState()?.position ?? null;
- }
- return null;
+ const state = dragState();
+ return state?.target === entity() ? state.position : null;
});
- const toggleTag = (tag: typeof c.Muted) => {
- const eid = props.layer.eid;
- if (hasComponent(world, eid, tag)) {
- removeComponent(world, eid, tag);
- } else {
- addComponent(world, eid, tag);
- }
- }
-
const toggleMuted = (e?: Event) => {
e?.stopPropagation();
- toggleTag(c.Muted);
+ editor.editProperty(entity(), 'muted', !muted());
};
+
const toggleHidden = (e?: Event) => {
e?.stopPropagation();
- toggleTag(c.Hidden);
+ editor.editProperty(entity(), 'hidden', !hidden());
};
+
const toggleExpanded = (e?: Event) => {
e?.stopPropagation();
- toggleTag(c.Expanded);
+ editor.editProperty(entity(), 'expanded', !entity().has(Expanded));
};
+ /**
+ * Solo is monitoring rather than composition — it says what you want to
+ * hear right now, which the file has nothing to say about — so it is
+ * written to the trait and only one node holds it.
+ */
const toggleSoloed = (e?: Event) => {
e?.stopPropagation();
- const eid = props.layer.eid;
- const wasSoloed = hasComponent(world, eid, c.Soloed);
-
- clearComponent(world, c.Soloed, false);
- if (!wasSoloed) {
- addComponent(world, eid, c.Soloed);
- }
- }
-
- const handleSelect = (e: { shiftKey: boolean }) => {
- if (e.shiftKey) {
- addComponent(world, props.layer.eid, c.Selected, false);
- } else {
- clearComponent(world, c.Selected, false);
- addComponent(world, props.layer.eid, c.Selected, false);
- }
- }
+ const wasSoloed = soloed();
+ for (const other of world.query(Soloed)) other.remove(Soloed);
+ if (!wasSoloed) entity().add(Soloed);
+ };
+ /**
+ * A press selects, and becomes a drag if it travels far enough. Selecting
+ * first means the drag carries whatever the press selected, so dragging an
+ * unselected row moves that row and dragging a selected one moves the whole
+ * selection.
+ */
const handleRowPointerDown = (e: PointerEvent) => {
- if (e.button !== 0) return;
- if (resizedId() != null) return;
+ if (e.button !== 0 || resized() !== null) return;
if ((e.target as HTMLElement | null)?.closest('button')) return;
- handleSelect(e);
+ editor.select(entity(), { extend: e.shiftKey });
const startX = e.clientX;
const startY = e.clientY;
let dragging = false;
- const onMove = (ev: PointerEvent) => {
- const deltaX = Math.abs(ev.clientX - startX);
- const deltaY = Math.abs(ev.clientY - startY);
-
- if (!dragging && (deltaX > DRAG_THRESHOLD_PX || deltaY > DRAG_THRESHOLD_PX)) {
- dragging = true;
- }
+ const onMove = (event: PointerEvent) => {
+ dragging ||= Math.abs(event.clientX - startX) > DRAG_THRESHOLD_PX
+ || Math.abs(event.clientY - startY) > DRAG_THRESHOLD_PX;
- if (dragging) {
- setDragState(findClosestLayer(world, ev));
- }
+ if (dragging) setDragState(findDropTarget(event));
};
- const onUp = (ev: PointerEvent) => {
+ const onUp = (event: PointerEvent) => {
document.removeEventListener('pointermove', onMove);
document.removeEventListener('pointerup', onUp);
-
if (!dragging) return;
- dropSelection(world, findClosestLayer(world, ev));
-
+ dropSelection(world, findDropTarget(event));
setDragState(null);
- clearComponent(world, c.Selected, false);
};
document.addEventListener('pointermove', onMove);
document.addEventListener('pointerup', onUp);
};
- let startY = 0;
- let startHeight = 0;
+ let resizeStartY = 0;
+ let resizeStartHeight = 0;
+
+ const handleResizeMove = (e: PointerEvent) => {
+ const next = Math.max(MIN_CLIP_HEIGHT, Math.min(MAX_CLIP_HEIGHT, resizeStartHeight + e.clientY - resizeStartY));
+ editor.editProperty(entity(), 'clipHeight', next);
+ };
+
+ const handleResizeEnd = () => {
+ setResized(null);
+ document.removeEventListener('pointermove', handleResizeMove);
+ document.removeEventListener('pointerup', handleResizeEnd);
+ };
const handleResizeStart = (e: PointerEvent) => {
e.preventDefault();
e.stopPropagation();
- startY = e.clientY;
- startHeight = height();
- setResizedId(props.layer.eid);
+ resizeStartY = e.clientY;
+ resizeStartHeight = height();
+ setResized(entity());
document.addEventListener('pointermove', handleResizeMove);
document.addEventListener('pointerup', handleResizeEnd);
- }
+ };
- const handleResizeMove = (e: PointerEvent) => {
- const deltaY = e.clientY - startY;
- const newHeight = Math.max(MIN_CLIP_HEIGHT, Math.min(MAX_CLIP_HEIGHT, startHeight + deltaY));
- setComponent(world, props.layer.eid, c.ClipHeight, newHeight);
- }
+ const handleRemove = () => editor.remove(entity());
- const handleResizeEnd = () => {
- setResizedId(null);
- document.removeEventListener('pointermove', handleResizeMove);
- document.removeEventListener('pointerup', handleResizeEnd);
- }
+ /**
+ * The column reads top-down while the file reads bottom-up: the last child
+ * of an element is the one drawn on top, so "front" is the end of the file
+ * and "back" is the beginning.
+ */
+ const handleReorder = (target: 'front' | 'back') => {
+ const parent = getParentEntity(entity());
+ if (!parent) return;
- const handleRemove = () => deleteEntity(world, props.layer.eid);
- const handleReorder = (target: 'front' | 'back') => reorderEntity(world, props.layer.eid, target);
-
- const handlePointerEnter = () => {
- clearComponent(world, c.Hovering, false);
- addComponent(world, props.layer.eid, c.Hovering, false);
- }
- const handlePointerLeave = () => {
- clearComponent(world, c.Hovering, false);
- removeComponent(world, props.layer.eid, c.Hovering, false);
- }
+ const siblings = getEntityChildren(world, parent).filter((sibling) => sibling !== entity());
+ editor.reparent(entity(), parent, target === 'back' ? siblings[0] : undefined);
+ };
const startEditing = () => {
- originalName = c.Name[eid()] ?? '';
- world.history.startTransaction('Rename layer');
+ originalName = name();
setEditing(true);
};
const finishEditing = (input: HTMLInputElement) => {
if (!editing()) return;
- if (!input.value.trim()) {
- setComponent(world, eid(), c.Name, originalName);
- }
- world.history.commitTransaction();
+ // An empty name is not a name; the row falls back to what it had.
+ if (!input.value.trim()) editor.editProperty(entity(), 'name', originalName);
setEditing(false);
};
const handleNameInput = (e: InputEvent) => {
- const input = e.currentTarget as HTMLInputElement;
- setComponent(world, eid(), c.Name, input.value ?? '');
+ editor.editProperty(entity(), 'name', (e.currentTarget as HTMLInputElement).value);
};
const handleNameKeyDown = (e: KeyboardEvent) => {
+ // The canvas is listening for keys; a name being typed is not a shortcut.
e.stopPropagation();
const input = e.currentTarget as HTMLInputElement;
+
if (e.key === 'Escape') {
- setComponent(world, eid(), c.Name, originalName);
+ editor.editProperty(entity(), 'name', originalName);
input.blur();
} else if (e.key === 'Enter') {
input.blur();
@@ -258,18 +226,21 @@ export function NodeLayer(props: NodeLayerProps) {
<ContextMenuTrigger
as="div"
class="w-full text-muted-foreground group relative select-none"
- data-layer-eid={props.layer.eid}
- onPointerEnter={handlePointerEnter}
- onPointerLeave={handlePointerLeave}
+ onPointerEnter={() => setRowHover(world, entity())}
+ onPointerLeave={() => setRowHover(world, null)}
onPointerDown={handleRowPointerDown}
classList={{
'bg-accent': selected(),
- 'bg-accent/70': !selected() && hovering() && resizedId() == null,
- 'bg-accent/40': !selected() && !(hovering() && resizedId() == null) && props.ancestorSelected,
+ 'bg-accent/70': !selected() && hovering() && resized() === null,
+ 'bg-accent/40': !selected() && !(hovering() && resized() === null) && props.ancestorSelected,
}}
style={{ height: height() + 'px' }}
>
- <div class="w-full pl-0.5 pr-2 flex items-center justify-between h-full">
+ <div
+ data-layer-row
+ ref={(el) => registerRow(el, entity())}
+ class="w-full pl-0.5 pr-2 flex items-center justify-between h-full"
+ >
<div
data-layer-label
class="flex-1 min-w-0 overflow-hidden"
@@ -281,15 +252,16 @@ export function NodeLayer(props: NodeLayerProps) {
<div
class="flex items-center gap-0.5 w-max"
style={{
- 'padding-left': `${depth() * NESTED_INDENT_PX}px`,
+ 'padding-left': `${props.depth * NESTED_INDENT_PX}px`,
transform: editing() ? 'none' : 'translateX(calc(var(--layer-x, 0px) * -1))',
}}
>
<button
- disabled={!canExpand()}
+ disabled={!props.layer.expandable}
onClick={toggleExpanded}
- class="size-4 shrink-0 flex items-center justify-center overflow-clip invisible group-hover/layers:visible focus-ring rounded-sm">
- <Show when={canExpand()}>
+ class="size-4 shrink-0 flex items-center justify-center overflow-clip invisible group-hover/layers:visible focus-ring rounded-sm"
+ >
+ <Show when={props.layer.expandable}>
<Icon name={props.expanded ? "chevron-down" : "chevron-right"} class="size-6 hover:text-foreground" />
</Show>
</button>
@@ -322,7 +294,7 @@ export function NodeLayer(props: NodeLayerProps) {
<div
class="items-center flex gap-0.5 shrink-0 overflow-hidden group-hover:w-auto"
classList={{
- 'hidden': resizedId() != null,
+ 'hidden': resized() !== null,
'w-0': !(muted() || soloed() || hidden()),
'w-auto': muted() || soloed() || hidden(),
}}
@@ -377,7 +349,7 @@ export function NodeLayer(props: NodeLayerProps) {
</div>
</div>
- {/* Resize handle at the bottom */}
+ {/* Drag the bottom edge to make the row taller. */}
<div
class="absolute bottom-0 left-0 right-0 h-[3px] cursor-ns-resize translate-y-0.5 z-20 group/resize"
classList={{ 'hidden': dragState() !== null }}
@@ -385,11 +357,11 @@ export function NodeLayer(props: NodeLayerProps) {
>
<div
class="absolute left-0 right-0 top-px h-px transition-colors group-hover/resize:bg-primary"
- classList={{ 'bg-primary': resizedId() === props.layer.eid }}
+ classList={{ 'bg-primary': resized() === entity() }}
/>
</div>
- {/* Drag-to-reparent drop indicators */}
+ {/* Where a drop would land. */}
<Show when={dropPosition() === 'above'}>
<div class="pointer-events-none absolute top-0 left-0 right-0 h-[2px] bg-primary z-30" />
</Show>
@@ -402,15 +374,9 @@ export function NodeLayer(props: NodeLayerProps) {
</ContextMenuTrigger>
<ContextMenuPortal>
<ContextMenuContent class="w-[160px]">
- <ContextMenuItem onSelect={toggleMuted}>
- {muted() ? 'Unmute' : 'Mute'}
- </ContextMenuItem>
- <ContextMenuItem onSelect={toggleSoloed}>
- {soloed() ? 'Unsolo' : 'Solo'}
- </ContextMenuItem>
- <ContextMenuItem onSelect={toggleHidden}>
- {hidden() ? 'Unhide' : 'Hide'}
- </ContextMenuItem>
+ <ContextMenuItem onSelect={toggleMuted}>{muted() ? 'Unmute' : 'Mute'}</ContextMenuItem>
+ <ContextMenuItem onSelect={toggleSoloed}>{soloed() ? 'Unsolo' : 'Solo'}</ContextMenuItem>
+ <ContextMenuItem onSelect={toggleHidden}>{hidden() ? 'Unhide' : 'Hide'}</ContextMenuItem>
<ContextMenuSeparator />
<ContextMenuItem onSelect={() => handleReorder('front')}>
Bring to front
@@ -421,223 +387,52 @@ export function NodeLayer(props: NodeLayerProps) {
<ContextMenuShortcut>[</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuSeparator />
- <ContextMenuItem onSelect={handleRemove}>
- Remove
- </ContextMenuItem>
+ <ContextMenuItem onSelect={handleRemove}>Remove</ContextMenuItem>
</ContextMenuContent>
</ContextMenuPortal>
</ContextMenu>
)
}
-export function findClosestLayer(world: EngineWorld, ev: PointerEvent): LayerDragState | null {
- const c = world.components;
-
- const el = document.elementFromPoint(ev.clientX, ev.clientY) as HTMLElement | null;
- const match = el?.closest('[data-layer-eid]') as HTMLElement | null;
- if (!match) return null;
-
- const targetEid = Number(match.dataset.layerEid);
- if (!Number.isFinite(targetEid)) return null;
-
- // Never drop relative to a row that is itself part of the dragged selection.
- if (hasComponent(world, targetEid, c.Selected)) return null;
-
- const rect = match.getBoundingClientRect();
- const relY = ev.clientY - rect.top;
-
- // Groups are containers, so their middle band is an `inside` (reparent-as-
- // child) zone — regardless of whether they're currently expanded. Everything
- // else only gets `above`/`below` sibling zones.
- let position: DropPosition;
- if (isGroup(world, targetEid)) {
- if (relY < rect.height * 0.25) position = 'above';
- else if (relY > rect.height * 0.75) position = 'below';
- else position = 'inside';
- } else {
- position = relY < rect.height * 0.5 ? 'above' : 'below';
- }
-
- if (position === 'below') {
- const next = nextDroppableRow(world, match);
- if (next !== null) {
- return { position: 'above', targetEid: next };
- }
- }
-
- return { position, targetEid };
-}
-
/**
- * The eid of the first non-selected geometry/group row that follows `row` in
- * visual order, or null if `row` is the last droppable row. Sub-item and
- * keyframe rows don't carry `data-layer-eid` and are skipped.
+ * Which entity a row's element stands for. A drop is found by hit-testing the
+ * DOM (`elementFromPoint`), which hands back an element and nothing else, so
+ * the entity is kept beside it — an id in a data attribute would have to be
+ * turned back into an entity, and koota hands out ids again once they are
+ * free. Marked on the row's own content rather than the context-menu trigger
+ * around it, so this does not rest on that forwarding a ref; the two have the
+ * same box either way.
*/
-function nextDroppableRow(world: EngineWorld, row: HTMLElement): number | null {
- const c = world.components;
- const rows = Array.from(document.querySelectorAll<HTMLElement>('[data-layer-eid]'));
- const idx = rows.indexOf(row);
- if (idx === -1) return null;
-
- for (let i = idx + 1; i < rows.length; i++) {
- const eid = Number(rows[i].dataset.layerEid);
- if (!Number.isFinite(eid)) continue;
- // Skip rows being dragged — they aren't valid drop anchors.
- if (hasComponent(world, eid, c.Selected)) continue;
- return eid;
- }
+const ROW_ENTITIES = new WeakMap<HTMLElement, Entity>();
- return null;
+function registerRow(element: HTMLElement, entity: Entity): void {
+ ROW_ENTITIES.set(element, entity);
}
-/**
- * Reparent the entire geometry selection relative to a drop target.
- * - `inside` — selection becomes the last children of the target.
- * - `above` / `below` — selection becomes siblings of the target, in order.
- *
- * The selection's DFS order from the timeline index is preserved; descendants
- * of already-selected items are dropped (they move with the ancestor). The
- * operation is a no-op if the target sits inside the dragged subtree.
- */
-function dropSelection(world: EngineWorld, target: LayerDragState | null) {
- if (!target || target.targetEid === null || target.position === null) return;
-
- const c = world.components;
- const targetEid = target.targetEid;
- const position = target.position;
-
- const newParent = position === 'inside'
- ? targetEid
- : getParentEntity(world, targetEid);
- if (newParent === null) return;
-
- // Collect the selection in DFS order, skipping descendants of items whose
- // ancestor is already selected — those move with their ancestor.
- const selection = new Set(query(world, [c.Selected, Or(c.Geometry, c.Group, c.AdjustmentLayer), Not(c.Deleted)]));
- if (selection.size === 0) return;
-
- const ordered: number[] = [];
- const walk = (nodes: TimelineNode[]) => {
- for (const node of nodes) {
- if (selection.has(node.eid)) {
- ordered.push(node.eid);
- continue;
- }
- walk(node.children);
- }
- };
- walk(world.timelineIndex().layers);
- if (ordered.length === 0) return;
-
- // Block cycles: target cannot lie inside any dragged entity's subtree.
- for (const eid of ordered) {
- if (getEntityTree(world, eid).includes(targetEid)) return;
- }
-
- world.history.transaction('Reparent layers', () => {
- // Dropping into a collapsed group would hide the moved items — expand it so
- // the result is visible.
- if (position === 'inside' && !hasComponent(world, newParent, c.Expanded)) {
- addComponent(world, newParent, c.Expanded);
- }
-
- // Reparent items whose current parent differs from the new parent.
- let reparented = false;
- for (const eid of ordered) {
- const currentParent = getParentEntity(world, eid);
- if (currentParent === newParent) continue;
- if (currentParent !== null) removeChild(world, eid, currentParent);
- appendChild(world, eid, newParent);
- reparented = true;
- }
-
- // Rebuild ItemIndex on the new parent's geometry siblings, inserting the
- // moved set at the position resolved from the drop target.
- //
- // The layers panel renders siblings top-to-bottom in *descending* ItemIndex
- // (the index builder sorts ascending then reverses), so all reasoning here
- // is done in that same top-to-bottom display order — `ordered` is already
- // collected top-to-bottom — and the indices are written back descending so
- // they survive the round-trip through the index builder.
- const siblings = [...query(world, [ChildOf(newParent), Not(c.Deleted), Or(c.Geometry, c.Group, c.AdjustmentLayer)])];
- siblings.sort(sortByItemIndex(world)).reverse();
-
- const movedSet = new Set(ordered);
- const rest = siblings.filter(eid => !movedSet.has(eid));
-
- let insertIndex: number;
- if (position === 'inside') {
- // Drop at the top of the target's children, directly beneath its row.
- insertIndex = 0;
- } else {
- const anchorPos = rest.indexOf(targetEid);
- if (anchorPos === -1) return;
- insertIndex = position === 'above' ? anchorPos : anchorPos + 1;
- }
-
- const newOrder = [...rest.slice(0, insertIndex), ...ordered, ...rest.slice(insertIndex)];
-
- // A reparent already pushed history and left the moved items carrying a
- // stale ItemIndex from their old parent, so their position in `siblings`
- // (sorted by that stale value) is meaningless — always rewrite indices in
- // that case. The order-equality check is only a valid no-op guard for a
- // pure same-parent reorder.
- let changed = reparented || newOrder.length !== siblings.length;
- for (let i = 0; !changed && i < newOrder.length; i++) {
- if (newOrder[i] !== siblings[i]) changed = true;
- }
- if (!changed) return;
-
- // Top of the display list gets the highest ItemIndex.
- const top = newOrder.length - 1;
- for (const [idx, sib] of newOrder.entries()) {
- setComponent(world, sib, c.ItemIndex, top - idx);
- }
- });
+export function getRowEntity(element: HTMLElement): Entity | undefined {
+ return ROW_ENTITIES.get(element);
}
-function getLayerIcon(world: EngineWorld, layer: TimelineNode) {
- if (isMask(world, layer.eid)) {
- return "mask-small"
- }
-
- if (isAdjustmentLayer(world, layer.eid)) {
- return "adjustment-layer"
- }
-
- if (isScene(world, layer.eid)) {
- return "scene-frame-small"
- }
-
- if (isSequence(world, layer.eid)) {
- return "timeline-sequence-small"
+function getLayerIcon(world: World, layer: TimelineNode) {
+ const entity = layer.entity;
+
+ if (isMask(entity)) return "mask-small";
+ if (isAdjustmentLayer(entity)) return "adjustment-layer";
+ if (isScene(entity)) return "scene-frame-small";
+ if (isSequence(entity)) return "timeline-sequence-small";
+ if (isGroup(entity)) return "group";
+ if (isCaption(entity)) return "captions-small";
+ if (isText(entity)) return "text-small";
+
+ switch (findGeometryAsset(world, entity)?.type) {
+ case 'IMAGE':
+ return "image-small";
+ case 'VIDEO':
+ case 'SEQUENCE':
+ return "video-small";
+ case 'AUDIO':
+ return "audio-small";
+ default:
+ return "rectangle-small";
}
-
- if (isGroup(world, layer.eid)) {
- return "group"
- }
-
- if (isCaption(world, layer.eid)) {
- return "captions-small"
- }
-
- if (isText(world, layer.eid)) {
- return "text-small"
- }
-
- const asset = findGeometryAsset(world, layer.eid);
-
- if (asset?.type === 'IMAGE') {
- return "image-small"
- }
-
- if (asset?.type === 'VIDEO') {
- return "video-small"
- }
-
- if (asset?.type === 'AUDIO') {
- return "audio-small"
- }
-
- return "rectangle-small"
-}
\ No newline at end of file
+}
diff --git a/apps/web/src/components/timeline/layers/reparent.ts b/apps/web/src/components/timeline/layers/reparent.ts
new file mode 100644
index 00000000..865ed476
--- /dev/null
+++ b/apps/web/src/components/timeline/layers/reparent.ts
@@ -0,0 +1,157 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * Dragging rows onto other rows. Where a drop lands is worked out from the
+ * DOM — the rows are what the pointer is actually over — and the move itself
+ * is a reparent through the editor, so the elements move in the file.
+ *
+ * Order comes out of the file too: a parent's children are drawn in the order
+ * they are written, so moving an element in front of another is the whole of
+ * reordering. Nothing here has to renumber anything.
+ */
+
+import {
+ Expanded,
+ Geometry,
+ Group,
+ Selected,
+ buildTimelineLayers,
+ getActiveEntity,
+ getEntityChildren,
+ getEntityTree,
+ getParentEntity,
+ isGroup,
+} from '@diffusionstudio/runtime';
+import { Or } from 'koota';
+
+import { getDocumentEditor } from '@/engine/editor';
+import { getRowEntity } from './node';
+
+import type { Entity, World } from 'koota';
+import type { TimelineNode } from '@diffusionstudio/runtime';
+import type { DropPosition, LayerDragState } from './context';
+
+/**
+ * The row under the pointer and which part of it, or null over anything else.
+ * A row already being dragged is not a target: dropping a thing next to
+ * itself means nothing.
+ */
+export function findDropTarget(event: PointerEvent): LayerDragState | null {
+ const element = document.elementFromPoint(event.clientX, event.clientY) as HTMLElement | null;
+ const row = element?.closest<HTMLElement>('[data-layer-row]');
+ if (!row) return null;
+
+ const target = getRowEntity(row);
+ if (!target?.isAlive() || target.has(Selected)) return null;
+
+ const rect = row.getBoundingClientRect();
+ const offset = event.clientY - rect.top;
+
+ // A group can be dropped into as well as beside, so it gives up its middle
+ // to that. Everything else is one edge or the other.
+ let position: DropPosition;
+ if (isGroup(target)) {
+ if (offset < rect.height * 0.25) position = 'above';
+ else if (offset > rect.height * 0.75) position = 'below';
+ else position = 'inside';
+ } else {
+ position = offset < rect.height * 0.5 ? 'above' : 'below';
+ }
+
+ // Below one row is above the next, and 'above' is the only one the drop
+ // itself can express (it anchors on the row it goes in front of). The last
+ // row has no next, so it keeps its 'below'.
+ if (position === 'below') {
+ const next = nextDroppableRow(row);
+ if (next) return { target: next, position: 'above' };
+ }
+
+ return { target, position };
+}
+
+/** The first row after `row` that is not itself being dragged. */
+function nextDroppableRow(row: HTMLElement): Entity | undefined {
+ const rows = [...document.querySelectorAll<HTMLElement>('[data-layer-row]')];
+
+ for (const candidate of rows.slice(rows.indexOf(row) + 1)) {
+ const entity = getRowEntity(candidate);
+ if (entity?.isAlive() && !entity.has(Selected)) return entity;
+ }
+
+ return undefined;
+}
+
+/**
+ * Moves the selection to where the drag ended: inside the target, or in front
+ * of it among its siblings. Only the tops of the selected subtrees move —
+ * a selected node under another selected node travels with it.
+ */
+export function dropSelection(world: World, drop: LayerDragState | null): void {
+ if (!drop) return;
+
+ const editor = getDocumentEditor(world);
+ const { target, position } = drop;
+
+ const parent = position === 'inside' ? target : getParentEntity(target);
+ if (!parent) return;
+
+ const selection = new Set(world.query(Selected, Or(Geometry, Group)));
+ // Top to bottom as the column shows them, so what they were relative to
+ // each other survives the move. The query's own order means nothing.
+ const moving = columnOrder(world).filter((entity) => {
+ if (!selection.has(entity)) return false;
+ for (let ancestor = getParentEntity(entity); ancestor; ancestor = getParentEntity(ancestor)) {
+ if (selection.has(ancestor)) return false;
+ }
+ return true;
+ });
+ if (moving.length === 0) return;
+
+ // Into its own subtree is not a move, it is a loop.
+ for (const entity of moving) {
+ if (getEntityTree(world, entity).includes(parent)) return;
+ }
+
+ // Dropped into a collapsed group, the rows would land out of sight.
+ if (position === 'inside' && !parent.has(Expanded)) {
+ editor.editProperty(parent, 'expanded', true);
+ }
+
+ // The column reads bottom-up: the last child of an element is drawn on top
+ // of its siblings, so it is the topmost row. Dropping *above* a row
+ // therefore means going *after* it in the file, which is in front of
+ // whatever was written next — and the rows go in bottom one first, so each
+ // lands in front of the one before it and their order comes out as it was.
+ const anchor = position === 'inside' ? undefined : nextSibling(world, parent, target);
+
+ for (const entity of [...moving].reverse()) {
+ editor.reparent(entity, parent, anchor);
+ }
+}
+
+/** Every clip row of the scene on show, top to bottom as the column has them. */
+function columnOrder(world: World): Entity[] {
+ const scene = getActiveEntity(world);
+ if (scene === null) return [];
+
+ const ordered: Entity[] = [];
+
+ const walk = (nodes: TimelineNode[]): void => {
+ for (const node of nodes) {
+ if (node.kind === 'geometry') ordered.push(node.entity);
+ walk(node.children);
+ }
+ };
+
+ walk(buildTimelineLayers(world, scene));
+
+ return ordered;
+}
+
+/** The element written after `entity` among `parent`'s children, if any. */
+function nextSibling(world: World, parent: Entity, entity: Entity): Entity | undefined {
+ const siblings = getEntityChildren(world, parent);
+ return siblings[siblings.indexOf(entity) + 1];
+}
diff --git a/apps/web/src/components/timeline/layers/sub-item.tsx b/apps/web/src/components/timeline/layers/sub-item.tsx
index 5012cb8b..977a9358 100644
--- a/apps/web/src/components/timeline/layers/sub-item.tsx
+++ b/apps/web/src/components/timeline/layers/sub-item.tsx
@@ -3,89 +3,60 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { createMemo, Show } from 'solid-js';
-import { hasComponent } from 'bitecs';
+import { useTag, useWorld } from '@diffusionstudio/koota-solid';
+import {
+ ColorStop,
+ Effect,
+ Expanded,
+ Hovering,
+ Name,
+ Paint,
+ PaintType,
+ Selected,
+ Shadow,
+ Stroke,
+} from '@diffusionstudio/runtime';
import { Icon } from '@/components/ui/icon';
-import { useEngine } from '@/context/engine';
-import { addComponent, clearComponent, removeComponent, useEntityTag, EffectType, PaintType } from '@/components/engine';
+import { useEditor } from '@/engine/hooks';
+import { KEYFRAME_TRACK_HEIGHT } from '@/engine/timeline';
+import { effectOption } from '@/components/sidebar-right/inspector/effect-types';
import { NESTED_INDENT_PX } from './config';
+import { setRowHover } from './hover';
-import type { TimelineNode } from '@/components/engine/api/timeline-index';
-import { KEYFRAME_TRACK_HEIGHT } from '@/components/engine/timeline/config';
+import type { Entity } from 'koota';
+import type { LayerRowProps } from './layer';
-// The bitecs world's effect labels. The koota panel keeps its own
-// ("../sidebar-right/inspector/effect-types"), keyed by the runtime's enum;
-// this row goes with the rest of the bitecs timeline.
-const EFFECT_LABELS: Partial<Record<EffectType, string>> = {
- [EffectType.LAYER_BLUR]: 'Layer Blur',
- [EffectType.BRIGHTNESS]: 'Brightness',
- [EffectType.CONTRAST]: 'Contrast',
- [EffectType.GRAYSCALE]: 'Grayscale',
- [EffectType.HUE_ROTATION]: 'Hue Rotation',
- [EffectType.INVERT]: 'Invert',
- [EffectType.SATURATE]: 'Saturate',
- [EffectType.SEPIA]: 'Sepia',
+const PAINT_NAMES: Partial<Record<PaintType, string>> = {
+ [PaintType.LINEAR_GRADIENT]: 'Gradient',
+ [PaintType.RADIAL_GRADIENT]: 'Gradient',
+ [PaintType.SOLID]: 'Solid',
+ [PaintType.IMAGE]: 'Image',
+ [PaintType.VIDEO]: 'Video',
+ [PaintType.SEQUENCE]: 'Sequence',
+ [PaintType.WAVEFORM]: 'Waveform',
+ [PaintType.HTML]: 'HTML',
+ [PaintType.SURFACE]: 'Surface',
+ [PaintType.SHADER]: 'Shader',
};
-type SubItemLayerProps = {
- layer: TimelineNode;
- depth?: number;
- expanded: boolean;
- ancestorSelected: boolean;
-}
-
-export function SubItemLayer(props: SubItemLayerProps) {
- const { world } = useEngine();
+/**
+ * A row for one of a clip's parts — a fill, a stroke, a shadow, an effect —
+ * which is here only so the keyframe rows under it have something to hang
+ * from. It has no clip of its own on the canvas beside it.
+ */
+export function SubItemLayer(props: LayerRowProps) {
+ const world = useWorld();
+ const editor = useEditor();
- const c = world.components;
+ const entity = () => props.layer.entity;
- const eid = () => props.layer.eid;
-
- const depth = () => props.depth ?? 0;
- const canExpand = () => props.layer.expandable;
- const hovering = useEntityTag(c.Hovering, eid);
- const selected = useEntityTag(c.Selected, eid);
- const name = createMemo(() => {
- if (c.Name[eid()]) return c.Name[eid()];
- if (hasComponent(world, eid(), c.Stroke)) return 'Stroke';
- if (hasComponent(world, eid(), c.Shadow)) return 'Shadow';
- if (hasComponent(world, eid(), c.Paint)) {
- if (c.Paint[eid()] === PaintType.LINEAR_GRADIENT) return 'Gradient';
- if (c.Paint[eid()] === PaintType.RADIAL_GRADIENT) return 'Gradient';
- if (c.Paint[eid()] === PaintType.SOLID) return 'Solid';
- if (c.Paint[eid()] === PaintType.IMAGE) return 'Image';
- if (c.Paint[eid()] === PaintType.VIDEO) return 'Video';
- if (c.Paint[eid()] === PaintType.SEQUENCE) return 'Sequence';
- if (c.Paint[eid()] === PaintType.WAVEFORM) return 'Waveform';
- if (c.Paint[eid()] === PaintType.HTML) return 'HTML';
- if (c.Paint[eid()] === PaintType.SURFACE) return 'Surface';
- if (c.Paint[eid()] === PaintType.SHADER) return 'Shader';
- return 'Fill';
- }
- if (hasComponent(world, eid(), c.ColorStop)) return 'Stop';
- if (hasComponent(world, eid(), c.Effect)) {
- const type = c.Effect.type[eid()] as EffectType;
- return EFFECT_LABELS[type] ?? `Sub-item ${eid()}`;
- }
- return `Sub-item ${eid()}`;
- });
+ const hovering = useTag(entity, Hovering);
+ const selected = useTag(entity, Selected);
+ const name = createMemo(() => describe(entity()));
const toggleExpanded = () => {
- const eid = props.layer.eid;
- if (hasComponent(world, eid, c.Expanded)) {
- removeComponent(world, eid, c.Expanded);
- } else {
- addComponent(world, eid, c.Expanded);
- }
- }
-
- const handlePointerEnter = () => {
- clearComponent(world, c.Hovering, false);
- addComponent(world, props.layer.eid, c.Hovering, false);
- }
- const handlePointerLeave = () => {
- clearComponent(world, c.Hovering, false);
- removeComponent(world, props.layer.eid, c.Hovering, false);
- }
+ editor.editProperty(entity(), 'expanded', !entity().has(Expanded));
+ };
return (
<div
@@ -95,23 +66,24 @@ export function SubItemLayer(props: SubItemLayerProps) {
'bg-accent/70': !selected() && hovering(),
'bg-accent/40': !selected() && !hovering() && props.ancestorSelected,
}}
- onPointerEnter={handlePointerEnter}
- onPointerLeave={handlePointerLeave}
+ onPointerEnter={() => setRowHover(world, entity())}
+ onPointerLeave={() => setRowHover(world, null)}
style={{ height: KEYFRAME_TRACK_HEIGHT + 'px' }}
>
<div data-layer-label class="flex-1 min-w-0 overflow-hidden">
<div
class="flex items-center gap-0.5 w-max"
style={{
- 'padding-left': `${depth() * NESTED_INDENT_PX}px`,
+ 'padding-left': `${props.depth * NESTED_INDENT_PX}px`,
transform: 'translateX(calc(var(--layer-x, 0px) * -1))',
}}
>
<button
- disabled={!canExpand()}
+ disabled={!props.layer.expandable}
onClick={toggleExpanded}
- class="size-4 shrink-0 flex items-center justify-center overflow-clip invisible group-hover/layers:visible focus-ring rounded-sm mr-0.5">
- <Show when={canExpand()}>
+ class="size-4 shrink-0 flex items-center justify-center overflow-clip invisible group-hover/layers:visible focus-ring rounded-sm mr-0.5"
+ >
+ <Show when={props.layer.expandable}>
<Icon name={props.expanded ? "chevron-down" : "chevron-right"} class="size-6 hover:text-foreground" />
</Show>
</button>
@@ -123,3 +95,23 @@ export function SubItemLayer(props: SubItemLayerProps) {
</div>
)
}
+
+/** What the part calls itself, or what kind of part it is. */
+function describe(entity: Entity): string {
+ const name = entity.get(Name)?.value;
+ if (name) return name;
+
+ if (entity.has(Stroke)) return 'Stroke';
+ if (entity.has(Shadow)) return 'Shadow';
+ if (entity.has(ColorStop)) return 'Stop';
+
+ if (entity.has(Paint)) {
+ return PAINT_NAMES[entity.get(Paint)!.value as PaintType] ?? 'Fill';
+ }
+
+ if (entity.has(Effect)) {
+ return effectOption(entity.get(Effect)?.type).label;
+ }
+
+ return 'Sub-item';
+}
diff --git a/apps/web/src/components/timeline/timeline.tsx b/apps/web/src/components/timeline/timeline.tsx
index 294c9643..a94d28f6 100644
--- a/apps/web/src/components/timeline/timeline.tsx
+++ b/apps/web/src/components/timeline/timeline.tsx
@@ -2,180 +2,75 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
import { onCleanup, onMount } from 'solid-js';
import { toast } from 'somoto';
-import { useEngine } from '@/context/engine';
-import { secondsToFrames, getNextName, GeometryType, PaintType, createEntity, switchActiveScene, addComponent, appendChild, setComponent, resizeEntity, getAssetFile } from '@/components/engine';
-import { loadAsset } from "@/components/engine";
+import { useWorld } from '@diffusionstudio/koota-solid';
+import { FrameRate, framesToSeconds } from '@diffusionstudio/runtime';
+import { droppedFiles, importFiles } from '@/engine/asset-actions';
+import { insertAsset } from '@/engine/insert-asset';
+import { useLibrary } from '@/engine/library';
import { useTimeline } from '@/context/timeline';
+import { ASSET_DRAG_TYPE } from '@/components/sidebar-left/folder-item';
-import type { Asset } from '@/components/engine/db';
-import type { Transcript } from '@diffusionstudio/api-contract';
+import type { Asset } from '@diffusionstudio/assets';
+/**
+ * The timeline's canvas. What is drawn on it is the timeline system's
+ * business (see `@/engine/timeline`); this is the element it draws on and
+ * what dropping an asset onto it means.
+ */
export function Timeline() {
+ const world = useWorld();
const timeline = useTimeline();
- const { world, camera } = useEngine();
-
-
- const c = world.components;
-
- let root!: HTMLDivElement;
+ const library = useLibrary();
+ onMount(() => timeline.attachCanvas());
+ onCleanup(() => timeline.detachCanvas());
- onMount(() => {
- timeline.attachCanvas();
- });
-
- onCleanup(() => {
- timeline.detachCanvas();
- });
-
- const handleDropEvent = async (event: DragEvent) => {
+ /**
+ * Assets dropped on the timeline start where they were dropped, unlike
+ * ones dropped on the canvas, which start at the playhead: the whole point
+ * of aiming at a place on the timeline is to say when.
+ */
+ const handleDrop = async (event: DragEvent) => {
event.preventDefault();
event.stopPropagation();
- const dropFrame = Math.max(0, timeline.clientToFrame(event.clientX));
-
- const droppedAssets: Asset[] = [];
- const assetIds = event.dataTransfer?.getData('application/x-asset-id')?.split(',').filter(Boolean);
- if (assetIds && assetIds.length > 0) {
- for (const id of assetIds) {
- const asset = world.assets.get(id);
- if (asset) droppedAssets.push(asset);
- }
- } else {
- const items = Array.from(event.dataTransfer?.items ?? []).filter(item => item.kind === 'file');
- for (const item of items) {
- try {
- droppedAssets.push(await loadAsset(world, item));
- } catch (e) {
- toast('Failed to import assets', {
- description: (e as Error).message,
- });
- }
- }
- }
-
- if (droppedAssets.length === 0) return;
-
- let activeScene = world.timelineIndex().root;
- if (activeScene === null) {
- const sized = droppedAssets.find(a => a.type === 'IMAGE' || a.type === 'VIDEO' || a.type === 'SEQUENCE');
- const sceneWidth = sized ? Math.round(sized.width) : 1920;
- const sceneHeight = sized ? Math.round(sized.height) : 1080;
- const worldX = Math.round(-sceneWidth / 2);
- const worldY = Math.round(-sceneHeight / 2);
-
- const sid = world.history.transaction('Add scene', () => {
- const sceneEid = createEntity(world);
- setComponent(world, sceneEid, c.Geometry, GeometryType.RECT);
- addComponent(world, sceneEid, c.Scene);
- addComponent(world, sceneEid, c.ClipsContent);
- setComponent(world, sceneEid, c.Playback, {});
- setComponent(world, sceneEid, c.Name, getNextName(world, 'Scene'));
- setComponent(world, sceneEid, c.Position, { x: worldX, y: worldY });
- resizeEntity(world, sceneEid, { width: sceneWidth, height: sceneHeight });
- setComponent(world, sceneEid, c.Color, 0x000000);
-
- const fillEid = createEntity(world);
- setComponent(world, fillEid, c.Paint, PaintType.SOLID);
- setComponent(world, fillEid, c.Color, 0x000000);
- appendChild(world, fillEid, sceneEid);
- return sceneEid;
- });
+ const lib = library();
+ if (!lib) return;
- switchActiveScene(world, sid);
- camera.centerRect({ x: worldX, y: worldY, width: sceneWidth, height: sceneHeight });
- activeScene = sid;
- }
-
- const sceneEid = activeScene;
- const delay = dropFrame;
+ const fps = world.get(FrameRate)?.value ?? 30;
+ const start = framesToSeconds(Math.max(0, timeline.clientToFrame(event.clientX)), fps);
- const placeAsset = async (asset: Asset) => {
- // Resolve async transcript data before opening the transaction so the
- // mutations stay synchronous — concurrent placeAsset calls (Promise.all)
- // must not interleave inside a shared transaction.
- let transcriptTrim: { start: number; end: number } | null = null;
- if (asset.type === 'TRANSCRIPT') {
- const file = await getAssetFile(asset);
- const transcript = JSON.parse(await file.text()) as Transcript;
- const lastWord = transcript.at(-1)?.words.at(-1);
- const firstWord = transcript.at(0)?.words.at(0);
- transcriptTrim = {
- start: secondsToFrames(firstWord?.start),
- end: secondsToFrames(lastWord?.end),
- };
+ const place = (asset: Asset) => {
+ if (!insertAsset(world, asset, { start })) {
+ toast("Nothing to insert into", { description: "Open a scene first." });
}
-
- world.history.transaction('Add clip', () => {
- const eid = createEntity(world);
- setComponent(world, eid, c.Name, asset.name);
- setComponent(world, eid, c.Delay, delay);
-
- if (asset.type === 'IMAGE' || asset.type === 'VIDEO' || asset.type === 'SEQUENCE') {
- // Author dimensions from the source media; spatial layout is centered by
- // the transform system since no Position is set here.
- const width = Math.round(asset.width);
- const height = Math.round(asset.height);
- resizeEntity(world, eid, { width, height });
- setComponent(world, eid, c.KeepAspectRatio, { width, height });
- }
-
- if (asset.type === 'TRANSCRIPT') {
- setComponent(world, eid, c.Geometry, GeometryType.TEXT);
- setComponent(world, eid, c.Caption, {});
- if (transcriptTrim) setComponent(world, eid, c.Trim, transcriptTrim);
- } else {
- setComponent(world, eid, c.Geometry, GeometryType.RECT);
-
- // Other assets need a paint component
- let paint = PaintType.IMAGE;
- if (asset.type === 'SEQUENCE') paint = PaintType.SEQUENCE;
- if (asset.type === 'VIDEO') paint = PaintType.VIDEO;
- if (asset.type === 'AUDIO') paint = PaintType.WAVEFORM;
- const fillEid = createEntity(world);
- setComponent(world, fillEid, c.Paint, paint);
- setComponent(world, fillEid, c.AssetId, asset.id);
-
- if (asset.type === 'AUDIO') {
- addComponent(world, fillEid, c.Hidden);
- }
-
- appendChild(world, fillEid, eid);
- }
-
- if (asset.type === 'AUDIO') {
- addComponent(world, eid, c.Audio);
- resizeEntity(world, eid, { width: 500, height: 150 });
- }
-
- if (asset.type === 'AUDIO' || asset.type === 'TRANSCRIPT') {
- setComponent(world, eid, c.AssetId, asset.id);
- }
-
- appendChild(world, eid, sceneEid);
- });
};
- await Promise.all(droppedAssets.map(asset => placeAsset(asset)));
- }
+ const ids = event.dataTransfer?.getData(ASSET_DRAG_TYPE)?.split(',').filter(Boolean) ?? [];
+ for (const id of ids) {
+ const asset = lib.get(id);
+ if (asset) place(asset);
+ }
+
+ const files = droppedFiles(event);
+ if (files.length) {
+ for (const asset of await importFiles(lib, files, '')) place(asset);
+ }
+ };
const handleDragOver = (event: DragEvent) => {
event.preventDefault();
event.dataTransfer!.dropEffect = 'copy';
- }
+ };
return (
- <div
- ref={root}
- class="relative size-full"
- >
+ <div class="relative size-full">
<canvas
class="absolute inset-0"
id="timeline-canvas"
- on:drop={handleDropEvent}
+ on:drop={handleDrop}
on:dragover={handleDragOver}
/>
</div>
diff --git a/apps/web/src/context/timeline.tsx b/apps/web/src/context/timeline.tsx
index cd19cd44..f6bfac47 100644
--- a/apps/web/src/context/timeline.tsx
+++ b/apps/web/src/context/timeline.tsx
@@ -3,15 +3,16 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { createContext, useContext, type JSX } from 'solid-js';
+import { useWorld } from '@diffusionstudio/koota-solid';
import { assert } from '@/utils';
-import { createTimeline } from '@/components/engine/timeline';
+import { createTimelineController } from '@/engine/timeline';
-type TimelineContextValue = ReturnType<typeof createTimeline>;
+type TimelineContextValue = ReturnType<typeof createTimelineController>;
const TimelineContext = createContext<TimelineContextValue>();
export function TimelineProvider(props: { children: JSX.Element }) {
- const timeline = createTimeline();
+ const timeline = createTimelineController(useWorld());
return (
<TimelineContext.Provider value={timeline}>
diff --git a/apps/web/src/engine/asset-actions.ts b/apps/web/src/engine/asset-actions.ts
index 62fd35d4..df1da213 100644
--- a/apps/web/src/engine/asset-actions.ts
+++ b/apps/web/src/engine/asset-actions.ts
@@ -9,6 +9,7 @@ import { toast } from "somoto";
import { importFiles as importFilesInto, pickFiles, saveAssetAs as saveAs } from "@diffusionstudio/assets";
import { AssetId, reactToAssetChange } from "@diffusionstudio/runtime";
import { insertAsset } from "./insert-asset";
+import { forgetPoster } from "./timeline/media";
import type { SequenceAsset } from "@diffusionstudio/assets";
import type { World } from "koota";
@@ -55,7 +56,10 @@ export async function replaceAssetSource(library: AssetLibrary, asset: Asset): P
const path = file ? library.fs.pathOf?.(file) : null;
if (!path) return null;
try {
- return await library.relink(asset, path);
+ const relinked = await library.relink(asset, path);
+ // The picture the timeline is showing is of the file it used to be.
+ forgetPoster(asset.id);
+ return relinked;
} catch (error) {
toast.error("Failed to replace", { description: (error as Error).message });
return null;
diff --git a/apps/web/src/engine/create-engine.ts b/apps/web/src/engine/create-engine.ts
index f15e77e8..b9c034e6 100644
--- a/apps/web/src/engine/create-engine.ts
+++ b/apps/web/src/engine/create-engine.ts
@@ -7,6 +7,7 @@ import { hudSystem } from './hud';
import { createSignal, type Accessor, type Setter } from 'solid-js';
import { AssetSelection, Hud, Keys, Pointer, PointerEvents, ProjectConfig, SnapLines } from './traits';
import { inputSystem } from './input/input-system';
+import { timelineSystem, TimelineSurface } from './timeline';
import { shortcutSystem } from './input/shortcuts';
import type { RuntimeWorld } from '@diffusionstudio/runtime';
@@ -47,7 +48,7 @@ class Engine {
public constructor(projectId: string, options: EngineOptions = {}) {
this.world = createRuntimeWorld(projectId);
- this.world.add(Pointer, Keys, SnapLines, Hud, PointerEvents, AssetSelection, ProjectConfig);
+ this.world.add(Pointer, Keys, SnapLines, Hud, PointerEvents, AssetSelection, ProjectConfig, TimelineSurface);
this.unsubscribe.push(
this.world.onAdd(ChildOf('*'), () => (this.interactiveDirty = true)),
@@ -185,6 +186,7 @@ class Engine {
transformSystem(this.world);
renderSystem(this.world);
hudSystem(this.world);
+ timelineSystem(this.world);
}
public start(): void {
diff --git a/apps/web/src/engine/editor.ts b/apps/web/src/engine/editor.ts
index e133b834..563d2b91 100644
--- a/apps/web/src/engine/editor.ts
+++ b/apps/web/src/engine/editor.ts
@@ -633,7 +633,13 @@ export class DocumentEditor {
*/
public reparent(entity: Entity, parent: Entity, anchor?: Entity): boolean {
if (!entity.get(Source)?.value || !parent.get(Source)?.value) return false;
- if (getParentEntity(entity) === parent && anchor === undefined) return false;
+ // Appending where it already is, last, is the one move that changes
+ // nothing. Appending from anywhere else in the same parent is a real
+ // move — it is how a node is sent to the end of its siblings.
+ if (getParentEntity(entity) === parent && anchor === undefined) {
+ const siblings = getEntityChildren(this.world, parent);
+ if (siblings[siblings.length - 1] === entity) return false;
+ }
// Checked here rather than left to the document: a move into itself is
// a no-op there (nothing to report), and one into its own subtree is
// caught only after the node has already left the parent it had.
diff --git a/apps/web/src/engine/hooks/index.ts b/apps/web/src/engine/hooks/index.ts
index ca53ede0..b8c0d6a7 100644
--- a/apps/web/src/engine/hooks/index.ts
+++ b/apps/web/src/engine/hooks/index.ts
@@ -9,3 +9,4 @@ export * from './use-tool';
export * from './use-selection';
export * from './use-asset-selection';
export * from './use-derived';
+export * from './use-timeline-index';
diff --git a/apps/web/src/engine/hooks/use-timeline-index.ts b/apps/web/src/engine/hooks/use-timeline-index.ts
new file mode 100644
index 00000000..cd5f9188
--- /dev/null
+++ b/apps/web/src/engine/hooks/use-timeline-index.ts
@@ -0,0 +1,54 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { buildTimelineLayers, getActiveEntity } from '@diffusionstudio/runtime';
+import { useWorld } from '@diffusionstudio/koota-solid';
+
+import { useDerived } from './use-derived';
+
+import type { TimelineIndexValue, TimelineNode } from '@diffusionstudio/runtime';
+import type { Accessor } from 'solid-js';
+
+/**
+ * The rows of the scene on show, for the DOM column that labels them. The
+ * canvas builds its own copy as it draws; this one is sampled once per engine
+ * tick and only reported when the shape of the tree actually changed, since
+ * every rebuild is a fresh set of objects and re-rendering the column on
+ * every frame would be the whole cost of having one.
+ */
+export function useTimelineIndex(): Accessor<TimelineIndexValue> {
+ const world = useWorld();
+
+ return useDerived<TimelineIndexValue>(
+ () => {
+ const root = getActiveEntity(world);
+ return { root, layers: root === null ? [] : buildTimelineLayers(world, root) };
+ },
+ (prev, next) => prev.root === next.root && sameLayers(prev.layers, next.layers),
+ );
+}
+
+/**
+ * Whether two builds describe the same rows. Compared field by field rather
+ * than by a serialized key: no allocation, and it stops at the first row that
+ * differs, which is what usually happens when anything changed at all.
+ */
+function sameLayers(a: TimelineNode[], b: TimelineNode[]): boolean {
+ if (a.length !== b.length) return false;
+
+ for (let i = 0; i < a.length; i++) {
+ const left = a[i]!;
+ const right = b[i]!;
+
+ if (
+ left.entity !== right.entity
+ || left.kind !== right.kind
+ || left.expanded !== right.expanded
+ || left.expandable !== right.expandable
+ || !sameLayers(left.children, right.children)
+ ) return false;
+ }
+
+ return true;
+}
diff --git a/apps/web/src/engine/input/cursor.ts b/apps/web/src/engine/input/cursor.ts
index c279773e..8b3ecebe 100644
--- a/apps/web/src/engine/input/cursor.ts
+++ b/apps/web/src/engine/input/cursor.ts
@@ -9,8 +9,8 @@
* cursor goes through here (the camera controller included), so the last
* writer always wins and no one has to know who wrote it before.
*
- * The timeline keeps its own copy in `components/engine/utils/cursor.ts`
- * (trim cursors, the old world's canvas); this one is the stage's.
+ * This one is the stage's; the timeline sets the cursor on its own canvas
+ * through `@/hooks/use-cursor`, which is where the trim cursors live.
*/
import { RenderSurface } from '@diffusionstudio/runtime';
diff --git a/apps/web/src/engine/timeline/config.ts b/apps/web/src/engine/timeline/config.ts
new file mode 100644
index 00000000..bd65c127
--- /dev/null
+++ b/apps/web/src/engine/timeline/config.ts
@@ -0,0 +1,47 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+export const MIN_CLIP_HEIGHT = 28;
+export const MAX_CLIP_HEIGHT = 116;
+export const KEYFRAME_TRACK_HEIGHT = 32;
+export const TARGET_MAJOR_TICK_DISTANCE = 160;
+
+export const TIMELINE_RESOLUTION_RANGE = [0.03, 120] as const;
+export const DEFAULT_TIMELINE_RESOLUTION = 1 / 0.7;
+export const DEFAULT_CLIP_HEIGHT = 40;
+
+export const TIMELINE_PADDING_LEFT = 8;
+
+export const WHEEL_LINE_HEIGHT = 16;
+export const WHEEL_PAGE_HEIGHT = 600;
+
+export const ZOOM_DELTA_CLAMP = 50;
+export const ZOOM_SENSITIVITY = 0.01;
+
+export const SCROLL_X_SENSITIVITY = 2;
+
+/** How near, in pixels, a dragged edge has to be to snap to something. */
+export const SNAP_DISTANCE = 10;
+
+/** How wide a clip's trim handle is, at most. */
+export const TRIM_HANDLE_WIDTH = 10;
+
+export const CLICK_DISTANCE_THRESHOLD = 5;
+export const DOUBLE_CLICK_THRESHOLD = 500;
+
+export const RULER_HEIGHT = 36;
+export const RULER_TICK_HEIGHT_MAJOR = 9;
+export const RULER_TICK_HEIGHT_MINOR = 3;
+export const RULER_LABEL_Y = 18;
+
+export const CLIP_CORNER_RADIUS = 4;
+export const CLIP_FONT = '11px Inter';
+export const CLIP_LABEL_X = 6;
+export const CLIP_LABEL_Y = 10;
+export const CLIP_LABEL_HEIGHT = 20;
+
+export const CLIP_BREAKPOINTS = {
+ xs: 32,
+ sm: 40,
+}
diff --git a/apps/web/src/engine/timeline/constants.ts b/apps/web/src/engine/timeline/constants.ts
new file mode 100644
index 00000000..316338d8
--- /dev/null
+++ b/apps/web/src/engine/timeline/constants.ts
@@ -0,0 +1,131 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { secondsToFrames } from "@diffusionstudio/runtime";
+
+export const RULER_INTERVALS = [
+ {
+ numerator: secondsToFrames(600),
+ denominator: 10,
+ },
+ {
+ numerator: secondsToFrames(300),
+ denominator: 5,
+ },
+ {
+ numerator: secondsToFrames(120),
+ denominator: 4,
+ },
+ {
+ numerator: secondsToFrames(60),
+ denominator: 6,
+ },
+ {
+ numerator: secondsToFrames(30),
+ denominator: 10,
+ },
+ {
+ numerator: secondsToFrames(10),
+ denominator: 10,
+ },
+ {
+ numerator: secondsToFrames(5),
+ denominator: 10,
+ },
+ {
+ numerator: secondsToFrames(3),
+ denominator: 10,
+ },
+ {
+ numerator: secondsToFrames(2),
+ denominator: 10,
+ },
+ {
+ numerator: secondsToFrames(1),
+ denominator: 10,
+ },
+ {
+ numerator: 15,
+ denominator: 5,
+ },
+ {
+ numerator: 10,
+ denominator: 10,
+ },
+ {
+ numerator: 5,
+ denominator: 5,
+ },
+];
+
+// window.getComputedStyle(document.body).getPropertyValue('--background')
+
+export const COLORS = {
+ background: {
+ default: '#1C1C1C',
+ muted: '#292929',
+ accent: '#292929',
+ },
+ border: {
+ darker: '#1C1C1C',
+ input: '#3E3F41',
+ ring: '#008CFF',
+ scrubber: '#F43535', // ambient red
+ },
+ ruler: {
+ tick: 'rgba(53, 53, 53, 1)',
+ text: 'rgba(75, 75, 75, 1)',
+ },
+ clip: {
+ group: {
+ background: '#40464F',
+ primary: '#545D67',
+ foreground: '#EDF0F3',
+ },
+ video: {
+ background: '#0F3C8A',
+ primary: '#70A7FF', // Waveform
+ foreground: '#CCE0FF', // Label
+ },
+ audio: {
+ background: '#004732',
+ primary: '#0DBF8A',
+ foreground: '#CBFAED',
+ },
+ caption: {
+ background: '#7B1E5A',
+ primary: '#AA317B', // Word background
+ foreground: '#F9DCED',
+ },
+ image: {
+ background: '#A55912',
+ foreground: '#F9F2EC',
+ },
+ text: {
+ background: '#303E4F',
+ foreground: '#E7EFF9',
+ },
+ shape: {
+ background: '#933325',
+ foreground: '#FAEDEB',
+ },
+ scene: {
+ background: '#066284',
+ primary: '#5AB9DD',
+ foreground: '#DBEAF0',
+ },
+ mask: {
+ background: '#5455DE',
+ foreground: '#EAE8FC',
+ },
+ adjustment: {
+ background: '#4827B0',
+ foreground: '#E7DFFB',
+ },
+ html: {
+ background: '#2B525F',
+ foreground: '#E7F4F9',
+ },
+ },
+} as const;
diff --git a/apps/web/src/engine/timeline/controller.ts b/apps/web/src/engine/timeline/controller.ts
new file mode 100644
index 00000000..386f1ee3
--- /dev/null
+++ b/apps/web/src/engine/timeline/controller.ts
@@ -0,0 +1,275 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * The DOM around the timeline: which canvas it draws on, how big that is,
+ * and what the wheel does to it. Everything it changes is view state on the
+ * scene (see `./view`), which the draw pass reads back a frame later — the
+ * controller itself never paints.
+ */
+
+import { assert, clamp } from '@/utils';
+import { createPointer } from './pointer';
+import { TimelineSurface } from './surface';
+import {
+ getResolution,
+ getScrollX,
+ getScrollY,
+ getTimelineScene,
+ pixelsToFrames,
+ setResolution,
+ setScrollX,
+ setScrollY,
+ updateTimelineTransform,
+} from './view';
+import {
+ SCROLL_X_SENSITIVITY,
+ TIMELINE_RESOLUTION_RANGE,
+ WHEEL_LINE_HEIGHT,
+ WHEEL_PAGE_HEIGHT,
+ ZOOM_DELTA_CLAMP,
+ ZOOM_SENSITIVITY,
+} from './config';
+import { getFrameRate } from './view';
+
+import type { Entity, World } from 'koota';
+
+export type TimelineController = ReturnType<typeof createTimelineController>;
+
+export function createTimelineController(world: World) {
+ const surface = world.get(TimelineSurface)!;
+
+ const pointer = createPointer({
+ get marquee() { return surface.marquee; },
+ get canvas() { return surface.canvas; },
+ get ctx() { return surface.ctx; },
+ });
+
+ let layersEl: HTMLElement | null = null;
+ let layersViewportEl: HTMLElement | null = null;
+ // How far the row labels have been scrolled sideways, in pixels. A DOM
+ // concern rather than the scene's: it is the same for every scene.
+ let layerScrollX = 0;
+
+ /** Runs `action` against the scene on show, if there is one. */
+ const withScene = (action: (scene: Entity) => void): void => {
+ const scene = getTimelineScene(world);
+ if (scene !== null) action(scene);
+ };
+
+ const applyResize = (): void => {
+ const { canvas } = surface;
+ const parent = canvas?.parentElement;
+ if (!parent || !canvas) return;
+
+ surface.layout = parent.getBoundingClientRect();
+ applyScroll();
+
+ const dpr = window.devicePixelRatio;
+
+ canvas.style.width = `${surface.layout.width}px`;
+ canvas.style.height = `${surface.layout.height}px`;
+
+ const width = Math.floor(surface.layout.width * dpr);
+ const height = Math.floor(surface.layout.height * dpr);
+ if (canvas.width === width && canvas.height === height) return;
+
+ canvas.width = width;
+ canvas.height = height;
+ };
+
+ const resize = (): void => {
+ applyResize();
+ withScene((scene) => updateTimelineTransform(world, scene));
+ };
+
+ const observer = new ResizeObserver(resize);
+
+ const layerObserver = new ResizeObserver(() => {
+ applyScroll();
+ withScene((scene) => updateTimelineTransform(world, scene));
+ });
+
+ /**
+ * Moves the row labels to match the scroll the canvas is drawn at. They
+ * are DOM, so they cannot be drawn scrolled — they are translated instead,
+ * and the scroll is clamped to what there is to scroll through.
+ */
+ const applyScroll = (): void => {
+ if (!layersEl || !layersViewportEl) return;
+
+ withScene((scene) => {
+ const scrollY = clamp(getScrollY(world, scene), 0, Math.max(0, layersEl!.scrollHeight - layersViewportEl!.clientHeight));
+ setScrollY(world, scene, scrollY);
+ layersEl!.style.transform = `translateY(${-scrollY}px)`;
+ });
+
+ // The labels scroll sideways only as far as the widest one overflows.
+ let maxScrollX = 0;
+ for (const label of layersEl.querySelectorAll<HTMLElement>('[data-layer-label]')) {
+ maxScrollX = Math.max(maxScrollX, label.scrollWidth - label.clientWidth);
+ }
+ layerScrollX = clamp(layerScrollX, 0, maxScrollX);
+ layersEl.style.setProperty('--layer-x', `${layerScrollX}px`);
+ };
+
+ /**
+ * One axis at a time, in the order the gesture is most likely to have
+ * meant: zoom while a modifier is held, then whichever of the two scroll
+ * axes the wheel moved further in.
+ */
+ const handleWheel = (event: WheelEvent): void => {
+ event.preventDefault();
+
+ withScene((scene) => {
+ const { deltaX, deltaY } = normalizeWheel(event);
+ const resolution = getResolution(world, scene);
+ const scrollX = getScrollX(world, scene);
+
+ if (event.ctrlKey || event.metaKey) {
+ const rect = surface.canvas?.getBoundingClientRect();
+ const anchor = event.clientX - (rect?.left ?? 0);
+
+ const delta = clamp(deltaY, -ZOOM_DELTA_CLAMP, ZOOM_DELTA_CLAMP);
+ const next = clamp(
+ resolution * Math.exp(-delta * ZOOM_SENSITIVITY),
+ 1 / TIMELINE_RESOLUTION_RANGE[1],
+ 1 / TIMELINE_RESOLUTION_RANGE[0],
+ );
+
+ // The frame under the pointer stays under it: the scroll takes
+ // up whatever the change in scale moved it by.
+ setResolution(world, scene, next);
+ setScrollX(world, scene, scrollX + anchor / resolution - anchor / next);
+ } else if (Math.abs(deltaX) > Math.abs(deltaY)) {
+ setScrollX(world, scene, scrollX + (deltaX * SCROLL_X_SENSITIVITY) / resolution);
+ } else {
+ setScrollY(world, scene, getScrollY(world, scene) + deltaY);
+ applyScroll();
+ }
+
+ updateTimelineTransform(world, scene);
+ });
+ };
+
+ /** The wheel over the row labels, which scroll but do not zoom. */
+ const scroll = (event: WheelEvent): void => {
+ event.preventDefault();
+
+ withScene((scene) => {
+ const { deltaX, deltaY } = normalizeWheel(event);
+
+ if (Math.abs(deltaX) > Math.abs(deltaY)) {
+ layerScrollX += deltaX;
+ } else {
+ setScrollY(world, scene, getScrollY(world, scene) + deltaY);
+ }
+
+ applyScroll();
+ updateTimelineTransform(world, scene);
+ });
+ };
+
+ const clientToFrame = (clientX: number): number => {
+ const scene = getTimelineScene(world);
+ if (scene === null) return 0;
+
+ const resolution = getResolution(world, scene);
+ const rect = surface.canvas?.getBoundingClientRect();
+
+ return pixelsToFrames(clientX - (rect?.left ?? 0) + getScrollX(world, scene) * resolution, resolution);
+ };
+
+ const clientToTime = (clientX: number): number => clientToFrame(clientX) / getFrameRate(world);
+
+ const setMinimized = (minimized: boolean): void => {
+ surface.minimized = minimized;
+ };
+
+ const attachCanvas = (): void => {
+ const canvas = document.getElementById('timeline-canvas') as HTMLCanvasElement | null;
+ assert(canvas, 'Timeline canvas must be defined');
+
+ const parent = canvas.parentElement;
+ assert(parent, 'Timeline canvas must have a parent element');
+
+ surface.canvas = canvas;
+ surface.ctx = canvas.getContext('2d');
+ surface.layout = parent.getBoundingClientRect();
+
+ observer.observe(parent);
+
+ canvas.addEventListener('wheel', handleWheel);
+ canvas.addEventListener('pointerdown', pointer.down, { passive: true });
+
+ applyResize();
+ };
+
+ const detachCanvas = (): void => {
+ observer.disconnect();
+
+ surface.canvas?.removeEventListener('wheel', handleWheel);
+ surface.canvas?.removeEventListener('pointerdown', pointer.down);
+
+ // Dropped rather than kept stale: the draw pass no-ops until another
+ // canvas is attached.
+ surface.canvas = null;
+ surface.ctx = null;
+ };
+
+ /**
+ * The row labels and the pointer, which outlive the canvas: the timeline
+ * can be collapsed and reopened without the rows going anywhere.
+ */
+ const mount = (): void => {
+ const layers = document.querySelector<HTMLElement>('[data-timeline-layers]');
+ assert(layers, 'Timeline layers element must be defined');
+
+ const viewport = document.querySelector<HTMLElement>('[data-timeline-layers-viewport]');
+ assert(viewport, 'Timeline layers viewport element must be defined');
+
+ layersEl = layers;
+ layersViewportEl = viewport;
+ surface.pointer = pointer;
+
+ layerObserver.observe(layers);
+
+ // On the body, so a drag that leaves the canvas still finishes.
+ document.body.addEventListener('pointermove', pointer.move, { passive: true });
+ document.body.addEventListener('pointerup', pointer.up, { passive: true });
+ };
+
+ const unmount = (): void => {
+ layerObserver.disconnect();
+ surface.pointer = null;
+
+ document.body.removeEventListener('pointermove', pointer.move);
+ document.body.removeEventListener('pointerup', pointer.up);
+ };
+
+ return {
+ mount,
+ unmount,
+ attachCanvas,
+ detachCanvas,
+ scroll,
+ clientToFrame,
+ clientToTime,
+ setMinimized,
+ };
+}
+
+/**
+ * Wheel deltas in the units the event says they are in. A line or a page is
+ * whatever the platform decides; pixels are the only comparable unit.
+ */
+function normalizeWheel(event: WheelEvent): { deltaX: number; deltaY: number } {
+ const scale = event.deltaMode === WheelEvent.DOM_DELTA_LINE
+ ? WHEEL_LINE_HEIGHT
+ : event.deltaMode === WheelEvent.DOM_DELTA_PAGE
+ ? WHEEL_PAGE_HEIGHT
+ : 1;
+
+ return { deltaX: event.deltaX * scale, deltaY: event.deltaY * scale };
+}
diff --git a/apps/web/src/engine/timeline/drag.ts b/apps/web/src/engine/timeline/drag.ts
new file mode 100644
index 00000000..036dcede
--- /dev/null
+++ b/apps/web/src/engine/timeline/drag.ts
@@ -0,0 +1,201 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * Moving and trimming clips. A gesture lives on the entities it touches — the
+ * snapshot traits say a drag is in flight and what it started from — rather
+ * than in a variable here, so a clip scrolled off screen and back is still
+ * being dragged, and so the drag survives the frame it began on.
+ *
+ * Every write goes through the editor's time props, so dragging a clip in the
+ * timeline is the same edit as typing its start into the inspector.
+ */
+
+import {
+ AdjustmentLayer,
+ ClipDragOrigin,
+ Computed,
+ Geometry,
+ Group,
+ KeyframeDragOrigin,
+ Selected,
+ Start,
+ TrimDragOrigin,
+ findAssetDuration,
+ store,
+} from '@diffusionstudio/runtime';
+import { Not, Or } from 'koota';
+
+import { clamp } from '@/utils';
+import { resolveSequentialOverlaps } from '../overlap';
+import { moveEntityTo, trimIn, trimOut } from '../timing';
+import { findSnapDelta, findSnapFrame } from './snapping';
+import { framesToPixels, pixelsToFrames } from './view';
+
+import type { Entity, World } from 'koota';
+import type { TimelineSurfaceState } from './surface';
+
+const NODES = Or(Geometry, Group, AdjustmentLayer);
+
+/** Which edge of a clip a trim is holding. */
+export type TrimEdge = 'in' | 'out';
+
+/**
+ * Opens and closes the gestures in flight, once a frame before anything is
+ * drawn.
+ *
+ * Closing is what has to happen here rather than where the drag was applied:
+ * a gesture ends when the pointer is let go, which is not an event any clip
+ * receives — the clip only ever hears that it is still being dragged.
+ */
+export function updateDragGestures(world: World, surface: TimelineSurfaceState): void {
+ const position = surface.pointer?.position;
+ const dragging = !!position && position.state !== 'idle';
+
+ if (!dragging) {
+ endGesture(world, ClipDragOrigin);
+ endGesture(world, TrimDragOrigin);
+ // Keyframes have no sequence to settle: they sit where they are put.
+ for (const keyframe of world.query(KeyframeDragOrigin)) keyframe.remove(KeyframeDragOrigin);
+ return;
+ }
+
+ // A drag of a selected clip is a drag of the selection, so the rest of it
+ // is snapshotted the moment the first clip starts moving. Done here, once,
+ // rather than per clip: a clip scrolled out of view is not drawn, and it
+ // would otherwise be left behind by the drag.
+ if (world.query(NODES, ClipDragOrigin, Selected).length > 0) {
+ for (const entity of world.query(NODES, Selected, Not(ClipDragOrigin))) {
+ beginClipDrag(world, entity);
+ }
+ }
+}
+
+/**
+ * Ends whichever gesture `origin` marks: the snapshots come off, and the
+ * sequences the clips landed in are settled around them (the clips that moved
+ * win, and their neighbours give way).
+ */
+function endGesture(world: World, origin: typeof ClipDragOrigin | typeof TrimDragOrigin): void {
+ const moved = [...world.query(NODES, origin)];
+ if (moved.length === 0) return;
+
+ for (const entity of moved) entity.remove(origin);
+
+ resolveSequentialOverlaps(world, moved);
+}
+
+/** Notes where `entity` is, so the frames of the drag can be measured from it. */
+export function beginClipDrag(world: World, entity: Entity): void {
+ const computed = store(world, Computed);
+ const eid = entity.id();
+
+ entity.add(ClipDragOrigin);
+ entity.set(ClipDragOrigin, {
+ authored: entity.get(Start)?.value ?? 0,
+ start: computed.start[eid] ?? 0,
+ end: computed.end[eid] ?? 0,
+ });
+}
+
+/**
+ * Places `entity` at where it started plus how far the pointer has come,
+ * pulled to a snap if one is near.
+ */
+export function applyClipDrag(
+ world: World,
+ surface: TimelineSurfaceState,
+ entity: Entity,
+ resolution: number,
+): void {
+ const origin = entity.get(ClipDragOrigin)!;
+ const offset = pixelsToFrames(draggedPixels(surface), resolution);
+
+ // One snap for the whole drag, found from every clip in it, so clips
+ // dragged together stay the same distance apart.
+ const snap = findSnapDelta(world, resolution, offset);
+ if (snap) surface.snapX = framesToPixels(snap.frame, resolution);
+
+ moveEntityTo(world, entity, origin.start + offset - (snap?.delta ?? 0));
+}
+
+/** Notes where `entity`'s edges are, so a trim can be measured from them. */
+export function beginTrim(world: World, entity: Entity): void {
+ const computed = store(world, Computed);
+ const eid = entity.id();
+
+ entity.add(TrimDragOrigin);
+ entity.set(TrimDragOrigin, {
+ start: computed.start[eid] ?? 0,
+ end: computed.end[eid] ?? 0,
+ });
+}
+
+/**
+ * Moves the edge being held to where the pointer has taken it, within what
+ * the clip can actually do: never past its other edge, and never past the end
+ * of what it has to play.
+ */
+export function applyTrim(
+ world: World,
+ surface: TimelineSurfaceState,
+ entity: Entity,
+ edge: TrimEdge,
+ resolution: number,
+): void {
+ const origin = entity.get(TrimDragOrigin)!;
+ const offset = pixelsToFrames(draggedPixels(surface), resolution);
+
+ const [min, max] = trimBounds(world, entity, edge, origin);
+ const wanted = (edge === 'in' ? origin.start : origin.end) + offset;
+
+ // Snapped only where the snap is somewhere the edge could have gone
+ // anyway; otherwise it would look like it stuck and then slipped.
+ const snapped = findSnapFrame(world, resolution, wanted);
+ const frame = clamp(snapped !== null && snapped > min && snapped < max ? snapped : wanted, min, max);
+
+ if (snapped !== null && frame === snapped) surface.snapX = framesToPixels(frame, resolution);
+
+ if (edge === 'in') trimIn(world, entity, frame);
+ else trimOut(world, entity, frame);
+}
+
+/**
+ * How far the edge can go. Each clip is its own row, so a neighbour is no
+ * constraint — only the clip's other edge, and how much source there is left
+ * to show at this end.
+ */
+function trimBounds(
+ world: World,
+ entity: Entity,
+ edge: TrimEdge,
+ origin: { start: number; end: number },
+): [min: number, max: number] {
+ let min = edge === 'in' ? 0 : origin.start + 1;
+ let max = edge === 'in' ? origin.end - 1 : Number.POSITIVE_INFINITY;
+
+ const duration = findAssetDuration(world, entity);
+ if (duration === null) return [min, max];
+
+ const computed = entity.get(Computed);
+ const rate = computed?.playbackRate || 1;
+ // The scene frame the source starts at, and the one it runs out at.
+ const sourceStart = computed?.origin ?? 0;
+
+ if (edge === 'in') min = Math.max(min, sourceStart);
+ else max = Math.min(max, sourceStart + duration / rate);
+
+ return [min, max];
+}
+
+/** How far the pointer has come since the press, in pixels. */
+function draggedPixels(surface: TimelineSurfaceState): number {
+ const position = surface.pointer?.position;
+ return position && position.state !== 'idle' ? position.deltaX : 0;
+}
+
+/** Whether a gesture is currently moving `entity`. */
+export function isDragging(entity: Entity): boolean {
+ return entity.has(ClipDragOrigin) || entity.has(TrimDragOrigin);
+}
diff --git a/apps/web/src/engine/timeline/index.ts b/apps/web/src/engine/timeline/index.ts
new file mode 100644
index 00000000..ad16eba3
--- /dev/null
+++ b/apps/web/src/engine/timeline/index.ts
@@ -0,0 +1,14 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+export * from './config';
+export * from './constants';
+export * from './controller';
+export * from './drag';
+export * from './snapping';
+export * from './layout';
+export * from './style';
+export * from './surface';
+export * from './timeline';
+export * from './view';
diff --git a/apps/web/src/engine/timeline/layout.ts b/apps/web/src/engine/timeline/layout.ts
new file mode 100644
index 00000000..75c49330
--- /dev/null
+++ b/apps/web/src/engine/timeline/layout.ts
@@ -0,0 +1,59 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * How tall the timeline's rows are and where they sit. The row tree itself
+ * comes from the runtime (`buildTimelineLayers`); this is only its geometry,
+ * shared by the canvas that draws the clips and the DOM column that labels
+ * them, so a row and its label stay the same height.
+ */
+
+import { ClipHeight, Timeline, store } from '@diffusionstudio/runtime';
+
+import { DEFAULT_CLIP_HEIGHT, KEYFRAME_TRACK_HEIGHT } from './config';
+
+import type { TimelineNode } from '@diffusionstudio/runtime';
+import type { Entity, World } from 'koota';
+
+/**
+ * Where the walk has got to and how tall the row it is in is. One object
+ * threaded through the render pass rather than state on the surface: it only
+ * means anything while the walk is running.
+ */
+export type RowCursor = {
+ top: number;
+ height: number;
+};
+
+/** A row is as tall as its clip was left, or as tall as a keyframe row is. */
+export function getNodeHeight(node: TimelineNode): number {
+ if (node.kind === 'geometry') {
+ return node.entity.get(ClipHeight)?.value ?? DEFAULT_CLIP_HEIGHT;
+ }
+
+ return KEYFRAME_TRACK_HEIGHT;
+}
+
+/** A row plus everything expanded under it. */
+export function getSubtreeHeight(node: TimelineNode): number {
+ let height = getNodeHeight(node);
+ for (const child of node.children) height += getSubtreeHeight(child);
+ return height;
+}
+
+/** Every row of `nodes`, expanded ones included. */
+export function getRowsHeight(nodes: TimelineNode[]): number {
+ let height = 0;
+ for (const node of nodes) height += getSubtreeHeight(node);
+ return height;
+}
+
+/**
+ * The matrix a clip is drawn under: the scene's scrolled view (see
+ * `updateTimelineTransform`) dropped to the top of the row being drawn, so a
+ * render function can work in the row's own space from (0, 0).
+ */
+export function getRowTransform(world: World, scene: Entity, top: number): DOMMatrix | undefined {
+ return store(world, Timeline).transform[scene.id()]?.translate(0, top);
+}
diff --git a/apps/web/src/engine/timeline/media.ts b/apps/web/src/engine/timeline/media.ts
new file mode 100644
index 00000000..dba81144
--- /dev/null
+++ b/apps/web/src/engine/timeline/media.ts
@@ -0,0 +1,62 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * The pictures the timeline draws inside clips. The library derives and keeps
+ * an asset's poster (see `AssetCache.thumbnail`); what is needed here is the
+ * same picture decoded and ready to draw, which asking for it every frame
+ * would not give — so it is asked for once and kept until the asset changes.
+ *
+ * Keyed by asset rather than by clip: two clips of the same footage show the
+ * same picture, and a clip that is split into two does not need a second copy
+ * of anything.
+ */
+
+import { Library } from '@diffusionstudio/runtime';
+
+import type { Asset } from '@diffusionstudio/assets';
+import type { World } from 'koota';
+
+/** How wide a poster is asked for: the widest tile any row height draws. */
+const POSTER_WIDTH = 300;
+
+const posters = new Map<string, ImageBitmap | null>();
+const loading = new Set<string>();
+
+/**
+ * The asset's poster, or null until it has loaded (the load is started here).
+ * Null is also the answer for an asset that has no picture to show, and is
+ * remembered as such so it is not asked for again.
+ */
+export function resolvePoster(world: World, asset: Asset): ImageBitmap | null {
+ const existing = posters.get(asset.id);
+ if (existing !== undefined) return existing;
+
+ if (loading.has(asset.id)) return null;
+ loading.add(asset.id);
+
+ const cache = world.get(Library)?.cache;
+ if (!cache) {
+ loading.delete(asset.id);
+ return null;
+ }
+
+ void cache
+ .thumbnail(asset, POSTER_WIDTH)
+ .then((blob) => (blob ? createImageBitmap(blob) : null))
+ .then((bitmap) => posters.set(asset.id, bitmap))
+ .catch(() => posters.set(asset.id, null))
+ .finally(() => loading.delete(asset.id));
+
+ return null;
+}
+
+/**
+ * Forgets an asset's poster, so the next frame asks for it again. Called when
+ * an asset is re-pointed at another file, which the picture would outlive.
+ */
+export function forgetPoster(assetId: string): void {
+ posters.get(assetId)?.close();
+ posters.delete(assetId);
+}
diff --git a/apps/web/src/engine/timeline/paths.ts b/apps/web/src/engine/timeline/paths.ts
new file mode 100644
index 00000000..b2e56573
--- /dev/null
+++ b/apps/web/src/engine/timeline/paths.ts
@@ -0,0 +1,7 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+export const KNOB_PATH = new Path2D("M6.47987 12.372C5.68637 13.2449 4.31359 13.2449 3.52004 12.372L0.520121 9.07213C0.185444 8.70399 4.73995e-07 8.22432 4.61669e-07 7.72679L3.19793e-07 2C2.92428e-07 0.89543 0.895431 1.57728e-07 2 2.54292e-07L7.99987 7.78817e-07C9.10449 8.75386e-07 9.99994 0.895511 9.99987 2.00013L9.99949 7.72692C9.99946 8.22437 9.81405 8.70396 9.47945 9.07206L6.47987 12.372Z");
+export const IN_POINT_PATH = new Path2D("M1.95052 0H4.61719V10H1.95052V8.5V6.25L0.771284 5.97362C-0.257794 5.73243 -0.257794 4.26757 0.771284 4.02638L1.95052 3.75V1.5V0Z");
+export const OUT_POINT_PATH = new Path2D("M2.66667 0H0V10H2.66667V8.5V6.25L3.8459 5.97362C4.87498 5.73243 4.87498 4.26757 3.8459 4.02638L2.66667 3.75V1.5V0Z");
diff --git a/apps/web/src/engine/timeline/pointer.ts b/apps/web/src/engine/timeline/pointer.ts
new file mode 100644
index 00000000..6807f79d
--- /dev/null
+++ b/apps/web/src/engine/timeline/pointer.ts
@@ -0,0 +1,299 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { assert } from "@/utils";
+import * as cfg from "./config";
+
+import type { Marquee } from "./surface";
+
+type MousePosition = {
+ state: 'idle';
+ currentX: number;
+ currentY: number;
+} | {
+ state: 'pressed' | 'pressing' | 'lifted';
+ initialX: number;
+ initialY: number;
+ timestamp: number;
+ currentX: number;
+ currentY: number;
+ deltaX: number;
+ deltaY: number;
+}
+
+type TransformedRegion = {
+ minX: number;
+ minY: number;
+ maxX: number;
+ maxY: number;
+ id: string;
+ passthrough: boolean;
+}
+
+type HitRegions = {
+ prev: Array<TransformedRegion>;
+ next: Array<TransformedRegion>;
+ id: string;
+ index: number;
+};
+
+export type TimelinePointer = ReturnType<typeof createPointer>;
+
+type PointerOptions = {
+ marquee?: Marquee | null;
+ canvas?: HTMLCanvasElement | null;
+ ctx?: CanvasRenderingContext2D | null;
+};
+
+export function createPointer(options: PointerOptions) {
+ let position: MousePosition | null = null;
+ let pressRegionID: string | null = null;
+ let hitRegions: HitRegions = {
+ prev: [],
+ next: [],
+ id: '',
+ index: 0,
+ };
+ let lastClickPosition: {
+ x: number;
+ y: number;
+ timestamp: number;
+ } | null = null;
+
+ let shiftPressed = false;
+
+ function down(event: PointerEvent) {
+ const rect = options.canvas?.getBoundingClientRect();
+ if (!rect || event.button !== 0) return;
+
+ const currentX = event.clientX - rect.left;
+ const currentY = event.clientY - rect.top;
+
+ position = {
+ state: 'pressed',
+ timestamp: performance.now(),
+ initialX: currentX,
+ initialY: currentY,
+ currentX,
+ currentY,
+ deltaX: 0,
+ deltaY: 0,
+ };
+
+ shiftPressed = event.shiftKey;
+ }
+
+ function move(event: PointerEvent) {
+ const rect = options.canvas?.getBoundingClientRect();
+ if (!rect) return;
+
+ shiftPressed = event.shiftKey;
+
+ const currentX = event.clientX - rect.left;
+ const currentY = event.clientY - rect.top;
+
+ if (!position) {
+ position = {
+ state: 'idle',
+ currentX,
+ currentY,
+ };
+ return;
+ }
+
+ if (position.state != 'idle') {
+ position.deltaX = currentX - position.initialX;
+ position.deltaY = currentY - position.initialY;
+ }
+
+ position.currentX = currentX;
+ position.currentY = currentY;
+ }
+
+ function up(event: PointerEvent) {
+ const rect = options.canvas?.getBoundingClientRect();
+ if (!rect) return;
+ if (!position || position.state === 'idle') return;
+
+ const currentX = event.clientX - rect.left;
+ const currentY = event.clientY - rect.top;
+
+ position = {
+ ...position,
+ state: 'lifted',
+ currentX,
+ currentY,
+ deltaX: currentX - position.initialX,
+ deltaY: currentY - position.initialY,
+ };
+ pressRegionID = null;
+ shiftPressed = event.shiftKey;
+ }
+
+
+ function scope(id: string) {
+ hitRegions.id = id;
+ hitRegions.index = 0;
+
+ return pointer;
+ }
+
+ function region(x = 0, y = 0, w: number, h: number, id?: string, passthrough = false) {
+ assert(options.ctx, 'Canvas context must be set');
+
+ if (!position) return {
+ hovering: false,
+ clicked: false,
+ pressing: false,
+ pressed: false,
+ dragging: false,
+ doubleClicked: false,
+ intersectsMarquee: false,
+ };
+
+ const regionData = {
+ id: `${hitRegions.id}/${id ?? hitRegions.index}`,
+ ...transformRegionToCanvas(options.ctx, x, y, w, h),
+ passthrough,
+ };
+
+ hitRegions.index++;
+ hitRegions.next.push(regionData);
+
+ // Mouse position is in CSS pixels, scale to device pixels to match canvas coordinates
+ const mouseX = position.currentX * window.devicePixelRatio;
+ const mouseY = position.currentY * window.devicePixelRatio;
+
+ const matches = hitRegions.prev.filter(({ minX, minY, maxX, maxY }) => (
+ mouseX >= minX &&
+ mouseX < maxX &&
+ mouseY >= minY &&
+ mouseY < maxY
+ ));
+
+ const lastMatch = matches.findLast(({ passthrough }) => !passthrough);
+
+ let isPrimary = lastMatch?.id === regionData.id;
+ let isPassive = matches.some(({ passthrough, id }) => passthrough && id == regionData.id);
+ let hovering = isPrimary || isPassive;
+ let pressing = hovering && position?.state == 'pressing';
+ let pressed = hovering && position?.state == 'pressed';
+
+ // Track the region that was pressed down on
+ if (pressed) {
+ pressRegionID = lastMatch?.id ?? null;
+ }
+
+ // Assign dragging state after a distance threshold has been passed
+ let dragging = false;
+ if (position?.state == 'pressing' && pressRegionID == regionData.id) {
+ const distance = Math.sqrt(position.deltaX ** 2 + position.deltaY ** 2);
+ dragging = distance >= cfg.CLICK_DISTANCE_THRESHOLD;
+ }
+
+ // Only consider click if pointer hasn't moved beyond the distance threshold
+ let clicked = false;
+ if (position?.state == 'lifted') {
+ const distance = Math.sqrt(position.deltaX ** 2 + position.deltaY ** 2);
+ clicked = distance < cfg.CLICK_DISTANCE_THRESHOLD && hovering;
+ }
+
+ let doubleClicked = false;
+ if (clicked && lastClickPosition) {
+ const delta = performance.now() - lastClickPosition.timestamp;
+ const distance = Math.sqrt(
+ Math.pow(position.currentX - lastClickPosition.x, 2) +
+ Math.pow(position.currentY - lastClickPosition.y, 2)
+ );
+ doubleClicked = delta < cfg.DOUBLE_CLICK_THRESHOLD && hovering && distance < 10;
+ }
+
+ let intersectsMarquee = false;
+ if (options.marquee) {
+ // Convert marquee selection from CSS pixels to canvas coordinates
+ const marqueeMinX = options.marquee.x * window.devicePixelRatio;
+ const marqueeMaxX = (options.marquee.x + options.marquee.width) * window.devicePixelRatio;
+ const marqueeMinY = options.marquee.y * window.devicePixelRatio;
+ const marqueeMaxY = (options.marquee.y + options.marquee.height) * window.devicePixelRatio;
+
+ // Check intersection: regions intersect if they overlap
+ intersectsMarquee = !(
+ regionData.maxX < marqueeMinX ||
+ regionData.minX > marqueeMaxX ||
+ regionData.maxY < marqueeMinY ||
+ regionData.minY > marqueeMaxY
+ );
+ }
+
+ return {
+ hovering, // If the pointer is over the rect
+ clicked, // If the rect was clicked this frame
+ pressing, // If the pointer is pressing down on the rect this frame
+ pressed, // If the just started pressing down on the rect this frame
+ dragging, // If the pointer is dragging the rect this frame
+ doubleClicked, // If the rect was double clicked this frame
+ intersectsMarquee, // If the region intersects the marquee selection
+ };
+ }
+
+ function reset() {
+ hitRegions.prev = [...hitRegions.next];
+ hitRegions.next.length = 0;
+
+ if (position?.state == 'lifted') {
+ position = {
+ ...position,
+ state: 'idle',
+ };
+ lastClickPosition = {
+ x: position.currentX,
+ y: position.currentY,
+ timestamp: performance.now(),
+ };
+ }
+
+ if (position?.state == 'pressed') {
+ position = {
+ ...position,
+ state: 'pressing',
+ };
+ }
+ }
+
+ const pointer = {
+ get position() {
+ return position;
+ },
+ get shiftPressed() {
+ return shiftPressed;
+ },
+ down,
+ move,
+ up,
+ scope,
+ region,
+ reset,
+ };
+
+ return pointer;
+}
+
+function transformRegionToCanvas(ctx: CanvasRenderingContext2D, x: number, y: number, w: number, h: number) {
+ // Transform the region to canvas coordinates when storing it
+ const transform = ctx.getTransform();
+
+ // Transform all four corners of the region to canvas coordinates
+ const topLeft = transform.transformPoint(new DOMPoint(x, y));
+ const topRight = transform.transformPoint(new DOMPoint(x + w, y));
+ const bottomLeft = transform.transformPoint(new DOMPoint(x, y + h));
+ const bottomRight = transform.transformPoint(new DOMPoint(x + w, y + h));
+
+ // Compute bounding box in canvas coordinates
+ return {
+ minX: Math.min(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x),
+ maxX: Math.max(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x),
+ minY: Math.min(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y),
+ maxY: Math.max(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y),
+ };
+}
diff --git a/apps/web/src/engine/timeline/render/caption.ts b/apps/web/src/engine/timeline/render/caption.ts
new file mode 100644
index 00000000..76673b8d
--- /dev/null
+++ b/apps/web/src/engine/timeline/render/caption.ts
@@ -0,0 +1,94 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { Computed, resolveCaptionDecoder, secondsToFrames, store } from '@diffusionstudio/runtime';
+
+import { CLIP_BREAKPOINTS, CLIP_CORNER_RADIUS, CLIP_FONT, CLIP_LABEL_HEIGHT } from '../config';
+import { getClipStyle } from '../style';
+import { truncateText } from '../text';
+import { framesToPixels, getFrameRate, getResolution } from '../view';
+
+import type { Entity, World } from 'koota';
+import type { RowCursor } from '../layout';
+import type { TimelineSurfaceState } from '../surface';
+
+const GROUP_GAP = 2;
+const GROUP_PADDING = 8;
+const GROUP_PADDING_TOP = 12;
+
+/**
+ * What a caption clip says, as a block per word group along the clip. The
+ * groups come from the same decoder the canvas draws from, so the timeline
+ * and the picture agree on where each phrase falls.
+ */
+export function renderCaption(
+ world: World,
+ scene: Entity,
+ surface: TimelineSurfaceState,
+ entity: Entity,
+ row: RowCursor,
+): void {
+ const ctx = surface.ctx!;
+
+ const groups = resolveCaptionDecoder(world, entity)?.groups;
+ if (!groups?.length) return;
+
+ const computed = store(world, Computed);
+ const resolution = getResolution(world, scene);
+ const fps = getFrameRate(world);
+
+ const eid = entity.id();
+ const start = computed.start[eid] ?? 0;
+ const end = computed.end[eid] ?? 0;
+ // The transcript's times are its own; the origin is where they begin on
+ // the scene's timeline.
+ const origin = computed.origin[eid] ?? 0;
+
+ const left = framesToPixels(start, resolution);
+ const style = getClipStyle(entity, null);
+
+ ctx.save();
+
+ ctx.beginPath();
+ ctx.roundRect(left, 0, framesToPixels(end, resolution) - left, row.height, CLIP_CORNER_RADIUS);
+ ctx.clip();
+
+ const top = row.height < CLIP_BREAKPOINTS.sm ? GROUP_PADDING_TOP : CLIP_LABEL_HEIGHT;
+ const height = row.height - top - 1;
+ if (height < 4) {
+ ctx.restore();
+ return;
+ }
+
+ ctx.fontStretch = 'ultra-condensed';
+ ctx.font = CLIP_FONT;
+ ctx.textAlign = 'left';
+ ctx.textBaseline = 'middle';
+
+ for (const group of groups) {
+ if (!group.length) continue;
+
+ const groupStart = origin + secondsToFrames(group[0]?.start, fps);
+ const groupEnd = origin + secondsToFrames(group[group.length - 1]?.end, fps);
+ // A group the clip has been trimmed past is not shown at all.
+ if (groupEnd < start || groupStart >= end) continue;
+
+ const x = framesToPixels(Math.max(groupStart, start), resolution);
+ const width = framesToPixels(Math.min(groupEnd, end), resolution) - x;
+ if (width < 2) continue;
+
+ ctx.fillStyle = style.primary!;
+ ctx.beginPath();
+ ctx.roundRect(x, top, width - GROUP_GAP, height, CLIP_CORNER_RADIUS);
+ ctx.fill();
+
+ const text = truncateText(ctx, group.map((word) => word.text).join(' '), width - GROUP_PADDING, CLIP_FONT);
+ if (text) {
+ ctx.fillStyle = style.foreground;
+ ctx.fillText(text, x + 4, top + Math.round(height / 2));
+ }
+ }
+
+ ctx.restore();
+}
diff --git a/apps/web/src/engine/timeline/render/clip.ts b/apps/web/src/engine/timeline/render/clip.ts
new file mode 100644
index 00000000..5c1f31df
--- /dev/null
+++ b/apps/web/src/engine/timeline/render/clip.ts
@@ -0,0 +1,322 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { Caption, CaptionType, Chars, ClipDragOrigin, Computed, Hidden, Name, Selected, TrimDragOrigin, fitsChildren, isCaption, isGroup, isText, store } from '@diffusionstudio/runtime';
+
+import { getDocumentEditor } from '../../editor';
+import {
+ CLIP_BREAKPOINTS,
+ CLIP_CORNER_RADIUS,
+ CLIP_FONT,
+ CLIP_LABEL_X,
+ CLIP_LABEL_Y,
+ CLIP_LABEL_HEIGHT,
+ TRIM_HANDLE_WIDTH,
+} from '../config';
+import { applyClipDrag, applyTrim, beginClipDrag, beginTrim } from '../drag';
+import { getClipAsset, getClipStyle } from '../style';
+import { truncateText } from '../text';
+import { framesToPixels, getResolution, getViewport } from '../view';
+import { renderCaption } from './caption';
+import { renderGroup } from './group';
+import { renderThumbnails } from './thumbnails';
+import { renderWaveform } from './waveform';
+
+import type { Entity, World } from 'koota';
+import type { Asset } from '@diffusionstudio/assets';
+import type { ClipStyle } from '../style';
+import type { RowCursor } from '../layout';
+import type { TimelineSurfaceState } from '../surface';
+
+/**
+ * One clip: its body, what it holds, its label and its border, plus the
+ * selection gestures that belong to its rectangle. The context is already
+ * translated to the top of the row it is in, so everything here is drawn from
+ * y = 0 and the row's own height.
+ */
+export function renderClip(
+ world: World,
+ scene: Entity,
+ surface: TimelineSurfaceState,
+ entity: Entity,
+ row: RowCursor,
+): void {
+ const { ctx, pointer } = surface;
+ if (!ctx || !pointer) return;
+
+ const computed = store(world, Computed);
+ const resolution = getResolution(world, scene);
+
+ let left = framesToPixels(computed.start[entity.id()] ?? 0, resolution);
+ const width = framesToPixels(computed.end[entity.id()] ?? 0, resolution) - left;
+
+ // Every region of this clip is scoped to it, so two clips at the same
+ // place on two rows are told apart by the pointer.
+ pointer.scope(String(entity.id()));
+
+ const asset = getClipAsset(world, entity);
+ const style = getClipStyle(entity, asset);
+
+ handleBody(world, surface, entity, left, width, row, resolution);
+
+ // A drag that has just moved the clip has moved where it is drawn, so the
+ // left edge is read again; the width does not change with it.
+ if (entity.has(ClipDragOrigin)) {
+ left = framesToPixels(computed.start[entity.id()] ?? 0, resolution);
+ }
+
+ ctx.save();
+ ctx.beginPath();
+ ctx.roundRect(left, 0, width, row.height, CLIP_CORNER_RADIUS);
+ ctx.fillStyle = style.background;
+ ctx.fill();
+ ctx.restore();
+
+ renderContent(world, scene, surface, entity, asset, style, row);
+
+ renderLabel(world, scene, surface, entity, style.foreground, left, width, row);
+
+ // Last, so it sits over whatever the clip drew inside itself.
+ const selected = entity.has(Selected);
+
+ ctx.save();
+ ctx.beginPath();
+ ctx.roundRect(left, 0, width, row.height, CLIP_CORNER_RADIUS);
+ ctx.clip();
+ ctx.strokeStyle = selected ? surface.colors.border.ring : surface.colors.border.darker;
+ ctx.lineWidth = selected ? 2 : 1;
+ ctx.stroke();
+ ctx.restore();
+
+ handleTrim(world, surface, entity, left, width, row, resolution);
+}
+
+/**
+ * The handles at either end of the clip. They sit inside its edges rather
+ * than straddling them, so a clip is always grabbable in the middle however
+ * narrow it is — a third of the clip at most, and never more than half, or a
+ * short clip would be nothing but handles.
+ */
+function handleTrim(
+ world: World,
+ surface: TimelineSurfaceState,
+ entity: Entity,
+ left: number,
+ width: number,
+ row: RowCursor,
+ resolution: number,
+): void {
+ // A container that takes its bounds from its children has no edge of its
+ // own to take hold of — moving one would be moving a child. One that
+ // authors its own end does, and is trimmed like any other clip.
+ if (fitsChildren(entity)) return;
+
+ const pointer = surface.pointer!;
+ const handle = Math.min(Math.max(width / 3, 3), width / 2, TRIM_HANDLE_WIDTH);
+
+ const inHandle = pointer.region(left, 0, handle, row.height);
+ const outHandle = pointer.region(left + width - handle, 0, handle, row.height);
+
+ // The cursor points into the clip, at the frames the handle would take.
+ if (inHandle.hovering || inHandle.dragging) surface.cursor = 'trim-right';
+ else if (outHandle.hovering || outHandle.dragging) surface.cursor = 'trim-left';
+
+ const edge = inHandle.dragging ? 'in' : outHandle.dragging ? 'out' : null;
+ if (edge === null) return;
+
+ if (!entity.has(TrimDragOrigin)) beginTrim(world, entity);
+
+ applyTrim(world, surface, entity, edge, resolution);
+}
+
+/**
+ * What the clip holds, drawn inside it: what a group has in it, the picture
+ * of a still, the audio of a sound, the words of a caption. Each is clipped
+ * to the clip's own body by the border that is drawn over it afterwards.
+ */
+function renderContent(
+ world: World,
+ scene: Entity,
+ surface: TimelineSurfaceState,
+ entity: Entity,
+ asset: Asset | null,
+ style: ClipStyle,
+ row: RowCursor,
+): void {
+ if (isGroup(entity)) {
+ renderGroup(world, scene, surface, entity, row);
+ return;
+ }
+
+ if (isCaption(entity)) {
+ renderCaption(world, scene, surface, entity, row);
+ return;
+ }
+
+ // Too short a row to hold anything but its label.
+ const tall = row.height > CLIP_BREAKPOINTS.sm;
+
+ switch (asset?.type) {
+ case 'IMAGE':
+ case 'SEQUENCE':
+ if (tall) renderThumbnails(world, scene, surface, entity, asset, row);
+ return;
+
+ case 'AUDIO':
+ renderWaveform(world, scene, surface, entity, row, {
+ color: style.primary ?? style.foreground,
+ offsetY: tall ? CLIP_LABEL_HEIGHT : 2,
+ padding: 4,
+ });
+ return;
+
+ case 'VIDEO': {
+ // A video shows both: its picture along the top of the row, its
+ // sound along the bottom.
+ const strip = tall ? Math.round((row.height - CLIP_LABEL_HEIGHT) / 2) : 0;
+ if (strip > 0) {
+ renderThumbnails(world, scene, surface, entity, asset, row, {
+ top: CLIP_LABEL_HEIGHT,
+ height: strip,
+ });
+ }
+ renderWaveform(world, scene, surface, entity, row, {
+ color: style.primary ?? style.foreground,
+ offsetY: tall ? CLIP_LABEL_HEIGHT + strip : 2,
+ padding: 2,
+ });
+ return;
+ }
+ }
+}
+
+/**
+ * What the clip's body means: a click selects it, a drag moves it, and while
+ * a marquee is out, being under it does the selecting. One region for all
+ * three, since they are all the same rectangle — asking twice would make the
+ * pointer think there were two of them.
+ *
+ * Selection is a document property, so it goes through the editor like every
+ * other change: a click in the timeline ends up in the file the same way a
+ * click on the canvas does.
+ */
+function handleBody(
+ world: World,
+ surface: TimelineSurfaceState,
+ entity: Entity,
+ left: number,
+ width: number,
+ row: RowCursor,
+ resolution: number,
+): void {
+ const pointer = surface.pointer!;
+ const editor = getDocumentEditor(world);
+
+ const { clicked, dragging, intersectsMarquee } = pointer.region(left, 0, width, row.height);
+ const selected = entity.has(Selected);
+
+ // A press that travels starts a move. The press selected the clip first,
+ // so a drag of an unselected clip moves that one and a drag of a selected
+ // one moves everything selected (see `updateDragGestures`).
+ if (dragging && !entity.has(ClipDragOrigin) && !entity.has(TrimDragOrigin)) {
+ beginClipDrag(world, entity);
+ }
+ if (entity.has(ClipDragOrigin)) {
+ applyClipDrag(world, surface, entity, resolution);
+ }
+
+ // While a marquee is out, the clips it covers are the selection: entering
+ // it selects and leaving it deselects, so dragging back the way you came
+ // undoes what the drag did.
+ if (surface.marquee) {
+ if (intersectsMarquee && !selected) editor.select(entity, { extend: true });
+ if (!intersectsMarquee && selected) editor.deselect(entity);
+ return;
+ }
+
+ if (!clicked) return;
+
+ if (pointer.shiftPressed) editor.select(entity, { extend: true });
+ else editor.select(entity);
+}
+
+/**
+ * The clip's name, or what a text clip says. Pinned to the left edge of the
+ * viewport while the clip runs off it, so a clip longer than the screen is
+ * still labelled.
+ */
+function renderLabel(
+ world: World,
+ scene: Entity,
+ surface: TimelineSurfaceState,
+ entity: Entity,
+ color: string,
+ left: number,
+ width: number,
+ row: RowCursor,
+): void {
+ const ctx = surface.ctx!;
+
+ // A caption's row is mostly its words; below a certain height the label
+ // would be all there is room for, so the words win.
+ if (isCaption(entity) && row.height < CLIP_BREAKPOINTS.sm) return;
+
+ const label = clipLabel(entity);
+ if (!label) return;
+
+ const [viewportLeft] = getViewport(world, scene, surface.layout.width);
+ const x = Math.max(left, viewportLeft);
+ const maxWidth = left + width - x - 2 * CLIP_LABEL_X;
+
+ ctx.save();
+ ctx.translate(x, 0);
+
+ ctx.fillStyle = color;
+ ctx.fontStretch = 'ultra-condensed';
+ ctx.font = CLIP_FONT;
+ ctx.textAlign = 'left';
+ ctx.textBaseline = 'middle';
+
+ const fitted = truncateText(ctx, label, maxWidth, CLIP_FONT);
+ if (fitted) {
+ // Tall enough for the label to sit at the top of the clip; below that
+ // there is only the middle.
+ ctx.fillText(fitted, CLIP_LABEL_X, row.height >= CLIP_BREAKPOINTS.xs ? CLIP_LABEL_Y : row.height / 2);
+ }
+
+ ctx.restore();
+}
+
+const CAPTION_PRESETS: Record<CaptionType, string> = {
+ [CaptionType.CLASSIC]: 'Classic',
+ [CaptionType.CASCADE]: 'Cascade',
+ [CaptionType.SPOTLIGHT]: 'Spotlight',
+ [CaptionType.WHISPER]: 'Whisper',
+ [CaptionType.PAPER]: 'Paper',
+ [CaptionType.GUINEA]: 'Guinea',
+ [CaptionType.STARK]: 'Stark',
+};
+
+/**
+ * What the clip calls itself. A plain text clip goes by what it says, and a
+ * caption that has not been named goes by its preset — read here rather than
+ * written to the entity, which is what the old timeline did: a renderer
+ * naming the thing it is drawing is an edit nobody asked for.
+ */
+function clipLabel(entity: Entity): string {
+ const name = entity.get(Name)?.value;
+
+ if (isCaption(entity)) {
+ return name || `${CAPTION_PRESETS[entity.get(Caption)?.type ?? CaptionType.CLASSIC]} Captions`;
+ }
+
+ if (isText(entity)) return entity.get(Chars)?.value ?? name ?? '';
+
+ return name ?? '';
+}
+
+/** Whether a clip is drawn faded: hidden nodes still take up their row. */
+export function getClipAlpha(entity: Entity): number {
+ return entity.has(Hidden) ? 0.5 : 1;
+}
diff --git a/apps/web/src/engine/timeline/render/group.ts b/apps/web/src/engine/timeline/render/group.ts
new file mode 100644
index 00000000..9925e86f
--- /dev/null
+++ b/apps/web/src/engine/timeline/render/group.ts
@@ -0,0 +1,66 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { Cache, Computed, store } from '@diffusionstudio/runtime';
+
+import { CLIP_CORNER_RADIUS } from '../config';
+import { framesToPixels, getResolution } from '../view';
+
+import type { Entity, World } from 'koota';
+import type { RowCursor } from '../layout';
+import type { TimelineSurfaceState } from '../surface';
+
+/** How far below the label the children are drawn. */
+const CHILD_TOP = 20;
+/** Below this there is no room to say anything, so nothing is drawn. */
+const MIN_CHILD_HEIGHT = 12;
+
+/**
+ * What a collapsed group holds, as bars inside its own clip: enough to see
+ * where its children sit in time without expanding it. Clipped to the
+ * group's rounded body so a child reaching past the group's own bounds does
+ * not spill out of it.
+ */
+export function renderGroup(
+ world: World,
+ scene: Entity,
+ surface: TimelineSurfaceState,
+ entity: Entity,
+ row: RowCursor,
+): void {
+ const ctx = surface.ctx!;
+
+ const children = entity.get(Cache)?.children ?? [];
+ if (children.length === 0) return;
+
+ const height = row.height - CHILD_TOP;
+ if (height < MIN_CHILD_HEIGHT) return;
+
+ const computed = store(world, Computed);
+ const resolution = getResolution(world, scene);
+
+ const start = computed.start[entity.id()] ?? 0;
+ const end = computed.end[entity.id()] ?? 0;
+ const left = framesToPixels(start, resolution);
+
+ ctx.save();
+
+ ctx.beginPath();
+ ctx.roundRect(left, 0, framesToPixels(end, resolution) - left, row.height, CLIP_CORNER_RADIUS);
+ ctx.clip();
+
+ ctx.fillStyle = surface.colors.clip.group.primary;
+
+ for (const child of children) {
+ const childLeft = framesToPixels(Math.max(start, computed.start[child.id()] ?? 0), resolution);
+ const childWidth = framesToPixels(Math.min(end, computed.end[child.id()] ?? 0), resolution) - childLeft;
+ if (childWidth < 2) continue;
+
+ ctx.beginPath();
+ ctx.roundRect(childLeft, CHILD_TOP, childWidth, height, CLIP_CORNER_RADIUS);
+ ctx.fill();
+ }
+
+ ctx.restore();
+}
diff --git a/apps/web/src/engine/timeline/render/index.ts b/apps/web/src/engine/timeline/render/index.ts
new file mode 100644
index 00000000..30084c21
--- /dev/null
+++ b/apps/web/src/engine/timeline/render/index.ts
@@ -0,0 +1,16 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+export * from './caption';
+export * from './clip';
+export * from './group';
+export * from './keyframes';
+export * from './layers';
+export * from './marquee';
+export * from './playhead';
+export * from './ruler';
+export * from './snap';
+export * from './thumbnails';
+export * from './waveform';
+export * from './workarea';
diff --git a/apps/web/src/engine/timeline/render/keyframes.ts b/apps/web/src/engine/timeline/render/keyframes.ts
new file mode 100644
index 00000000..03449660
--- /dev/null
+++ b/apps/web/src/engine/timeline/render/keyframes.ts
@@ -0,0 +1,177 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import {
+ Cache,
+ Computed,
+ Hovering,
+ Keyframe,
+ KeyframeDragOrigin,
+ Selected,
+ findClosestParentGeometry,
+ framesToSeconds,
+ store,
+} from '@diffusionstudio/runtime';
+
+import { getDocumentEditor } from '../../editor';
+import { KEYFRAME_TRACK_HEIGHT } from '../config';
+import { getRowTransform } from '../layout';
+import { framesToPixels, getFrameRate, getResolution, getViewport, pixelsToFrames } from '../view';
+
+import type { Entity, World } from 'koota';
+import type { RowCursor } from '../layout';
+import type { TimelineSurfaceState } from '../surface';
+
+const COLOR = 'rgba(242, 242, 242, 0.64)';
+const SIZE = 8;
+const HALF = SIZE / 2;
+// A diamond is a small target, so what can be grabbed is a little larger than
+// what is drawn.
+const HITBOX = SIZE + 4;
+const HITBOX_HALF = HITBOX / 2;
+
+/**
+ * One track's keyframes, as diamonds along its row. A keyframe is drawn
+ * hollow at either end of the track and solid in between, so the stretch that
+ * is actually animated reads at a glance.
+ *
+ * The times are the clip's own, which is what makes them travel with it and
+ * run at its speed; the clip's origin is what puts them on the scene's
+ * timeline.
+ */
+export function renderKeyframeTrack(
+ world: World,
+ scene: Entity,
+ surface: TimelineSurfaceState,
+ track: Entity,
+ row: RowCursor,
+): void {
+ const { ctx, pointer } = surface;
+ if (!ctx || !pointer) return;
+
+ const keyframes = track.get(Cache)?.keyframes ?? [];
+ if (keyframes.length === 0) return;
+
+ const clip = findClosestParentGeometry(track);
+ const computed = clip?.get(Computed);
+ if (!computed) return;
+
+ const transform = getRowTransform(world, scene, row.top);
+ if (!transform) return;
+
+ const resolution = getResolution(world, scene);
+ const [viewportLeft, viewportRight] = getViewport(world, scene, surface.layout.width);
+ const rate = computed.playbackRate || 1;
+ const centerY = KEYFRAME_TRACK_HEIGHT / 2;
+
+ const times = store(world, Keyframe).time;
+ const first = times[keyframes[0]!.id()] ?? 0;
+ const last = times[keyframes[keyframes.length - 1]!.id()] ?? 0;
+
+ ctx.save();
+ ctx.setTransform(transform);
+ ctx.lineWidth = 1;
+
+ if (track.has(Hovering)) {
+ ctx.fillStyle = surface.colors.background.muted;
+ ctx.globalAlpha = 0.6;
+ ctx.fillRect(viewportLeft, 0, viewportRight - viewportLeft, KEYFRAME_TRACK_HEIGHT);
+ ctx.globalAlpha = 1;
+ }
+
+ pointer.scope(String(track.id()));
+
+ for (const keyframe of keyframes) {
+ const time = times[keyframe.id()] ?? 0;
+ const x = framesToPixels(computed.origin + time / rate, resolution);
+
+ if (x + HITBOX_HALF < viewportLeft || x - HITBOX_HALF > viewportRight) continue;
+
+ handleKeyframe(world, surface, keyframe, time, x, centerY, rate, resolution);
+
+ const selected = keyframe.has(Selected);
+ ctx.strokeStyle = selected ? surface.colors.border.ring : COLOR;
+ ctx.fillStyle = selected ? surface.colors.border.ring : COLOR;
+
+ diamond(ctx, x, centerY);
+ ctx.stroke();
+
+ // The ends of a track are half-filled, on the side facing the frames
+ // that hold: before the first and after the last, nothing changes.
+ const isFirst = time === first;
+ const isLast = time === last;
+
+ if (isFirst === isLast) {
+ ctx.fill();
+ } else {
+ ctx.beginPath();
+ ctx.moveTo(x, centerY - HALF);
+ ctx.lineTo(x + (isFirst ? HALF : -HALF), centerY);
+ ctx.lineTo(x, centerY + HALF);
+ ctx.closePath();
+ ctx.fill();
+ }
+ }
+
+ ctx.restore();
+}
+
+/**
+ * Selecting and moving one keyframe. A keyframe's time is a prop of its
+ * element, so a drag of it is an edit like any other; the drag is measured
+ * from where it started, in the clip's own time.
+ */
+function handleKeyframe(
+ world: World,
+ surface: TimelineSurfaceState,
+ keyframe: Entity,
+ time: number,
+ x: number,
+ centerY: number,
+ rate: number,
+ resolution: number,
+): void {
+ const pointer = surface.pointer!;
+ const editor = getDocumentEditor(world);
+
+ const { clicked, dragging, intersectsMarquee } = pointer.region(
+ x - HITBOX_HALF,
+ centerY - HITBOX_HALF,
+ HITBOX,
+ HITBOX,
+ String(keyframe.id()),
+ );
+
+ const selected = keyframe.has(Selected);
+
+ if (surface.marquee) {
+ if (intersectsMarquee && !selected) editor.select(keyframe, { extend: true });
+ if (!intersectsMarquee && selected) editor.deselect(keyframe);
+ } else if (clicked) {
+ editor.select(keyframe, { extend: pointer.shiftPressed });
+ }
+
+ if (dragging && !keyframe.has(KeyframeDragOrigin)) {
+ keyframe.add(KeyframeDragOrigin);
+ keyframe.set(KeyframeDragOrigin, { time });
+ }
+
+ const origin = keyframe.get(KeyframeDragOrigin);
+ const position = pointer.position;
+ if (!origin || !position || position.state === 'idle') return;
+
+ // The pointer moves in scene frames; the keyframe lives in the clip's,
+ // which run `rate` times as fast.
+ const moved = pixelsToFrames(position.deltaX, resolution) * rate;
+ editor.editProperty(keyframe, 'time', framesToSeconds(Math.max(0, origin.time + moved), getFrameRate(world)));
+}
+
+function diamond(ctx: CanvasRenderingContext2D, x: number, y: number): void {
+ ctx.beginPath();
+ ctx.moveTo(x, y - HALF);
+ ctx.lineTo(x + HALF, y);
+ ctx.lineTo(x, y + HALF);
+ ctx.lineTo(x - HALF, y);
+ ctx.closePath();
+}
diff --git a/apps/web/src/engine/timeline/render/layers.ts b/apps/web/src/engine/timeline/render/layers.ts
new file mode 100644
index 00000000..a6f3cfe1
--- /dev/null
+++ b/apps/web/src/engine/timeline/render/layers.ts
@@ -0,0 +1,141 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import {
+ AdjustmentLayer,
+ ChildOf,
+ Computed,
+ Geometry,
+ Group,
+ Hovering,
+ Sequential,
+ buildTimelineLayers,
+ store,
+} from '@diffusionstudio/runtime';
+import { Or } from 'koota';
+
+import { getNodeHeight, getRowTransform, getSubtreeHeight } from '../layout';
+import { getResolution, getViewport, pixelsToFrames } from '../view';
+import { isDragging } from '../drag';
+import { getClipAlpha, renderClip } from './clip';
+import { renderKeyframeTrack } from './keyframes';
+
+import type { Entity, World } from 'koota';
+import type { TimelineNode } from '@diffusionstudio/runtime';
+import type { RowCursor } from '../layout';
+import type { TimelineSurfaceState } from '../surface';
+
+// A little either side of the viewport, so a clip does not pop in at the
+// moment its edge crosses the boundary.
+const VIEWPORT_PADDING = 10;
+
+/**
+ * The rows of the scene on show, top to bottom. The tree comes from the
+ * runtime; what happens here is the geometry — how tall each row is, where
+ * the walk has got to, and which rows are near enough to the viewport to be
+ * worth drawing at all.
+ */
+export function renderLayers(world: World, scene: Entity, surface: TimelineSurfaceState): void {
+ const ctx = surface.ctx;
+ if (!ctx) return;
+
+ const resolution = getResolution(world, scene);
+ const [left, right] = getViewport(world, scene, surface.layout.width);
+ const minFrame = pixelsToFrames(left - VIEWPORT_PADDING, resolution);
+ const maxFrame = pixelsToFrames(right + VIEWPORT_PADDING, resolution);
+
+ const computed = store(world, Computed);
+ const row: RowCursor = { top: 0, height: 0 };
+
+ ctx.save();
+
+ const walk = (nodes: TimelineNode[], parent: Entity | null): void => {
+ for (const node of nodes) {
+ if (node.kind === 'keyframe-track') {
+ row.height = getNodeHeight(node);
+ renderKeyframeTrack(world, scene, surface, node.entity, row);
+ row.top += row.height;
+ continue;
+ }
+
+ // A sub-item is a label in the DOM column and nothing on the
+ // canvas; what hangs under it is what matters here.
+ if (node.kind === 'sub-item') {
+ row.top += getNodeHeight(node);
+ walk(node.children, parent);
+ continue;
+ }
+
+ const start = computed.start[node.entity.id()] ?? 0;
+ const end = computed.end[node.entity.id()] ?? 0;
+
+ // Off screen in time: the whole subtree can be skipped, since an
+ // expanded child is drawn within its parent's row.
+ if (end < minFrame || start > maxFrame) {
+ row.top += getSubtreeHeight(node);
+ continue;
+ }
+
+ // The children of a sequence are drawn in the sequence's own row
+ // (see `renderRow`), so they are not rows of their own.
+ if (parent === null || !parent.has(Sequential)) {
+ renderRow(world, scene, surface, node, row);
+ }
+
+ row.top += getNodeHeight(node);
+ walk(node.children, node.entity);
+ }
+ };
+
+ walk(buildTimelineLayers(world, scene), null);
+
+ ctx.restore();
+}
+
+/**
+ * One row. A clip is drawn as itself; a sequence is drawn as its children
+ * side by side along the row, since a sequence has no time of its own to
+ * show and its point is that its children share one line.
+ */
+function renderRow(
+ world: World,
+ scene: Entity,
+ surface: TimelineSurfaceState,
+ node: TimelineNode,
+ row: RowCursor,
+): void {
+ const ctx = surface.ctx!;
+
+ const transform = getRowTransform(world, scene, row.top);
+ if (!transform) return;
+
+ ctx.setTransform(transform);
+
+ row.height = getNodeHeight(node);
+ ctx.globalAlpha = getClipAlpha(node.entity);
+
+ if (node.entity.has(Hovering)) {
+ const [left, right] = getViewport(world, scene, surface.layout.width);
+ ctx.globalAlpha = 0.5;
+ ctx.fillStyle = surface.colors.background.accent;
+ ctx.fillRect(left, 0, right - left, row.height);
+ ctx.globalAlpha = 1;
+ }
+
+ if (!node.entity.has(Sequential)) {
+ renderClip(world, scene, surface, node.entity, row);
+ return;
+ }
+
+ // A clip being dragged is drawn after the ones it is passing over, so it
+ // stays on top of them for as long as it is moving.
+ const children = [...world.query(Or(Geometry, Group, AdjustmentLayer), ChildOf(node.entity))];
+
+ for (const child of children) {
+ if (!isDragging(child)) renderClip(world, scene, surface, child, row);
+ }
+ for (const child of children) {
+ if (isDragging(child)) renderClip(world, scene, surface, child, row);
+ }
+}
diff --git a/apps/web/src/engine/timeline/render/marquee.ts b/apps/web/src/engine/timeline/render/marquee.ts
new file mode 100644
index 00000000..1c4559ff
--- /dev/null
+++ b/apps/web/src/engine/timeline/render/marquee.ts
@@ -0,0 +1,71 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { getDocumentEditor } from '../../editor';
+
+import type { World } from 'koota';
+import type { TimelineSurfaceState } from '../surface';
+
+/**
+ * The empty space behind the rows, and what a gesture on it means: a click
+ * clears the selection, a drag pulls a marquee out. The marquee itself is
+ * only recorded here — which clips fall inside it is each clip's own
+ * business, decided as it draws (see `renderClip`).
+ *
+ * Runs before the rows so its region sits beneath theirs: the pointer takes
+ * the last region drawn over it, so a clip drawn afterwards wins.
+ */
+export function updateMarquee(world: World, surface: TimelineSurfaceState): void {
+ const { canvas, pointer } = surface;
+ if (!canvas || !pointer) return;
+
+ const { clicked, dragging } = pointer
+ .scope('canvas')
+ .region(0, 0, canvas.width, canvas.height);
+
+ if (clicked) {
+ getDocumentEditor(world).clearSelection();
+ }
+
+ if (!dragging) {
+ surface.marquee = null;
+ return;
+ }
+
+ const position = pointer.position;
+ if (!position || position.state === 'idle') return;
+
+ // Opening the marquee clears what was selected, so the drag starts from
+ // nothing and each clip decides for itself from there.
+ if (!surface.marquee) {
+ getDocumentEditor(world).clearSelection();
+ }
+
+ surface.marquee = {
+ x: Math.min(position.currentX, position.initialX),
+ y: Math.min(position.currentY, position.initialY),
+ width: Math.abs(position.currentX - position.initialX),
+ height: Math.abs(position.currentY - position.initialY),
+ };
+}
+
+/** The marquee rectangle itself, over the rows it is selecting. */
+export function renderMarquee(surface: TimelineSurfaceState): void {
+ const { ctx, marquee } = surface;
+ if (!ctx || !marquee) return;
+
+ ctx.save();
+
+ ctx.fillStyle = surface.colors.border.ring;
+ ctx.strokeStyle = surface.colors.border.ring;
+ ctx.lineWidth = 1;
+
+ ctx.globalAlpha = 0.2;
+ ctx.fillRect(marquee.x, marquee.y, marquee.width, marquee.height);
+
+ ctx.globalAlpha = 1;
+ ctx.strokeRect(marquee.x, marquee.y, marquee.width, marquee.height);
+
+ ctx.restore();
+}
diff --git a/apps/web/src/engine/timeline/render/playhead.ts b/apps/web/src/engine/timeline/render/playhead.ts
new file mode 100644
index 00000000..a6643a79
--- /dev/null
+++ b/apps/web/src/engine/timeline/render/playhead.ts
@@ -0,0 +1,157 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { KNOB_PATH } from '../paths';
+import { RULER_HEIGHT } from '../config';
+import { framesToPixels, getCurrentFrame, getFrameRate, getResolution, getScrollX } from '../view';
+
+import type { Entity, World } from 'koota';
+import type { TimelineSurfaceState } from '../surface';
+
+// The knob shown in place of the line while the timeline is collapsed.
+const PILL_TOP = 8;
+const PILL_HEIGHT = 19;
+const PILL_PADDING_X = 4;
+const PILL_RADIUS = 9;
+const PILL_CHAR_WIDTH = 6;
+
+// The playhead trails a motion blur while it moves, wide in proportion to how
+// fast it is going. Its width is a critically damped spring so that starting,
+// stopping and scrubbing all ease rather than snap.
+const GRADIENT_PX_PER_VELOCITY = 25;
+const GRADIENT_RESPONSE_TIME = 0.15;
+const GRADIENT_MAX_WIDTH = 150;
+
+let gradientWidth = 0;
+let gradientVelocity = 0;
+let lastFrame = 0;
+let lastTimestamp = 0;
+
+export function renderPlayhead(world: World, scene: Entity, surface: TimelineSurfaceState): void {
+ const { ctx, canvas } = surface;
+ if (!ctx || !canvas) return;
+
+ const frame = getCurrentFrame(world, scene);
+ const scrollX = getScrollX(world, scene);
+ const resolution = getResolution(world, scene);
+
+ ctx.save();
+ ctx.translate(-(scrollX * resolution), 0);
+ ctx.translate(framesToPixels(frame, resolution), 0);
+
+ if (surface.minimized) {
+ renderCollapsedPlayhead(surface, frame);
+ ctx.restore();
+ return;
+ }
+
+ // The knob, sitting in the ruler.
+ ctx.save();
+ ctx.translate(-5, 2);
+ ctx.fillStyle = surface.colors.border.ring;
+ ctx.strokeStyle = surface.colors.border.darker;
+ ctx.lineWidth = 2;
+ ctx.stroke(KNOB_PATH);
+ ctx.fill(KNOB_PATH);
+ ctx.restore();
+
+ ctx.translate(0, RULER_HEIGHT);
+
+ renderMotionTrail(world, surface, frame);
+
+ ctx.beginPath();
+ ctx.moveTo(0, 0);
+ ctx.lineTo(0, canvas.height);
+ ctx.closePath();
+
+ ctx.strokeStyle = surface.colors.border.darker;
+ ctx.lineWidth = 3;
+ ctx.stroke();
+
+ ctx.strokeStyle = surface.colors.border.ring;
+ ctx.lineWidth = 1;
+ ctx.stroke();
+
+ ctx.restore();
+}
+
+/**
+ * The trail behind the playhead. Its width follows the playhead's *measured*
+ * velocity rather than the playback speed, so a scrub trails as much as a
+ * play does and a pause settles rather than cutting off; the sign says which
+ * way it is going, and the trail is drawn on the side being left behind.
+ */
+function renderMotionTrail(world: World, surface: TimelineSurfaceState, frame: number): void {
+ const { ctx, canvas } = surface;
+ if (!ctx || !canvas) return;
+
+ const now = performance.now();
+ const elapsed = lastTimestamp ? (now - lastTimestamp) / 1000 : 0;
+ lastTimestamp = now;
+
+ const velocity = elapsed > 0 ? (frame - lastFrame) / getFrameRate(world) / elapsed : 0;
+ lastFrame = frame;
+
+ const target = Math.max(-GRADIENT_MAX_WIDTH, Math.min(GRADIENT_MAX_WIDTH, GRADIENT_PX_PER_VELOCITY * velocity));
+
+ // Closed form, so any frame length is stable — a long stall cannot
+ // overshoot the way a stepped integration would.
+ if (elapsed > 0) {
+ const omega = 2 / GRADIENT_RESPONSE_TIME;
+ const dt = Math.min(elapsed, 0.25);
+ const decay = Math.exp(-omega * dt);
+ const error = gradientWidth - target;
+ const b = gradientVelocity + omega * error;
+ gradientWidth = target + (error + b * dt) * decay;
+ gradientVelocity = (gradientVelocity - omega * b * dt) * decay;
+ }
+
+ const width = Math.abs(gradientWidth);
+ if (width <= 0.5) return;
+
+ const reverse = gradientWidth < 0;
+ const gradient = ctx.createLinearGradient(reverse ? width : -width, 0, 0, 0);
+ gradient.addColorStop(0, surface.colors.border.ring + '00');
+ gradient.addColorStop(1, surface.colors.border.ring);
+
+ ctx.fillStyle = gradient;
+ ctx.globalAlpha = 0.16;
+ ctx.fillRect(reverse ? 0 : -width, 0, width, canvas.height);
+ ctx.globalAlpha = 1;
+}
+
+/** Collapsed, the playhead is a stub with the frame number in it. */
+function renderCollapsedPlayhead(surface: TimelineSurfaceState, frame: number): void {
+ const ctx = surface.ctx!;
+ const label = `${Math.round(frame)}`;
+ const width = label.length * PILL_CHAR_WIDTH + PILL_PADDING_X * 2;
+
+ ctx.font = '400 10px JetBrains Mono';
+ ctx.textAlign = 'center';
+ ctx.textBaseline = 'middle';
+
+ ctx.beginPath();
+ ctx.moveTo(0, PILL_TOP + PILL_HEIGHT);
+ ctx.lineTo(0, RULER_HEIGHT);
+
+ ctx.strokeStyle = surface.colors.border.input;
+ ctx.lineWidth = 4;
+ ctx.stroke();
+
+ ctx.strokeStyle = surface.colors.border.ring;
+ ctx.lineWidth = 2;
+ ctx.stroke();
+
+ ctx.beginPath();
+ ctx.roundRect(-width / 2, PILL_TOP, width, PILL_HEIGHT, PILL_RADIUS);
+
+ ctx.fillStyle = surface.colors.border.ring;
+ ctx.strokeStyle = surface.colors.border.darker;
+ ctx.lineWidth = 1;
+ ctx.fill();
+ ctx.stroke();
+
+ ctx.fillStyle = 'white';
+ ctx.fillText(label, 0, PILL_TOP + PILL_HEIGHT / 2 + 0.5);
+}
diff --git a/apps/web/src/engine/timeline/render/ruler.ts b/apps/web/src/engine/timeline/render/ruler.ts
new file mode 100644
index 00000000..f914ec4a
--- /dev/null
+++ b/apps/web/src/engine/timeline/render/ruler.ts
@@ -0,0 +1,134 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { Playback, Workarea, setPlayhead } from '@diffusionstudio/runtime';
+
+import { assert } from '@/utils';
+import { RULER_INTERVALS } from '../constants';
+import {
+ RULER_HEIGHT,
+ RULER_LABEL_Y,
+ RULER_TICK_HEIGHT_MAJOR,
+ RULER_TICK_HEIGHT_MINOR,
+ TARGET_MAJOR_TICK_DISTANCE,
+} from '../config';
+import { framesToPixels, getFrameRate, getResolution, getScrollX, getViewport, pixelsToFrames } from '../view';
+
+import type { Entity, World } from 'koota';
+import type { TimelineSurfaceState } from '../surface';
+
+/**
+ * The time ruler, and the two gestures that belong to it: a drag scrubs the
+ * playhead, a shift-drag sets the work area. Both are ephemeral state, so
+ * neither is an edit — the file has nothing to say about where the playhead
+ * is or what part of the scene is being previewed.
+ */
+export function renderRuler(world: World, scene: Entity, surface: TimelineSurfaceState): void {
+ const { ctx, pointer } = surface;
+ if (!ctx || !pointer) return;
+
+ const scrollX = getScrollX(world, scene);
+ const resolution = getResolution(world, scene);
+ const fps = getFrameRate(world);
+
+ ctx.save();
+ ctx.translate(-(scrollX * resolution), 0);
+
+ const [minX, maxX] = getViewport(world, scene, surface.layout.width);
+
+ const { dragging, clicked } = pointer
+ .scope('ruler')
+ .region(minX, 0, maxX - minX, RULER_HEIGHT);
+
+ // A scrub takes over from playback: the playhead goes where it is put.
+ if (clicked || (dragging && !pointer.shiftPressed)) {
+ assert(pointer.position, 'Pointer position must be set');
+
+ // Set rather than written through the store, and only when it would
+ // change something: the transport button is watching.
+ if (scene.get(Playback)?.playing) scene.set(Playback, { playing: false });
+
+ setPlayhead(world, scene, pixelsToFrames(pointer.position.currentX + scrollX * resolution, resolution));
+ }
+
+ if (dragging && pointer.shiftPressed) {
+ assert(pointer.position, 'Pointer position must be set');
+ assert(pointer.position.state !== 'idle', 'Pointer must be pressing');
+
+ const start = pixelsToFrames(pointer.position.initialX + scrollX * resolution, resolution);
+ const end = pixelsToFrames(pointer.position.currentX + scrollX * resolution, resolution);
+
+ scene.add(Workarea);
+ scene.set(Workarea, { start, end });
+
+ // The playhead follows the edge being dragged, so the frame under the
+ // pointer is the one on the canvas.
+ setPlayhead(world, scene, pointer.position.currentX > pointer.position.initialX ? end : start);
+ }
+
+ const interval = getRulerInterval(resolution);
+ const firstFrame = pixelsToFrames(minX, resolution);
+ const lastFrame = pixelsToFrames(maxX, resolution);
+ const step = Math.max(1, Math.round(interval.numerator / interval.denominator));
+
+ const majorOffset = RULER_HEIGHT - RULER_TICK_HEIGHT_MAJOR;
+ const minorOffset = RULER_HEIGHT - RULER_TICK_HEIGHT_MINOR;
+
+ ctx.font = '300 10px JetBrains Mono';
+ ctx.fillStyle = surface.colors.ruler.text;
+ ctx.textBaseline = 'middle';
+ ctx.textAlign = 'center';
+
+ for (let frame = Math.floor(firstFrame / step) * step; frame <= lastFrame; frame += step) {
+ const x = framesToPixels(frame, resolution);
+ let y = minorOffset;
+
+ // Only the ticks of the interval itself are labelled; the rest are the
+ // subdivisions between them.
+ if (frame % interval.numerator === 0) {
+ y = majorOffset;
+ ctx.fillText(formatTickLabel(frame, fps), x, RULER_LABEL_Y);
+ }
+
+ ctx.lineWidth = 1;
+ ctx.strokeStyle = surface.colors.ruler.tick;
+ ctx.beginPath();
+ ctx.moveTo(x, y);
+ ctx.lineTo(x, RULER_HEIGHT);
+ ctx.stroke();
+ }
+
+ ctx.restore();
+}
+
+/** Whole seconds read as a clock, anything finer as a frame count. */
+function formatTickLabel(frame: number, fps: number): string {
+ if (frame === 0) return '0';
+ if (frame % fps !== 0) return `${frame}f`;
+
+ const total = Math.round(frame / fps);
+ const minutes = Math.floor(total / 60).toString().padStart(2, '0');
+ const seconds = Math.floor(total % 60).toString().padStart(2, '0');
+
+ return `${minutes}:${seconds}`;
+}
+
+/**
+ * The tick interval whose labelled ticks land nearest the target spacing at
+ * this zoom, so the ruler stays about as dense however far in it is.
+ */
+function getRulerInterval(resolution: number) {
+ let closest = RULER_INTERVALS[0]!;
+ let closestDelta = Infinity;
+
+ for (const interval of RULER_INTERVALS) {
+ const delta = Math.abs(framesToPixels(interval.numerator, resolution) - TARGET_MAJOR_TICK_DISTANCE);
+ if (delta < closestDelta) {
+ closest = interval;
+ closestDelta = delta;
+ }
+ }
+
+ return closest;
+}
diff --git a/apps/web/src/engine/timeline/render/snap.ts b/apps/web/src/engine/timeline/render/snap.ts
new file mode 100644
index 00000000..78adea24
--- /dev/null
+++ b/apps/web/src/engine/timeline/render/snap.ts
@@ -0,0 +1,34 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { RULER_HEIGHT } from '../config';
+import { getResolution, getScrollX } from '../view';
+
+import type { Entity, World } from 'koota';
+import type { TimelineSurfaceState } from '../surface';
+
+/**
+ * The line a dragged edge has stuck to, down the full height of the rows.
+ * Cleared as it is drawn: whatever is dragging says again next frame, and a
+ * drag that has ended says nothing.
+ */
+export function renderSnapLine(world: World, scene: Entity, surface: TimelineSurfaceState): void {
+ const { ctx, canvas, snapX } = surface;
+ if (!ctx || !canvas || snapX === null) return;
+
+ ctx.save();
+ ctx.translate(-(getScrollX(world, scene) * getResolution(world, scene)), 0);
+
+ ctx.beginPath();
+ ctx.moveTo(snapX, RULER_HEIGHT);
+ ctx.lineTo(snapX, canvas.height / window.devicePixelRatio);
+
+ ctx.strokeStyle = surface.colors.border.scrubber;
+ ctx.lineWidth = 1;
+ ctx.stroke();
+
+ ctx.restore();
+
+ surface.snapX = null;
+}
diff --git a/apps/web/src/engine/timeline/render/thumbnails.ts b/apps/web/src/engine/timeline/render/thumbnails.ts
new file mode 100644
index 00000000..c4f2281f
--- /dev/null
+++ b/apps/web/src/engine/timeline/render/thumbnails.ts
@@ -0,0 +1,104 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { Computed, store } from '@diffusionstudio/runtime';
+
+import { CLIP_LABEL_HEIGHT } from '../config';
+import { resolvePoster } from '../media';
+import { framesToPixels, getResolution, getViewport } from '../view';
+
+import type { Asset } from '@diffusionstudio/assets';
+import type { Entity, World } from 'koota';
+import type { RowCursor } from '../layout';
+import type { TimelineSurfaceState } from '../surface';
+
+/**
+ * Tile widths, chosen by row height. Three fixed sizes rather than one
+ * derived from the height, so dragging a row taller does not slide every
+ * tile along as it goes.
+ */
+const TILE_WIDTHS = [80, 100, 120] as const;
+
+const TOP = 20;
+const INSET = 2;
+
+/**
+ * The clip's picture, tiled along it. An image shows the same frame however
+ * long it runs, so tiling its poster is the whole truth about it; a video's
+ * poster is only its first frame, so this says where the footage is rather
+ * than what is in it.
+ */
+export function renderThumbnails(
+ world: World,
+ scene: Entity,
+ surface: TimelineSurfaceState,
+ entity: Entity,
+ asset: Asset,
+ row: RowCursor,
+ options: { top?: number; height?: number } = {},
+): void {
+ const ctx = surface.ctx!;
+
+ const poster = resolvePoster(world, asset);
+ if (!poster) return;
+
+ const top = options.top ?? TOP;
+ const height = options.height ?? row.height - CLIP_LABEL_HEIGHT;
+ if (height < 4) return;
+
+ const computed = store(world, Computed);
+ const resolution = getResolution(world, scene);
+
+ const left = framesToPixels(computed.start[entity.id()] ?? 0, resolution);
+ const width = framesToPixels(computed.end[entity.id()] ?? 0, resolution) - left;
+ if (width < 4) return;
+
+ const tile = tileWidth(row.height);
+ const crop = coverCrop(poster.width, poster.height, tile, height);
+
+ // Only the tiles on screen: a clip an hour long is as cheap to draw as a
+ // clip a second long.
+ const [viewportLeft, viewportRight] = getViewport(world, scene, surface.layout.width);
+ const first = Math.max(0, Math.floor((viewportLeft - left) / tile));
+ const last = Math.min(Math.ceil(width / tile) - 1, Math.floor((viewportRight - left) / tile));
+
+ ctx.save();
+
+ ctx.beginPath();
+ ctx.roundRect(left + INSET, top, width - INSET * 2, height - INSET, 2);
+ ctx.clip();
+
+ for (let i = first; i <= last; i++) {
+ ctx.drawImage(
+ poster,
+ crop.x, crop.y, crop.width, crop.height,
+ left + i * tile, top, tile, height,
+ );
+ }
+
+ ctx.restore();
+}
+
+function tileWidth(rowHeight: number): number {
+ if (rowHeight >= 100) return TILE_WIDTHS[2];
+ if (rowHeight >= 70) return TILE_WIDTHS[1];
+ return TILE_WIDTHS[0];
+}
+
+/**
+ * The part of the picture to draw, so a tile is filled rather than letterboxed
+ * — the same as `object-fit: cover`, cropped from the middle.
+ */
+function coverCrop(sourceWidth: number, sourceHeight: number, width: number, height: number) {
+ const source = sourceWidth / sourceHeight;
+ const target = width / height;
+
+ if (source > target) {
+ const cropped = sourceHeight * target;
+ return { x: (sourceWidth - cropped) / 2, y: 0, width: cropped, height: sourceHeight };
+ }
+
+ const cropped = sourceWidth / target;
+ return { x: 0, y: (sourceHeight - cropped) / 2, width: sourceWidth, height: cropped };
+}
diff --git a/apps/web/src/engine/timeline/render/waveform.ts b/apps/web/src/engine/timeline/render/waveform.ts
new file mode 100644
index 00000000..88514246
--- /dev/null
+++ b/apps/web/src/engine/timeline/render/waveform.ts
@@ -0,0 +1,104 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { AssetId, Cache, Computed, findAssetDuration, getSourceWindow, resolveWaveformPeaks, store } from '@diffusionstudio/runtime';
+
+import { framesToPixels, getResolution } from '../view';
+
+import type { Entity, World } from 'koota';
+import type { RowCursor } from '../layout';
+import type { TimelineSurfaceState } from '../surface';
+
+/** How wide a bar is, and how much of that is the gap after it. */
+const BAR_WIDTH = 2;
+const BAR_GAP = 1;
+const MIN_BAR_HEIGHT = 1;
+
+type WaveformOptions = {
+ color: string;
+ /** How far down the clip the waveform starts. */
+ offsetY: number;
+ /** How much of the height to leave above and below it. */
+ padding: number;
+};
+
+/**
+ * The clip's audio, as bars across the part of the waveform it plays.
+ *
+ * The peaks belong to the asset, not the clip: they summarise the whole file
+ * in a fixed number of bars (`PEAK_BARS`), and the clip's own source window
+ * says which stretch of them is its own. That is what makes a split cost
+ * nothing — both halves read the same summary, from different ends of it.
+ *
+ * The summary is coarse, so a clip zoomed in far enough draws a smoothed
+ * version of its audio rather than its samples.
+ */
+export function renderWaveform(
+ world: World,
+ scene: Entity,
+ surface: TimelineSurfaceState,
+ entity: Entity,
+ row: RowCursor,
+ options: WaveformOptions,
+): void {
+ const ctx = surface.ctx!;
+
+ const source = findPeakSource(entity);
+ if (!source) return;
+
+ const peaks = resolveWaveformPeaks(world, source);
+ if (!peaks || peaks.length === 0) return;
+
+ const height = row.height - options.offsetY - options.padding;
+ if (height < 2) return;
+
+ const computed = store(world, Computed);
+ const resolution = getResolution(world, scene);
+
+ const left = framesToPixels(computed.start[entity.id()] ?? 0, resolution);
+ const width = framesToPixels(computed.end[entity.id()] ?? 0, resolution) - left;
+ if (width < BAR_WIDTH) return;
+
+ // Which stretch of the asset this clip plays, as a fraction of it: the
+ // source window is in source frames, and the peaks span all of them.
+ const duration = findAssetDuration(world, entity) ?? 0;
+ const window = getSourceWindow(entity);
+ const from = duration > 0 ? window.in / duration : 0;
+ const to = duration > 0 ? Math.min(1, window.out / duration) : 1;
+
+ const step = BAR_WIDTH + BAR_GAP;
+ const count = Math.floor(width / step);
+ if (count <= 0) return;
+
+ const centerY = options.offsetY + height / 2;
+
+ ctx.save();
+ ctx.fillStyle = options.color;
+
+ for (let i = 0; i < count; i++) {
+ // Where along the clip this bar is, mapped into the asset's summary.
+ const position = from + ((i + 0.5) / count) * (to - from);
+ const value = (peaks[Math.min(peaks.length - 1, Math.floor(position * peaks.length))] ?? 0) / 255;
+ const barHeight = Math.max(value * height, MIN_BAR_HEIGHT);
+
+ ctx.fillRect(left + i * step, centerY - barHeight / 2, BAR_WIDTH, barHeight);
+ }
+
+ ctx.restore();
+}
+
+/**
+ * What holds the asset the peaks come from: the clip itself when it is its
+ * own source (an audio clip), otherwise whichever fill names one. Whether
+ * that asset has any audio is `resolveWaveformPeaks`'s to say.
+ */
+function findPeakSource(entity: Entity): Entity | null {
+ if (entity.has(AssetId)) return entity;
+
+ for (const fill of entity.get(Cache)?.fills ?? []) {
+ if (fill.has(AssetId)) return fill;
+ }
+
+ return null;
+}
diff --git a/apps/web/src/engine/timeline/render/workarea.ts b/apps/web/src/engine/timeline/render/workarea.ts
new file mode 100644
index 00000000..0a5fa7ef
--- /dev/null
+++ b/apps/web/src/engine/timeline/render/workarea.ts
@@ -0,0 +1,109 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { Workarea } from '@diffusionstudio/runtime';
+
+import { assert } from '@/utils';
+import { IN_POINT_PATH, OUT_POINT_PATH } from '../paths';
+import { RULER_HEIGHT } from '../config';
+import { framesToPixels, getResolution, getScrollX, pixelsToFrames } from '../view';
+
+import type { Entity, World } from 'koota';
+import type { TimelineSurfaceState } from '../surface';
+
+const HANDLE_WIDTH = 5;
+const BAR_HEIGHT = 10;
+
+// Where the work area was when the drag started: every frame of the drag is
+// measured from there, so a slow drag and a fast one land in the same place.
+let dragOrigin: [start: number, end: number] = [0, 0];
+
+/**
+ * The work area — the stretch of the scene that plays and exports — as a bar
+ * in the ruler with a handle at each end. Drag the bar to move the window,
+ * a handle to move one edge, double-click to drop it altogether.
+ *
+ * Nothing here is an edit: the work area is a way of looking at the scene,
+ * not part of it, so it lives only as long as the world does.
+ */
+export function renderWorkarea(world: World, scene: Entity, surface: TimelineSurfaceState): void {
+ const { ctx, pointer } = surface;
+ if (!ctx || !pointer) return;
+ if (!scene.has(Workarea)) return;
+
+ const workarea = scene.get(Workarea)!;
+ const scrollX = getScrollX(world, scene);
+ const resolution = getResolution(world, scene);
+
+ const inPx = framesToPixels(workarea.start, resolution);
+ const outPx = framesToPixels(workarea.end, resolution);
+ const y = RULER_HEIGHT - BAR_HEIGHT;
+
+ /** The delta of the drag in flight, in frames. */
+ const draggedFrames = (): number => {
+ assert(pointer.position, 'Pointer position must be set');
+ assert(pointer.position.state !== 'idle', 'Pointer must be pressing');
+ return pixelsToFrames(pointer.position.deltaX, resolution);
+ };
+
+ ctx.save();
+ ctx.translate(-(scrollX * resolution), 0);
+
+ // The bar itself: drag to move the window, double-click to remove it.
+ {
+ const { dragging, pressed, doubleClicked } = pointer
+ .scope('workarea')
+ .region(inPx, y, outPx - inPx, BAR_HEIGHT);
+
+ if (pressed) dragOrigin = [workarea.start, workarea.end];
+
+ if (dragging) {
+ const delta = draggedFrames();
+ scene.set(Workarea, { start: dragOrigin[0] + delta, end: dragOrigin[1] + delta });
+ }
+
+ if (doubleClicked) {
+ scene.remove(Workarea);
+ ctx.restore();
+ return;
+ }
+
+ // Difference rather than a fill: the bar reads over the ruler's ticks
+ // and over the playhead alike, whatever is behind it.
+ ctx.globalCompositeOperation = 'difference';
+ ctx.fillStyle = 'rgba(100, 100, 100, 1)';
+ ctx.fillRect(inPx, y, outPx - inPx, BAR_HEIGHT);
+ ctx.globalCompositeOperation = 'source-over';
+ }
+
+ ctx.lineWidth = 1;
+ ctx.fillStyle = surface.colors.border.ring;
+ ctx.strokeStyle = surface.colors.border.darker;
+
+ // The in handle, drawn from its own origin so the path needs no offset.
+ {
+ ctx.translate(inPx - HANDLE_WIDTH, y);
+ ctx.stroke(IN_POINT_PATH);
+ ctx.fill(IN_POINT_PATH);
+
+ const { dragging, pressed } = pointer.region(0, 0, HANDLE_WIDTH, BAR_HEIGHT);
+
+ if (pressed) dragOrigin = [workarea.start, workarea.end];
+ if (dragging) scene.set(Workarea, { start: dragOrigin[0] + draggedFrames() });
+ }
+
+ // The out handle, relative to the in handle the context is now at.
+ {
+ ctx.translate(outPx - inPx + HANDLE_WIDTH, 0);
+ ctx.stroke(OUT_POINT_PATH);
+ ctx.fill(OUT_POINT_PATH);
+
+ const { dragging, pressed } = pointer.region(0, 0, HANDLE_WIDTH, BAR_HEIGHT);
+
+ if (pressed) dragOrigin = [workarea.start, workarea.end];
+ if (dragging) scene.set(Workarea, { end: dragOrigin[1] + draggedFrames() });
+ }
+
+ ctx.restore();
+}
diff --git a/apps/web/src/engine/timeline/snapping.ts b/apps/web/src/engine/timeline/snapping.ts
new file mode 100644
index 00000000..52d3fb90
--- /dev/null
+++ b/apps/web/src/engine/timeline/snapping.ts
@@ -0,0 +1,130 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * What a dragged edge sticks to: the start of the scene, the playhead, and
+ * the edges of every clip that is not moving. Worked out in frames rather
+ * than pixels — a frame is what the timeline is actually made of, and
+ * rounding to pixels and back makes a clip jitter by a frame either side of
+ * the snap at fractional zooms.
+ */
+
+import {
+ AdjustmentLayer,
+ ChildOf,
+ ClipDragOrigin,
+ Computed,
+ Geometry,
+ Group,
+ Selected,
+ TrimDragOrigin,
+ getActiveEntity,
+ getParentEntity,
+ store,
+} from '@diffusionstudio/runtime';
+import { Or } from 'koota';
+
+import { SNAP_DISTANCE } from './config';
+import { framesToPixels, getCurrentFrame } from './view';
+
+import type { Entity, World } from 'koota';
+
+const NODES = Or(Geometry, Group, AdjustmentLayer);
+
+/**
+ * The frames worth snapping to. Anything whose own time moves with the drag
+ * is useless as a target — an ancestor whose bounds come from what is being
+ * dragged, or a descendant carried along by it — so those are left out along
+ * with the dragged clips themselves.
+ */
+export function getSnapFrames(world: World): number[] {
+ const scene = getActiveEntity(world);
+ if (scene === null) return [];
+
+ const computed = store(world, Computed);
+
+ const active = [...world.query(NODES, ClipDragOrigin), ...world.query(NODES, TrimDragOrigin)];
+ const excluded = new Set<Entity>(active);
+
+ for (const entity of active) {
+ for (let parent = getParentEntity(entity); parent && parent !== scene; parent = getParentEntity(parent)) {
+ excluded.add(parent);
+ }
+ for (const descendant of descendantsOf(world, entity)) excluded.add(descendant);
+ }
+
+ // The beginning of the scene and the playhead are always worth landing on.
+ const frames = new Set<number>([0, getCurrentFrame(world, scene)]);
+
+ for (const entity of descendantsOf(world, scene)) {
+ if (excluded.has(entity) || entity.has(Selected)) continue;
+ frames.add(computed.start[entity.id()] ?? 0);
+ frames.add(computed.end[entity.id()] ?? 0);
+ }
+
+ return [...frames];
+}
+
+/**
+ * How far off a snap the drag currently is, in frames, for the caller to take
+ * off its own offset — or 0 when nothing is near enough. Every dragged clip
+ * offers both its edges, and the nearest pairing of an edge to a snap frame
+ * wins, so a clip snaps by whichever end is closest to something.
+ *
+ * The edges come from the snapshots taken when the drag began rather than
+ * from where the clips are now: a group's edges are its children's, which
+ * have already moved.
+ */
+export function findSnapDelta(world: World, resolution: number, offsetFrames: number): { delta: number; frame: number } | null {
+ const origins = store(world, ClipDragOrigin);
+
+ const edges = new Set<number>();
+ for (const entity of world.query(NODES, ClipDragOrigin)) {
+ const eid = entity.id();
+ edges.add((origins.start[eid] ?? 0) + offsetFrames);
+ edges.add((origins.end[eid] ?? 0) + offsetFrames);
+ }
+
+ return nearestSnap(getSnapFrames(world), edges, resolution);
+}
+
+/**
+ * The snap a single frame is nearest to — what a trim uses, where there is
+ * one edge being moved rather than a clip's two.
+ */
+export function findSnapFrame(world: World, resolution: number, frame: number): number | null {
+ return nearestSnap(getSnapFrames(world), new Set([frame]), resolution)?.frame ?? null;
+}
+
+function nearestSnap(frames: number[], edges: Set<number>, resolution: number): { delta: number; frame: number } | null {
+ let best: { delta: number; frame: number; distance: number } | null = null;
+
+ for (const frame of frames) {
+ for (const edge of edges) {
+ const delta = edge - frame;
+ const distance = Math.abs(framesToPixels(delta, resolution));
+ if (distance >= SNAP_DISTANCE) continue;
+ if (best && distance >= best.distance) continue;
+ best = { delta, frame, distance };
+ }
+ }
+
+ return best === null ? null : { delta: best.delta, frame: best.frame };
+}
+
+/** Every node under `entity`, however deep. */
+function descendantsOf(world: World, entity: Entity): Entity[] {
+ const found: Entity[] = [];
+
+ const walk = (parent: Entity): void => {
+ for (const child of world.query(NODES, ChildOf(parent))) {
+ found.push(child);
+ walk(child);
+ }
+ };
+
+ walk(entity);
+
+ return found;
+}
diff --git a/apps/web/src/engine/timeline/style.ts b/apps/web/src/engine/timeline/style.ts
new file mode 100644
index 00000000..e16a5bde
--- /dev/null
+++ b/apps/web/src/engine/timeline/style.ts
@@ -0,0 +1,61 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * What colour a clip is. A clip is coloured by what it *is* rather than by
+ * what it holds, so the tests run from the most specific kind outward — a
+ * caption is text geometry too, and would read as plain text if text were
+ * asked about first.
+ */
+
+import {
+ findGeometryAsset,
+ hasHtmlPaint,
+ hasSurfacePaint,
+ isAdjustmentLayer,
+ isCaption,
+ isGroup,
+ isMask,
+ isScene,
+ isText,
+} from '@diffusionstudio/runtime';
+
+import { COLORS } from './constants';
+
+import type { Asset } from '@diffusionstudio/assets';
+import type { Entity, World } from 'koota';
+
+export type ClipStyle = {
+ background: string;
+ foreground: string;
+ /** The colour of what is drawn inside: a waveform, a group's children. */
+ primary?: string;
+};
+
+export function getClipStyle(entity: Entity, asset: Asset | null): ClipStyle {
+ if (isCaption(entity)) return COLORS.clip.caption;
+ if (isText(entity)) return COLORS.clip.text;
+ if (isScene(entity)) return COLORS.clip.scene;
+ if (isGroup(entity)) return COLORS.clip.group;
+ if (isMask(entity)) return COLORS.clip.mask;
+ if (isAdjustmentLayer(entity)) return COLORS.clip.adjustment;
+ if (hasHtmlPaint(entity) || hasSurfacePaint(entity)) return COLORS.clip.html;
+
+ switch (asset?.type) {
+ case 'VIDEO':
+ case 'SEQUENCE':
+ return COLORS.clip.video;
+ case 'IMAGE':
+ return COLORS.clip.image;
+ case 'AUDIO':
+ return COLORS.clip.audio;
+ default:
+ return COLORS.clip.shape;
+ }
+}
+
+/** The asset a clip shows, whether it is the clip's own or one of its fills. */
+export function getClipAsset(world: World, entity: Entity): Asset | null {
+ return findGeometryAsset(world, entity);
+}
diff --git a/apps/web/src/engine/timeline/surface.ts b/apps/web/src/engine/timeline/surface.ts
new file mode 100644
index 00000000..0f8f7f51
--- /dev/null
+++ b/apps/web/src/engine/timeline/surface.ts
@@ -0,0 +1,72 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * The timeline's own canvas, on the world beside the stage's own
+ * (`RenderSurface`). A second surface rather than a second world: the
+ * timeline shows the same entities the canvas does, only against time
+ * instead of space.
+ *
+ * Interaction here is immediate-mode — a render function asks the pointer
+ * about the rectangle it is about to draw and acts on the answer in the same
+ * breath (see `./pointer`). That is not how the stage works, where the render
+ * pass leaves `HitRegions` behind for the input system to dispatch against;
+ * the difference is that a clip's rectangle *is* the drawing, so naming it a
+ * second time buys nothing.
+ *
+ * Held as one mutable object rather than a trait of fields, since a render
+ * pass writes to it as it goes (which row it is in, what the cursor should
+ * be) and none of it is worth an event.
+ */
+
+import { trait } from 'koota';
+
+import { COLORS } from './constants';
+
+import type { CursorType } from '@/hooks/use-cursor';
+import type { TimelinePointer } from './pointer';
+
+/** The drag rectangle, in CSS pixels of the timeline canvas. */
+export type Marquee = {
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+};
+
+export type TimelineSurfaceState = {
+ canvas: HTMLCanvasElement | null;
+ ctx: CanvasRenderingContext2D | null;
+ pointer: TimelinePointer | null;
+ /** The canvas parent's box, which is what everything is laid out in. */
+ layout: DOMRect;
+ /** Set by whichever render function claims the pointer, each frame. */
+ cursor: CursorType;
+ /** Collapsed to the ruler alone: the rows are not drawn. */
+ minimized: boolean;
+ marquee: Marquee | null;
+ /** Where the edge being dragged snapped this frame, in timeline pixels. */
+ snapX: number | null;
+ colors: typeof COLORS;
+};
+
+export function createTimelineSurface(): TimelineSurfaceState {
+ return {
+ canvas: null,
+ ctx: null,
+ pointer: null,
+ layout: new DOMRect(),
+ cursor: 'default',
+ minimized: false,
+ marquee: null,
+ snapX: null,
+ colors: COLORS,
+ };
+}
+
+/**
+ * The surface itself, on the world: the state is the trait's value, so
+ * `world.get(TimelineSurface)` hands back the live object to write to.
+ */
+export const TimelineSurface = trait(() => createTimelineSurface());
diff --git a/apps/web/src/engine/timeline/text.ts b/apps/web/src/engine/timeline/text.ts
new file mode 100644
index 00000000..723575b5
--- /dev/null
+++ b/apps/web/src/engine/timeline/text.ts
@@ -0,0 +1,56 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// Measuring is the expensive part of drawing a label, and a clip's label does
+// not change from frame to frame, so widths are kept. Widths measured before
+// the web fonts land describe the fallback and under-report once the real
+// font swaps in, so the cache is dropped when that happens.
+const WIDTHS = new Map<string, number>();
+
+document?.fonts.addEventListener('loadingdone', () => WIDTHS.clear());
+
+/**
+ * `text` if it fits in `maxWidth`, otherwise as much of it as fits with an
+ * ellipsis, or null when not even the ellipsis does. `font` keys the cache,
+ * since the same string measures differently in two of them.
+ */
+export function truncateText(
+ ctx: CanvasRenderingContext2D,
+ text: string,
+ maxWidth: number,
+ font: string = 'default',
+): string | null {
+ const measure = (value: string): number => {
+ const key = `${font}-${value}`;
+ const cached = WIDTHS.get(key);
+ if (cached !== undefined) return cached;
+
+ const width = ctx.measureText(value).width;
+ WIDTHS.set(key, width);
+ return width;
+ };
+
+ if (measure(text) <= maxWidth) return text;
+
+ const available = maxWidth - measure('...');
+ if (available <= 0) return null;
+
+ // Binary search rather than a walk: a long label is measured a handful of
+ // times instead of once per character.
+ let low = 0;
+ let high = text.length;
+ let best = 0;
+
+ while (low <= high) {
+ const mid = Math.floor((low + high) / 2);
+ if (measure(text.slice(0, mid)) <= available) {
+ best = mid;
+ low = mid + 1;
+ } else {
+ high = mid - 1;
+ }
+ }
+
+ return best > 0 ? `${text.slice(0, best)}...` : null;
+}
diff --git a/apps/web/src/engine/timeline/timeline.ts b/apps/web/src/engine/timeline/timeline.ts
new file mode 100644
index 00000000..46b4d6bb
--- /dev/null
+++ b/apps/web/src/engine/timeline/timeline.ts
@@ -0,0 +1,75 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * The timeline's draw pass, run once a frame by the engine loop alongside the
+ * stage's own. Immediate mode: the render functions decide what the pointer
+ * did to the thing they are about to draw as they draw it, so a frame both
+ * paints the timeline and handles what was done to it (see `./surface`).
+ */
+
+import { useCursor } from '@/hooks/use-cursor';
+import { RULER_HEIGHT } from './config';
+import { renderLayers, renderMarquee, renderPlayhead, renderRuler, renderSnapLine, renderWorkarea, updateMarquee } from './render';
+import { updateDragGestures } from './drag';
+import { TimelineSurface } from './surface';
+import { ensureTimelineView, getTimelineScene } from './view';
+
+import type { World } from 'koota';
+
+const cursor = useCursor();
+
+export function timelineSystem(world: World): void {
+ const surface = world.get(TimelineSurface);
+ const { canvas, ctx, pointer } = surface ?? {};
+ if (!surface || !canvas || !ctx || !pointer) return;
+
+ const scene = getTimelineScene(world);
+ if (scene === null) return;
+
+ ensureTimelineView(world, scene);
+
+ try {
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+ ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
+
+ // Claimed during the pass by whatever the pointer is over; nothing
+ // claiming it means it is over nothing in particular.
+ surface.cursor = 'default';
+
+ // Before anything is drawn, and whether or not the rows are: a gesture
+ // that ended has to be closed before the clips it moved are laid out
+ // again, and collapsing the timeline mid-drag must not strand one.
+ updateDragGestures(world, surface);
+
+ // The rows are clipped to below the ruler, which is then painted over
+ // the top of them: a clip scrolled up under the ruler disappears
+ // behind it rather than through it.
+ if (!surface.minimized) {
+ updateMarquee(world, surface);
+
+ ctx.save();
+ ctx.beginPath();
+ ctx.rect(0, RULER_HEIGHT, canvas.width, canvas.height);
+ ctx.clip();
+ renderLayers(world, scene, surface);
+ renderMarquee(surface);
+ ctx.restore();
+ }
+
+ renderRuler(world, scene, surface);
+ renderWorkarea(world, scene, surface);
+ renderPlayhead(world, scene, surface);
+
+ if (!surface.minimized) renderSnapLine(world, scene, surface);
+
+ cursor.set(canvas, surface.cursor);
+ } catch (error) {
+ console.error(error);
+ } finally {
+ // The regions drawn this frame are what the next frame tests against.
+ pointer.reset();
+ }
+}
diff --git a/apps/web/src/engine/timeline/view.ts b/apps/web/src/engine/timeline/view.ts
new file mode 100644
index 00000000..e1ef3254
--- /dev/null
+++ b/apps/web/src/engine/timeline/view.ts
@@ -0,0 +1,119 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * Where the timeline is looking: how far it has scrolled and how many pixels
+ * a frame is worth. The state lives on the scene being shown (the runtime's
+ * `Timeline` trait) so every scene keeps its own place, and it is written
+ * through the store rather than `entity.set`: a scroll is not an edit of the
+ * project, so nothing should observe it and nothing reaches the file.
+ */
+
+import { Computed, FrameRate, Timeline, getActiveEntity, store } from '@diffusionstudio/runtime';
+
+import { DEFAULT_TIMELINE_RESOLUTION, RULER_HEIGHT, TIMELINE_PADDING_LEFT } from './config';
+
+import type { Entity, World } from 'koota';
+
+/** The scene the timeline shows: the one holding the playhead. */
+export function getTimelineScene(world: World): Entity | null {
+ return getActiveEntity(world);
+}
+
+/**
+ * The scene's view state, put there on first sight of it. The scroll starts
+ * slightly negative so frame 0 sits a padding's width in from the left edge
+ * rather than against it.
+ */
+export function ensureTimelineView(world: World, scene: Entity): void {
+ if (scene.has(Timeline)) return;
+
+ scene.add(Timeline);
+
+ const view = store(world, Timeline);
+ const eid = scene.id();
+ view.resolution[eid] = DEFAULT_TIMELINE_RESOLUTION;
+ view.scrollX[eid] = -TIMELINE_PADDING_LEFT / DEFAULT_TIMELINE_RESOLUTION;
+ view.scrollY[eid] = 0;
+ view.transform[eid] = new DOMMatrix();
+}
+
+/** Pixels per frame. Never 0, which would collapse the whole timeline onto one column. */
+export function getResolution(world: World, scene: Entity): number {
+ return store(world, Timeline).resolution[scene.id()] || DEFAULT_TIMELINE_RESOLUTION;
+}
+
+/** How far the timeline has scrolled, in frames (not pixels: zoom must not move it). */
+export function getScrollX(world: World, scene: Entity): number {
+ return store(world, Timeline).scrollX[scene.id()] ?? 0;
+}
+
+/** How far the rows have scrolled, in pixels. */
+export function getScrollY(world: World, scene: Entity): number {
+ return store(world, Timeline).scrollY[scene.id()] ?? 0;
+}
+
+/**
+ * Scrolls to `frames`, kept at or past the padding so the timeline cannot be
+ * pushed away from its own beginning.
+ */
+export function setScrollX(world: World, scene: Entity, frames: number): void {
+ const resolution = getResolution(world, scene);
+ store(world, Timeline).scrollX[scene.id()] = Math.max(-TIMELINE_PADDING_LEFT / resolution, frames);
+}
+
+export function setScrollY(world: World, scene: Entity, pixels: number): void {
+ store(world, Timeline).scrollY[scene.id()] = pixels;
+}
+
+export function setResolution(world: World, scene: Entity, resolution: number): void {
+ store(world, Timeline).resolution[scene.id()] = resolution;
+}
+
+export function framesToPixels(frames: number, resolution: number): number {
+ return Math.floor(frames * resolution);
+}
+
+export function pixelsToFrames(pixels: number, resolution: number): number {
+ return Math.round(pixels / resolution);
+}
+
+/** The playhead of the scene being shown, in frames. */
+export function getCurrentFrame(world: World, scene: Entity): number {
+ return store(world, Computed).localTime[scene.id()] ?? 0;
+}
+
+export function getFrameRate(world: World): number {
+ return world.get(FrameRate)?.value ?? 30;
+}
+
+/**
+ * The stretch of the timeline on screen, in its own pixels: what the render
+ * functions clip their loops to so a long project costs no more to draw than
+ * a short one.
+ */
+export function getViewport(world: World, scene: Entity, width: number): [left: number, right: number] {
+ const left = getScrollX(world, scene) * getResolution(world, scene);
+ return [left, left + width];
+}
+
+/**
+ * Re-derives the matrix the rows are drawn under: device pixels, scrolled,
+ * and below the ruler. Called whenever the scroll, the zoom or the canvas
+ * itself has moved.
+ */
+export function updateTimelineTransform(world: World, scene: Entity): void {
+ ensureTimelineView(world, scene);
+
+ const view = store(world, Timeline);
+ const eid = scene.id();
+ const dpr = window.devicePixelRatio;
+
+ const transform = new DOMMatrix();
+ transform.scaleSelf(dpr, dpr);
+ transform.translateSelf(-(view.scrollX[eid]! * view.resolution[eid]!), -view.scrollY[eid]!);
+ transform.translateSelf(0, RULER_HEIGHT);
+
+ view.transform[eid] = transform;
+}
diff --git a/apps/web/src/engine/timing.ts b/apps/web/src/engine/timing.ts
index 4e8c6fa1..a8405a13 100644
--- a/apps/web/src/engine/timing.ts
+++ b/apps/web/src/engine/timing.ts
@@ -16,6 +16,7 @@ import {
End,
FrameRate,
SourceOut,
+ Start,
framesToSeconds,
getSourceFrameAt,
getTimelineOrigin,
@@ -70,3 +71,29 @@ export function trimOut(world: World, entity: Entity, frame: number): void {
}
editTime(world, entity, 'end', frame - getTimelineOrigin(entity));
}
+
+/**
+ * Moves the node so it starts at scene frame `frame`, keeping everything else
+ * about it: the same stretch of its source plays, for the same length, only
+ * later or earlier.
+ *
+ * Start and End are both parent-relative — a node's length is `end - start` —
+ * so a move is both of them by the same amount. Writing only the start would
+ * leave the end where it was and stretch the clip, which is a trim.
+ *
+ * A container that takes its bounds from its children authors no End, and so
+ * only its start moves: its children are placed against its origin, and they
+ * all travel with it.
+ */
+export function moveEntityTo(world: World, entity: Entity, frame: number): void {
+ const start = frame - getTimelineOrigin(entity);
+ const delta = start - (entity.get(Start)?.value ?? 0);
+ if (delta === 0) return;
+
+ // Before the start, which moves the origin the end would then be read
+ // against — both are worked out from what the node says now.
+ const end = entity.get(End)?.value;
+ if (end !== undefined) editTime(world, entity, 'end', end + delta);
+
+ editTime(world, entity, 'start', start);
+}
diff --git a/apps/web/src/pages/editor.tsx b/apps/web/src/pages/editor.tsx
index d954d231..6f92b2c9 100644
--- a/apps/web/src/pages/editor.tsx
+++ b/apps/web/src/pages/editor.tsx
@@ -9,7 +9,7 @@ import { Soundboard, Inspector } from "@/components/sidebar-right";
import { FloatingProjectHeader, SidebarLeft } from "@/components/sidebar-left";
import { useLayout, MIN_TIMELINE_HEIGHT } from "@/context/layout";
import { useEditorApi } from "@/context/dapi";
-import { RULER_HEIGHT } from "@/components/engine/timeline/config";
+import { RULER_HEIGHT } from "@/engine/timeline";
import { createEffect, onCleanup } from 'solid-js';
import { toast } from 'somoto';
import { useWorld } from '@diffusionstudio/koota-solid';
diff --git a/packages/runtime/src/traits/interaction.ts b/packages/runtime/src/traits/interaction.ts
index e87bb952..0266e8df 100644
--- a/packages/runtime/src/traits/interaction.ts
+++ b/packages/runtime/src/traits/interaction.ts
@@ -27,11 +27,20 @@ export const Timeline = trait({
transform: () => new DOMMatrix(),
});
-// Snapshot of a clip's timing at the moment a drag started.
-export const ClipDragOrigin = trait({ delay: 0, start: 0, end: 0 });
-
+// What a clip's timing was when a drag of it began, so every frame of the
+// drag places it at origin + delta rather than nudging it by the last
+// frame's movement — a slow drag and a fast one over the same distance then
+// land in the same place, and nothing accumulates.
+//
+// `authored` is the Start the clip was written with, which is what the drag
+// rewrites. `start`/`end` are the absolute bounds it had, which a group needs
+// separately: a group's edges come from its children, not from its own Start,
+// so they cannot be worked out from `authored` alone.
+export const ClipDragOrigin = trait({ authored: 0, start: 0, end: 0 });
+
+// What a keyframe's time was when a drag of it began. Same reason.
export const KeyframeDragOrigin = trait({ time: 0 });
-// Snapshot of the clip's start/end frames at the moment a trim interaction
-// began. Tagged on the single clip being trimmed; removed on release.
+// What a clip's bounds were when a trim of it began; the edge not being
+// dragged is what the other one is measured against.
export const TrimDragOrigin = trait({ start: 0, end: 0 });