Enhance TimeSettings functionality and refactor inspector components

- Integrated `TimeSettings` into the `Inspector` component, allowing for improved time management of selected entities.
- Refactored `time.tsx` to utilize new hooks and streamline time property edits, enhancing performance and maintainability.
- Updated asset actions to support frame rate adjustments for sequences, ensuring accurate playback timing.
- Cleaned up commented-out code and improved readability across related components, preparing for future feature expansions.
- Updated migration documentation to reflect changes in time handling and inspector functionality.
This commit is contained in:
konstantin-paulus 2026-08-19 16:48:33 +02:00
parent a498cb2192
commit f7bedbb59f
8 changed files with 182 additions and 109 deletions

View File

@ -22,9 +22,9 @@ import { InspectorHeader } from "./inspector-header";
import { BackgroundSettings } from "./background";
import { SceneTemplatePanel } from "./scene-template";
import { AssetInfoPanel } from "./asset-info";
import { TimeSettings } from "./time";
// import { Alignment } from "./alignment";
// import { ExportPanel } from "./export";
// import { TimeSettings } from "./time";
// import { TransformSettings } from "./transform";
// import { LayoutPanel } from "./layout";
// import { AppearanceSettings } from "./appearance";
@ -125,9 +125,9 @@ export function Inspector() {
<BackgroundSettings />
</Show>
{/* <Show when={includesTarget("shape", "text", "audio", "scene", "caption", "group", "mask", "adjustment")}>
<Show when={includesTarget("shape", "text", "audio", "scene", "caption", "group", "mask", "adjustment")}>
<TimeSettings selection={nodes()} />
</Show> */}
</Show>
{/* <Show when={includesTarget("shape", "text", "audio", "scene", "caption", "group", "mask", "adjustment")}>
<TransformSettings selection={nodes()} />

View File

@ -10,22 +10,6 @@ import { Icon } from "@/components/ui/icon";
import { IncrementDecrementControl } from "@/components/ui/increment-decrement-control";
import { PanelSection } from "@/components/ui/panel-section";
import { ControlledTextField } from "@/components/ui/text-field";
import { useEngine } from "@/context/engine";
import {
useEntityState,
useEntityTag,
PaintType,
computeTrimStart,
computeTrimEnd,
setComponent,
removeComponent,
isScene,
isGroup,
resolveSequentialOverlaps,
useAssets,
saveAsset,
} from "@/components/engine";
import { secondsToFrames } from "@/components/engine/utils/time";
import {
Checkbox,
CheckboxControl,
@ -46,8 +30,28 @@ import {
} from "@/components/ui/context-menu";
import { createStoredSignal } from "@/lib/store";
import { store } from "@/init";
import { hasComponent } from "bitecs";
import { useTrait, useWorld } from "@diffusionstudio/koota-solid";
import {
Computed,
End,
FrameRate,
Geometry,
PlaybackRate,
SourceOut,
findGeometryAsset,
framesToSeconds,
getSourceFrameAt,
getTimelineOrigin,
isGroupLike,
isScene,
secondsToFrames,
} from "@diffusionstudio/runtime";
import { useDerived, useEditor } from "@/engine/hooks";
import { useLibrary } from "@/engine/library";
import { setSequenceFrameRate } from "@/engine/asset-actions";
import type { DocumentEditor } from "@/engine/editor";
import type { Entity, World } from "koota";
type TimeAddon = "inOut" | "playbackRate";
type TimeAddons = Partial<Record<TimeAddon, boolean>>;
@ -91,126 +95,137 @@ function parseTimeInput(value: string) {
return hours * 3600 + minutes * 60 + seconds;
}
type TimeProp = 'start' | 'end' | 'sourceIn' | 'sourceOut';
/**
* Writes a time prop from a frame count of this project; the file spells
* times in seconds. `null` unsets it: the document drops the trait, and the
* writer spells it as the attribute's absence (`false` is the one PropValue
* it removes for). A `start` or `sourceIn` of 0 is unset too, since absence
* is what 0 reads as on those.
*/
function editTime(world: World, editor: DocumentEditor, entity: Entity, name: TimeProp, frames: number | null) {
const unset = frames === null || (frames === 0 && (name === 'start' || name === 'sourceIn'));
const fps = world.get(FrameRate)?.value ?? 30;
editor.editProperty(entity, name, unset ? false : framesToSeconds(frames, fps));
}
/**
* Moves the node's in point to scene frame `frame`, keeping the rest of the
* clip where it is: the runtime's `trimEntityIn`, as edits. The out point is
* only implied while the node authors no End, so it is pinned first, or the
* tail would follow the head; then Start moves and SourceIn rolls forward by
* as much as the head lost.
*/
function trimIn(world: World, editor: DocumentEditor, entity: Entity, frame: number) {
if (!entity.has(End)) {
editTime(world, editor, entity, 'end', (entity.get(Computed)?.end ?? 0) - getTimelineOrigin(entity));
}
// Both read the origin, which the Start write moves.
const start = frame - getTimelineOrigin(entity);
const source = getSourceFrameAt(entity, frame);
editTime(world, editor, entity, 'start', start);
editTime(world, editor, entity, 'sourceIn', source);
}
/**
* Moves the node's out point to scene frame `frame` (the runtime's
* `trimEntityOut`, as edits). The source window follows only when the node
* authors one; otherwise End alone says where the clip runs out.
*/
function trimOut(world: World, editor: DocumentEditor, entity: Entity, frame: number) {
if (entity.has(SourceOut)) {
editTime(world, editor, entity, 'sourceOut', getSourceFrameAt(entity, frame));
}
editTime(world, editor, entity, 'end', frame - getTimelineOrigin(entity));
}
type TimeSettingsProps = {
selection: Set<number>;
selection: Entity[];
};
/**
* Where the node sits on the timeline and for how long. Edits are time props
* (`start`/`end`/`sourceIn`/`sourceOut`/`playbackRate`) written through the
* editor; the resolved bounds are read from Computed, which the systems
* write, hence `useDerived`.
*/
export function TimeSettings(props: TimeSettingsProps) {
const { world } = useEngine();
const assets = useAssets(world);
const c = world.components;
const eid = () => props.selection.values().next().value!;
const world = useWorld();
const editor = useEditor();
const library = useLibrary();
const entity = () => props.selection[0]!;
const [addons, setAddons] = createStoredSignal(
store.define<TimeAddons>("time.addons", {})
);
const playbackRate = useEntityState(c.PlaybackRate, eid, 1);
const trimStart = useEntityState(c.Trim.start, eid, 0);
const start = useEntityState(c.Computed.start, eid, 0);
const end = useEntityState(c.Computed.end, eid, 0);
const fillEids = useEntityState(c.Cache.fills, eid, []);
const frameRate = useTrait(world, FrameRate);
const fps = () => frameRate()?.value ?? 30;
const isContainer = createMemo(() => isScene(world, eid()) || isGroup(world, eid()));
const hasTrim = useEntityTag(c.Trim, eid);
const playbackRate = useTrait(entity, PlaybackRate);
const authoredEnd = useTrait(entity, End);
const start = useDerived(() => entity().get(Computed)?.start ?? 0);
const end = useDerived(() => entity().get(Computed)?.end ?? 0);
const playbackRatePercent = createMemo(() => Math.round(playbackRate() * 100));
const isContainer = createMemo(() => isGroupLike(entity()));
// A container authoring an End spans that rather than fitting its children.
const hasTrim = () => authoredEnd() !== undefined;
const startSeconds = createMemo(() => start() / world.frameRate);
const endSeconds = createMemo(() => end() / world.frameRate);
const playbackRatePercent = createMemo(() => Math.round((playbackRate()?.value ?? 1) * 100));
const startSeconds = createMemo(() => start() / fps());
const endSeconds = createMemo(() => end() / fps());
const durationSeconds = createMemo(() => endSeconds() - startSeconds());
const supportsPlaybackRate = createMemo(() => hasComponent(world, eid(), c.Geometry) && !isScene(world, eid()));
const supportsPlaybackRate = createMemo(() => entity().has(Geometry) && !isScene(entity()));
const sequenceAsset = createMemo(() => {
for (const fid of fillEids() ?? []) {
if (c.Paint[fid] !== PaintType.SEQUENCE) continue;
const id = c.AssetId[fid];
if (!id) continue;
const asset = assets.get(id);
if (asset?.type === 'SEQUENCE') return asset;
}
return null;
// The sequence the node shows, if any; its frame rate is the asset's, so
// it is sampled too (`library.update` changes it in place).
const sequenceAsset = useDerived(() => {
const asset = findGeometryAsset(world, entity());
return asset?.type === 'SEQUENCE' ? asset : null;
});
const sequenceFrameRate = useDerived(() => sequenceAsset()?.frameRate ?? null);
const handleAddAddon = (addon: TimeAddon) => setAddons({ ...addons(), [addon]: true });
const handleRemoveAddon = (addon: TimeAddon) => setAddons({ ...addons(), [addon]: false });
// Apply a timing edit as one undo step, then trim/remove/split any sibling it
// now overlaps inside a Sequential container (no-op elsewhere).
const commitTimeChange = (label: string, mutate: () => void) => {
world.history.transaction(label, () => {
mutate();
resolveSequentialOverlaps(world, [eid()]);
});
};
const assignPlaybackRate = (newRate: number) => {
const newDelay = Math.round(start() - trimStart() / newRate);
commitTimeChange("Set speed", () => {
setComponent(world, eid(), c.PlaybackRate, newRate);
setComponent(world, eid(), c.Delay, newDelay);
});
// The rate scales the source window onto the timeline around the node's
// start, so the start stays put on its own; 1 is the default, so it is unset.
const assignPlaybackRate = (rate: number) => {
editor.editProperty(entity(), 'playbackRate', rate === 1 ? false : rate);
};
const handleInChange = (event: Event & { currentTarget: HTMLInputElement }) => {
const parsed = parseTimeInput(event.currentTarget.value);
if (parsed === null) return;
const parsedFrame = secondsToFrames(parsed);
const start = computeTrimStart(world, eid(), parsedFrame);
commitTimeChange("Set in point", () => setComponent(world, eid(), c.Trim, { start }));
trimIn(world, editor, entity(), secondsToFrames(parsed, fps()));
};
const handleOutChange = (event: Event & { currentTarget: HTMLInputElement }) => {
const parsed = parseTimeInput(event.currentTarget.value);
if (parsed === null) return;
const parsedFrame = secondsToFrames(parsed);
const end = computeTrimEnd(world, eid(), parsedFrame);
commitTimeChange("Set out point", () => setComponent(world, eid(), c.Trim, { end }));
trimOut(world, editor, entity(), secondsToFrames(parsed, fps()));
};
const handleLengthChange = (durationSec: number) => {
const newEnd = start() + secondsToFrames(durationSec);
const end = computeTrimEnd(world, eid(), newEnd);
commitTimeChange("Set length", () => setComponent(world, eid(), c.Trim, { end }));
}
trimOut(world, editor, entity(), start() + secondsToFrames(durationSec, fps()));
};
const toggleTrim = (checked: boolean) => {
commitTimeChange("Toggle trim", () => {
if (checked) {
// Seed the explicit window with the current computed bounds so enabling
// the trim doesn't visibly resize the container.
const trimStart = computeTrimStart(world, eid(), start());
const trimEnd = computeTrimEnd(world, eid(), end());
setComponent(world, eid(), c.Trim, { start: trimStart, end: trimEnd });
} else {
removeComponent(world, eid(), c.Trim);
}
});
if (checked) {
trimIn(world, editor, entity(), start());
} else {
editTime(world, editor, entity(), 'end', null);
}
};
const handleFrameRateChange = (newRate: number) => {
const asset = sequenceAsset();
if (!asset) return;
const clamped = Math.max(1, Math.min(240, Math.round(newRate)));
if (clamped === asset.frameRate) return;
// The persisted asset stores duration + fps; the frame count is implied.
// Re-derive it so the new duration covers exactly the same frames.
const frameCount = Math.max(1, Math.round(asset.duration * asset.frameRate));
const newDurationInSeconds = frameCount / clamped;
saveAsset(world, { ...asset, frameRate: clamped, duration: newDurationInSeconds });
const newDuration = secondsToFrames(newDurationInSeconds);
if ((c.Trim.end[eid()] ?? 0) > newDuration) {
commitTimeChange("Set frame rate", () => setComponent(world, eid(), c.Trim, { end: newDuration }));
}
const lib = library();
if (!asset || !lib) return;
setSequenceFrameRate(world, lib, asset, newRate);
};
return (
@ -302,8 +317,6 @@ export function TimeSettings(props: TimeSettingsProps) {
</ContextMenu>
</Show>
<Show when={addons().inOut}>
<ContextMenu>
<ContextMenuTrigger<typeof ControlRow>
@ -330,11 +343,11 @@ export function TimeSettings(props: TimeSettingsProps) {
</ContextMenu>
</Show>
<Show when={sequenceAsset()}>
{(asset) => (
<Show when={sequenceFrameRate()}>
{(rate) => (
<ControlRow label="Frame Rate">
<ControlledTextField
value={Math.round(asset().frameRate)}
value={Math.round(rate())}
min={1}
max={240}
step={1}

View File

@ -7,8 +7,10 @@
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 type { SequenceAsset } from "@diffusionstudio/assets";
import type { World } from "koota";
import type { Asset, AssetLibrary } from "@diffusionstudio/assets";
@ -66,3 +68,20 @@ export function insertAssetAtPlayhead(world: World, asset: Asset): void {
toast("Nothing to insert into", { description: "Open a project first." });
}
}
/**
* Sets the rate an image sequence plays its frames at. The frame count is
* what the sequence is, so its duration follows; every clip showing it is
* re-derived (the asset length is one of the caps on a clip's span).
*/
export function setSequenceFrameRate(world: World, library: AssetLibrary, asset: SequenceAsset, frameRate: number): void {
const clamped = Math.max(1, Math.min(240, Math.round(frameRate)));
if (clamped === asset.frameRate) return;
const frames = Math.max(1, Math.round(asset.duration * asset.frameRate));
library.update(asset, { frameRate: clamped, duration: frames / clamped });
for (const entity of world.query(AssetId)) {
if (entity.get(AssetId)?.value === asset.id) reactToAssetChange(world, entity);
}
}

View File

@ -37,6 +37,14 @@ class Engine {
public running: Accessor<boolean>;
private setRunning: Setter<boolean>;
/**
* Ticks once per frame, after the systems ran. The one signal derived
* state is sampled against (see `useDerived`); a plain Solid signal, so
* following the tick costs no koota subscription.
*/
public frame: Accessor<number>;
private setFrame: Setter<number>;
public constructor(projectId: string, options: EngineOptions = {}) {
this.world = createRuntimeWorld(projectId);
this.world.add(Pointer, Keys, SnapLines, Hud, PointerEvents, AssetSelection);
@ -47,6 +55,7 @@ class Engine {
);
[this.running, this.setRunning] = createSignal(false);
[this.frame, this.setFrame] = createSignal(0);
this.ownsAudioContext = options.audioContext === undefined;
this.audioContext = options.audioContext ?? new AudioContext({ latencyHint: 'playback' });
@ -123,6 +132,7 @@ class Engine {
}
this.runSystems();
this.setFrame((count) => count + 1);
const keys = this.world.get(Keys);
if (keys?.justPressed || keys?.justLifted) {

View File

@ -8,3 +8,4 @@ export * from './use-editor';
export * from './use-tool';
export * from './use-selection';
export * from './use-asset-selection';
export * from './use-derived';

View File

@ -0,0 +1,29 @@
/* 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 { createMemo, untrack, type Accessor } from 'solid-js';
import { useEngineContext } from '../context';
/**
* Reactive read of state the systems write without events (the Computed
* store, anything else written through `store()`), which `useTrait` cannot
* follow. `read` is sampled once per engine tick (`Engine.frame`, one
* signal shared by every caller), after the systems ran, and the accessor
* notifies only when the sample differs from the last one (`===` unless
* `equals` says otherwise, so return primitives or pass a comparator).
* Signals `read` touches are not tracked; the tick is the only trigger.
*/
export function useDerived<T>(read: () => T, equals?: (prev: T, next: T) => boolean): Accessor<T> {
const { frame } = useEngineContext();
const sample = () => {
frame();
return untrack(read);
};
// An explicit `equals: undefined` would switch the memo's default
// comparison off, not leave it alone.
return equals ? createMemo(sample, undefined, { equals }) : createMemo(sample);
}

View File

@ -165,6 +165,8 @@ apps/web now depends on `@diffusionstudio/runtime` and `@diffusionstudio/koota-s
**AssetInfoPanel is on the koota world.** Asset selection became an app-side world trait, `AssetSelection` (`engine/traits.ts`, `{ id }`, attached by `Engine`), read and written through `useAssetSelection()` (`engine/hooks/use-asset-selection.ts`: `id`, `asset` resolved against the library's reactive list so a removed asset drops out by itself, `select`). The assets panel uses it instead of its local signal, the inspector classifies `asset` from it (after keyframes, before nodes, as before), and a click on empty canvas clears it in `input/interactions.ts` next to `clearSelection`. The panel reads `@diffusionstudio/assets` records (`assetName`, `stat.size/mtime` for size and "Modified", `generation.id` for the supabase prompt/model rows, plus a "Source" row since files are linked, not copied) and previews via `getAssetFile` + the library's `cache.peaks` (shared with the asset bar); SEQUENCE previews by its first frame. "Insert at playhead"/"Replace source" are `insertAssetAtPlayhead`/`replaceAssetSource` in `engine/asset-actions.ts`, shared with the asset item's context menu; a relink mints a new id, so the panel re-selects the returned asset.
**TimeSettings is on the koota world**, and with it the first pieces every timing surface will share. Reads: the resolved bounds are `Computed.start/end`, which the systems write without events, so `useTrait` cannot follow them; `useDerived(read, equals?)` (`engine/hooks/use-derived.ts`) samples `read` once per engine tick against `Engine.frame`, a plain Solid signal the loop bumps after `runSystems` (one signal for every caller, no koota subscription per hook), and notifies only when the sample changes. Use it for any Computed read in UI (Transform/Layout will). Writes stay `editor.editProperty`; `DocumentEditor` keeps to primitives, and what a property means is the panel's business. `time.tsx` has the helpers: `editTime(world, editor, entity, 'start'|'end'|'sourceIn'|'sourceOut', frames | null)` converts project frames to the seconds the file spells (`framesToSeconds`; the desktop writer rounds to 2 decimals, exact up to 100 fps), `null` unsets (`false` is the PropValue the writer removes an attribute for, and the document drops the trait for it), and a `start`/`sourceIn` of 0 is unset too since absence reads as 0; `trimIn/trimOut(world, editor, entity, sceneFrame)` are the runtime's `trimEntityIn/Out` as edits (pin End when absent, then Start + SourceIn computed before either is written, since the Start write moves the origin; SourceOut follows only when authored). When the timeline needs the same, lift them into a shared module then. The panel: Length and In/Out go through `trimOut`/`trimIn`; Speed is `playbackRate` (1 unsets; the start stays put on its own because the rate only scales the source window around it, so the bitecs Delay fixup is gone); "Trim content" on a scene/group is `hasEnd` (a container authoring an End spans it instead of fitting its children), toggled on via `trimIn(entity, Computed.start)`, which writes Start = SourceIn = the first child's offset so the children stay where they are, and off via `editTime('end', null)`; the sequence Frame Rate row is `setSequenceFrameRate` in `engine/asset-actions.ts` (`library.update` in place, then `reactToAssetChange` for every AssetId holder; no End clamp, caps resolve at read time). Runtime fix that came with it: `pinEndToCurrentBounds` pins `Computed.end - origin` instead of `Start + duration`, which was wrong for a container fitting children that start after 0. Not carried over: the bitecs `resolveSequentialOverlaps` call after each timing edit (the runtime's version writes traits without reporting; it needs an editor-side port before the app can call it), and `history.transaction` labels (no undo layer).
**Left stale on purpose** (deferred to a docs/examples pass): all eight `examples/*.tsx` still open with `<rect scene="...">` and no `<stage>`, so `tsc -p examples --noEmit` fails on them; `reference/jsx/{scene,roots}.md` still describes the pre-`<stage>` model where a mount grafts roots into an existing document; and `reference/jsx/{audio-sync,video,audio,elements}.md` still document `key` as the `syncTo` target. Deliberately not half-migrated: `reference/jsx` is stale as a whole, and patching the `key` rows alone would hide that.
## Remaining work (suggested order)

View File

@ -186,10 +186,9 @@ export function fitsChildren(entity: Entity, ignore?: Ignorable): boolean {
export function pinEndToCurrentBounds(world: World, entity: Entity): void {
if (entity.has(End)) return;
const start = entity.get(Start)?.value ?? 0;
const duration = store(world, Computed).duration[entity.id()] ?? 0;
const end = store(world, Computed).end[entity.id()] ?? 0;
entity.add(End({ value: start + duration }));
entity.add(End({ value: end - getTimelineOrigin(entity) }));
}
/**