Skip to content

The VS Code of video editing, built for agents.

TypeScript

Refactor Object Mask Bar and Enhance Progress Display

konstantin-paulusb603f8b1da29
diff --git a/apps/web/src/components/canvas/object-mask-bar.tsx b/apps/web/src/components/canvas/object-mask-bar.tsx
index af570fb4..55e1087a 100644
--- a/apps/web/src/components/canvas/object-mask-bar.tsx
+++ b/apps/web/src/components/canvas/object-mask-bar.tsx
@@ -6,7 +6,6 @@ import { For, Show, createResource, createSignal, onMount, splitProps } from "so
import { Portal } from "solid-js/web";
import { SAM2_MODELS, downloadSize, sam2Model } from "@diffusionstudio/sam2/models";
import { useWorld } from "@diffusionstudio/koota-solid";
-import { Effect, Name, getParentNode } from "@diffusionstudio/runtime";
import { Button } from "@/components/ui/button";
import { Icon } from "@/components/ui/icon";
import { Separator } from "@/components/ui/separator";
@@ -38,10 +37,8 @@ import {
setBrushRadius,
setObjectMaskMode,
setObjectMaskOp,
- targetEffect,
trackObjectMask,
} from "@/engine/object-mask";
-import { effectOption } from "../sidebar-right/inspector/effect-types";
import type { JSX } from "solid-js";
import type { Sam2Model, Sam2ModelId } from "@diffusionstudio/sam2/models";
@@ -108,7 +105,11 @@ export function ObjectMaskBar() {
const session = state();
switch (session?.status) {
case "tracking":
- return `Tracking ${Math.floor((session.total === 0 ? 0 : session.completed / session.total) * 100)}%`;
+ return (
+ <FixedWidth widest="Tracking 100%">
+ {`Tracking ${percent(session.total === 0 ? 0 : session.completed / session.total)}`}
+ </FixedWidth>
+ );
case "saving":
return "Saving...";
default:
@@ -116,17 +117,6 @@ export function ObjectMaskBar() {
}
};
- // Where the mask goes: under the effect the tool was started for, else
- // under an opacity effect on the clip — the cut-out.
- const destination = () => {
- const effect = targetEffect();
- if (!effect?.isAlive()) return "Track the object through its clip and cut it out";
- const clip = getParentNode(effect);
- const label = effectOption(effect.get(Effect)?.type).label;
- const name = clip?.get(Name)?.value;
- return `Track the object and mask ${label}${name ? ` on ${name}` : ""}`;
- };
-
return (
<div class="absolute bottom-16 left-1/2 -translate-x-1/2 z-10 rounded-xl px-1.5 py-1 bg-background border border-border flex gap-1 items-center">
<ModelMenu disabled={busy()} />
@@ -144,7 +134,7 @@ export function ObjectMaskBar() {
<TooltipTrigger as={Button} disabled={state()?.status !== "seeded"} onClick={() => trackObjectMask(world)}>
{confirmLabel()}
</TooltipTrigger>
- <TooltipContent shortcut="⌘↵">{destination()}</TooltipContent>
+ <TooltipContent shortcut="⌘↵"></TooltipContent>
</Tooltip>
</div>
);
@@ -190,9 +180,16 @@ function ModelMenu(props: { disabled: boolean }) {
<Show when={load()?.phase === "error"}>
<Icon name="alert-warning" />
</Show>
- {sam2Model(objectMaskModel()).label}
- <Show when={load()?.phase === "download" || load()?.phase === "compile"}>
- <span class="text-xxs font-normal tabular-nums">{loadProgress(load()!)}</span>
+ <Show
+ when={load()?.phase === "download" || load()?.phase === "compile"}
+ fallback={sam2Model(objectMaskModel()).label}
+ >
+ <FixedWidth widest={<ModelProgress label={sam2Model(objectMaskModel()).label} percent="100%" />}>
+ <ModelProgress
+ label={sam2Model(objectMaskModel()).label}
+ percent={percent(load()!.phase === "download" ? load()!.progress : null)}
+ />
+ </FixedWidth>
</Show>
</Button>
)}
@@ -377,9 +374,34 @@ function keepEscape(event: KeyboardEvent) {
event.stopPropagation();
}
-function loadProgress(load: ObjectMaskModelLoad): string {
- if (load.phase === "compile" || load.progress === null) return "...";
- return `${Math.floor(load.progress * 100)}%`;
+/**
+ * Content as wide as `widest` whatever it says, centered in that width: the
+ * button around it holds still as a number in it counts up.
+ */
+function FixedWidth(props: { widest: JSX.Element; children: JSX.Element }) {
+ return (
+ <span class="inline-grid tabular-nums">
+ <span class="invisible col-start-1 row-start-1 flex items-center justify-center gap-1" aria-hidden="true">
+ {props.widest}
+ </span>
+ <span class="col-start-1 row-start-1 flex items-center justify-center gap-1">{props.children}</span>
+ </span>
+ );
+}
+
+/** The model's name with its download's progress after it. */
+function ModelProgress(props: { label: string; percent: string }) {
+ return (
+ <>
+ {props.label}
+ <span class="text-xxs font-normal">{props.percent}</span>
+ </>
+ );
+}
+
+/** 0 to 1 as a whole percentage; null, while there is no number yet, as an ellipsis. */
+function percent(value: number | null): string {
+ return value === null ? "" : `${Math.floor(value * 100)}%`;
}
function describeModelLoad(load: ObjectMaskModelLoad): string {
diff --git a/apps/web/src/engine/object-mask/overlay.ts b/apps/web/src/engine/object-mask/overlay.ts
index 2066e477..bd4385dc 100644
--- a/apps/web/src/engine/object-mask/overlay.ts
+++ b/apps/web/src/engine/object-mask/overlay.ts
@@ -12,7 +12,14 @@ import { maskFrame } from './frame';
import { handleObjectMaskInteraction, heldObjectMaskLabel } from './interaction';
import { currentSourceFrame, getVideoRect, pointOnVideo, videoPointAt, videoPointToDevice } from './media';
import {
- brushRadius, clearTargetEffect, getObjectHover, getObjectMask, getObjectTrack, getTargetClip, objectMaskMode,
+ brushRadius,
+ clearTargetEffect,
+ getObjectHover,
+ getObjectMask,
+ getObjectTrack,
+ getTargetClip,
+ objectMaskMode,
+ resetObjectMaskTools,
} from './store';
import { clearObjectHover, hoverObjectMask } from './tracking';
@@ -59,6 +66,7 @@ export function drawObjectMasks(world: World, ctx: Ctx2D, resolution: number): v
clearObjectHover();
clearTargetEffect();
endMaskStroke();
+ resetObjectMaskTools();
}
return;
}
diff --git a/apps/web/src/engine/object-mask/store.ts b/apps/web/src/engine/object-mask/store.ts
index fee14314..3ad2db9d 100644
--- a/apps/web/src/engine/object-mask/store.ts
+++ b/apps/web/src/engine/object-mask/store.ts
@@ -143,9 +143,15 @@ const [objectMaskMode, setObjectMaskMode] = createSignal<ObjectMaskMode>('points
const [objectMaskOp, setObjectMaskOp] = createSignal<ObjectMaskOp>('add');
const [brushRadius, setBrushRadius] = createSignal(DEFAULT_BRUSH_RADIUS);
-/** The tool's mode, op and brush size, reactive; kept while the tool is put down. */
+/** The tool's mode, op and brush size, reactive; the brush size is kept while the tool is put down. */
export { objectMaskMode, setObjectMaskMode, objectMaskOp, setObjectMaskOp, brushRadius, setBrushRadius };
+/** Back to points that add, as the tool is put down: it comes up again as it first did. */
+export function resetObjectMaskTools(): void {
+ setObjectMaskMode('points');
+ setObjectMaskOp('add');
+}
+
// ── The model ────────────────────────────────────────────────
const [objectMaskModel, storeObjectMaskModel] = createStoredSignal(