Compare commits

...

2 Commits

Author SHA1 Message Date
konstantin-paulus
af39a55a13 Remove ClipPathBar Component and Integrate Command System
- Deleted the `ClipPathBar` component to streamline the toolbar and improve command management.
- Updated the `Toolbar` component to remove references to the `ClipPathBar` and adjusted the command handling logic.
- Introduced a new `CommandBar` for managing pending commands, enhancing user interaction with command execution.
- Refactored the `ClipPathsSettings` to utilize the new command system for initiating clip path actions.
- Added a `CommandContext` to manage command states and interactions across the application.
2026-09-30 16:21:26 +02:00
konstantin-paulus
f9963a15b0 Refactor Toolbar Component and Integrate Command Bar
- Replaced the `ActionBar` with a new `CommandBar` and `CommandPalette` for improved command management in the `Toolbar` component.
- Enhanced the toolbar's functionality by adding command cancellation logic based on tool selection and prompt input state.
- Removed the `ActionBar` component and updated related imports and usages to streamline the codebase.
- Updated the `useMediaSelection` hook to utilize the new `resolveMedia` function for better media handling.
2026-09-30 15:55:57 +02:00
20 changed files with 679 additions and 416 deletions

View File

@ -1,68 +0,0 @@
/* 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 { Show, createEffect } from "solid-js";
import { useTrait, useWorld } from "@diffusionstudio/koota-solid";
import { Name } from "@diffusionstudio/runtime";
import { Button } from "@/components/ui/button";
import { Separator } from "@/components/ui/separator";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import { useDerived } from "@/engine/hooks";
import { applyClipPaths, cancelClipPath, getClipPathPicks, getClipPathTarget } from "@/engine/clip-path";
import type { Entity } from "koota";
/**
* The clip path tool's action bar, over the canvas while the tool is up: how
* many of the selected shapes would clip the target, and the way out —
* Cancel leaves the document as it was, Confirm makes them clip paths of the
* target.
*/
export function ClipPathBar() {
const world = useWorld();
const target = useDerived(getClipPathTarget);
const picks = useDerived(() => getClipPathPicks(world), sameEntities);
// Nothing to aim at (the target was deleted, or undone away): the tool has no work left.
createEffect(() => {
if (!target()) cancelClipPath(world);
});
return (
<Show when={target()}>
{(node) => (
<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">
<span class="px-2 text-xs text-muted-foreground whitespace-nowrap">
{picks().length === 0
? "Pick a shape to use as the clipping source"
: `${picks().length} ${picks().length === 1 ? "shape" : "shapes"} selected`}
</span>
<Separator orientation="vertical" class="min-h-5" />
<Tooltip>
<TooltipTrigger as={Button} variant="ghost" class="text-muted-foreground" onClick={() => cancelClipPath(world)}>
Cancel
</TooltipTrigger>
<TooltipContent shortcut="Esc">Cancel</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger as={Button} disabled={picks().length === 0} onClick={() => applyClipPaths(world)}>
Confirm
</TooltipTrigger>
<TooltipContent shortcut="⌘↵">Clip <TargetName target={node()} /></TooltipContent>
</Tooltip>
</div>
)}
</Show>
);
}
function TargetName(props: { target: Entity }) {
const name = useTrait(() => props.target, Name);
return <span class="text-foreground">{name()?.value || "this layer"}</span>;
}
function sameEntities(a: Entity[], b: Entity[]): boolean {
return a.length === b.length && a.every((entity, i) => entity === b[i]);
}

View File

