Skip to content

The VS Code of video editing, built for agents.

TypeScript

Enhance effects inspector and object mask handling

konstantin-paulus6ce93217bb20
diff --git a/apps/web/src/components/sidebar-right/inspector/effect-types.ts b/apps/web/src/components/sidebar-right/inspector/effect-types.ts
index e1593809..e11da5df 100644
--- a/apps/web/src/components/sidebar-right/inspector/effect-types.ts
+++ b/apps/web/src/components/sidebar-right/inspector/effect-types.ts
@@ -27,13 +27,13 @@ export type EffectOption = {
export const EFFECT_OPTIONS: EffectOption[] = [
{ name: "blur", label: "Layer Blur", unit: "px", value: 8 },
{ name: "brightness", label: "Brightness", unit: "amount", value: 0.8 },
+ { name: "opacity", label: "Opacity", unit: "amount", value: 1 },
{ name: "contrast", label: "Contrast", unit: "amount", value: 0.8 },
{ name: "grayscale", label: "Grayscale", unit: "amount", value: 0.5 },
{ name: "hueRotate", label: "Hue Rotation", unit: "deg", value: 100 },
{ name: "invert", label: "Invert", unit: "amount", value: 0.5 },
{ name: "saturate", label: "Saturate", unit: "amount", value: 0.8 },
{ name: "sepia", label: "Sepia", unit: "amount", value: 0.5 },
- { name: "opacity", label: "Opacity", unit: "amount", value: 1 },
];
/** What the panel's plus inserts, spelled out. */
diff --git a/apps/web/src/components/sidebar-right/inspector/effects-inspector.tsx b/apps/web/src/components/sidebar-right/inspector/effects-inspector.tsx
index 0ae83505..705d30d8 100644
--- a/apps/web/src/components/sidebar-right/inspector/effects-inspector.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/effects-inspector.tsx
@@ -15,8 +15,11 @@ import {
import {
DropdownMenu,
DropdownMenuContent,
+ DropdownMenuGroup,
+ DropdownMenuGroupLabel,
DropdownMenuItem,
DropdownMenuPortal,
+ DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Icon } from "@/components/ui/icon";
@@ -34,7 +37,7 @@ import {
import { SliderInput } from "@/components/ui/slider-input";
import { ControlledTextField } from "@/components/ui/text-field";
import { useHas, useTrait, useWorld } from "@diffusionstudio/koota-solid";
-import { AssetId, Cache, Computed, Effect, Hidden, getParentNode } from "@diffusionstudio/runtime";
+import { Cache, Computed, Effect, Hidden } from "@diffusionstudio/runtime";
import { useDerived, useEditor } from "@/engine/hooks";
import { removeKeyframeTrack, syncKeyframe } from "@/engine/keyframes";
import { copyObjectMask, useObjectMasks } from "@/engine/object-mask";
@@ -78,16 +81,7 @@ export function EffectsInspector(props: EffectsInspectorProps) {
const value = useDerived(() => props.effect.get(Computed)?.value ?? 0);
const masks = useDerived(() => props.effect.get(Cache)?.masks ?? NO_MASKS);
- const { footage, masks: sources } = useObjectMasks(() => getParentNode(props.effect));
-
- const used = useDerived(() =>
- (props.effect.get(Cache)?.masks ?? NO_MASKS).map((mask) => mask.get(AssetId)?.value ?? "").join("\n"),
- );
-
- const shareable = createMemo(() => {
- const taken = new Set(used().split("\n"));
- return sources().filter((source) => !taken.has(source.asset.id));
- });
+ const groups = useObjectMasks();
const editingMask = createMemo(() => {
const mask = pickedMask();
@@ -225,7 +219,7 @@ export function EffectsInspector(props: EffectsInspectorProps) {
</ControlRow>
</Show>
- <Show when={footage()}>
+ <Show when={groups().length > 0 || masks().length > 0}>
<For each={masks()}>
{(mask, index) => (
<ControlRow label="Mask" labelClass={index() > 0 ? "invisible" : undefined}>
@@ -243,7 +237,7 @@ export function EffectsInspector(props: EffectsInspectorProps) {
<DropdownMenuTrigger
as="button"
type="button"
- disabled={shareable().length === 0}
+ disabled={groups().length === 0}
class="flex h-7 w-full min-w-0 cursor-default items-center gap-2 overflow-clip rounded-md bg-input p-1 pr-2 text-xs text-muted-foreground transition-colors hover:bg-input/80 disabled:opacity-50 disabled:hover:bg-input"
>
<div class="flex size-5 shrink-0 items-center justify-center overflow-clip rounded-sm bg-secondary text-foreground">
@@ -253,11 +247,23 @@ export function EffectsInspector(props: EffectsInspectorProps) {
</DropdownMenuTrigger>
<DropdownMenuPortal>
<DropdownMenuContent class="max-h-[min(var(--kb-popper-content-available-height),275px)]">
- <For each={shareable()}>
- {(source) => (
- <DropdownMenuItem onSelect={() => copyObjectMask(world, props.effect, source)}>
- {source.name}
- </DropdownMenuItem>
+ <For each={groups()}>
+ {(group, index) => (
+ <>
+ <Show when={index() > 0}>
+ <DropdownMenuSeparator />
+ </Show>
+ <DropdownMenuGroup>
+ <DropdownMenuGroupLabel class="truncate">{group.name}</DropdownMenuGroupLabel>
+ <For each={group.masks}>
+ {(source) => (
+ <DropdownMenuItem onSelect={() => copyObjectMask(world, props.effect, source)}>
+ {source.name}
+ </DropdownMenuItem>
+ )}
+ </For>
+ </DropdownMenuGroup>
+ </>
)}
</For>
</DropdownMenuContent>
diff --git a/apps/web/src/components/sidebar-right/inspector/object-mask.tsx b/apps/web/src/components/sidebar-right/inspector/object-mask.tsx
index e67238d5..7bc113a9 100644
--- a/apps/web/src/components/sidebar-right/inspector/object-mask.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/object-mask.tsx
@@ -16,12 +16,20 @@ import {
import { Icon } from "@/components/ui/icon";
import { IncrementDecrementControl } from "@/components/ui/increment-decrement-control";
import { Keyframe } from "@/components/ui/keyframe";
-import { Select, SelectContent, SelectItem, SelectPortal, SelectTrigger, SelectValue } from "@/components/ui/select";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectPortal,
+ SelectSection,
+ SelectTrigger,
+ SelectValue,
+} from "@/components/ui/select";
import { SliderInput } from "@/components/ui/slider-input";
import { Switch, SwitchControl, SwitchInput, SwitchThumb } from "@/components/ui/switch";
import { ControlledTextField } from "@/components/ui/text-field";
import { useHas, useTrait, useWorld } from "@diffusionstudio/koota-solid";
-import { AssetId, Computed, Hidden, Library, Mask, VideoDecoderHandle, getParentNode } from "@diffusionstudio/runtime";
+import { AssetId, Computed, Hidden, Library, Mask, VideoDecoderHandle } from "@diffusionstudio/runtime";
import { useDerived, useEditor } from "@/engine/hooks";
import { syncKeyframe } from "@/engine/keyframes";
import {
@@ -33,7 +41,7 @@ import {
import type { Entity, World } from "koota";
import type { MaskAsset } from "@diffusionstudio/assets";
-import type { MaskRestore, ObjectMaskSource } from "@/engine/object-mask";
+import type { MaskRestore, ObjectMaskGroup, ObjectMaskSource } from "@/engine/object-mask";
type ObjectMaskInspectorProps = {
/** The `<mask>`; its `src` is the tracked picture. */
@@ -44,7 +52,7 @@ type ObjectMaskInspectorProps = {
/**
* One `<mask>`'s settings, opened from its row in the effect's inspector the
- * way a paint opens its picker: which tracked mask of the clip it is (the
+ * way a paint opens its picker: which mask of the library it is (the
* header), how strongly it limits the effect, smoothing, feather and
* inversion, plus a way to hide it; its row in the effect removes it. A mask
* whose file cannot be read — its decoder failed, as a video's does — can be
@@ -109,9 +117,12 @@ export function ObjectMaskInspector(props: ObjectMaskInspectorProps) {
const name = useDerived(() => objectMaskName(world, props.mask));
- // The tracked masks of the clip's footage: the header picks which of them this mask is.
- const { masks: sources } = useObjectMasks(() => getParentNode(getParentNode(props.mask)));
- const source = () => sources().find((option) => option.asset.id === props.mask.get(AssetId)?.value);
+ // Every mask in the library, by folder: the header picks which of them this mask is.
+ const groups = useObjectMasks();
+ const source = () =>
+ groups()
+ .flatMap((group) => group.masks)
+ .find((option) => option.asset.id === props.mask.get(AssetId)?.value);
/** Points the mask at another tracked mask's frames, placed where they were written for. */
const switchSource = (next: ObjectMaskSource | null) => {
@@ -123,17 +134,21 @@ export function ObjectMaskInspector(props: ObjectMaskInspectorProps) {
return (
<FloatingInspector open anchorRef={props.anchorRef} width={248}>
<FloatingInspectorHeader class="items-center justify-between px-2">
- <Select<ObjectMaskSource>
+ <Select<ObjectMaskSource, ObjectMaskGroup>
value={source()}
onChange={switchSource}
- options={sources()}
+ options={groups()}
optionValue={(option) => option.asset.id}
optionTextValue="name"
+ optionGroupChildren="masks"
itemComponent={(itemProps) => (
<SelectItem item={itemProps.item}>
{itemProps.item.rawValue.name}
</SelectItem>
)}
+ sectionComponent={(sectionProps) => (
+ <SelectSection>{sectionProps.section.rawValue.name}</SelectSection>
+ )}
>
<SelectTrigger>
<SelectValue<ObjectMaskSource>>
diff --git a/apps/web/src/engine/object-mask/copy.tsx b/apps/web/src/engine/object-mask/copy.tsx
index 816fc4d2..0a54acf5 100644
--- a/apps/web/src/engine/object-mask/copy.tsx
+++ b/apps/web/src/engine/object-mask/copy.tsx
@@ -2,7 +2,7 @@
* 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 { assetName } from '@diffusionstudio/assets';
+import { assetFolder, assetName, basename } from '@diffusionstudio/assets';
import { Mask } from '@diffusionstudio/reconciler';
import { AssetId, Blur, Cache, Library, Mask as MaskTrait, getParentNode } from '@diffusionstudio/runtime';
@@ -21,25 +21,37 @@ export type ObjectMaskSource = {
sourceIn: number;
};
+/** The masks of one library folder, under the folder's name: a section of the mask menus. */
+export type ObjectMaskGroup = {
+ /** What to call the section: the folder's own name. */
+ name: string;
+ masks: ObjectMaskSource[];
+};
+
/**
- * The tracked masks of the video `footage` (its asset id) in `library`: every
- * mask file whose recipe names it, whichever effect — or none — holds it now.
- * The recipe is the record: a mask outlives the effects that used it, and
- * the library is where it is found again. It reads the library's asset list,
- * a signal, so a memo over it runs again only when the library changes (see
+ * Every mask in `library`, grouped by the folder it is in — a mask is not
+ * tied to the clip it was tracked on, so any effect can take any of them.
+ * Folders keep the library's order. It reads the library's asset list, a
+ * signal, so a memo over it runs again only when the library changes (see
* `useObjectMasks`).
*/
-export function objectMasksOf(library: AssetLibrary, footage: string): ObjectMaskSource[] {
- const sources: ObjectMaskSource[] = [];
+export function objectMaskGroups(library: AssetLibrary): ObjectMaskGroup[] {
+ const groups = new Map<string, ObjectMaskGroup>();
for (const asset of library.list()) {
- if (asset.type !== 'MASK' || asset.recipe?.source !== footage) continue;
- sources.push({
+ if (asset.type !== 'MASK') continue;
+ const folder = assetFolder(asset);
+ let group = groups.get(folder);
+ if (!group) {
+ group = { name: basename(folder) || 'Library', masks: [] };
+ groups.set(folder, group);
+ }
+ group.masks.push({
asset,
name: assetName(asset).replace(/\.[^.]+$/, ''),
- sourceIn: asset.recipe.first / asset.frameRate,
+ sourceIn: asset.recipe ? asset.recipe.first / asset.frameRate : 0,
});
}
- return sources;
+ return [...groups.values()];
}
/**
diff --git a/apps/web/src/engine/object-mask/use-object-masks.ts b/apps/web/src/engine/object-mask/use-object-masks.ts
index 13361a94..2b9cfd2c 100644
--- a/apps/web/src/engine/object-mask/use-object-masks.ts
+++ b/apps/web/src/engine/object-mask/use-object-masks.ts
@@ -2,45 +2,22 @@
* 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 { findGeometryAsset } from '@diffusionstudio/runtime';
-import { useWorld } from '@diffusionstudio/koota-solid';
import { createMemo } from 'solid-js';
-import { useDerived } from '../hooks';
import { useLibrary } from '../library';
-import { objectMasksOf } from './copy';
+import { objectMaskGroups } from './copy';
import type { Accessor } from 'solid-js';
-import type { Entity } from 'koota';
-import type { ObjectMaskSource } from './copy';
-
-export type ObjectMasks = {
- /** The id of the video the clip plays, or null when it plays none. */
- footage: Accessor<string | null>;
- /** The tracked masks of that video in the library (see `objectMasksOf`). */
- masks: Accessor<ObjectMaskSource[]>;
-};
+import type { ObjectMaskGroup } from './copy';
/**
- * The tracked masks of `clip`'s footage, reactively. Which video the clip
- * plays is sampled per tick as an id — a lookup, compared by value — and the
- * library is scanned only when that id or the library's asset list changes,
- * not every frame.
+ * Every mask in the library, grouped by its folder (see `objectMaskGroups`),
+ * reactively: scanned again only when the library's asset list changes.
*/
-export function useObjectMasks(clip: () => Entity | null | undefined): ObjectMasks {
- const world = useWorld();
+export function useObjectMasks(): Accessor<ObjectMaskGroup[]> {
const library = useLibrary();
-
- const footage = useDerived(() => {
- const node = clip();
- const asset = node ? findGeometryAsset(world, node) : null;
- return asset?.type === 'VIDEO' ? asset.id : null;
- });
- const masks = createMemo(() => {
- const id = footage();
+ return createMemo(() => {
const current = library();
- return id && current ? objectMasksOf(current, id) : [];
+ return current ? objectMaskGroups(current) : [];
});
-
- return { footage, masks };
}