Skip to content

The VS Code of video editing, built for agents.

TypeScript

feat: implement dynamic toolbar slot positioning and update related components

konstantin-paulusd8ca02ae026f
diff --git a/apps/web/src/components/canvas/action-bar.tsx b/apps/web/src/components/canvas/action-bar.tsx
index 3d059c4e..8c7b88ec 100644
--- a/apps/web/src/components/canvas/action-bar.tsx
+++ b/apps/web/src/components/canvas/action-bar.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/. */
-// The bar over the canvas, above the toolbar, that a tool or a command puts
+// The bar over the canvas, beside the toolbar, that a tool or a command puts
// up while it works: what it applies to, and the way out. The pieces here are
// what bars share; what goes between them is each bar's own.
@@ -15,15 +15,20 @@ import { Icon } from "@/components/ui/icon";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import { getClipFallbackName } from "@/engine/timeline/style";
import { cx } from "@/lib/cva";
+import { useLayout } from "@/context/layout";
+import { BAR_SLOT_CLASS } from "./toolbar-slot";
import type { Entity } from "koota";
import type { JSX } from "solid-js";
export function ActionBar(props: { class?: string; children: JSX.Element }) {
+ const { toolbarSlot } = useLayout();
+
return (
<div
class={cx(
- "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",
+ "absolute z-10 rounded-xl px-1.5 py-1 bg-background border border-border flex gap-1 items-center",
+ BAR_SLOT_CLASS[toolbarSlot()],
props.class,
)}
>
diff --git a/apps/web/src/components/canvas/desktop-app-banner.tsx b/apps/web/src/components/canvas/desktop-app-banner.tsx
index 3e1cc99a..21661722 100644
--- a/apps/web/src/components/canvas/desktop-app-banner.tsx
+++ b/apps/web/src/components/canvas/desktop-app-banner.tsx
@@ -10,15 +10,18 @@ import { createStoredSignal } from "@/lib/store";
import { desktopAppDownloadLabel, downloadDesktopApp } from "@/lib/desktop-app";
import { track } from "@/lib/analytics";
import { store } from "@/init";
+import { cx } from "@/lib/cva";
+import { useLayout } from "@/context/layout";
const BANNER_IMAGE = new URL("@/assets/images/desktop-app-banner.png", import.meta.url).href;
/**
* Dismissible promo for the desktop app, pinned to the bottom left of the
- * canvas. Hidden in the desktop build, which is the thing being advertised.
+ * canvas, or the bottom right while the toolbar is in that corner. Hidden in the desktop build, which is the thing being advertised.
*/
export function DesktopAppBanner() {
const { isDesktop } = useEditorApi();
+ const { toolbarSlot } = useLayout();
const [dismissed, setDismissed] = createStoredSignal(
store.define("canvas.desktop-app-banner-dismissed", false),
);
@@ -32,7 +35,12 @@ export function DesktopAppBanner() {
return (
<Show when={!dismissed() && !isDesktop}>
- <div class="absolute bottom-4 left-4 z-10 flex w-[220px] flex-col gap-1 rounded-md border border-border bg-background pb-3 shadow-[0px_0px_1px_2px_rgba(0,0,0,0.12),0px_4px_12px_8px_rgba(0,0,0,0.12)]">
+ <div
+ class={cx(
+ "absolute bottom-4 z-10 flex w-[220px] flex-col gap-1 rounded-md border border-border bg-background pb-3 shadow-[0px_0px_1px_2px_rgba(0,0,0,0.12),0px_4px_12px_8px_rgba(0,0,0,0.12)]",
+ toolbarSlot() === "bottom-left" ? "right-4" : "left-4",
+ )}
+ >
<div class="relative aspect-[220/122] w-full overflow-hidden rounded-t-md">
<img src={BANNER_IMAGE} alt="" class="pointer-events-none size-full object-cover" />
<button
diff --git a/apps/web/src/components/canvas/toolbar-slot.ts b/apps/web/src/components/canvas/toolbar-slot.ts
new file mode 100644
index 00000000..ea31fcd9
--- /dev/null
+++ b/apps/web/src/components/canvas/toolbar-slot.ts
@@ -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/. */
+
+// Where the toolbar goes in each of its slots (see `useLayout().toolbarSlot`),
+// and where the bars a tool or a command puts up go with it, on its inner side.
+
+import type { ToolbarSlot } from "@/context/layout";
+
+export const TOOLBAR_SLOT_CLASS: Record<ToolbarSlot, string> = {
+ "bottom": "bottom-4 left-1/2 -translate-x-1/2",
+ "top": "top-4 left-1/2 -translate-x-1/2",
+ "top-left": "top-4 left-4",
+ "top-right": "top-4 right-4",
+ "bottom-left": "bottom-4 left-4",
+ "bottom-right": "bottom-4 right-4",
+};
+
+/** Where a bar goes beside the toolbar in each slot: a step in from the edge it is on. */
+export const BAR_SLOT_CLASS: Record<ToolbarSlot, string> = {
+ "bottom": "bottom-16 left-1/2 -translate-x-1/2",
+ "top": "top-16 left-1/2 -translate-x-1/2",
+ "top-left": "top-16 left-4",
+ "top-right": "top-16 right-4",
+ "bottom-left": "bottom-16 left-4",
+ "bottom-right": "bottom-16 right-4",
+};
+
+export const isTopSlot = (slot: ToolbarSlot) => slot.startsWith("top");
diff --git a/apps/web/src/components/canvas/toolbar.tsx b/apps/web/src/components/canvas/toolbar.tsx
index 5da15f62..88999734 100644
--- a/apps/web/src/components/canvas/toolbar.tsx
+++ b/apps/web/src/components/canvas/toolbar.tsx
@@ -18,12 +18,17 @@ import { PromptInput } from "../genai/prompt-input";
import { CommandBar } from "../commands";
import { ObjectMaskBar } from "./object-mask-bar";
import { ClipPathBar } from "./clip-path-bar";
-import { For, Match, Show, Switch, createEffect, createMemo, createSignal, on } from "solid-js";
+import { For, Match, Show, Switch, createEffect, createMemo, createSignal, on, onCleanup } from "solid-js";
import { Tool, ToolType } from "@diffusionstudio/runtime";
import { useWorld } from "@diffusionstudio/koota-solid";
import { cancelClipPath, cancelObjectMask, clearClipPathTarget, useTool } from "@/engine";
import { usePromptInput } from "@/context/prompt-input";
import { useCommands } from "@/context/commands";
+import { TOOLBAR_SLOTS, useLayout } from "@/context/layout";
+import { TOOLBAR_SLOT_CLASS, isTopSlot } from "./toolbar-slot";
+import { cx } from "@/lib/cva";
+
+import type { ToolbarSlot } from "@/context/layout";
const CURSOR_TOOLS = [
{ tool: ToolType.MOVE, label: 'Move', shortcut: 'V', icon: 'move', menuIcon: 'move-small' },
@@ -43,10 +48,14 @@ type ShapeTool = (typeof SHAPE_TOOLS)[number];
// Outlives the toolbar, which unmounts with the canvas.
const [lastShapeTool, setLastShapeTool] = createSignal<ShapeTool>(SHAPE_TOOLS[0]);
+/** How far the pointer goes, pressed on the toolbar, before the press is a drag rather than a click. */
+const DRAG_THRESHOLD = 4;
+
export function Toolbar() {
const world = useWorld();
const { promptInputOpen, promptInputConfig, setPromptInputOpen } = usePromptInput();
const commands = useCommands();
+ const { toolbarSlot, setToolbarSlot } = useLayout();
const selectedTool = useTool();
const cursorTool = createMemo(() => CURSOR_TOOLS.find((cursor) => cursor.tool === selectedTool()));
const shapeTool = createMemo(() => SHAPE_TOOLS.find((shape) => shape.tool === selectedTool()));
@@ -82,6 +91,66 @@ export function Toolbar() {
world.set(Tool, { value: tool });
}
+ // Dragging the toolbar shows the slots it can go to and puts it in the one
+ // nearest the pointer as it goes; where it is let go of, it stays.
+ let toolbar!: HTMLDivElement;
+ const slots: Partial<Record<ToolbarSlot, HTMLDivElement>> = {};
+ const [dragging, setDragging] = createSignal(false);
+ let press: { x: number; y: number; pointerId: number } | undefined;
+
+ const nearestSlot = (x: number, y: number) => {
+ let nearest = toolbarSlot();
+ let nearestDistance = Infinity;
+ for (const slot of TOOLBAR_SLOTS) {
+ const rect = slots[slot]?.getBoundingClientRect();
+ if (!rect) continue;
+ const distance = Math.hypot(x - (rect.left + rect.width / 2), y - (rect.top + rect.height / 2));
+ if (distance < nearestDistance) {
+ nearest = slot;
+ nearestDistance = distance;
+ }
+ }
+ return nearest;
+ };
+
+ const handlePointerMove = (event: PointerEvent) => {
+ if (event.pointerId !== press?.pointerId) return;
+ if (!dragging()) {
+ if (Math.hypot(event.clientX - press.x, event.clientY - press.y) < DRAG_THRESHOLD) return;
+ // Captured, the press no longer ends in a click on the button it began on.
+ toolbar.setPointerCapture(event.pointerId);
+ setDragging(true);
+ }
+ setToolbarSlot(nearestSlot(event.clientX, event.clientY));
+ };
+
+ const handlePointerUp = (event: PointerEvent) => {
+ if (event.pointerId !== press?.pointerId) return;
+ press = undefined;
+ setDragging(false);
+ window.removeEventListener("pointermove", handlePointerMove);
+ window.removeEventListener("pointerup", handlePointerUp);
+ window.removeEventListener("pointercancel", handlePointerUp);
+ };
+
+ const handlePointerDown = (event: PointerEvent) => {
+ // Menus open on the press: one begun on their trigger is theirs.
+ if (event.button !== 0 || (event.target as Element).closest("[aria-haspopup]")) return;
+ press = { x: event.clientX, y: event.clientY, pointerId: event.pointerId };
+ window.addEventListener("pointermove", handlePointerMove);
+ window.addEventListener("pointerup", handlePointerUp);
+ window.addEventListener("pointercancel", handlePointerUp);
+ };
+
+ onCleanup(() => {
+ window.removeEventListener("pointermove", handlePointerMove);
+ window.removeEventListener("pointerup", handlePointerUp);
+ window.removeEventListener("pointercancel", handlePointerUp);
+ });
+
+ // Up top, the menus open downwards, into the canvas.
+ const menuPlacement = () => (isTopSlot(toolbarSlot()) ? "bottom-start" : "top-start");
+
return (
<>
<Show when={promptInputOpen()}>
@@ -97,7 +166,27 @@ export function Toolbar() {
</Match>
</Switch>
</Show>
- <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">
+ <Show when={dragging()}>
+ <For each={TOOLBAR_SLOTS}>
+ {(slot) => (
+ <div
+ ref={(el) => (slots[slot] = el)}
+ class={cx("absolute z-10 size-10.5 rounded-xl bg-foreground/10 pointer-events-none", TOOLBAR_SLOT_CLASS[slot])}
+ />
+ )}
+ </For>
+ </Show>
+ {/* Up top, it sits over the desktop window's drag strip, which would take its clicks. */}
+ <div
+ ref={toolbar}
+ class={cx(
+ "absolute rounded-xl p-1.5 bg-background border border-border-strong flex gap-2 items-center z-30",
+ TOOLBAR_SLOT_CLASS[toolbarSlot()],
+ dragging() && "cursor-grabbing",
+ )}
+ style="-webkit-app-region: no-drag;"
+ onPointerDown={handlePointerDown}
+ >
<div class="flex gap-1">
<Tooltip>
<TooltipTrigger
@@ -113,7 +202,7 @@ export function Toolbar() {
{(cursorTool() ?? CURSOR_TOOLS[0]!).label}
</TooltipContent>
</Tooltip>
- <DropdownMenu placement="top-start">
+ <DropdownMenu placement={menuPlacement()}>
<Tooltip>
<TooltipTrigger<typeof DropdownMenuTrigger>
as={(triggerProps: object) => (
@@ -173,7 +262,7 @@ export function Toolbar() {
</TooltipTrigger>
<TooltipContent shortcut={lastShapeTool().shortcut}>{lastShapeTool().label}</TooltipContent>
</Tooltip>
- <DropdownMenu placement="top-start">
+ <DropdownMenu placement={menuPlacement()}>
<Tooltip>
<TooltipTrigger<typeof DropdownMenuTrigger>
as={(triggerProps: object) => (
diff --git a/apps/web/src/components/genai/prompt-input.tsx b/apps/web/src/components/genai/prompt-input.tsx
index 829288c5..32c8fd53 100644
--- a/apps/web/src/components/genai/prompt-input.tsx
+++ b/apps/web/src/components/genai/prompt-input.tsx
@@ -60,6 +60,8 @@ import { droppedFiles, importFiles, pickFiles } from "@/engine/asset-actions";
import { useMediaSelection } from "./selection";
import { ASSET_DRAG_TYPE } from "@/components/sidebar-left/folder-item";
import { useBarKey } from "@/components/canvas/action-bar";
+import { isTopSlot } from "@/components/canvas/toolbar-slot";
+import { useLayout } from "@/context/layout";
import { usePromptInput } from "@/context/prompt-input";
import type { ModelId, Resolution } from "@diffusionstudio/api-contract";
@@ -83,6 +85,7 @@ export function PromptInput(props: PromptInputProps) {
const { images: selectedImages } = useMediaSelection();
const { generate } = useGenerate();
const { setPromptInputOpen } = usePromptInput();
+ const { toolbarSlot } = useLayout();
// Esc puts the box down, what was typed kept for next time: from the
// prompt, once the recent prompts are dismissed, and from anywhere else
@@ -400,7 +403,10 @@ export function PromptInput(props: PromptInputProps) {
return (
<div
- class="absolute w-xl max-w-[calc(100%-2rem)] bottom-16 z-10 mx-auto left-1/2 -translate-x-1/2 flex flex-col gap-2 rounded-xl border border-border bg-background p-2"
+ class={cx(
+ "absolute w-xl max-w-[calc(100%-2rem)] z-10 mx-auto left-1/2 -translate-x-1/2 flex flex-col gap-2 rounded-xl border border-border bg-background p-2",
+ isTopSlot(toolbarSlot()) ? "bottom-4" : "bottom-16",
+ )}
onDragOver={handleDragOver}
onDragEnter={handleDragEnter}
onDragLeave={handleDragLeave}
diff --git a/apps/web/src/context/layout.tsx b/apps/web/src/context/layout.tsx
index 40b39b0c..2dcd5868 100644
--- a/apps/web/src/context/layout.tsx
+++ b/apps/web/src/context/layout.tsx
@@ -7,11 +7,18 @@ import { assert } from '@/utils';
import { createStoredSignal } from '@/lib/store';
import { store } from '@/init';
+/** Where the toolbar sits over the canvas: one of six slots it snaps to when dragged, along the top and bottom edges. */
+export const TOOLBAR_SLOTS = ['bottom', 'top', 'top-left', 'top-right', 'bottom-left', 'bottom-right'] as const;
+
+export type ToolbarSlot = (typeof TOOLBAR_SLOTS)[number];
+
type LayoutContextValue = {
uiVisible: Accessor<boolean>;
timelineMinimized: Accessor<boolean>;
timelineHeight: Accessor<number>;
setTimelineHeight(height: number): void;
+ toolbarSlot: Accessor<ToolbarSlot>;
+ setToolbarSlot(slot: ToolbarSlot): void;
toggleUI(): void;
toggleTimeline(): void;
};
@@ -33,6 +40,12 @@ export function LayoutProvider(props: { children: JSX.Element }) {
store.define<boolean>('layout.timelineMinimized', false),
);
+ const [toolbarSlot, setToolbarSlot] = createStoredSignal(
+ store.define<ToolbarSlot>('layout.toolbarSlot', 'bottom', (value) =>
+ TOOLBAR_SLOTS.includes(value) ? value : 'bottom',
+ ),
+ );
+
const toggleUI = () => setUiVisible(!uiVisible());
const toggleTimeline = () => setTimelineMinimized(!timelineMinimized());
@@ -43,6 +56,8 @@ export function LayoutProvider(props: { children: JSX.Element }) {
timelineMinimized,
timelineHeight,
setTimelineHeight,
+ toolbarSlot,
+ setToolbarSlot,
toggleUI,
toggleTimeline,
}}>