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.

test(canvas): exercise maximized-only focus policy through shared behavior

review3486f485383e
diff --git a/CLAUDE.md b/CLAUDE.md
index fab356a1f..df51706ee 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -3818,7 +3818,9 @@ the Settings section and ShortcutsPanel start disagreeing about what a chord mea
**ONE exception, and it is not a walk-back of that rule (issue #743): a MAXIMIZED node**
(`isMaximized` — `data.premaxRect`, the flag `maximizeNodeToRect` writes and
`restoreMaximizedNode` clears) is framed against the same rectangle `maximizeTargetRect` placed
- it in, by passing `measureMaximizeInsets(box)` to `viewportForRect`.
+ it in, through `viewportForNodeFocus`, which passes `measureMaximizeInsets(box)` to
+ `viewportForRect` only for maximized nodes. `nodeFocus.policy.test.ts` exercises this shared
+ Canvas decision for ordinary, maximized and restored nodes in both zoom modes.
Maximize also measures `.controls-cluster` and `.dock` (#711): their screen rectangles reserve
top/bottom space with an 8px gap, consuming the existing 24px margin first. Chrome outside the
horizontally usable area contributes nothing. Menus and hover peeks never reserve a band. Both
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 0a513fe33..254700701 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -625,7 +625,9 @@ feature from the one they asked for.
Maximize placement and refocusing must use the same measured usable rectangle
(`measureMaximizeInsets`): pinned side panels plus persistent top controls and bottom dock.
Do not hardcode chrome heights or add the outer margin twice; transient menus must not resize
-terminals. Ordinary focus and zone snap keep their own policies.
+terminals. Ordinary focus and zone snap keep their own policies. Test the maximized-only
+focus decision through `viewportForNodeFocus`, the same helper Canvas calls, rather than
+passing preselected insets straight to the geometry function.
**Usage readouts distinguish failed reads from empty data.** For Claude, show the failure when
`status` is `error` and limits are empty, including beside other providers; preserve last-known
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 38539c8e5..f09e190d2 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -302,14 +302,13 @@ import {
} from '../lib/projectOpen'
import {
absolutePosition,
- isMaximized,
isMeasured,
nodeFitRect,
- viewportForRect,
+ viewportForNodeFocus,
type FocusableNode
} from '../lib/nodeFocus'
import { NODE_MAXIMIZE_MARGIN_PX, maximizeTargetRect } from '../lib/nodeMaximize'
-import { NO_INSETS, measurePinnedInsets, type ScreenInsets } from '../lib/pinnedInsets'
+import { measurePinnedInsets, type ScreenInsets } from '../lib/pinnedInsets'
import { measureMaximizeInsets, MAXIMIZE_CHROME_SELECTOR } from '../lib/maximizeInsets'
import { ZONE_GUTTER_PX, ZONES, zoneTargetRect, type ZoneId } from '../lib/nodeZones'
import {
@@ -7306,11 +7305,7 @@ export function Canvas() {
// `focusZoomToNode` off: keep the zoom the user settled on and only pan — the node still
// lands in the middle, only the rescale is dropped.
const keepZoom = useSettings.getState().settings.focusZoomToNode ? undefined : getZoom()
- // A MAXIMIZED node is framed against the rectangle its own placement used (issue #743);
- // everything else is centred in the whole pane, as it always was. `measureMaximizeInsets`
- // reads the DOM, so it is asked only for the node that can use the answer.
- const insets = isMaximized(node) ? measureMaximizeInsets(box) : NO_INSETS
- const viewport = viewportForRect(rect, box.width, box.height, keepZoom, insets)
+ const viewport = viewportForNodeFocus(node, rect, box, keepZoom)
if (viewport) void setViewport(viewport, { duration: 300 })
},
[setViewport, getInternalNode, getZoom]
diff --git a/src/renderer/canvas/canvas-wiring.test.tsx b/src/renderer/canvas/canvas-wiring.test.tsx
index f6cdff45c..a8c80face 100644
--- a/src/renderer/canvas/canvas-wiring.test.tsx
+++ b/src/renderer/canvas/canvas-wiring.test.tsx
@@ -182,19 +182,6 @@ describe('breadcrumb wiring the CLAUDE.md bullet calls load-bearing', () => {
expect(frame).toContain('setViewport(viewport, { duration: 300 })')
})
- it('centres the node in the pane and never solves chrome around it', () => {
- // Framing a single focused node against the chrome-free rectangle was reported wrong twice
- // ("too far right", "not in the middle"): the sessions sidebar is a 300px overlay and it is
- // open exactly when this is used. The free-rect solve stays in fitAll, which fits every node.
- const frame = CANVAS_SRC.slice(
- CANVAS_SRC.indexOf('const frameNode = useCallback'),
- CANVAS_SRC.indexOf('const goToNode = useCallback')
- )
- expect(frame).toContain('viewportForRect(rect, box.width, box.height, keepZoom, insets)')
- expect(frame).not.toContain('solveFitFrame')
- expect(frame).toContain('settings.focusZoomToNode ? undefined : getZoom()')
- })
-
it('the resume card slot is spent only on a card that can render, and only when opted in', () => {
// Gated on settings.showResumeCard (default off) FIRST — a disabled card must not spend the
// one-shot slot — then once per app run, only with a live stop, and never under the opaque
diff --git a/src/renderer/lib/nodeFocus.policy.test.ts b/src/renderer/lib/nodeFocus.policy.test.ts
new file mode 100644
index 000000000..0c4690dc1
--- /dev/null
+++ b/src/renderer/lib/nodeFocus.policy.test.ts
@@ -0,0 +1,67 @@
+// @vitest-environment jsdom
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import { maximizeNodeToRect, restoreMaximizedNode, type CanvasNode } from '../state/workspace'
+import { measureMaximizeInsets } from './maximizeInsets'
+import { maximizeTargetRect } from './nodeMaximize'
+import { nodeFitRect, viewportForNodeFocus } from './nodeFocus'
+
+const box = { left: 100, right: 1300, top: 36, bottom: 836, width: 1200, height: 800 }
+function addChrome() {
+ for (const [className, left, top, right, bottom] of [
+ ['sessions-sidebar--pinned', 114, 90, 422, 780],
+ ['controls-cluster', 950, 50, 1286, 84],
+ ['dock', 500, 770, 900, 822]
+ ] as const) {
+ const el = document.createElement('div')
+ el.className = className
+ el.getBoundingClientRect = () =>
+ ({ left, top, right, bottom, width: right - left, height: bottom - top }) as DOMRect
+ document.body.append(el)
+ }
+}
+const ordinary = (): CanvasNode => ({
+ id: 'term', type: 'terminal', position: { x: 40, y: 60 }, width: 660, height: 400,
+ data: { title: 'Terminal', color: '#fff', group: null }
+}) as CanvasNode
+
+afterEach(() => {
+ vi.restoreAllMocks()
+ document.body.replaceChildren()
+})
+
+describe('Canvas focus policy (#743, #711)', () => {
+ it.each([undefined, 0.7345, 2])('reserves chrome only while maximized (keepZoom=%s)', (zoom) => {
+ addChrome()
+ const node = ordinary()
+ const originalRect = nodeFitRect(node, [node])!
+ const camera = { x: -123, y: 87, zoom: zoom ?? 1 }
+ const target = maximizeTargetRect(camera, box.width, box.height, 24, measureMaximizeInsets(box))!
+ const maximized = maximizeNodeToRect([node], node.id, target)
+ const restored = restoreMaximizedNode(maximized, node.id)[0]
+ const readChrome = vi.spyOn(document, 'querySelectorAll')
+
+ // Normal and restored nodes centre in the whole pane, regardless of visible chrome.
+ for (const candidate of [node, restored]) {
+ const rect = nodeFitRect(candidate, [candidate])!
+ expect(rect).toEqual(originalRect)
+ const focus = viewportForNodeFocus(candidate, rect, box, zoom)!
+ expect((rect.x + rect.width / 2) * focus.zoom + focus.x).toBeCloseTo(600)
+ expect((rect.y + rect.height / 2) * focus.zoom + focus.y).toBeCloseTo(400)
+ if (zoom !== undefined) expect(focus.zoom).toBe(zoom)
+ }
+ expect(readChrome).not.toHaveBeenCalled()
+
+ const rect = nodeFitRect(maximized[0], maximized)!
+ const focus = viewportForNodeFocus(maximized[0], rect, box, zoom)!
+ // Measured reservations: left=322, top=32, bottom=50. Test actual screen placement.
+ expect((rect.x + rect.width / 2) * focus.zoom + focus.x).toBeCloseTo(761)
+ expect((rect.y + rect.height / 2) * focus.zoom + focus.y).toBeCloseTo(391)
+ expect(rect.y * focus.zoom + focus.y + box.top).toBeGreaterThanOrEqual(92 - 1e-8)
+ expect((rect.y + rect.height) * focus.zoom + focus.y + box.top).toBeLessThanOrEqual(762 + 1e-8)
+ if (zoom !== undefined) {
+ expect(focus.zoom).toBe(zoom)
+ expect(focus.x).toBeCloseTo(camera.x)
+ expect(focus.y).toBeCloseTo(camera.y)
+ }
+ })
+})
diff --git a/src/renderer/lib/nodeFocus.ts b/src/renderer/lib/nodeFocus.ts
index 980700875..09624d6c2 100644
--- a/src/renderer/lib/nodeFocus.ts
+++ b/src/renderer/lib/nodeFocus.ts
@@ -1,6 +1,7 @@
import { getViewportForBounds, type Rect, type Viewport } from '@xyflow/system'
-import { NO_INSETS, type ScreenInsets } from './pinnedInsets'
+import { NO_INSETS, type RectLike, type ScreenInsets } from './pinnedInsets'
+import { measureMaximizeInsets } from './maximizeInsets'
/**
* "Zoom to this node" geometry, computed without React Flow's `fitView`.
@@ -182,3 +183,15 @@ export function isMeasured(
): boolean {
return !!(numeric(node?.measured?.width) && numeric(node?.measured?.height))
}
+
+/** Shared Canvas focus policy: only maximized nodes use their placement's chrome reservations.
+ * Ordinary/restored nodes stay centred in the whole pane, without measuring overlay geometry. */
+export function viewportForNodeFocus(
+ node: { data?: { premaxRect?: unknown } },
+ rect: Rect,
+ box: RectLike & { width: number; height: number },
+ zoom?: number
+): Viewport | null {
+ const insets = isMaximized(node) ? measureMaximizeInsets(box) : NO_INSETS
+ return viewportForRect(rect, box.width, box.height, zoom, insets)
+}