Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(canvas): pure arrangeNodes + alignNodes layout helpers

enescb0b8ffda9ec
diff --git a/src/renderer/state/workspace.layout.test.ts b/src/renderer/state/workspace.layout.test.ts
new file mode 100644
index 000000000..cd0ad3546
--- /dev/null
+++ b/src/renderer/state/workspace.layout.test.ts
@@ -0,0 +1,72 @@
+import { describe, it, expect } from 'vitest'
+import { arrangeNodes, alignNodes, type CanvasNode } from './workspace'
+
+// Minimal node stub: only the fields the layout fns read (id, position, width/height, parentId).
+const n = (id: string, x: number, y: number, w = 100, h = 50): CanvasNode =>
+ ({ id, type: 'terminal', position: { x, y }, width: w, height: h, data: { title: id, color: '#fff', group: null } }) as CanvasNode
+
+describe('arrangeNodes', () => {
+ it('lays out a row left-to-right from the bounding-box origin with the gap', () => {
+ const out = arrangeNodes([n('a', 50, 90), n('b', 10, 200)], ['a', 'b'], { layout: 'row', gap: 20 })
+ const a = out.find((x) => x.id === 'a')!
+ const b = out.find((x) => x.id === 'b')!
+ // origin = bounding-box top-left of current positions = (10, 90)
+ expect(a.position).toEqual({ x: 10, y: 90 })
+ expect(b.position).toEqual({ x: 10 + 100 + 20, y: 90 })
+ })
+
+ it('lays out a column top-to-bottom', () => {
+ const out = arrangeNodes([n('a', 0, 0), n('b', 300, 300)], ['a', 'b'], { layout: 'column', gap: 10 })
+ expect(out.find((x) => x.id === 'a')!.position).toEqual({ x: 0, y: 0 })
+ expect(out.find((x) => x.id === 'b')!.position).toEqual({ x: 0, y: 50 + 10 })
+ })
+
+ it('grid wraps at cols and rows advance by the tallest node in the row', () => {
+ const out = arrangeNodes(
+ [n('a', 0, 0, 100, 50), n('b', 0, 0, 100, 80), n('c', 0, 0, 100, 50)],
+ ['a', 'b', 'c'],
+ { layout: 'grid', cols: 2, gap: 10, origin: { x: 0, y: 0 } }
+ )
+ expect(out.find((x) => x.id === 'a')!.position).toEqual({ x: 0, y: 0 })
+ expect(out.find((x) => x.id === 'b')!.position).toEqual({ x: 110, y: 0 })
+ // row 2 starts below the tallest of row 1 (80) + gap
+ expect(out.find((x) => x.id === 'c')!.position).toEqual({ x: 0, y: 90 })
+ })
+
+ it('skips unknown ids and parented nodes; empty selection is a no-op', () => {
+ const child = { ...n('kid', 5, 5), parentId: 'g1' } as CanvasNode
+ const nodes = [n('a', 7, 7), child]
+ const out = arrangeNodes(nodes, ['a', 'kid', 'ghost'], { layout: 'row', origin: { x: 0, y: 0 } })
+ expect(out.find((x) => x.id === 'kid')!.position).toEqual({ x: 5, y: 5 }) // untouched
+ expect(out.find((x) => x.id === 'a')!.position).toEqual({ x: 0, y: 0 })
+ expect(arrangeNodes(nodes, ['ghost'])).toBe(nodes) // nothing resolvable → same array
+ })
+})
+
+describe('alignNodes', () => {
+ const pair = () => [n('a', 10, 20, 100, 50), n('b', 200, 300, 60, 80)]
+ it('left aligns x to the min x', () => {
+ const out = alignNodes(pair(), ['a', 'b'], 'left')
+ expect(out.map((x) => x.position.x)).toEqual([10, 10])
+ })
+ it('right aligns right edges to the max right edge', () => {
+ const out = alignNodes(pair(), ['a', 'b'], 'right')
+ // max right = 200+60=260 → a.x=260-100=160, b.x=200
+ expect(out.find((x) => x.id === 'a')!.position.x).toBe(160)
+ expect(out.find((x) => x.id === 'b')!.position.x).toBe(200)
+ })
+ it('vcenter aligns vertical centers; hcenter aligns horizontal centers', () => {
+ const v = alignNodes(pair(), ['a', 'b'], 'vcenter')
+ // bbox y: 20..380 → center 200 → a.y=200-25=175, b.y=200-40=160
+ expect(v.find((x) => x.id === 'a')!.position.y).toBe(175)
+ expect(v.find((x) => x.id === 'b')!.position.y).toBe(160)
+ const h = alignNodes(pair(), ['a', 'b'], 'hcenter')
+ // bbox x: 10..260 → center 135 → a.x=85, b.x=105
+ expect(h.find((x) => x.id === 'a')!.position.x).toBe(85)
+ expect(h.find((x) => x.id === 'b')!.position.x).toBe(105)
+ })
+ it('unknown ids only → same array', () => {
+ const nodes = pair()
+ expect(alignNodes(nodes, ['ghost'], 'left')).toBe(nodes)
+ })
+})
diff --git a/src/renderer/state/workspace.ts b/src/renderer/state/workspace.ts
index 1a78507d7..6b1b7a3ca 100644
--- a/src/renderer/state/workspace.ts
+++ b/src/renderer/state/workspace.ts
@@ -534,6 +534,86 @@ const GROUP_HEADER = 34
const nodeW = (n: CanvasNode) => n.measured?.width ?? (n.width as number) ?? 0
const nodeH = (n: CanvasNode) => n.measured?.height ?? (n.height as number) ?? 0
+export type ArrangeLayout = 'grid' | 'row' | 'column'
+
+/**
+ * Repositions the given top-level ids into a non-overlapping layout starting at `origin`
+ * (default: the bounding-box top-left of their current positions). 'row' packs left-to-right,
+ * 'column' top-to-bottom, 'grid' wraps at `cols` (default ~square) with each row advancing by
+ * its tallest member. Unknown ids and parented (grouped) nodes are skipped; returns the input
+ * array unchanged when nothing resolves. Pure and deterministic.
+ */
+export function arrangeNodes(
+ nodes: CanvasNode[],
+ ids: string[],
+ opts?: { layout?: ArrangeLayout; cols?: number; gap?: number; origin?: { x: number; y: number } }
+): CanvasNode[] {
+ const set = new Set(ids)
+ const members = nodes.filter((nd) => set.has(nd.id) && !nd.parentId)
+ if (members.length === 0) return nodes
+ const layout = opts?.layout ?? 'grid'
+ const gap = opts?.gap ?? 40
+ const origin = opts?.origin ?? {
+ x: Math.min(...members.map((m) => m.position.x)),
+ y: Math.min(...members.map((m) => m.position.y))
+ }
+ const cols =
+ layout === 'row' ? members.length : layout === 'column' ? 1 : Math.max(1, opts?.cols ?? Math.ceil(Math.sqrt(members.length)))
+
+ const pos = new Map<string, { x: number; y: number }>()
+ let x = origin.x
+ let y = origin.y
+ let rowH = 0
+ members.forEach((m, i) => {
+ if (i > 0 && i % cols === 0) {
+ x = origin.x
+ y += rowH + gap
+ rowH = 0
+ }
+ pos.set(m.id, { x, y })
+ x += nodeW(m) + gap
+ rowH = Math.max(rowH, nodeH(m))
+ })
+ return nodes.map((nd) => (pos.has(nd.id) ? { ...nd, position: pos.get(nd.id)! } : nd))
+}
+
+export type AlignEdge = 'left' | 'right' | 'top' | 'bottom' | 'hcenter' | 'vcenter'
+
+/**
+ * Snaps the given top-level ids to a shared edge/center computed from their joint bounding box.
+ * left/right/hcenter move x; top/bottom/vcenter move y. Unknown/parented ids are skipped;
+ * returns the input array unchanged when nothing resolves. Pure.
+ */
+export function alignNodes(nodes: CanvasNode[], ids: string[], edge: AlignEdge): CanvasNode[] {
+ const set = new Set(ids)
+ const members = nodes.filter((nd) => set.has(nd.id) && !nd.parentId)
+ if (members.length === 0) return nodes
+ const minX = Math.min(...members.map((m) => m.position.x))
+ const maxR = Math.max(...members.map((m) => m.position.x + nodeW(m)))
+ const minY = Math.min(...members.map((m) => m.position.y))
+ const maxB = Math.max(...members.map((m) => m.position.y + nodeH(m)))
+ const cx = (minX + maxR) / 2
+ const cy = (minY + maxB) / 2
+ const move = (m: CanvasNode): { x: number; y: number } => {
+ switch (edge) {
+ case 'left':
+ return { x: minX, y: m.position.y }
+ case 'right':
+ return { x: maxR - nodeW(m), y: m.position.y }
+ case 'hcenter':
+ return { x: cx - nodeW(m) / 2, y: m.position.y }
+ case 'top':
+ return { x: m.position.x, y: minY }
+ case 'bottom':
+ return { x: m.position.x, y: maxB - nodeH(m) }
+ case 'vcenter':
+ return { x: m.position.x, y: cy - nodeH(m) / 2 }
+ }
+ }
+ const set2 = new Set(members.map((m) => m.id))
+ return nodes.map((nd) => (set2.has(nd.id) ? { ...nd, position: move(nd) } : nd))
+}
+
/**
* Wraps the given top-level node ids in a new group frame: creates the group sized to
* enclose them and reparents the children (positions become relative to the group).