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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(canvas): snap a resized node onto the grid, not by it
Bas Miliusd7dd9197ef77
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsxindex b34dd2aee..92bfbccee 100644--- a/src/renderer/canvas/Canvas.tsx+++ b/src/renderer/canvas/Canvas.tsx@@ -433,6 +433,7 @@ import type { KanbanCreateChoice, KanbanSession } from '../components/kanban/Kanimport { assignNode, assignedTo, defaultKanban, labelsForCard, migrateProjectTags, resolveColumnRef, unassigned } from '../lib/kanban'import { registerWorkspaceDirty } from '../state/workspaceDirty'import { snapNodeToGrid } from '../lib/nodeSizing'+import { snapResizeChanges } from '../lib/resizeSnap'import { canClearDirty, canCommitCanvas, canCreateOnCanvas } from '../state/persistGuards'import { isHidden } from '../lib/ui-visibility'import { boardLogEvents } from '../lib/boardLogDiff'@@ -2945,8 +2946,14 @@ export function Canvas() {}return true})- onNodesChange(managed)- if (managed.some((c) => c.type !== 'select')) markDirty()+ // Re-snap what the resizer proposes before it is applied, so a resize tracks the grid+ // during the drag instead of correcting itself on release (see lib/resizeSnap.ts).+ const settings = useSettings.getState().settings+ const snapped = settings.snapToGrid+ ? snapResizeChanges(managed, nodesRef.current, settings.gridSize || GRID)+ : managed+ onNodesChange(snapped)+ if (snapped.some((c) => c.type !== 'select')) markDirty()},[onNodesChange, markDirty, ephParentPosition])diff --git a/src/renderer/lib/nodeSizing.test.ts b/src/renderer/lib/nodeSizing.test.tsindex 3324cdeb3..11230c28e 100644--- a/src/renderer/lib/nodeSizing.test.ts+++ b/src/renderer/lib/nodeSizing.test.ts@@ -51,6 +51,23 @@ describe('snapNodeToGrid', () => {expect(r.height).toBe(216)})+ // What the resize wiring needs: React Flow adds a grid-multiple DELTA to whatever width the+ // node started at, so an off-grid node can never reach a multiple by resizing. One snap has to+ // land it there from any start, or the correction only moves the problem.+ it('lands any off-grid box on grid multiples in one step', () => {+ for (let width = 263; width < 420; width += 7) {+ const r = snapNodeToGrid(20, 'terminal', { x: 13, y: 7, width, height: width - 90 })++ expect([r.x % 20, r.y % 20, r.width % 20, r.height % 20]).toEqual([0, 0, 0, 0])+ }+ })++ it('is idempotent, so the next resize starts from a clean base', () => {+ const once = snapNodeToGrid(20, 'terminal', { x: 13, y: 7, width: 331, height: 199 })++ expect(snapNodeToGrid(20, 'terminal', once)).toEqual(once)+ })+it('every kind has a positive min-size entry', () => {const kinds: NodeKind[] = ['terminal', 'sticky', 'group', 'editor', 'diff',diff --git a/src/renderer/lib/resizeSnap.test.ts b/src/renderer/lib/resizeSnap.test.tsnew file mode 100644index 000000000..74b7dde1b--- /dev/null+++ b/src/renderer/lib/resizeSnap.test.ts@@ -0,0 +1,101 @@+import { describe, expect, it } from 'vitest'+import type { NodeChange } from '@xyflow/react'+import type { CanvasNode } from '../state/workspace'+import { snapResizeChanges } from './resizeSnap'++const G = 20++/** A terminal node sitting off-grid, which is the only case the whole module exists for. */+function node(over: Partial<CanvasNode> = {}): CanvasNode {+ return {+ id: 'n1',+ type: 'terminal',+ position: { x: 13, y: 7 },+ data: {},+ measured: { width: 613, height: 371 },+ ...over+ } as CanvasNode+}++const resize = (width: number, height: number, resizing = true): NodeChange<CanvasNode> =>+ ({ id: 'n1', type: 'dimensions', resizing, dimensions: { width, height } }) as NodeChange<CanvasNode>++const dims = (changes: NodeChange<CanvasNode>[]): { width: number; height: number } | undefined =>+ changes.flatMap((c) => (c.type === 'dimensions' && c.dimensions ? [c.dimensions] : []))[0]++const pos = (changes: NodeChange<CanvasNode>[]): { x: number; y: number } | undefined =>+ changes.flatMap((c) => (c.type === 'position' && c.position ? [c.position] : []))[0]++describe('snapResizeChanges', () => {+ it('lands an in-flight resize on grid multiples, not on start + a multiple', () => {+ // What React Flow proposes: 613 + 20, off-grid like the 613 it started from. Snapped, the+ // node runs from x 20 to a right edge on 640, so 620 wide.+ const out = snapResizeChanges([resize(633, 391)], [node()], G)++ expect(dims(out)).toEqual({ width: 620, height: 400 })+ })++ it('does the same on the final change, so releasing does not undo the drag', () => {+ const out = snapResizeChanges([resize(633, 391, false)], [node()], G)++ expect(dims(out)).toEqual({ width: 620, height: 400 })+ })++ it('adds a position change when snapping moves the anchored edge', () => {+ // A right/bottom-handle drag sends no position change of its own.+ const out = snapResizeChanges([resize(633, 391)], [node()], G)++ expect(pos(out)).toEqual({ x: 20, y: 0 })+ })++ it('adds none when the node already sits on the grid', () => {+ const out = snapResizeChanges([resize(633, 391)], [node({ position: { x: 40, y: 60 } })], G)++ expect(pos(out)).toBeUndefined()+ })++ it('rewrites a left-handle drag in place rather than appending a second position', () => {+ const changes: NodeChange<CanvasNode>[] = [+ { id: 'n1', type: 'position', position: { x: -7, y: 7 } } as NodeChange<CanvasNode>,+ resize(633, 391)+ ]+ const out = snapResizeChanges(changes, [node()], G)++ expect(out.filter((c) => c.type === 'position')).toHaveLength(1)+ expect(pos(out)).toEqual({ x: 0, y: 0 })+ })++ it('leaves a MEASUREMENT change alone: only the resizer sets `resizing`', () => {+ // Forcing the ResizeObserver's reading onto the grid would fight the DOM on every node.+ const measured: NodeChange<CanvasNode>[] = [+ { id: 'n1', type: 'dimensions', dimensions: { width: 613, height: 371 } } as NodeChange<CanvasNode>+ ]++ expect(snapResizeChanges(measured, [node()], G)).toEqual(measured)+ })++ it('passes a batch with no resize through untouched', () => {+ const changes: NodeChange<CanvasNode>[] = [+ { id: 'n1', type: 'select', selected: true } as NodeChange<CanvasNode>+ ]++ expect(snapResizeChanges(changes, [node()], G)).toBe(changes)+ })++ it('keeps a collapsed node at its bar height', () => {+ const collapsed = node({+ data: { title: 'n', color: '#0a84ff', group: null, collapsed: true },+ measured: { width: 613, height: 34 }+ })+ const out = snapResizeChanges([resize(633, 34)], [collapsed], G)++ expect(dims(out)?.height).toBe(34)+ expect(dims(out)?.width).toBe(620)+ })++ it('leaves a change for a node it cannot find alone', () => {+ const changes = [resize(633, 391)]++ expect(snapResizeChanges(changes, [], G)).toEqual(changes)+ })+})diff --git a/src/renderer/lib/resizeSnap.ts b/src/renderer/lib/resizeSnap.tsnew file mode 100644index 000000000..2a8a59634--- /dev/null+++ b/src/renderer/lib/resizeSnap.ts@@ -0,0 +1,80 @@+import type { NodeChange } from '@xyflow/react'+import type { NodeKind } from '@shared/types'+import type { CanvasNode } from '../state/workspace'+import { snapNodeToGrid } from './nodeSizing'++/**+ * React Flow snaps the resize DELTA, never the resulting edges. From `getDimensionsAfterResize`+ * (@xyflow/system): `newWidth = startWidth + distX`, where both pointer reads are snapped so+ * `distX` is a grid multiple. Added to an off-grid start width, the result is off-grid forever:+ * the size the user aims for is unreachable however carefully they drag.+ *+ * So the resizer's changes are re-snapped BEFORE they are applied, which is what makes the node+ * track the grid during the drag rather than jump when it ends. `resizing` is the marker: only+ * the resizer sets it, while the ResizeObserver's measurement changes carry no such field and+ * must pass through untouched, or every node's measured size would be forced onto the grid.+ *+ * A right/bottom-handle drag sends no position change, so one is ADDED when snapping moves the+ * anchored edge. Without it the node keeps an off-grid x while its width is measured from a+ * snapped one, and the right edge lands between grid lines again.+ */+export function snapResizeChanges(+ changes: NodeChange<CanvasNode>[],+ nodes: CanvasNode[],+ grid: number+): NodeChange<CanvasNode>[] {+ const resizing = new Set(+ changes.flatMap((c) => (c.type === 'dimensions' && typeof c.resizing === 'boolean' ? [c.id] : []))+ )+ if (!resizing.size || grid <= 0) return changes++ const boxes = new Map<string, { x: number; y: number; width: number; height: number }>()+ for (const id of resizing) {+ const node = nodes.find((n) => n.id === id)+ if (!node) continue+ const moved = changes.find((c) => c.type === 'position' && c.id === id && c.position)+ const sized = changes.find((c) => c.type === 'dimensions' && c.id === id && c.dimensions)+ const position = (moved?.type === 'position' && moved.position) || node.position+ const dimensions = sized?.type === 'dimensions' ? sized.dimensions : undefined+ const width = dimensions?.width ?? node.measured?.width ?? (node.width as number) ?? 0+ const height = dimensions?.height ?? node.measured?.height ?? (node.height as number) ?? 0+ const snapped = snapNodeToGrid(grid, (node.type ?? 'terminal') as NodeKind, {+ x: position.x,+ y: position.y,+ width,+ height+ })+ // A collapsed node keeps its collapsed bar height, exactly as align-to-grid leaves it.+ // `+ 0` normalizes the -0 that rounding a small negative coordinate produces, which would+ // otherwise ride into node positions on every resize near the origin.+ boxes.set(id, {+ x: snapped.x + 0,+ y: snapped.y + 0,+ width: snapped.width,+ height: node.data?.collapsed ? height : snapped.height+ })+ }++ const positioned = new Set(+ changes.flatMap((c) => (c.type === 'position' && boxes.has(c.id) ? [c.id] : []))+ )+ const applied = changes.map((change) => {+ const box = 'id' in change ? boxes.get(change.id) : undefined+ if (!box) return change+ if (change.type === 'position' && change.position) {+ return { ...change, position: { x: box.x, y: box.y } }+ }+ if (change.type === 'dimensions' && change.dimensions) {+ return { ...change, dimensions: { width: box.width, height: box.height } }+ }+ return change+ })++ for (const [id, box] of boxes) {+ if (positioned.has(id)) continue+ const node = nodes.find((n) => n.id === id)+ if (!node || (node.position.x === box.x && node.position.y === box.y)) continue+ applied.push({ id, type: 'position', position: { x: box.x, y: box.y } })+ }+ return applied+}