From f4421b216fd0fec2cec1d931afe3b6a04cdf0699 Mon Sep 17 00:00:00 2001 From: iris Date: Fri, 20 Nov 2026 18:32:23 +0100 Subject: [PATCH] swarm ui: the header's tabs can be dragged onto each other to reorder them Group tabs now show the grip and drag like agent tabs, and every tab in the strip takes the place of the tab another one is dropped on; the order is kept with the open tabs. An agent's tab keeps its other drag, onto a terminal to split it. moveTab is a pure function with tests. --- .../packages/swarm-ui/src/shell/Shell.css | 4 ++ .../packages/swarm-ui/src/shell/Shell.tsx | 52 ++++++++++++++----- .../packages/swarm-ui/src/shell/tab-order.ts | 37 +++++++++++++ .../swarm-ui/src/shell/useDynamicTabs.ts | 9 +++- .../packages/swarm-ui/test/tab-order.test.mjs | 26 ++++++++++ 5 files changed, 114 insertions(+), 14 deletions(-) create mode 100644 frontend/packages/swarm-ui/src/shell/tab-order.ts create mode 100644 frontend/packages/swarm-ui/test/tab-order.test.mjs diff --git a/frontend/packages/swarm-ui/src/shell/Shell.css b/frontend/packages/swarm-ui/src/shell/Shell.css index 5bf662492..5c6e06e28 100644 --- a/frontend/packages/swarm-ui/src/shell/Shell.css +++ b/frontend/packages/swarm-ui/src/shell/Shell.css @@ -223,6 +223,10 @@ padding-left: 1.3em; cursor: grab; } +/* The tab a dragged tab would take the place of. */ +.shell-tab-drop { + box-shadow: inset 2px 0 0 var(--purple); +} .shell-tab-grip { position: absolute; left: 0.35em; diff --git a/frontend/packages/swarm-ui/src/shell/Shell.tsx b/frontend/packages/swarm-ui/src/shell/Shell.tsx index 930bc1a66..d6729440f 100644 --- a/frontend/packages/swarm-ui/src/shell/Shell.tsx +++ b/frontend/packages/swarm-ui/src/shell/Shell.tsx @@ -52,6 +52,7 @@ import { terminalAgentOf, } from "../agent/terminal/split-layout.js"; import { endAgentDrag, startAgentDrag } from "../agent/terminal/termDrag.js"; +import { draggedTab, endTabDrag, startTabDrag } from "./tab-order.js"; import { useTermGroups } from "../agent/terminal/useTermGroups.js"; import { useSwarmName } from "./useSwarmName.js"; import "./Shell.css"; @@ -167,6 +168,7 @@ function DynamicTabLink({ onClose, agent, onRename, + onMove, }: { href: string; label: string; @@ -176,29 +178,52 @@ function DynamicTabLink({ agent?: string; /** Set on a group's tab: a double click renames the group. */ onRename?: (name: string) => void; + /** Called with the tab dragged onto this one, which takes this tab's place. */ + onMove: (from: string) => void; }) { const [editing, setEditing] = useState(false); + const [over, setOver] = useState(false); return ( startAgentDrag(e, agent) - } - onDragEnd={agent === undefined ? undefined : endAgentDrag} + draggable={!editing} + onDragStart={(e) => { + startTabDrag(e, href); + if (agent !== undefined) startAgentDrag(e, agent); + }} + onDragEnd={() => { + endTabDrag(); + endAgentDrag(); + setOver(false); + }} + onDragOver={(e) => { + const from = draggedTab(); + if (from === null || from === href) return; + e.preventDefault(); + setOver(true); + }} + onDragLeave={() => setOver(false)} + onDrop={(e) => { + const from = draggedTab(); + setOver(false); + if (from === null || from === href) return; + e.preventDefault(); + onMove(from); + }} > - {agent !== undefined && ( - - )} + {editing && onRename !== undefined ? ( moveTabTo(from, tab.href)} onRename={ groupId === null ? undefined diff --git a/frontend/packages/swarm-ui/src/shell/tab-order.ts b/frontend/packages/swarm-ui/src/shell/tab-order.ts new file mode 100644 index 000000000..1d0569c4f --- /dev/null +++ b/frontend/packages/swarm-ui/src/shell/tab-order.ts @@ -0,0 +1,37 @@ +// The order of the header's dynamic tabs, and the drag that changes it. A tab +// dropped on another takes that tab's place and the ones between shift by one. +// As in `termDrag`, a drop target reads a drag's payload only on drop, so the +// dragged tab's `href` also sits in a module variable for the hover. +export const TAB_DRAG_TYPE = "application/x-hive-tab"; + +/** `tabs` with the tab at `from` moved to the place of the tab at `to`. */ +export function moveTab( + tabs: T[], + from: string, + to: string, +): T[] { + const i = tabs.findIndex((t) => t.href === from); + const j = tabs.findIndex((t) => t.href === to); + if (i < 0 || j < 0 || i === j) return tabs; + const next = [...tabs]; + const [moved] = next.splice(i, 1); + next.splice(j, 0, moved); + return next; +} + +let dragged: string | null = null; + +export function startTabDrag(e: DragEvent, href: string) { + dragged = href; + e.dataTransfer?.setData(TAB_DRAG_TYPE, href); + if (e.dataTransfer) e.dataTransfer.effectAllowed = "move"; +} + +export function endTabDrag() { + dragged = null; +} + +/** The tab being dragged, `null` when the drag is something else. */ +export function draggedTab(): string | null { + return dragged; +} diff --git a/frontend/packages/swarm-ui/src/shell/useDynamicTabs.ts b/frontend/packages/swarm-ui/src/shell/useDynamicTabs.ts index e2a89ad07..d39f8eaff 100644 --- a/frontend/packages/swarm-ui/src/shell/useDynamicTabs.ts +++ b/frontend/packages/swarm-ui/src/shell/useDynamicTabs.ts @@ -20,6 +20,7 @@ import { readLocalSetting, useLocalSetting, } from "@hive/shared/settings-storage.js"; +import { moveTab } from "./tab-order.js"; export interface DynamicTab { href: string; @@ -34,6 +35,8 @@ export interface UseDynamicTabsResult { open: (tab: DynamicTab) => void; /** Removes the tab. If it was the active route, navigates to `fallbackHref` (default `/agents`, since agent terminal tabs are the only kind today). */ close: (href: string, fallbackHref?: string) => void; + /** Moves the tab `from` to the place of the tab `to`, and keeps the order. */ + move: (from: string, to: string) => void; } export function useDynamicTabs(): UseDynamicTabsResult { @@ -83,5 +86,9 @@ export function useDynamicTabs(): UseDynamicTabsResult { if (location === href) navigate(fallbackHref); } - return { tabs, open, close }; + function move(from: string, to: string) { + setTabs(moveTab(tabs, from, to)); + } + + return { tabs, open, close, move }; } diff --git a/frontend/packages/swarm-ui/test/tab-order.test.mjs b/frontend/packages/swarm-ui/test/tab-order.test.mjs new file mode 100644 index 000000000..f8d9333f7 --- /dev/null +++ b/frontend/packages/swarm-ui/test/tab-order.test.mjs @@ -0,0 +1,26 @@ +// The order of the header's dynamic tabs. Run by `npm test`. +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { moveTab } from "../src/shell/tab-order.ts"; + +const tabs = ["a", "b", "c", "d"].map((href) => ({ href })); +const order = (list) => list.map((t) => t.href).join(""); + +test("a tab moved right takes the place of the tab it lands on", () => { + assert.equal(order(moveTab(tabs, "a", "c")), "bcad"); +}); + +test("a tab moved left lands before the tab it is dropped on", () => { + assert.equal(order(moveTab(tabs, "d", "b")), "adbc"); +}); + +test("dropping a tab on itself, or naming a tab that is gone, changes nothing", () => { + assert.equal(moveTab(tabs, "b", "b"), tabs); + assert.equal(moveTab(tabs, "x", "b"), tabs); + assert.equal(moveTab(tabs, "b", "x"), tabs); +}); + +test("the list it was given is left as it was", () => { + moveTab(tabs, "a", "d"); + assert.equal(order(tabs), "abcd"); +});