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): cover the header maximize button and gate the re-fit

Erik Zwep1489805ad484
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 9ed5b374b..2ead64318 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -249,7 +249,7 @@ import {
type FocusableNode
} from '../lib/nodeFocus'
import { NODE_MAXIMIZE_MARGIN_PX, maximizeTargetRect } from '../lib/nodeMaximize'
-import { measurePinnedInsets } from '../lib/pinnedInsets'
+import { measurePinnedInsets, type ScreenInsets } from '../lib/pinnedInsets'
import { ZONE_GUTTER_PX, ZONES, zoneTargetRect, type ZoneId } from '../lib/nodeZones'
import {
recordBreadcrumb,
@@ -6436,24 +6436,28 @@ export function Canvas() {
* Maximize is a MODE, not a one-shot placement — a zone snap is the one-shot kind and stays
* where it was put. While maximize is on the node claims the whole usable canvas, so pinning a
* panel (the node would slide under it), unpinning one (the node would sit short of the space it
- * just gained), or a panel simply changing width (`uiScale`, the drawer's wide variant) has to
- * move it.
+ * just gained), or a panel changing width (`uiScale`, the drawer's wide variant) has to move it.
*
- * A ResizeObserver rather than the pin flags alone: the explorer drawer animates in over 160ms
- * (`nt-drawer-in`), so measuring one frame after the state change catches it mid-slide. The
- * observer fires again as it settles. `refitMaximizedNode` is idempotent, so the extra ticks
- * cost nothing and the workspace is only marked dirty when a node actually moved.
+ * Gated on the INSETS changing, not on the flags: `explorerOpen` is also true for the unpinned
+ * overlay explorer, and without the gate opening that overlay would recompute the rect from the
+ * CURRENT viewport and teleport a maximized node into view — panning and window resizes
+ * deliberately do not re-fit, and an unpinned panel must not either.
*/
+ const lastInsetsRef = useRef<ScreenInsets | null>(null)
const fitMaximizedToUsableArea = useCallback(() => {
- if (!nodesRef.current.some((n) => n.data.premaxRect)) return
const wrap = flowWrapRef.current?.getBoundingClientRect()
if (!wrap) return
+ const insets = measurePinnedInsets(wrap)
+ const prev = lastInsetsRef.current
+ lastInsetsRef.current = insets
+ if (prev && prev.left === insets.left && prev.right === insets.right) return
+ if (!nodesRef.current.some((n) => n.data.premaxRect)) return
const rect = maximizeTargetRect(
getViewport(),
wrap.width,
wrap.height,
NODE_MAXIMIZE_MARGIN_PX,
- measurePinnedInsets(wrap)
+ insets
)
if (!rect) return
const current = nodesRef.current
@@ -6462,20 +6466,41 @@ export function Canvas() {
current
)
if (fitted === current) return
- setNodes((ns) => ns.reduce((acc, n) => (n.data.premaxRect ? refitMaximizedNode(acc, n.id, rect) : acc), ns))
+ setNodes((ns) =>
+ ns.reduce((acc, n) => (n.data.premaxRect ? refitMaximizedNode(acc, n.id, rect) : acc), ns)
+ )
markDirty()
}, [getViewport, setNodes, markDirty])
useEffect(() => {
fitMaximizedToUsableArea()
- if (typeof ResizeObserver === 'undefined') return
- const observer = new ResizeObserver(() => fitMaximizedToUsableArea())
- for (const el of document.querySelectorAll('.sessions-sidebar--pinned, .drawer--pinned')) {
- observer.observe(el)
+ const panels = Array.from(
+ document.querySelectorAll('.sessions-sidebar--pinned, .drawer--pinned')
+ )
+ // A ResizeObserver covers width changes (`uiScale`, the drawer's wide variant) but NOT the
+ // drawer's entry animation, which is opacity + transform — a transform never fires it, so the
+ // panel is measured ~10px off its settled position. `animationend` closes exactly that gap.
+ const observer =
+ typeof ResizeObserver === 'undefined'
+ ? null
+ : new ResizeObserver(() => fitMaximizedToUsableArea())
+ const onSettled = (): void => fitMaximizedToUsableArea()
+ for (const el of panels) {
+ observer?.observe(el)
+ // `animationcancel` too: an animation cut short (the panel re-toggled mid-slide) never fires
+ // `animationend`, and would leave the node measured against the halfway position.
+ el.addEventListener('animationend', onSettled)
+ el.addEventListener('animationcancel', onSettled)
+ }
+ return () => {
+ observer?.disconnect()
+ for (const el of panels) {
+ el.removeEventListener('animationend', onSettled)
+ el.removeEventListener('animationcancel', onSettled)
+ }
}
- return () => observer.disconnect()
- // The pin/open flags mount and unmount the panels, so they decide WHAT to observe; the
- // observer then covers every size change of whatever is docked.
+ // The pin/open flags mount and unmount the panels, so they decide WHAT to observe; the gate
+ // inside the callback decides whether an observation is worth acting on.
}, [sessionsPinned, sessionsDismissed, explorerOpen, explorer.pinned, fitMaximizedToUsableArea])
/**
diff --git a/src/renderer/nodes/MaximizeButton.tsx b/src/renderer/nodes/MaximizeButton.tsx
index 8aee3d6a3..8cd52dda7 100644
--- a/src/renderer/nodes/MaximizeButton.tsx
+++ b/src/renderer/nodes/MaximizeButton.tsx
@@ -11,7 +11,8 @@ import { IconMaximize, IconRestoreSize } from '../components/icons'
import { commandTooltip } from '../lib/keybindingOverrides'
import { markWorkspaceDirty } from '../state/workspaceDirty'
import { maximizeNodeToRect, restoreMaximizedNode, type CanvasNode } from '../state/workspace'
-import { maximizeTargetRect } from '../lib/nodeMaximize'
+import { NODE_MAXIMIZE_MARGIN_PX, maximizeTargetRect } from '../lib/nodeMaximize'
+import { measurePinnedInsets } from '../lib/pinnedInsets'
export function MaximizeButton({ id, maximized }: { id: string; maximized: boolean }) {
const { setNodes, getViewport } = useReactFlow()
@@ -21,8 +22,17 @@ export function MaximizeButton({ id, maximized }: { id: string; maximized: boole
setNodes((ns) => {
const flow = ns as CanvasNode[]
if (maximized) return restoreMaximizedNode(flow, id)
- const { width, height } = store.getState()
- const rect = maximizeTargetRect(getViewport(), width, height)
+ const { width, height, domNode } = store.getState()
+ // Same usable area the canvas commands compute: a node maximized from its header must clear
+ // the pinned side panels too — this button is the primary maximize path.
+ const wrap = domNode?.getBoundingClientRect()
+ const rect = maximizeTargetRect(
+ getViewport(),
+ width,
+ height,
+ NODE_MAXIMIZE_MARGIN_PX,
+ wrap ? measurePinnedInsets(wrap) : undefined
+ )
return rect ? maximizeNodeToRect(flow, id, rect) : ns
})
// Direct setNodes bypasses handleNodesChange, so the project must be marked dirty
diff --git a/src/renderer/state/workspace.maximize.test.ts b/src/renderer/state/workspace.maximize.test.ts
index 1678ca366..239a7b897 100644
--- a/src/renderer/state/workspace.maximize.test.ts
+++ b/src/renderer/state/workspace.maximize.test.ts
@@ -209,6 +209,22 @@ describe('refitMaximizedNode', () => {
expect(refitMaximizedNode(maxed, 'a', NARROWER)).not.toBe(maxed)
})
+ it('treats a sub-pixel difference as already fitted', () => {
+ // React Flow repopulates `measured` from the DOM (device-pixel-snapped), and a grouped node's
+ // rect round-trips through rootPosition in floats — at fractional zoom neither lands exactly
+ // on the computed rect. Without the tolerance every panel toggle would rewrite the node.
+ const maxed = maximizeNodeToRect([term('a', { x: 40, y: 60 })], 'a', MAXED)
+ const drift = {
+ x: MAXED.x + 0.2,
+ y: MAXED.y - 0.3,
+ width: MAXED.width + 0.4,
+ height: MAXED.height - 0.1
+ }
+ expect(refitMaximizedNode(maxed, 'a', drift)).toBe(maxed)
+ // A real move still lands.
+ expect(refitMaximizedNode(maxed, 'a', { ...MAXED, x: MAXED.x + 8 })).not.toBe(maxed)
+ })
+
it('leaves a node that is not maximized alone', () => {
// A hand-placed or zone-snapped node is a placement the user owns.
const plain = [term('a', { x: 40, y: 60 })]
diff --git a/src/renderer/state/workspace.ts b/src/renderer/state/workspace.ts
index 20f047c13..85192469b 100644
--- a/src/renderer/state/workspace.ts
+++ b/src/renderer/state/workspace.ts
@@ -1334,12 +1334,14 @@ function withNodeRect(
return fitAncestorChain(next, node.parentId)
}
-/**
- * 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
- * now — and re-fit the ancestor frames back down around it. No-op when the node is missing or
- * not maximized.
- */
+/** Sub-pixel tolerance for "the node is already there". Exact equality is not available: a grouped
+ * node's rect round-trips through `rootPosition` in floats, and `nodeW` prefers React Flow's
+ * `measured` width, which it repopulates from the DOM device-pixel-snapped — at fractional zoom
+ * that never equals `innerW / zoom` exactly. Without the tolerance every re-measure would rewrite
+ * the node and bump the workspace rev, which on a shared SSH/git project is a commit per toggle. */
+const SAME_PX = 0.5
+const samePx = (a: number, b: number): boolean => Math.abs(a - b) <= SAME_PX
+
/**
* Re-fit an ALREADY maximized node to a new frame, keeping its restore rect.
*
@@ -1361,16 +1363,22 @@ export function refitMaximizedNode(
// and lets the caller decide by identity whether anything actually moved.
const root = rootPosition(node, nodes)
if (
- root.x === rect.x &&
- root.y === rect.y &&
- nodeW(node) === rect.width &&
- nodeH(node) === rect.height
+ samePx(root.x, rect.x) &&
+ samePx(root.y, rect.y) &&
+ samePx(nodeW(node) || (node.style?.width as number) || 0, rect.width) &&
+ samePx(nodeH(node) || (node.style?.height as number) || 0, rect.height)
) {
return nodes
}
return withNodeRect(nodes, node, rect, { premaxRect })
}
+/**
+ * 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
+ * now — and re-fit the ancestor frames back down around it. No-op when the node is missing or
+ * not maximized.
+ */
export function restoreMaximizedNode(nodes: CanvasNode[], nodeId: string): CanvasNode[] {
const node = nodes.find((n) => n.id === nodeId)
const prev = node?.data.premaxRect