@ -15,13 +15,13 @@ import {
DropdownMenuShortcut,
} from "@/components/ui/dropdown-menu";
import { PromptInput } from "../genai/prompt-input";
import { ActionBar } from "../genai/action-bar";
import { CommandBar, CommandPalette, useCommands } from "../commands";
import { ObjectMaskBar } from "./object-mask-bar";
import { ClipPathBar } from "./clip-path-bar";
import { For, Match, Show, Switch, createEffect, createMemo } from "solid-js";
import { For, Match, Show, Switch, createMemo, createSignal } from "solid-js";
import { Tool, ToolType } from "@diffusionstudio/runtime";
import { useWorld } from "@diffusionstudio/koota-solid";
import { clearClipPathTarget, useTool } from "@/engine";
import { useTool } from "@/engine";
import { usePendingCommand } from "@/context/command";
import { usePromptInput } from "@/context/prompt-input";
const CURSOR_TOOLS = [
@ -32,16 +32,13 @@ const CURSOR_TOOLS = [
export function Toolbar() {
const world = useWorld();
const { promptInputOpen, promptInputConfig, openPromptInput, setPromptInputOpen } = usePromptInput();
const { promptInputOpen, promptInputConfig, setPromptInputOpen } = usePromptInput();
const selectedTool = useTool();
const commands = useCommands();
const { pending } = usePendingCommand();
const [paletteOpen, setPaletteOpen] = createSignal(false);
const cursorTool = createMemo(() => CURSOR_TOOLS.find((cursor) => cursor.tool === selectedTool()));
createEffect(() => {
if (selectedTool() !== ToolType.CLIP_PATH) {
clearClipPathTarget();
}
});
const handleToolChange = (tool: ToolType) => {
world.set(Tool, { value: tool });
}
@ -52,15 +49,16 @@ export function Toolbar() {
<PromptInput initialConfig={promptInputConfig()} />
</Show>
<Show when={!promptInputOpen()}>
<Switch fallback={<ActionBar openPromptInput={openPromptInput} />}>
<Switch>
<Match when={selectedTool() === ToolType.OBJECT_MASK}>
<ObjectMaskBar />
</Match>
<Match when={selectedTool() === ToolType.CLIP_PATH}>
<ClipPathBar />
<Match when={pending()}>
<CommandBar />
</Match>
</Switch>
</Show>
<CommandPalette commands={commands} open={paletteOpen()} onOpenChange={setPaletteOpen} />
<div class="absolute bottom-4 left-1/2 -translate-x-1/2 rounded-xl p-1.5 bg-background border border-border-strong flex gap-2 items-center z-10">
<div class="flex gap-1">
<Tooltip>
@ -164,6 +162,18 @@ export function Toolbar() {
</TooltipTrigger>
<TooltipContent>AI generate</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger
as={Button}
size="icon-square"
class={paletteOpen() ? 'text-foreground' : 'text-muted-foreground'}
variant={paletteOpen() ? 'default' : 'ghost'}
onClick={() => setPaletteOpen(true)}
>
<Icon name="tool.actions-spotlight" />
</TooltipTrigger>
<TooltipContent shortcut="⌘P">Actions</TooltipContent>
</Tooltip>
</div>
</>
);

View File

@ -0,0 +1,79 @@
/* 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 { Show } from "solid-js";
import { useWorld } from "@diffusionstudio/koota-solid";
import { Button } from "@/components/ui/button";
import { Icon } from "@/components/ui/icon";
import { Separator } from "@/components/ui/separator";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import { useDerived } from "@/engine/hooks";
import { canRunCommand, getCommandPicks } from "@/engine/command";
import { usePendingCommand } from "@/context/command";
import type { Command } from "@/engine/command";
import type { Entity } from "koota";
const NO_PICKS: Entity[] = [];
/**
* The bar a pending command puts over the canvas while it waits on its
* picks: how many of the selected nodes it would run on, and the way out —
* Cancel takes it down, Confirm runs it on them.
*/
export function CommandBar() {
const world = useWorld();
const { pending, cancel, confirm } = usePendingCommand();
// What a command takes can hang on state the systems write without events
// (a node's source, what it paints), so the picks are sampled per tick.
const picks = useDerived(() => {
const command = pending();
return command ? getCommandPicks(world, command) : NO_PICKS;
}, sameEntities);
const status = (command: Command) => {
const count = picks().length;
const limit = command.limit ?? Infinity;
if (count === 0) return command.hint;
if (count > limit) return `Pick only ${limit === 1 ? "one" : limit} ${command.noun[limit === 1 ? 0 : 1]}`;
return `${count} ${command.noun[count === 1 ? 0 : 1]} selected`;
};
return (
<Show when={pending()}>
{(command) => (
<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">
<span class="flex items-center pr-1 text-xs text-foreground whitespace-nowrap">
<Icon name={command().icon} class="text-muted-foreground" />
{command().label}
</span>
<Separator orientation="vertical" class="min-h-5" />
<span class="px-2 text-xs text-muted-foreground whitespace-nowrap">{status(command())}</span>
<Separator orientation="vertical" class="min-h-5" />
<Tooltip>
<TooltipTrigger as={Button} variant="ghost" class="text-muted-foreground" onClick={cancel}>
Cancel
</TooltipTrigger>
<TooltipContent shortcut="Esc">Cancel</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger
as={Button}
disabled={!canRunCommand(command(), picks())}
onClick={confirm}
>
Confirm
</TooltipTrigger>
<TooltipContent shortcut="⌘↵">{command().label}</TooltipContent>
</Tooltip>
</div>
)}
</Show>
);
}
function sameEntities(a: Entity[], b: Entity[]): boolean {
return a.length === b.length && a.every((entity, i) => entity === b[i]);
}

View File

@ -0,0 +1,76 @@
/* 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 { For, onCleanup, onMount } from "solid-js";
import { Icon } from "@/components/ui/icon";
import {
CommandDialog,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from "@/components/ui/command";
import { usePendingCommand } from "@/context/command";
import type { Command } from "@/engine/command";
interface CommandPaletteProps {
commands: Command[];
open: boolean;
onOpenChange(open: boolean): void;
}
/**
* ⌘P: every command the editor can run on nodes. Picking one does not run
* it — it puts up the command bar to pick the nodes it runs on, starting
* from whatever of the selection it takes.
*/
export function CommandPalette(props: CommandPaletteProps) {
const { begin } = usePendingCommand();
// Bound here rather than in the engine's shortcut table: the palette is
// the page's to open, and ⌘P should reach it from a text field too.
const handleShortcut = (event: KeyboardEvent) => {
if (!(event.metaKey || event.ctrlKey) || event.shiftKey || event.altKey) return;
if (event.key.toLowerCase() !== "p") return;
event.preventDefault();
props.onOpenChange(!props.open);
};
onMount(() => window.addEventListener("keydown", handleShortcut));
onCleanup(() => window.removeEventListener("keydown", handleShortcut));
const handleSelect = (command: Command) => {
props.onOpenChange(false);
begin(command);
};
return (
<CommandDialog open={props.open} onOpenChange={props.onOpenChange}>
<CommandInput placeholder="Search actions…" />
<CommandList>
<CommandEmpty>No actions found.</CommandEmpty>
<CommandGroup heading="Actions">
<For each={props.commands}>
{(command) => (
<CommandItem
value={command.label}
keywords={[command.description, ...(command.keywords ?? [])]}
onSelect={() => handleSelect(command)}
>
<Icon name={command.icon} class="text-foreground" />
<div class="flex min-w-0 flex-col">
<span>{command.label}</span>
<span class="truncate text-xs text-muted-foreground">{command.description}</span>
</div>
</CommandItem>
)}
</For>
</CommandGroup>
</CommandList>
</CommandDialog>
);
}

View File

@ -0,0 +1,180 @@
/* 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 ⌘P offers: each command names the nodes it takes, and runs on the
// ones picked for it once the bar is confirmed (see `engine/command.ts`).
import { AssetId, PaintType, isScene } from "@diffusionstudio/runtime";
import { useEditor } from "@/engine/hooks";
import { useLibrary } from "@/engine/library";
import { usePromptInput } from "@/context/prompt-input";
import { createDefaultConfig } from "@/components/genai/prompt-input";
import { resolveMedia } from "@/components/genai/selection";
import { transformTarget, useTransforms } from "@/components/genai/use-transforms";
import { useAutoCaptions } from "@/components/genai/use-auto-captions";
import { generationConfigs, isGenerated } from "@/components/genai/use-generation-records";
import { useGenerateImage } from "@/components/genai/use-generate-image";
import { useGenerateVideo } from "@/components/genai/use-generate-video";
import { useGenerateVoice } from "@/components/genai/use-generate-voice";
import { useGenerateAudio } from "@/components/genai/use-generate-audio";
import { toast } from "somoto";
import type { TransformType } from "@diffusionstudio/jsx";
import type { Command } from "@/engine/command";
import type { GenerationConfig } from "@/components/genai/schemas";
import type { Entity } from "koota";
export function useCommands(): Command[] {
const editor = useEditor();
const library = useLibrary();
const { openPromptInput } = usePromptInput();
const { apply } = useTransforms();
const { generate: autoCaptions } = useAutoCaptions();
const { generate: generateImage } = useGenerateImage();
const { generate: generateVideo } = useGenerateVideo();
const { generate: generateVoice } = useGenerateVoice();
const { generate: generateAudio } = useGenerateAudio();
/** Whether `node` paints a picture the library holds, the kind the prompt box takes as a reference. */
const isLibraryImage = (node: Entity): boolean => {
const { source, paint } = resolveMedia(node);
const id = source.get(AssetId)?.value;
return paint === PaintType.IMAGE && id !== undefined && library()?.get(id)?.type === "IMAGE";
};
/**
* The prompt box reads its references and frames off the selection, so a
* command that hands nodes to it selects exactly those first.
*/
const promptWith = (nodes: Entity[], config: GenerationConfig) => {
editor.select(nodes);
openPromptInput(config);
};
const generate = (config: GenerationConfig): Promise<void> => {
switch (config.mode) {
case "IMAGE":
return generateImage(config);
case "VIDEO":
return generateVideo(config);
case "VOICE":
return generateVoice(config);
case "AUDIO":
return generateAudio(config);
}
};
/** What each of `nodes` was generated with; says so when none of them can say. */
const configsOf = async (nodes: Entity[]): Promise<GenerationConfig[]> => {
const lib = library();
const configs = lib ? await generationConfigs(nodes.map((node) => resolveMedia(node).source), lib) : [];
if (configs.length === 0) {
toast("No generation config found", { description: "This asset wasn't generated with a prompt." });
}
return configs;
};
const transformCommand = (
type: TransformType,
command: Omit<Command, "id" | "accepts" | "run">,
): Command => ({
...command,
id: type,
accepts: (node) => transformTarget(node, type) !== undefined,
run: (nodes) => apply(type, nodes),
});
return [
{
id: "auto-captions",
label: "Auto-caption scene",
description: "Transcribe the scene and add captions, or take a new transcription",
icon: "captions",
keywords: ["subtitles", "transcribe"],
hint: "Pick the scenes to caption",
noun: ["scene", "scenes"],
accepts: isScene,
run: autoCaptions,
},
transformCommand("upscale", {
label: "Upscale image or video",
description: "Raise the resolution of the source with AI",
icon: "arrow-scale",
keywords: ["resolution", "enhance"],
hint: "Pick images or videos to upscale",
noun: ["layer", "layers"],
}),
transformCommand("removeBackground", {
label: "Remove image background",
description: "Cut the subject out and leave the background transparent",
icon: "ai-generate",
keywords: ["cutout", "matte"],
hint: "Pick images to remove the background from",
noun: ["image", "images"],
}),
transformCommand("addAudio", {
label: "Add audio to video",
description: "Generate a soundtrack that fits what the video shows",
icon: "generate-audio",
keywords: ["sound", "score"],
hint: "Pick videos to add audio to",
noun: ["video", "videos"],
}),
{
id: "edit-with-prompt",
label: "Edit image with prompt",
description: "Open the prompt box with the images as references",
icon: "ai-generate",
keywords: ["generate", "image"],
hint: "Pick images to edit",
noun: ["image", "images"],
accepts: isLibraryImage,
run: (nodes) => promptWith(nodes, createDefaultConfig("IMAGE")),
},
{
id: "make-video",
label: "Make video from image",
description: "Open the prompt box with the image as the first frame",
icon: "film-video-export",
keywords: ["generate", "animate"],
hint: "Pick an image to start the video from",
noun: ["image", "images"],
limit: 1,
accepts: isLibraryImage,
run: (nodes) => promptWith(nodes, createDefaultConfig("VIDEO")),
},
{
id: "rerun",
label: "Rerun generation",
description: "Generate a new take with the same prompt and settings",
icon: "rerun",
keywords: ["regenerate", "again"],
hint: "Pick generated layers to rerun",
noun: ["generation", "generations"],
accepts: (node) => isGenerated(resolveMedia(node).source, library()),
run: async (nodes) => {
for (const config of await configsOf(nodes)) {
generate(config).catch((err) => {
toast("Rerun failed", { description: err instanceof Error ? err.message : String(err) });
});
}
},
},
{
id: "reuse-settings",
label: "Reuse generation settings",
description: "Open the prompt box with the prompt and settings it was made with",
icon: "reuse-settings",
keywords: ["generate", "prompt"],
hint: "Pick a generated layer to reuse",
noun: ["generation", "generations"],
limit: 1,
accepts: (node) => isGenerated(resolveMedia(node).source, library()),
run: async (nodes) => {
const [config] = await configsOf(nodes);
if (config) openPromptInput(config);
},
},
];
}

View File

@ -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 * from "./commands";
export * from "./command-bar";
export * from "./command-palette";

View File

@ -1,224 +0,0 @@
/* 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 { Button } from "@/components/ui/button";
import { Icon } from "@/components/ui/icon";
import { Separator } from "@/components/ui/separator";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuPortal,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { createMemo, Show } from "solid-js";
import { useGenerationRecords } from "./use-generation-records";
import { useGenerateImage } from "./use-generate-image";
import { useGenerateVideo } from "./use-generate-video";
import { useGenerateVoice } from "./use-generate-voice";
import { useGenerateAudio } from "./use-generate-audio";
import { useAutoCaptions } from "./use-auto-captions";
import { useMediaSelection } from "./selection";
import { useTransforms } from "./use-transforms";
import { createDefaultConfig } from "./prompt-input";
import { toast } from "somoto";
import type { GenerationConfig } from "./schemas";
interface ActionBarProps {
openPromptInput?(config: GenerationConfig): void;
}
export function ActionBar(props: ActionBarProps) {
const { imageNodes, videoNodes } = useMediaSelection();
const { isOn, toggle } = useTransforms();
const { generate: generateImage } = useGenerateImage();
const { generate: generateVideo } = useGenerateVideo();
const { generate: generateVoice } = useGenerateVoice();
const { generate: generateAudio } = useGenerateAudio();
const { generate: autoCaptions, hasScene } = useAutoCaptions();
const { isGenerated, totalCredits, firstConfig } = useGenerationRecords();
const isImage = createMemo(() => imageNodes().length > 0);
const isVideo = createMemo(() => videoNodes().length > 0);
const visible = createMemo(() => {
return isImage() || isVideo() || hasScene();
})
const handleRerun = () => {
const config = firstConfig();
if (!config) {
toast("No generation config found", { description: "This asset wasn't generated with a prompt." });
return;
}
const promise = (() => {
switch (config.mode) {
case "IMAGE":
return generateImage(config);
case "VIDEO":
return generateVideo(config);
case "VOICE":
return generateVoice(config);
case "AUDIO":
return generateAudio(config);
}
})();
promise.catch((err) => {
toast("Rerun failed", {
description: err instanceof Error ? err.message : String(err),
});
});
};
const handleEditWithPrompt = () => {
props.openPromptInput?.(createDefaultConfig("IMAGE"));
};
const handleMakeVideo = () => {
props.openPromptInput?.(createDefaultConfig("VIDEO"));
};
const handleReuse = () => {
const config = firstConfig();
if (!config) {
toast("No generation config found", { description: "This asset wasn't generated with a prompt." });
return;
}
props.openPromptInput?.(config);
};
return (
<>
<Show when={visible()}>
<div class="absolute bottom-16 left-1/2 -translate-x-1/2 z-10 rounded-xl p-1 bg-background border border-border flex gap-1 items-center">
<Show when={hasScene()}>
<Button variant="ghost" class="gap-0 pl-0.5 text-muted-foreground" onClick={autoCaptions}>
<Icon name="captions" />
Auto-Captions
</Button>
</Show>
<Show when={isImage()}>
<div class="flex gap-1 items-center">
<Button
variant="ghost"
class="gap-0 pl-0.5 text-muted-foreground"
classList={{ "text-foreground": isOn("removeBackground") }}
onClick={() => toggle("removeBackground")}
>
<Icon name="ai-generate" />
Remove background
</Button>
<Button
variant="ghost"
class="gap-0 pl-0.5 text-muted-foreground"
classList={{ "text-foreground": isOn("upscale") }}
onClick={() => toggle("upscale")}
>
<Icon name="arrow-scale" />
Upscale
</Button>
</div>
<Separator orientation="vertical" class="min-h-5" />
<div class="flex gap-1 items-center">
<Button variant="ghost" class="gap-0 pl-0.5 text-muted-foreground" onClick={handleEditWithPrompt}>
<Icon name="ai-generate" />
Edit with prompt
</Button>
<Show when={isGenerated()}>
<DropdownMenu placement="right">
<DropdownMenuTrigger<typeof Button>
as={(triggerProps) => (
<Button {...triggerProps} variant="ghost" class="gap-0 pr-0.5 text-muted-foreground">
More
<Icon name="chevron-down" />
</Button>
)}
/>
<DropdownMenuPortal>
<DropdownMenuContent>
<div class="flex items-center gap-1 px-0 pr-2 h-7">
<Icon name="ai-generate" class="size-6 text-muted-foreground" />
<span class="text-xs text-muted-foreground">{totalCredits()} AI credits used</span>
</div>
<Separator class="my-1" />
<DropdownMenuGroup>
<DropdownMenuItem onSelect={handleMakeVideo}>
<Icon name="film-video-export" class="size-6 mr-2 text-foreground" />
Make video
</DropdownMenuItem>
<DropdownMenuItem onSelect={handleRerun}>
<Icon name="rerun" class="size-6 mr-2 text-foreground" />
Rerun
</DropdownMenuItem>
<DropdownMenuItem onSelect={handleReuse}>
<Icon name="reuse-settings" class="size-6 mr-2 text-foreground" />
Reuse
</DropdownMenuItem>
</DropdownMenuGroup>
</DropdownMenuContent>
</DropdownMenuPortal>
</DropdownMenu>
</Show>
</div>
</Show>
<Show when={isVideo()}>
<div class="flex gap-1 items-center">
<Button
variant="ghost"
class="gap-0 pl-0.5 text-muted-foreground"
classList={{ "text-foreground": isOn("addAudio") }}
onClick={() => toggle("addAudio")}
>
<Icon name="generate-audio" />
Add audio
</Button>
<Button
variant="ghost"
class="gap-0 pl-0.5 text-muted-foreground"
classList={{ "text-foreground": isOn("upscale") }}
onClick={() => toggle("upscale")}
>
<Icon name="arrow-scale" />
Upscale
</Button>
<Show when={isGenerated()}>
<Separator orientation="vertical" class="min-h-5" />
<DropdownMenu placement="right">
<DropdownMenuTrigger<typeof Button>
as={(triggerProps) => (
<Button {...triggerProps} variant="ghost" class="gap-0 pr-0.5 text-muted-foreground">
More
<Icon name="chevron-down" />
</Button>
)}
/>
<DropdownMenuPortal>
<DropdownMenuContent>
<div class="flex items-center gap-1 px-0 pr-2 h-7">
<Icon name="ai-generate" class="size-6 text-muted-foreground" />
<span class="text-xs text-muted-foreground">{totalCredits()} AI credits used</span>
</div>
<Separator class="my-1" />
<DropdownMenuGroup>
<DropdownMenuItem onSelect={handleRerun}>
<Icon name="rerun" class="size-6 mr-2 text-foreground" />
Rerun
</DropdownMenuItem>
</DropdownMenuGroup>
</DropdownMenuContent>
</DropdownMenuPortal>
</DropdownMenu>
</Show>
</div>
</Show>
</div>
</Show>
</>
);
}

View File

@ -2,5 +2,4 @@
* 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 "./action-bar";
export * from "./prompt-input";

View File

@ -34,7 +34,7 @@ function isMediaPaint(paint: PaintType | undefined): paint is MediaPaint {
* else its first media fill. A node that paints no media at all is still its
* own source element — an `<audio>` carries its `src` like any other.
*/
function resolve(entity: Entity): SelectedNode {
export function resolveMedia(entity: Entity): SelectedNode {
const intrinsic = getIntrinsicPaint(entity);
if (isMediaPaint(intrinsic)) return { entity, source: entity, paint: intrinsic };
@ -50,7 +50,7 @@ export function useMediaSelection() {
const library = useLibrary();
const { nodes } = useSelection();
const selected = createMemo(() => nodes().map(resolve));
const selected = createMemo(() => nodes().map(resolveMedia));
const painted = (paint: MediaPaint) =>
createMemo(() => selected().filter((node) => node.paint === paint).map((node) => node.source));

View File

@ -2,11 +2,10 @@
* 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 } from "solid-js";
import { Captions, authoredElement } from "@diffusionstudio/reconciler";
import { Caption, getEntityTree, isScene } from "@diffusionstudio/runtime";
import { Caption, getEntityTree } from "@diffusionstudio/runtime";
import { useWorld } from "@diffusionstudio/koota-solid";
import { useEditor, useSelection } from "@/engine/hooks";
import { useEditor } from "@/engine/hooks";
import { toast } from "somoto";
import type { Entity } from "koota";
@ -14,10 +13,6 @@ import type { Entity } from "koota";
export function useAutoCaptions() {
const world = useWorld();
const editor = useEditor();
const { nodes } = useSelection();
const scenes = createMemo(() => nodes().filter(isScene));
const hasScene = () => scenes().length > 0;
const insertFresh = (scene: Entity) => {
const [entity] = editor.insertElement(scene, () => (
@ -27,19 +22,19 @@ export function useAutoCaptions() {
toast.error("Could not add captions", {
description: "The scene has no source to write the element into.",
});
return;
}
};
const generate = () => {
for (const scene of scenes()) {
/** Captions each of `scenes`: a fresh element, or a new take of the one it has. */
const generate = (scenes: Entity[]) => {
for (const scene of scenes) {
const existing = getEntityTree(world, scene).find(
(entity) => entity !== scene && entity.has(Caption),
);
if (!existing) {
insertFresh(scene);
return;
continue;
}
const authored = authoredElement(existing);
@ -49,14 +44,13 @@ export function useAutoCaptions() {
if (authored?.props.src !== undefined) {
editor.remove(existing);
insertFresh(scene);
return;
continue;
}
// A new take: a fresh seed re-transcribes the scene.
editor.editProperty(existing, "seed", Date.now());
editor.select(existing);
}
};
return { hasScene, generate };
return { generate };
}

View File

@ -5,6 +5,7 @@
import { createMemo, createResource } from "solid-js";
import { getAssetSpec, isAssetRef, isTransformSpec } from "@diffusionstudio/jsx";
import { authoredElement } from "@diffusionstudio/reconciler";
import { AssetId } from "@diffusionstudio/runtime";
import { useLibrary } from "@/engine/library";
import { supabase } from "@/lib/supabase";
import { useMediaSelection } from "./selection";
@ -19,6 +20,13 @@ import { generationConfigSchema, type GenerationConfig } from "./schemas";
import type { AssetInput, AssetRef, GenerateSpec } from "@diffusionstudio/jsx";
import type { AssetLibrary } from "@diffusionstudio/assets";
import type { Entity } from "koota";
interface UsageRecord {
id: string;
credits: number;
config: unknown;
}
/**
* The stored `usage_records.config` IS the server-side adapter input, which
@ -110,6 +118,62 @@ function generationUnder(ref: AssetRef): GenerateSpec | undefined {
return isAssetRef(spec.input) ? generationUnder(spec.input) : undefined;
}
/** The generation `source` declares its `src` to be, looked through its transforms. */
function declaredGeneration(source: Entity): GenerateSpec | undefined {
const src = authoredElement(source)?.props.src;
return isAssetRef(src) ? generationUnder(src) : undefined;
}
/** The server's id for the generation that made `source`'s asset, when one did. */
function generationIdOf(source: Entity, library: AssetLibrary | undefined): string | undefined {
const id = source.get(AssetId)?.value;
return (id && library?.get(id)?.generation?.id) || undefined;
}
/** Whether `source` holds a generation: one it declares, or an asset the server made. */
export function isGenerated(source: Entity, library: AssetLibrary | undefined): boolean {
return declaredGeneration(source) !== undefined || generationIdOf(source, library) !== undefined;
}
async function fetchUsageRecords(ids: string[]): Promise<UsageRecord[]> {
if (ids.length === 0 || !supabase) return [];
const { data, error } = await supabase
.from("usage_records")
.select("id,credits,config")
.in("id", ids);
if (error) {
console.error("[credits] Failed to load usage records", error);
return [];
}
return (data ?? []).map((row) => ({
id: row.id as string,
credits: (row.credits as number | null) ?? 0,
config: row.config as unknown,
}));
}
/**
* What each of `sources` was generated with, in order, as the prompt box
* would be set to run it again. A declaration answers for itself; an asset
* generated before the project was written in JSX is looked up on record.
* Sources with no generation behind them are left out.
*/
export async function generationConfigs(sources: Entity[], library: AssetLibrary): Promise<GenerationConfig[]> {
const declared = sources.map(declaredGeneration);
const recorded = sources.map((source, index) => (declared[index] ? undefined : generationIdOf(source, library)));
const records = await fetchUsageRecords([...new Set(recorded.filter((id): id is string => id !== undefined))]);
return sources
.map((_, index) => {
const spec = declared[index];
if (spec) return toPromptConfig(spec, library);
return toClientConfig(records.find((record) => record.id === recorded[index])?.config);
})
.filter((config): config is GenerationConfig => config !== undefined);
}
export function useGenerationRecords() {
const library = useLibrary();
const { bound, sources } = useMediaSelection();
@ -120,9 +184,7 @@ export function useGenerationRecords() {
// are looked through: the generation under them is what the box made.
const declarations = createMemo(() =>
sources()
.map((entity) => authoredElement(entity)?.props.src)
.filter(isAssetRef)
.map(generationUnder)
.map(declaredGeneration)
.filter((spec): spec is GenerateSpec => spec !== undefined),
);
@ -135,25 +197,7 @@ export function useGenerationRecords() {
return [...ids];
});
const [records] = createResource(() => generationIds(), async (ids) => {
if (ids.length === 0 || !supabase) return [];
const { data, error } = await supabase
.from("usage_records")
.select("id,credits,config")
.in("id", ids);
if (error) {
console.error("[credits] Failed to load usage records", error);
return [];
}
return (data ?? []).map((row) => ({
id: row.id as string,
credits: (row.credits as number | null) ?? 0,
config: row.config as unknown,
}));
},
);
const [records] = createResource(() => generationIds(), fetchUsageRecords);
const totalCredits = createMemo(() => {
return (records() ?? []).reduce((sum, r) => sum + r.credits, 0);

View File

@ -11,8 +11,9 @@
import { getAssetSpec, isAssetRef, isTransformSpec, mapAssetInputs, transform } from "@diffusionstudio/jsx";
import { authoredElement } from "@diffusionstudio/reconciler";
import { PaintType } from "@diffusionstudio/runtime";
import { useEditor } from "@/engine/hooks";
import { useMediaSelection } from "./selection";
import { resolveMedia, useMediaSelection } from "./selection";
import type { AssetInput, TransformType } from "@diffusionstudio/jsx";
import type { Entity } from "koota";
@ -44,6 +45,18 @@ function sourceOf(entity: Entity): AssetInput | undefined {
return typeof src === "string" && src !== "" ? src : isAssetRef(src) ? src : undefined;
}
/**
* The element `type` would be put over for `node`: the one its media source
* lives on, when that paints what the transform takes and has a `src` to wrap.
*/
export function transformTarget(node: Entity, type: TransformType): Entity | undefined {
const { source, paint } = resolveMedia(node);
if (paint === undefined || sourceOf(source) === undefined) return undefined;
if (type === "removeBackground" && paint !== PaintType.IMAGE) return undefined;
if (type === "addAudio" && paint !== PaintType.VIDEO) return undefined;
return source;
}
export function useTransforms() {
const editor = useEditor();
const { imageNodes, videoNodes } = useMediaSelection();
@ -82,5 +95,19 @@ export function useTransforms() {
}
};
return { isOn, toggle };
/**
* Puts `type` over the sources of `nodes` that do not ask for it yet — on,
* never off, and outermost like a toggle puts it.
*/
const apply = (type: TransformType, nodes: Entity[]): void => {
for (const node of nodes) {
const entity = transformTarget(node, type);
const src = entity && sourceOf(entity);
if (entity && src !== undefined && !hasTransform(src, type)) {
editor.editProperty(entity, "src", transform[type](src));
}
}
};
return { isOn, toggle, apply };
}

View File

@ -10,7 +10,8 @@ import { ItemRow } from "@/components/ui/item-row";
import { PanelSection } from "@/components/ui/panel-section";
import { useTrait, useWorld } from "@diffusionstudio/koota-solid";
import { Cache, Name } from "@diffusionstudio/runtime";
import { beginClipPathFor } from "@/engine/clip-path";
import { clipPathCommand } from "@/engine/clip-path";
import { usePendingCommand } from "@/context/command";
import { useDerived, useEditor } from "@/engine/hooks";
import type { Entity } from "koota";
@ -26,13 +27,14 @@ type ClipPathsSettingsProps = {
* The `<rect clipPath>` children of the selected node: the boxes it is
* clipped to (several intersect). A clip path is a rect like any other, so it
* has no inspector of its own — a row selects it and the transform, time and
* appearance panels are then its own. The plus picks up the clip path tool,
* whose bar over the canvas turns the rects selected there into clip paths
* of this node.
* appearance panels are then its own. The plus puts up the clip path
* command, whose bar over the canvas turns the rects selected there into
* clip paths of this node.
*/
export function ClipPathsSettings(props: ClipPathsSettingsProps) {
const world = useWorld();
const editor = useEditor();
const { begin } = usePendingCommand();
const entity = () => props.selection[0]!;
// Cache is derived state, written without change events.
@ -48,7 +50,7 @@ export function ClipPathsSettings(props: ClipPathsSettingsProps) {
size="icon"
variant="ghost"
class="text-muted-foreground"
onClick={() => beginClipPathFor(world, entity())}
onClick={() => begin(clipPathCommand(world, entity()))}
>
<Icon name="plus-add" />
</TooltipTrigger>

View File

@ -0,0 +1,111 @@
/* 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 { createContext, createEffect, createSignal, on, onCleanup, onMount, useContext } from "solid-js";
import { useWorld } from "@diffusionstudio/koota-solid";
import { Tool, ToolType } from "@diffusionstudio/runtime";
import { useDerived, useTool } from "@/engine/hooks";
import { canRunCommand, getCommandPicks } from "@/engine/command";
import { usePromptInput } from "@/context/prompt-input";
import { isInputTarget } from "@/utils/browser";
import type { Accessor, JSX } from "solid-js";
import type { Command } from "@/engine/command";
type CommandContextValue = {
/** The command waiting on its picks; null while no command is. */
pending: Accessor<Command | null>;
/**
* Puts `command` up to pick nodes for. Picking is selecting, so a tool
* that draws or aims at something is put down for the move tool first.
*/
begin: (command: Command) => void;
/** Takes the pending command down without running it. */
cancel: () => void;
/** Runs the pending command on its picks, and takes it down — when there are picks to run it on. */
confirm: () => void;
};
const CommandContext = createContext<CommandContextValue>();
/** Where a keypress belongs to something open over the editor, which has its own Esc. */
const LAYER = '[role="dialog"], [role="alertdialog"], [role="menu"], [role="listbox"]';
export function CommandProvider(props: { children: JSX.Element }) {
const world = useWorld();
const tool = useTool();
const { promptInputOpen } = usePromptInput();
const [pending, setPending] = createSignal<Command | null>(null);
const begin = (command: Command) => {
const current = world.get(Tool)?.value ?? ToolType.MOVE;
if (current !== ToolType.MOVE && current !== ToolType.HAND) world.set(Tool, { value: ToolType.MOVE });
setPending(command);
};
const cancel = () => {
const command = pending();
setPending(null);
command?.cancel?.();
};
const confirm = () => {
const command = pending();
if (!command) return;
const picks = getCommandPicks(world, command);
if (!canRunCommand(command, picks)) return;
setPending(null);
command.run(picks);
};
// A command picks by selecting: a tool that draws or aims, or the prompt
// box coming up, takes it down.
createEffect(on(tool, (value) => {
if (value !== ToolType.MOVE && value !== ToolType.HAND) cancel();
}, { defer: true }));
createEffect(on(promptInputOpen, (open) => {
if (open) cancel();
}, { defer: true }));
// Nothing left to work on (the node it was for was deleted, or undone away).
const available = useDerived(() => pending()?.available?.() ?? true);
createEffect(() => {
if (!available()) cancel();
});
/**
* Esc and ⌘↵ are the bar's while it is up. Caught on the way down, ahead of
* the engine's own — Esc there drops the selection the picks are, ⌘↵ wraps
* it in a scene.
*/
const handleKeyDown = (event: KeyboardEvent) => {
if (!pending() || isInputTarget(event)) return;
if (event.target instanceof Element && event.target.closest(LAYER)) return;
const mod = event.metaKey || event.ctrlKey;
if (event.key === "Escape" && !mod) cancel();
else if (event.key === "Enter" && mod && !event.shiftKey && !event.altKey) confirm();
else return;
event.preventDefault();
event.stopImmediatePropagation();
};
onMount(() => window.addEventListener("keydown", handleKeyDown, { capture: true }));
onCleanup(() => window.removeEventListener("keydown", handleKeyDown, { capture: true }));
return (
<CommandContext.Provider value={{ pending, begin, cancel, confirm }}>
{props.children}
</CommandContext.Provider>
);
}
export function usePendingCommand() {
const context = useContext(CommandContext);
if (!context) throw new Error("usePendingCommand must be used within a CommandProvider");
return context;
}

View File

@ -5,16 +5,14 @@
import {
Computed,
Flip,
Name,
Source,
Tool,
ToolType,
decompose2D,
entityAnchor,
entityOffset,
entityWorldMat,
getEntityTree,
getIntrinsicPaint,
getSelection,
invert2D,
isClipPath,
isShape,
@ -22,36 +20,42 @@ import {
store,
translate2D,
} from '@diffusionstudio/runtime';
import { createSignal } from 'solid-js';
import { getDocumentEditor } from './editor';
import { editTransform } from './input/interactions';
import type { Entity, World } from 'koota';
import type { Command } from './command';
import type { TransformWrite } from './input/interactions';
const round2 = (value: number): number => Math.round(value * 100) / 100;
// The node the tool is aimed at, reactive so the bar follows it.
const [clipPathTarget, setClipPathTarget] = createSignal<Entity | null>(null);
/**
* The command that clips `target`: the bar over the canvas counts the
* selected rects that would clip it, Confirm makes them its clip paths, and
* either way out leaves `target` selected. The selection stays on it until a
* rect is picked.
*/
export function clipPathCommand(world: World, target: Entity): Command {
const editor = getDocumentEditor(world);
const reselect = () => {
if (target.isAlive()) editor.select(target);
};
/** The node the picked rects will clip, reactive; null while the tool is down. */
export { clipPathTarget };
export function getClipPathTarget(): Entity | null {
const target = clipPathTarget();
return target?.isAlive() ? target : null;
}
/** Picks the tool to add clip paths to `target`. The selection stays on it until a rect is picked. */
export function beginClipPathFor(world: World, target: Entity): void {
setClipPathTarget(target);
world.set(Tool, { value: ToolType.CLIP_PATH });
}
/** Forgets the target: the tool is down. */
export function clearClipPathTarget(): void {
setClipPathTarget(null);
return {
id: 'clip-path',
label: `Clip ${target.get(Name)?.value || 'this layer'}`,
description: 'Use the picked shapes as clip paths of the layer',
icon: 'mask-small',
hint: 'Pick a shape to use as the clipping source',
noun: ['shape', 'shapes'],
accepts: (entity) => canClipWith(world, target, entity),
run: (rects) => {
applyClipPaths(world, target, rects);
reselect();
},
available: () => target.isAlive(),
cancel: reselect,
};
}
/**
@ -66,32 +70,20 @@ export function canClipWith(world: World, target: Entity, entity: Entity): boole
return !getEntityTree(world, entity).includes(target);
}
/** The selected rects Confirm would apply to the target, in selection order. */
export function getClipPathPicks(world: World): Entity[] {
const target = getClipPathTarget();
if (!target) return [];
return getSelection(world).filter((entity) => canClipWith(world, target, entity));
}
/**
* Makes the picked rects clip paths of the target, one undo step, and puts
* the tool down with the target selected. Each rect keeps its place on the
* canvas: its matrix is carried from the parent it had into the target's
* Makes `rects` clip paths of `target`, one undo step. Each rect keeps its
* place on the canvas: its matrix is carried from the parent it had into the target's
* space and written back as `x`, `y` and `rotation`, with whatever scale the
* move adds (a target drawn at half size, say) folded into its `width` and
* `height` rather than its own scale.
*/
export function applyClipPaths(world: World): void {
const target = getClipPathTarget();
const picks = getClipPathPicks(world);
if (!target || picks.length === 0) return;
export function applyClipPaths(world: World, target: Entity, rects: Entity[]): void {
const editor = getDocumentEditor(world);
const computed = store(world, Computed);
const flip = store(world, Flip);
const targetInverse = invert2D(entityWorldMat(world, target));
for (const rect of picks) {
for (const rect of rects) {
const eid = rect.id();
const width = computed.width[eid] ?? 0;
const height = computed.height[eid] ?? 0;
@ -126,17 +118,4 @@ export function applyClipPaths(world: World): void {
if (nextHeight !== Math.round(height)) writes.push(['height', nextHeight]);
if (writes.length) editTransform(world, editor, rect, writes);
}
leaveClipPathTool(world, target);
}
/** Puts the tool down without applying anything; the selection goes back to the target. */
export function cancelClipPath(world: World): void {
leaveClipPathTool(world, getClipPathTarget());
}
function leaveClipPathTool(world: World, target: Entity | null): void {
clearClipPathTarget();
if (target) getDocumentEditor(world).select(target);
if (world.get(Tool)?.value === ToolType.CLIP_PATH) world.set(Tool, { value: ToolType.MOVE });
}

View File

@ -0,0 +1,52 @@
/* 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/. */
// A command runs on nodes the user picks for it: while one is pending the
// command bar is over the canvas, what is selected and the command takes is
// what it would run on, and Confirm (⌘↵) runs it on them. Which command is
// pending is the page's to hold (see `CommandProvider`); what it would run
// on is read off the world here.
import { AdjustmentLayer, Geometry, Group, Selected } from '@diffusionstudio/runtime';
import { Or } from 'koota';
import type { Entity, World } from 'koota';
export interface Command {
id: string;
/** Names what it applies to, so the palette reads as "do this to that". */
label: string;
/** One line under the label: what running it does. */
description: string;
icon: string;
/** More words the palette's search should find it by. */
keywords?: string[];
/** What the bar asks for while nothing it takes is selected. */
hint: string;
/** What it takes, as one and as many, for the bar's count. */
noun: readonly [one: string, many: string];
/** How many it takes at once, when it works on one thing at a time. */
limit?: number;
/** Whether a selected node is one it takes. */
accepts(node: Entity): boolean;
/** Runs it on the picked nodes. */
run(nodes: Entity[]): void;
/** Whether it still has what it works on; the bar comes down once it has not. */
available?(): boolean;
/** What taking it down without running it leaves behind. */
cancel?(): void;
}
/** The node kinds a command can be run on. */
const NODES = Or(Geometry, Group, AdjustmentLayer);
/** The selected nodes `command` takes: what Confirm would run it on. */
export function getCommandPicks(world: World, command: Command): Entity[] {
return [...world.query(Selected, NODES)].filter((node) => command.accepts(node));
}
/** Whether `picks` are something `command` can run on: any at all, and no more than it takes. */
export function canRunCommand(command: Command, picks: Entity[]): boolean {
return picks.length > 0 && picks.length <= (command.limit ?? Infinity);
}

View File

@ -92,7 +92,6 @@ const TOOL_CURSORS: Record<ToolType, { idle: CursorType; pressed?: CursorType }>
[ToolType.TEXT]: { idle: 'crosshair' },
[ToolType.TEXT_EDIT]: { idle: 'text' },
[ToolType.OBJECT_MASK]: { idle: 'crosshair' },
[ToolType.CLIP_PATH]: { idle: 'default' },
};
/** The cursor the armed tool asks for, pressed or at rest. The object mask's brush draws its own ring, so it hides the cursor. */

View File

@ -34,7 +34,6 @@ import {
import { Not, Or } from 'koota';
import { zoomBy, zoomTo, zoomToFit, zoomToSelection } from '../camera';
import { applyClipPaths, cancelClipPath } from '../clip-path';
import { getDocumentEditor } from '../editor';
import { groupSelection, ungroupSelection, unwrapSequenceSelection, wrapSelectionInScene, wrapSelectionInSequence } from '../group';
import { getEditHistory } from '../history';
@ -453,12 +452,7 @@ const maskStrokeToUndo = (world: World): boolean => {
return objectMaskTool(world) && track?.status === 'seeded' && track.strokes.length > 0;
};
const clipPathTool = (world: World): boolean => world.get(Tool)?.value === ToolType.CLIP_PATH;
const PRESSED_SHORTCUTS: readonly Shortcut[] = [
// Clip path tool shortcuts, ahead of the Esc and ⌘↵ the rest of the editor answers to.
{ keys: ['escape'], action: cancelClipPath, active: clipPathTool },
{ keys: ['enter', 'mod', '!shift', '!alt'], action: applyClipPaths, active: clipPathTool },
{ keys: ['z', 'mod', '!shift'], action: undoEdit },
{ keys: ['z', 'mod', 'shift'], action: redoEdit },
{ keys: ['backspace'], action: deleteSelection },

View File

@ -7,6 +7,7 @@ import { Navigate, useLocation, useNavigate } from '@solidjs/router';
import { EditorPage } from './editor';
import { LayoutProvider } from "@/context/layout";
import { PromptInputProvider } from "@/context/prompt-input";
import { CommandProvider } from "@/context/command";
import { EditorApiProvider } from '@/dapi';
import { ExportProvider } from '@/context/export';
import { ProjectProvider } from '@/context/project';
@ -60,9 +61,11 @@ export function ProjectPage() {
<TimelineProvider>
<ExportProvider>
<PromptInputProvider>
<LayoutProvider>
<EditorPage />
</LayoutProvider>
<CommandProvider>
<LayoutProvider>
<EditorPage />
</LayoutProvider>
</CommandProvider>
</PromptInputProvider>
</ExportProvider>
</TimelineProvider>

View File

@ -177,5 +177,4 @@ export enum ToolType {
TEXT,
TEXT_EDIT,
OBJECT_MASK,
CLIP_PATH,
}