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.

fix(canvas): end maximize mode on user drag/resize and before tidy/arrange

Ara Babigiancb247f2151c9
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index a5ba5ca39..e0618d971 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -834,6 +834,8 @@ import {
claudeLaunchCommand,
toggleCollapsed,
canToggleCollapse,
+ endMaximizeOnUserGeometry,
+ movedGestureEnds,
alignNodes,
arrangeByLineage,
arrangeGroupChildren,
@@ -4650,6 +4652,8 @@ export function Canvas() {
setNodes((ns) => (ns.some((n) => n.selected) ? ns.map((n) => ({ ...n, selected: false })) : ns))
}, [ephSelId, setNodes])
+ // Ids mid-resize (a `resizing: true` change seen, its end not yet): see movedGestureEnds.
+ const resizingIdsRef = useRef(new Set<string>())
const handleNodesChange: typeof onNodesChange = useCallback(
(changes) => {
// Ephemeral nodes (subagent / loop) live outside the managed state. Persist their drag
@@ -4709,9 +4713,12 @@ export function Canvas() {
? snapResizeChanges(managed, nodesRef.current, snapSettings.gridSize || GRID)
: managed
onNodesChange(snapped)
+ // A user drag/resize of a maximized node ends maximize mode where it now stands.
+ const ended = movedGestureEnds(managed, resizingIdsRef.current)
+ if (ended.size) setNodes((ns) => endMaximizeOnUserGeometry(ns, ended))
if (snapped.some((c) => c.type !== 'select')) markDirty()
},
- [onNodesChange, markDirty, ephParentPosition]
+ [onNodesChange, setNodes, markDirty, ephParentPosition]
)
// Resolve a node's agent id, with a tags fallback for not-yet-migrated legacy nodes and a
diff --git a/src/renderer/canvas/maximize-mode-wiring.test.ts b/src/renderer/canvas/maximize-mode-wiring.test.ts
new file mode 100644
index 000000000..e3f1c003d
--- /dev/null
+++ b/src/renderer/canvas/maximize-mode-wiring.test.ts
@@ -0,0 +1,23 @@
+// A user drag or resize of a maximized node must end maximize mode (endMaximizeOnUserGeometry,
+// unit-tested in state/workspace.maximize.test.ts). Canvas has no render harness, so the call
+// site is pinned here: handleNodesChange feeds the MANAGED changes (never ghost/ephemeral ones)
+// through movedGestureEnds with a gesture memory that outlives the batch (a resize's live changes
+// and its end arrive separately), and calls setNodes only when a gesture really ended — a bare
+// re-measure or a click on a grab band must not end maximize mode or cost a setNodes.
+import fs from 'node:fs'
+import path from 'node:path'
+import { describe, expect, it } from 'vitest'
+
+const SRC = fs.readFileSync(path.join(__dirname, 'Canvas.tsx'), 'utf8').replace(/\r\n/g, '\n')
+
+describe('maximize mode ends on a user gesture', () => {
+ it('handleNodesChange ends it only for gestures that moved the node', () => {
+ const start = SRC.indexOf('const handleNodesChange: typeof onNodesChange = useCallback(')
+ const body = SRC.slice(start, SRC.indexOf('\n )\n', start))
+ expect(body).toMatch(
+ /onNodesChange\(snapped\)[\s\S]*const ended = movedGestureEnds\(managed, resizingIdsRef\.current\)\s*if \(ended\.size\) setNodes\(\(ns\) => endMaximizeOnUserGeometry\(ns, ended\)\)/
+ )
+ expect(SRC).toMatch(/const resizingIdsRef = useRef\(new Set<string>\(\)\)/)
+ expect(body).toMatch(/\[onNodesChange, setNodes, markDirty, ephParentPosition\]/)
+ })
+})
diff --git a/src/renderer/state/workspace.maximize.test.ts b/src/renderer/state/workspace.maximize.test.ts
index 920c14750..cd46da163 100644
--- a/src/renderer/state/workspace.maximize.test.ts
+++ b/src/renderer/state/workspace.maximize.test.ts
@@ -1,5 +1,9 @@
import { describe, it, expect } from 'vitest'
import {
+ arrangeNodes,
+ endMaximizeOnUserGeometry,
+ movedGestureEnds,
+ tidyCanvas,
flowToNodeStates,
maximizeNodeToRect,
nodeStatesToFlow,
@@ -273,6 +277,43 @@ describe('placeNodeInRect (zone snap, issue #394 v1)', () => {
})
})
+describe('leaving maximize mode', () => {
+ it('a user drag-stop or a real resize-end drops premaxRect where the node stands', () => {
+ const maxed = maximizeNodeToRect([term('a', { x: 40, y: 60 }), term('b', { x: 900, y: 0 })], 'a', RECT)
+ const ended = (changes: Parameters<typeof movedGestureEnds>[0], resizing = new Set<string>()) =>
+ endMaximizeOnUserGeometry(maxed, movedGestureEnds(changes, resizing))
+ // Mid-gesture and re-measure changes carry no end flag: mode stays on.
+ expect(ended([{ type: 'position', id: 'a', dragging: true }])).toBe(maxed)
+ expect(ended([{ type: 'dimensions', id: 'a' }])).toBe(maxed)
+ // A drag end (React Flow sends it only when positions changed) ends maximize mode in place.
+ const dragged = ended([{ type: 'position', id: 'a', dragging: false }])[0]
+ expect(dragged.data.premaxRect).toBeUndefined()
+ expect(dragged.position).toEqual(maxed[0].position)
+ // A resize end ends it only after live resizing changes, which may arrive in earlier batches.
+ const resizing = new Set<string>()
+ expect(ended([{ type: 'dimensions', id: 'a', resizing: true }], resizing)).toBe(maxed)
+ expect(ended([{ type: 'dimensions', id: 'a', resizing: false }], resizing)[0].data.premaxRect).toBeUndefined()
+ expect(resizing.size).toBe(0)
+ })
+
+ it('a plain click on a grab band (resize end with no resize) keeps maximize mode and Restore', () => {
+ const maxed = maximizeNodeToRect([term('a', { x: 40, y: 60 })], 'a', RECT)
+ const ids = movedGestureEnds([{ type: 'dimensions', id: 'a', resizing: false }], new Set())
+ expect(ids.size).toBe(0)
+ expect(endMaximizeOnUserGeometry(maxed, ids)).toBe(maxed)
+ })
+
+ it('tidy and arrange restore maximized nodes before packing', () => {
+ const maxed = maximizeNodeToRect([term('a', { x: 40, y: 60 }), term('b', { x: 900, y: 0 })], 'a', RECT)
+ for (const next of [tidyCanvas(maxed), arrangeNodes(maxed, ['a', 'b'])]) {
+ const a = next.find((n) => n.id === 'a')!
+ expect(a.data.premaxRect).toBeUndefined()
+ expect(a.width).toBe(320)
+ expect(a.height).toBe(240)
+ }
+ })
+})
+
describe('NODE_MIN_SIZES clamp (programmatic resizes bypass the NodeResizer)', () => {
// A third zone of a zoomed-in viewport is narrower than a terminal's 260 floor.
const TINY = { x: 10, y: 20, width: 200, height: 100 }
diff --git a/src/renderer/state/workspace.ts b/src/renderer/state/workspace.ts
index 4c1ac1050..ee6de2b4c 100644
--- a/src/renderer/state/workspace.ts
+++ b/src/renderer/state/workspace.ts
@@ -1420,10 +1420,14 @@ export function arrangeNodes(
ids: string[],
opts?: { layout?: ArrangeLayout; cols?: number; gap?: number; origin?: { x: number; y: number } }
): CanvasNode[] {
- const byId = new Map(nodes.map((nd) => [nd.id, nd]))
- const members = [...new Set(ids)].flatMap((id) => byId.get(id) ?? [])
+ let byId = new Map(nodes.map((nd) => [nd.id, nd]))
+ let members = [...new Set(ids)].flatMap((id) => byId.get(id) ?? [])
// Only meaningful within one coordinate space (see commonParentId) — mixed containers → no-op.
if (members.length === 0 || new Set(members.map((m) => m.parentId ?? null)).size > 1) return nodes
+ const memberIds = new Set(members.map((m) => m.id))
+ nodes = restoreMaximizedWhere(nodes, (nd) => memberIds.has(nd.id))
+ byId = new Map(nodes.map((nd) => [nd.id, nd]))
+ members = [...memberIds].flatMap((id) => byId.get(id) ?? [])
const layout = opts?.layout ?? 'grid'
const gap = opts?.gap ?? 40
const origin = opts?.origin ?? {
@@ -1632,12 +1636,13 @@ export function arrangeByLineage(
opts?: { gap?: number; origin?: { x: number; y: number }; containerId?: string | null }
): CanvasNode[] {
const containerId = opts?.containerId ?? null
- const topLevel = nodes.filter((nd) => (nd.parentId ?? null) === containerId)
- if (topLevel.length < 2) return nodes
+ if (nodes.filter((nd) => (nd.parentId ?? null) === containerId).length < 2) return nodes
const { layers, loose } = lineageLayers(nodes, edges, containerId)
// No rope reached two different members: every node would land in the single `loose` band,
// which is a worse `Tidy canvas`, not a lineage view.
if (layers.length === 0) return nodes
+ nodes = restoreMaximizedWhere(nodes, (nd) => (nd.parentId ?? null) === containerId)
+ const topLevel = nodes.filter((nd) => (nd.parentId ?? null) === containerId)
const gap = opts?.gap ?? 40
const origin = opts?.origin ?? {
@@ -1727,8 +1732,10 @@ export function tidyCanvas(
edges: readonly LineageEdge[] = [],
opts?: { gap?: number }
): CanvasNode[] {
+ if (nodes.filter((nd) => !nd.parentId).length < 2) return nodes
+ // Every unit moves, and a frame is packed at the size its (maximized) child inflated it to.
+ nodes = restoreMaximizedWhere(nodes, () => true)
const units = nodes.filter((nd) => !nd.parentId).sort(byReadingPosition)
- if (units.length < 2) return nodes
const gap = opts?.gap ?? 40
const order = new Map(units.map((u, i) => [u.id, i]))
const unitOf = containerAncestors(nodes, null)
@@ -2194,6 +2201,53 @@ export function refitMaximizedNode(
return withNodeRect(nodes, node, rect, { premaxRect })
}
+/**
+ * Take every maximized node `pick` selects back to its remembered rect. A layout pass packs nodes
+ * by size: left maximized, a node is packed at full-viewport size, keeps a restore rect that later
+ * teleports it onto the new layout, and the panel-pin refit snaps it back to fullscreen.
+ */
+function restoreMaximizedWhere(nodes: CanvasNode[], pick: (n: CanvasNode) => boolean): CanvasNode[] {
+ return nodes.reduce(
+ (acc, n) => (n.data.premaxRect && pick(n) ? restoreMaximizedNode(acc, n.id) : acc),
+ nodes
+ )
+}
+
+/**
+ * The ids whose user gesture ENDED in `changes` having actually moved or resized the node: a drag
+ * end (`dragging: false`, which React Flow emits only when positions changed) or a resize end that
+ * followed at least one live `resizing: true` change for that id. React Flow's resizer emits
+ * `resizing: false` on every mouseup — a plain click on a grab band included — so the end alone is
+ * not evidence of a resize. `resizing` is the caller's gesture memory (ids mid-resize), carried
+ * across change batches and updated here. A re-measure carries neither flag.
+ */
+export function movedGestureEnds(
+ changes: readonly { type: string; id?: string; dragging?: boolean; resizing?: boolean }[],
+ resizing: Set<string>
+): Set<string> {
+ const ids = new Set<string>()
+ for (const c of changes) {
+ if (!c.id) continue
+ if (c.type === 'position' && c.dragging === false) ids.add(c.id)
+ else if (c.type === 'dimensions' && c.resizing === true) resizing.add(c.id)
+ else if (c.type === 'dimensions' && c.resizing === false && resizing.delete(c.id)) ids.add(c.id)
+ }
+ return ids
+}
+
+/**
+ * A user drag or resize ends maximize MODE for those nodes where they now stand: the node is no
+ * longer the viewport-sized window "Restore" assumes, so keeping `premaxRect` would leave a stale
+ * Restore in the header and let the panel-pin refit snap the user's placement back to fullscreen.
+ * `ids` comes from movedGestureEnds; programmatic placements never reach onNodesChange.
+ */
+export function endMaximizeOnUserGeometry(nodes: CanvasNode[], ids: ReadonlySet<string>): CanvasNode[] {
+ if (!nodes.some((n) => ids.has(n.id) && n.data.premaxRect)) return nodes
+ return nodes.map((n) =>
+ ids.has(n.id) && n.data.premaxRect ? { ...n, data: { ...n.data, premaxRect: undefined } } : n
+ )
+}
+
/**
* The toggle's second click: give the node back the rect `maximizeNodeToRect` remembered — the
* exact canvas spot it occupied, converted from root-space into wherever its parent chain sits