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): snap a resize in root space and carry the frame children

Bas Miliusaa8d19d0f4e0
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 92bfbccee..2b4c090a8 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -2948,9 +2948,9 @@ export function Canvas() {
})
// 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)
+ const snapSettings = useSettings.getState().settings
+ const snapped = snapSettings.snapToGrid
+ ? snapResizeChanges(managed, nodesRef.current, snapSettings.gridSize || GRID)
: managed
onNodesChange(snapped)
if (snapped.some((c) => c.type !== 'select')) markDirty()
diff --git a/src/renderer/lib/resizeSnap.test.ts b/src/renderer/lib/resizeSnap.test.ts
index 74b7dde1b..784ba0b7a 100644
--- a/src/renderer/lib/resizeSnap.test.ts
+++ b/src/renderer/lib/resizeSnap.test.ts
@@ -99,3 +99,88 @@ describe('snapResizeChanges', () => {
expect(snapResizeChanges(changes, [], G)).toEqual(changes)
})
})
+
+/**
+ * A group frame plus one child, the case React Flow compensates for itself: on a top/left drag
+ * `XYResizer` shifts every child by the origin delta IT chose, so rewriting the frame underneath
+ * those changes moves the children with it.
+ */
+const frame = (over: Partial<CanvasNode> = {}): CanvasNode =>
+ ({
+ id: 'g1',
+ type: 'group',
+ position: { x: 13, y: 7 },
+ data: {},
+ measured: { width: 613, height: 371 },
+ ...over
+ }) as CanvasNode
+
+const child = (over: Partial<CanvasNode> = {}): CanvasNode =>
+ ({
+ id: 'c1',
+ type: 'terminal',
+ parentId: 'g1',
+ extent: 'parent',
+ position: { x: 100, y: 100 },
+ data: {},
+ measured: { width: 320, height: 240 },
+ ...over
+ }) as CanvasNode
+
+const positionOf = (
+ changes: NodeChange<CanvasNode>[],
+ id: string
+): { x: number; y: number } | undefined =>
+ changes.flatMap((c) => (c.type === 'position' && c.id === id && c.position ? [c.position] : []))[0]
+
+const resizeOf = (id: string, width: number, height: number): NodeChange<CanvasNode> =>
+ ({ id, type: 'dimensions', resizing: true, dimensions: { width, height } }) as NodeChange<CanvasNode>
+
+const moveOf = (id: string, x: number, y: number): NodeChange<CanvasNode> =>
+ ({ id, type: 'position', position: { x, y } }) as NodeChange<CanvasNode>
+
+describe('snapResizeChanges with a group frame', () => {
+ it('keeps a child in place on a top-left drag', () => {
+ // React Flow proposes the frame at -7 and compensates the child to 120, which restores the
+ // child's original root x of 113. Snapping the frame to 0 without touching the child would
+ // leave it at 120 instead.
+ const changes = [moveOf('g1', -7, -13), resizeOf('g1', 633, 391), moveOf('c1', 120, 120)]
+ const out = snapResizeChanges(changes, [frame(), child()], G)
+
+ const framePos = positionOf(out, 'g1')!
+ const childPos = positionOf(out, 'c1')!
+ expect(framePos).toEqual({ x: 0, y: -20 })
+ expect({ x: framePos.x + childPos.x, y: framePos.y + childPos.y }).toEqual({ x: 113, y: 107 })
+ })
+
+ it('keeps a child in place when snapping moves an edge React Flow never moved', () => {
+ // A bottom-right drag emits no frame position change and no child changes at all, but the
+ // snap still pulls the frame origin from 13 to 20, so the child owes the same compensation.
+ const out = snapResizeChanges([resizeOf('g1', 633, 391)], [frame(), child()], G)
+
+ const framePos = positionOf(out, 'g1')!
+ const childPos = positionOf(out, 'c1')!
+ expect({ x: framePos.x + childPos.x, y: framePos.y + childPos.y }).toEqual({ x: 113, y: 107 })
+ })
+
+ it('emits no child change when the snap leaves the frame origin where it was', () => {
+ const on = frame({ position: { x: 40, y: 60 } })
+ const out = snapResizeChanges([resizeOf('g1', 633, 391)], [on, child()], G)
+
+ expect(positionOf(out, 'c1')).toBeUndefined()
+ })
+
+ it('snaps a nested frame against the CANVAS grid, not its own parent grid', () => {
+ const outer = frame({
+ id: 'g0',
+ position: { x: -28, y: -62 },
+ measured: { width: 900, height: 700 }
+ })
+ const inner = frame({ parentId: 'g0', extent: 'parent', position: { x: 41, y: 69 } })
+ const out = snapResizeChanges([resizeOf('g1', 633, 391)], [outer, inner], G)
+
+ const framePos = positionOf(out, 'g1') ?? inner.position
+ expect(Math.abs((framePos.x + outer.position.x) % G)).toBe(0)
+ expect(Math.abs((framePos.y + outer.position.y) % G)).toBe(0)
+ })
+})
diff --git a/src/renderer/lib/resizeSnap.ts b/src/renderer/lib/resizeSnap.ts
index 2a8a59634..6b769a786 100644
--- a/src/renderer/lib/resizeSnap.ts
+++ b/src/renderer/lib/resizeSnap.ts
@@ -1,7 +1,7 @@
import type { NodeChange } from '@xyflow/react'
import type { NodeKind } from '@shared/types'
import type { CanvasNode } from '../state/workspace'
-import { snapNodeToGrid } from './nodeSizing'
+import { containerOrigin, snapRectInRootSpace } from './gridSnap'
/**
* React Flow snaps the resize DELTA, never the resulting edges. From `getDimensionsAfterResize`
@@ -17,6 +17,10 @@ import { snapNodeToGrid } from './nodeSizing'
* 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.
+ *
+ * Two coordinate rules make this agree with React Flow's own drag snap and with group frames:
+ * the box is snapped in ROOT space (`snapRectInRootSpace`), and a frame's children are shifted
+ * by whatever the snap moved the frame's origin (`childShifts`).
*/
export function snapResizeChanges(
changes: NodeChange<CanvasNode>[],
@@ -28,9 +32,14 @@ export function snapResizeChanges(
)
if (!resizing.size || grid <= 0) return changes
+ const byId = new Map(nodes.map((node) => [node.id, node]))
const boxes = new Map<string, { x: number; y: number; width: number; height: number }>()
+ // How far the snap moved each resized node's origin away from what React Flow proposed. Its
+ // children are positioned against that origin, so they owe the opposite shift.
+ const shifts = new Map<string, { x: number; y: number }>()
+
for (const id of resizing) {
- const node = nodes.find((n) => n.id === id)
+ const node = byId.get(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)
@@ -38,43 +47,72 @@ export function snapResizeChanges(
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.
+ const snapped = snapRectInRootSpace(
+ { x: position.x, y: position.y, width, height },
+ containerOrigin(node.parentId, nodes),
+ grid,
+ (node.type ?? 'terminal') as NodeKind
+ )
boxes.set(id, {
- x: snapped.x + 0,
- y: snapped.y + 0,
+ x: snapped.x,
+ y: snapped.y,
width: snapped.width,
+ // A collapsed node keeps its collapsed bar height, exactly as align-to-grid leaves it.
height: node.data?.collapsed ? height : snapped.height
})
+ shifts.set(id, { x: snapped.x - position.x, y: snapped.y - position.y })
+ }
+
+ const childShifts = new Map<string, { x: number; y: number }>()
+ for (const [id, shift] of shifts) {
+ if (shift.x === 0 && shift.y === 0) continue
+ for (const node of nodes) {
+ if (node.parentId === id) {
+ childShifts.set(node.id, { x: -shift.x, y: -shift.y })
+ }
+ }
}
const positioned = new Set(
changes.flatMap((c) => (c.type === 'position' && boxes.has(c.id) ? [c.id] : []))
)
+ const shifted = new Set<string>()
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 (box) {
+ 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
}
- if (change.type === 'dimensions' && change.dimensions) {
- return { ...change, dimensions: { width: box.width, height: box.height } }
+ const shift = 'id' in change ? childShifts.get(change.id) : undefined
+ if (shift && change.type === 'position' && change.position) {
+ shifted.add(change.id)
+ return { ...change, position: { x: change.position.x + shift.x, y: change.position.y + shift.y } }
}
return change
})
for (const [id, box] of boxes) {
if (positioned.has(id)) continue
- const node = nodes.find((n) => n.id === id)
+ const node = byId.get(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 } })
}
+ // A right/bottom drag compensates no child of its own, because React Flow never moved the
+ // frame's origin there - the snap did.
+ for (const [id, shift] of childShifts) {
+ if (shifted.has(id)) continue
+ const node = byId.get(id)
+ if (!node) continue
+ applied.push({
+ id,
+ type: 'position',
+ position: { x: node.position.x + shift.x, y: node.position.y + shift.y }
+ })
+ }
return applied
}