Skip to content

The VS Code of video editing, built for agents.

TypeScript

Refactor StrokesSettings component for improved stroke management and inspector functionality

konstantin-paulus7484468a9c21
diff --git a/apps/web/src/components/sidebar-right/inspector/stroke-inspector.tsx b/apps/web/src/components/sidebar-right/inspector/stroke-inspector.tsx
new file mode 100644
index 00000000..dfaaa678
--- /dev/null
+++ b/apps/web/src/components/sidebar-right/inspector/stroke-inspector.tsx
@@ -0,0 +1,210 @@
+/* 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, createSignal } from "solid-js";
+import { Button } from "@/components/ui/button";
+import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
+import { Icon } from "@/components/ui/icon";
+import {
+ FloatingInspector,
+ FloatingInspectorContent,
+ FloatingInspectorHeader,
+ FloatingInspectorSeparator,
+ FloatingInspectorTitle,
+} from "@/components/ui/floating-inspector";
+import { ControlRow } from "@/components/ui/control-group";
+import { ColorOpacityRow } from "@/components/ui/color-opacity-row";
+import { ColorOpacityPicker } from "@/components/ui/color-opacity-picker";
+import { ControlledTextField } from "@/components/ui/text-field";
+import { SegmentedIconTabs } from "@/components/ui/segmented-icon-tabs";
+import { Keyframe } from "@/components/ui/keyframe";
+import { useTrait, useWorld } from "@diffusionstudio/koota-solid";
+import { Computed, StrokeJoin, StrokeStyle, colorToHex } from "@diffusionstudio/runtime";
+import { useDerived, useEditor } from "@/engine/hooks";
+import { syncKeyframe } from "@/engine/keyframes";
+
+import type { StrokeJoin as StrokeJoinName } from "@diffusionstudio/jsx";
+import type { Entity } from "koota";
+
+const JOIN_SEGMENTS: { value: StrokeJoinName; icon: string; label: string }[] = [
+ { value: "miter", icon: "line-join-miter", label: "Miter" },
+ { value: "bevel", icon: "line-join-bevel", label: "Bevel" },
+ { value: "round", icon: "line-join-round", label: "Round" },
+];
+
+const JOIN_NAMES: Record<StrokeJoin, StrokeJoinName> = {
+ [StrokeJoin.MITER]: "miter",
+ [StrokeJoin.BEVEL]: "bevel",
+ [StrokeJoin.ROUND]: "round",
+};
+
+/** `<stroke>`'s defaults; a control left at one of these unsets its prop. */
+const DEFAULT_OPACITY = 1;
+const DEFAULT_WIDTH = 1;
+const DEFAULT_MITER_LIMIT = 10;
+
+type StrokeInspectorProps = {
+ stroke: Entity;
+ anchorRef: HTMLElement;
+ onClose(): void;
+};
+
+/**
+ * One `<stroke>`: its paint (color and opacity) and its line style
+ * (`width`/`join`/`miterLimit`). A stroke is a solid color and takes no paint
+ * children, so the color half is the color picker alone, with no gradient or
+ * asset tab. `cap` has no control yet: it only shows on open paths (text
+ * glyphs) and there are no icons for it.
+ */
+export function StrokeInspector(props: StrokeInspectorProps) {
+ const world = useWorld();
+ const editor = useEditor();
+
+ let colorRowRef: HTMLDivElement | undefined;
+
+ const [pickingColor, setPickingColor] = createSignal(false);
+
+ const color = useDerived(() => props.stroke.get(Computed)?.color ?? 0);
+ const opacity = useDerived(() => props.stroke.get(Computed)?.opacity ?? DEFAULT_OPACITY);
+ const width = useDerived(() => props.stroke.get(Computed)?.strokeWidth ?? DEFAULT_WIDTH);
+
+ const style = useTrait(() => props.stroke, StrokeStyle);
+
+ const join = createMemo(() => JOIN_NAMES[style()?.join ?? StrokeJoin.MITER]);
+ const miterLimit = () => style()?.miterLimit ?? DEFAULT_MITER_LIMIT;
+
+ /** A stroke's color is required, never unset. */
+ const editColor = (value: number) => {
+ const hex = colorToHex(value);
+ editor.editProperty(props.stroke, "color", hex);
+ syncKeyframe(world, editor, props.stroke, "color", hex);
+ };
+
+ const editOpacity = (value: number) => {
+ const next = Math.round(value * 100) / 100;
+ editor.editProperty(props.stroke, "opacity", next === DEFAULT_OPACITY ? false : next);
+ syncKeyframe(world, editor, props.stroke, "opacity", next);
+ };
+
+ const editWidth = (value: number) => {
+ // Unlike a node's width this is the line width, not `resizeEntity`, so
+ // there is no track of its own to mint and the sync belongs after.
+ editor.editProperty(props.stroke, "width", value === DEFAULT_WIDTH ? false : value);
+ syncKeyframe(world, editor, props.stroke, "width", value);
+ };
+
+ const editJoin = (value: StrokeJoinName) => {
+ editor.editProperty(props.stroke, "join", value === "miter" ? false : value);
+ };
+
+ const editMiterLimit = (value: number) => {
+ editor.editProperty(
+ props.stroke,
+ "miterLimit",
+ value === DEFAULT_MITER_LIMIT ? false : value,
+ );
+ };
+
+ const handleClose = () => {
+ setPickingColor(false);
+ props.onClose();
+ };
+
+ return (
+ <>
+ <FloatingInspector open anchorRef={props.anchorRef} width={248}>
+ <FloatingInspectorHeader class="items-center justify-between">
+ <FloatingInspectorTitle>Stroke</FloatingInspectorTitle>
+ <Tooltip>
+ <TooltipTrigger
+ as={Button}
+ size="icon"
+ variant="ghost"
+ class="text-muted-foreground"
+ onClick={handleClose}
+ >
+ <Icon name="close-remove" />
+ </TooltipTrigger>
+ <TooltipContent>Close</TooltipContent>
+ </Tooltip>
+ </FloatingInspectorHeader>
+ <FloatingInspectorSeparator />
+ <FloatingInspectorContent class="flex flex-col gap-2 p-4">
+ <ControlRow label="Color" ref={colorRowRef}>
+ <ColorOpacityRow
+ color={color()}
+ onChangeColor={editColor}
+ opacity={opacity()}
+ onChangeOpacity={editOpacity}
+ onClick={() => setPickingColor(true)}
+ keyframe={<Keyframe target={props.stroke} property="opacity" />}
+ />
+ </ControlRow>
+
+ <ControlRow label="Weight">
+ <ControlledTextField
+ value={width()}
+ onNumber={editWidth}
+ step={1}
+ min={0}
+ autoSelect
+ limitEvents
+ keyframe={<Keyframe target={props.stroke} property="width" />}
+ />
+ </ControlRow>
+
+ <ControlRow label="Join">
+ <SegmentedIconTabs
+ value={join}
+ onChange={editJoin}
+ items={JOIN_SEGMENTS}
+ buttonClass="transition-colors"
+ iconClass="size-3.5 text-muted-foreground"
+ />
+ </ControlRow>
+
+ <ControlRow label="Miter">
+ <ControlledTextField
+ value={miterLimit()}
+ onNumber={editMiterLimit}
+ step={1}
+ min={1}
+ autoSelect
+ limitEvents
+ />
+ </ControlRow>
+ </FloatingInspectorContent>
+ </FloatingInspector>
+
+ <FloatingInspector open={pickingColor} anchorRef={() => colorRowRef} offset={26}>
+ <FloatingInspectorHeader>
+ <FloatingInspectorTitle>Color</FloatingInspectorTitle>
+ <div class="ml-auto">
+ <Tooltip>
+ <TooltipTrigger
+ as={Button}
+ size="icon"
+ variant="ghost"
+ class="text-muted-foreground"
+ onClick={() => setPickingColor(false)}
+ >
+ <Icon name="close-remove" class="size-6" />
+ </TooltipTrigger>
+ <TooltipContent>Close</TooltipContent>
+ </Tooltip>
+ </div>
+ </FloatingInspectorHeader>
+ <FloatingInspectorContent class="p-0">
+ <ColorOpacityPicker
+ color={color()}
+ opacity={opacity()}
+ onColorChange={editColor}
+ onOpacityChange={editOpacity}
+ keyframeTarget={props.stroke}
+ />
+ </FloatingInspectorContent>
+ </FloatingInspector>
+ </>
+ );
+}
diff --git a/apps/web/src/components/sidebar-right/inspector/strokes.tsx b/apps/web/src/components/sidebar-right/inspector/strokes.tsx
index 3c8e8aa7..71093e28 100644
--- a/apps/web/src/components/sidebar-right/inspector/strokes.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/strokes.tsx
@@ -5,9 +5,6 @@
import { For, Show, createMemo, createSignal } from "solid-js";
import { Button } from "@/components/ui/button";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
-import { ControlRow } from "@/components/ui/control-group";
-import { ColorOpacityPicker } from "@/components/ui/color-opacity-picker";
-import { ColorOpacityRow } from "@/components/ui/color-opacity-row";
import {
ContextMenu,
ContextMenuContent,
@@ -15,43 +12,19 @@ import {
ContextMenuSeparator,
ContextMenuTrigger,
} from "@/components/ui/context-menu";
-import {
- FloatingInspector,
- FloatingInspectorContent,
- FloatingInspectorHeader,
- FloatingInspectorTitle,
-} from "@/components/ui/floating-inspector";
import { Icon } from "@/components/ui/icon";
+import { ItemRow } from "@/components/ui/item-row";
import { PanelSection } from "@/components/ui/panel-section";
-import { ControlledTextField } from "@/components/ui/text-field";
-import { SegmentedIconTabs } from "@/components/ui/segmented-icon-tabs";
-import { Keyframe } from "@/components/ui/keyframe";
-import { useHas, useTrait, useWorld } from "@diffusionstudio/koota-solid";
+import { useHas } from "@diffusionstudio/koota-solid";
import { Stroke as StrokeElement } from "@diffusionstudio/reconciler";
-import { Cache, Computed, Hidden, StrokeJoin, StrokeStyle, colorToHex } from "@diffusionstudio/runtime";
+import { Cache, Computed, Hidden, colorToHex } from "@diffusionstudio/runtime";
import { useDerived, useEditor } from "@/engine/hooks";
-import { syncKeyframe } from "@/engine/keyframes";
-
-import type { StrokeJoin as StrokeJoinName } from "@diffusionstudio/jsx";
-import type { Entity, World } from "koota";
-import type { DocumentEditor } from "@/engine/editor";
+import { StrokeInspector } from "./stroke-inspector";
-const JOIN_SEGMENTS: { value: StrokeJoinName; icon: string; label: string }[] = [
- { value: "miter", icon: "line-join-miter", label: "Miter" },
- { value: "bevel", icon: "line-join-bevel", label: "Bevel" },
- { value: "round", icon: "line-join-round", label: "Round" },
-];
+import type { Entity } from "koota";
-const JOIN_NAMES: Record<StrokeJoin, StrokeJoinName> = {
- [StrokeJoin.MITER]: "miter",
- [StrokeJoin.BEVEL]: "bevel",
- [StrokeJoin.ROUND]: "round",
-};
-
-/** `<stroke>`'s defaults; a control left at one of these unsets its prop. */
+/** What "Add stroke" authors; `<stroke>`'s own default color. */
const DEFAULT_COLOR = "#000000";
-const DEFAULT_WIDTH = 1;
-const DEFAULT_MITER_LIMIT = 10;
// Stable identity, so a node without strokes does not resample every tick.
const NO_STROKES: Entity[] = [];
@@ -63,11 +36,10 @@ type StrokesSettingsProps = {
/**
* The `<stroke>` children of the selected node, in paint order (the list is
* shown topmost first, so the last element in the file is the first row).
- * Each stroke is its own element with its own paint *and* its own line style
- * (`width`/`join`/`miterLimit`), which is why the style controls sit under
- * each row rather than once per node as they did while the style belonged to
- * the node. `cap` has no control yet: it only shows on open paths (text
- * glyphs) and there are no icons for it.
+ * A row opens the stroke's own inspector; what it shows is the color, since
+ * that is the one thing a stroke always says. The line style
+ * (`width`/`join`/`miterLimit`) is the stroke's own and not the node's, so it
+ * lives in that inspector rather than under every row.
*/
export function StrokesSettings(props: StrokesSettingsProps) {
const editor = useEditor();
@@ -87,7 +59,7 @@ export function StrokesSettings(props: StrokesSettingsProps) {
if (stroke) setPicked(stroke);
};
- // Read back off the list, so the picker closes with the stroke it edits.
+ // Read back off the list, so removing a stroke closes the inspector on it.
const editing = createMemo(() => {
const stroke = picked();
return stroke !== undefined && strokes().includes(stroke) ? stroke : undefined;
@@ -133,10 +105,9 @@ export function StrokesSettings(props: StrokesSettingsProps) {
}
>
<For each={strokes().toReversed()}>
- {(stroke, index) => (
- <StrokeControls
+ {(stroke) => (
+ <StrokeRow
stroke={stroke}
- separated={index() > 0}
onSelect={() => setPicked(stroke)}
onRemove={() => editor.remove(stroke)}
onMoveUp={() => handleReorderStroke(stroke, 1)}
@@ -147,7 +118,7 @@ export function StrokesSettings(props: StrokesSettingsProps) {
</PanelSection>
<Show when={editing() !== undefined}>
- <StrokePicker
+ <StrokeInspector
stroke={editing()!}
anchorRef={anchorRef}
onClose={() => setPicked(undefined)}
@@ -157,174 +128,58 @@ export function StrokesSettings(props: StrokesSettingsProps) {
);
}
-/** Writes `color` and reports it; a stroke's color is required, never unset. */
-function editColor(world: World, editor: DocumentEditor, stroke: Entity, value: number) {
- const color = colorToHex(value);
- editor.editProperty(stroke, "color", color);
- syncKeyframe(world, editor, stroke, "color", color);
-}
-
-function editOpacity(world: World, editor: DocumentEditor, stroke: Entity, value: number) {
- const opacity = Math.round(value * 100) / 100;
- editor.editProperty(stroke, "opacity", opacity === 1 ? false : opacity);
- syncKeyframe(world, editor, stroke, "opacity", opacity);
-}
-
-type StrokeControlsProps = {
+type StrokeRowProps = {
stroke: Entity;
- separated: boolean;
onSelect(): void;
onRemove(): void;
onMoveUp(): void;
onMoveDown(): void;
};
-function StrokeControls(props: StrokeControlsProps) {
- const world = useWorld();
+function StrokeRow(props: StrokeRowProps) {
const editor = useEditor();
const color = useDerived(() => props.stroke.get(Computed)?.color ?? 0);
- const opacity = useDerived(() => props.stroke.get(Computed)?.opacity ?? 1);
- const width = useDerived(() => props.stroke.get(Computed)?.strokeWidth ?? DEFAULT_WIDTH);
-
- const style = useTrait(() => props.stroke, StrokeStyle);
const hidden = useHas(() => props.stroke, Hidden);
- const join = createMemo(() => JOIN_NAMES[style()?.join ?? StrokeJoin.MITER]);
- const miterLimit = () => style()?.miterLimit ?? DEFAULT_MITER_LIMIT;
-
- const handleWidthChange = (value: number) => {
- // Unlike a node's width this is the line width, not `resizeEntity`, so
- // there is no track of its own to mint and the sync belongs after.
- editor.editProperty(props.stroke, "width", value === DEFAULT_WIDTH ? false : value);
- syncKeyframe(world, editor, props.stroke, "width", value);
- };
-
- const handleJoinChange = (value: StrokeJoinName) => {
- editor.editProperty(props.stroke, "join", value === "miter" ? false : value);
- };
-
- const handleMiterLimitChange = (value: number) => {
- editor.editProperty(props.stroke, "miterLimit", value === DEFAULT_MITER_LIMIT ? false : value);
- };
-
const toggleHidden = () => {
editor.editProperty(props.stroke, "hidden", !hidden());
};
return (
- <div
- class="flex flex-col gap-2"
- classList={{ "border-t border-border pt-3": props.separated }}
- >
- <ContextMenu>
- <ContextMenuTrigger<typeof ControlRow>
- as={ControlRow}
+ <ContextMenu>
+ <ContextMenuTrigger>
+ <ItemRow
label="Stroke"
- class={hidden() ? "opacity-50" : undefined}
+ value={colorToHex(color()).replace("#", "")}
+ icon={<Icon name="rectangle-small" />}
+ onClick={props.onSelect}
+ disabled={hidden()}
>
- <ColorOpacityRow
- color={color()}
- onChangeColor={(value) => editColor(world, editor, props.stroke, value)}
- opacity={opacity()}
- onChangeOpacity={(value) => editOpacity(world, editor, props.stroke, value)}
- onClick={props.onSelect}
- keyframe={<Keyframe target={props.stroke} property="opacity" />}
- />
- </ContextMenuTrigger>
- <ContextMenuContent>
- <ContextMenuItem onSelect={props.onMoveUp}>Move Up</ContextMenuItem>
- <ContextMenuItem onSelect={props.onMoveDown}>Move Down</ContextMenuItem>
- <ContextMenuSeparator />
- <ContextMenuItem onSelect={toggleHidden}>
- {hidden() ? "Unhide" : "Hide"}
- </ContextMenuItem>
- <ContextMenuSeparator />
- <ContextMenuItem onSelect={props.onRemove}>Remove</ContextMenuItem>
- </ContextMenuContent>
- </ContextMenu>
-
- <ControlRow label="Weight">
- <ControlledTextField
- value={width()}
- onNumber={handleWidthChange}
- step={1}
- min={0}
- autoSelect
- limitEvents
- keyframe={<Keyframe target={props.stroke} property="width" />}
- />
- </ControlRow>
-
- <ControlRow label="Join">
- <SegmentedIconTabs
- value={join}
- onChange={handleJoinChange}
- items={JOIN_SEGMENTS}
- buttonClass="transition-colors"
- iconClass="size-3.5 text-muted-foreground"
- />
- </ControlRow>
-
- <ControlRow label="Miter">
- <ControlledTextField
- value={miterLimit()}
- onNumber={handleMiterLimitChange}
- step={1}
- min={1}
- autoSelect
- limitEvents
- />
- </ControlRow>
- </div>
- );
-}
-
-type StrokePickerProps = {
- stroke: Entity;
- anchorRef: HTMLElement;
- onClose(): void;
-};
-
-/**
- * A stroke's paint. Solid only: `<stroke>` takes a `color` and takes no paint
- * children, so there is no gradient or asset tab to offer.
- */
-function StrokePicker(props: StrokePickerProps) {
- const world = useWorld();
- const editor = useEditor();
-
- const color = useDerived(() => props.stroke.get(Computed)?.color ?? 0);
- const opacity = useDerived(() => props.stroke.get(Computed)?.opacity ?? 1);
-
- return (
- <FloatingInspector open anchorRef={props.anchorRef}>
- <FloatingInspectorHeader>
- <FloatingInspectorTitle>Color</FloatingInspectorTitle>
- <div class="ml-auto">
<Tooltip>
<TooltipTrigger
as={Button}
size="icon"
variant="ghost"
class="text-muted-foreground"
- onClick={props.onClose}
+ onClick={props.onRemove}
>
- <Icon name="close-remove" class="size-6" />
+ <Icon name="close-remove-small" />
</TooltipTrigger>
- <TooltipContent>Close</TooltipContent>
+ <TooltipContent>Remove stroke</TooltipContent>
</Tooltip>
- </div>
- </FloatingInspectorHeader>
- <FloatingInspectorContent class="p-0">
- <ColorOpacityPicker
- color={color()}
- opacity={opacity()}
- onColorChange={(value) => editColor(world, editor, props.stroke, value)}
- onOpacityChange={(value) => editOpacity(world, editor, props.stroke, value)}
- keyframeTarget={props.stroke}
- />
- </FloatingInspectorContent>
- </FloatingInspector>
+ </ItemRow>
+ </ContextMenuTrigger>
+ <ContextMenuContent>
+ <ContextMenuItem onSelect={props.onMoveUp}>Move Up</ContextMenuItem>
+ <ContextMenuItem onSelect={props.onMoveDown}>Move Down</ContextMenuItem>
+ <ContextMenuSeparator />
+ <ContextMenuItem onSelect={toggleHidden}>
+ {hidden() ? "Unhide" : "Hide"}
+ </ContextMenuItem>
+ <ContextMenuSeparator />
+ <ContextMenuItem onSelect={props.onRemove}>Remove</ContextMenuItem>
+ </ContextMenuContent>
+ </ContextMenu>
);
}