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.

3.5 KB
Raw Blame
import type { NodeKind } from '@shared/types'
import { STICKY_NODE_MIN_SIZE } from '@shared/default-node-size'

/**
 * Minimum width/height for each node kind. These are the single source of truth
 * for the `minWidth`/`minHeight` every node passes to React Flow's `<NodeResizer>` —
 * the components read them here instead of re-declaring the literals, so the two
 * can't drift apart.
 *
 * The resizer enforces these only during a drag. Programmatic size changes (e.g.
 * align-to-grid) bypass the resizer, so they must clamp to these values themselves
 * to avoid shrinking a node below what its resizer would allow.
 */
export const NODE_MIN_SIZES: Record<NodeKind, { width: number; height: number }> = {
  terminal: { width: 260, height: 160 },
  sticky: STICKY_NODE_MIN_SIZE,
  group: { width: 200, height: 140 },
  editor: { width: 320, height: 200 },
  diff: { width: 420, height: 220 },
  video: { width: 320, height: 200 },
  web: { width: 320, height: 200 },
  browser: { width: 360, height: 240 },
  files: { width: 220, height: 160 },
  subagent: { width: 180, height: 84 },
  loop: { width: 180, height: 84 },
  dino: { width: 400, height: 160 },
  trigger: { width: 240, height: 120 },
  simulator: { width: 200, height: 260 },
  whiteboard: { width: 320, height: 220 }
}

/** Height of a node when collapsed (header only). Re-exported by `state/workspace`. */
export const COLLAPSED_HEIGHT = 40

export interface Rect {
  x: number
  y: number
  width: number
  height: number
}

/**
 * Snap a rectangle so all four corners land on grid intersections: each edge
 * (left, top, right, bottom) rounds independently to its nearest grid line. The
 * width/height are then raised to the node kind's grid-aligned minimum so a small
 * node (or a large grid) never inverts or undershoots what its `<NodeResizer>`
 * would allow. Left/top stay at their snapped positions; only the right/bottom
 * edge extends when the minimum has to kick in.
 */
export function snapNodeToGrid(g: number, kind: NodeKind, r: Rect): Rect {
  const x = Math.round(r.x / g) * g
  const y = Math.round(r.y / g) * g
  let width = Math.round((r.x + r.width) / g) * g - x
  let height = Math.round((r.y + r.height) / g) * g - y
  const min = NODE_MIN_SIZES[kind] ?? { width: 0, height: 0 }
  const minW = Math.ceil(min.width / g) * g
  const minH = Math.ceil(min.height / g) * g
  if (width < minW) width = minW
  if (height < minH) height = minH
  return { x, y, width, height }
}

/**
 * Snap a rectangle onto the grid by growing it: left/top round DOWN, right/bottom round UP, so
 * every edge lands on a grid line and the rect never loses area. `snapNodeToGrid` rounds each
 * edge to the nearest line instead, which is right for a resize (the user is aiming an edge) and
 * wrong for a frame that has to keep a minimum clearance around what it wraps — nearest-rounding
 * pulls an edge inward by up to half a grid and eats the padding.
 */
export function expandRectToGrid(g: number, kind: NodeKind, r: Rect): Rect {
  // `+ 0` normalizes the -0 that flooring a small negative coordinate produces, which would
  // otherwise ride into node positions and out to project.json.
  const x = Math.floor(r.x / g) * g + 0
  const y = Math.floor(r.y / g) * g + 0
  let width = Math.ceil((r.x + r.width) / g) * g - x
  let height = Math.ceil((r.y + r.height) / g) * g - y
  const min = NODE_MIN_SIZES[kind] ?? { width: 0, height: 0 }
  const minW = Math.ceil(min.width / g) * g
  const minH = Math.ceil(min.height / g) * g
  if (width < minW) width = minW
  if (height < minH) height = minH
  return { x, y, width, height }
}