/** Viewport-local geometry shared by pointer and keyboard window manipulation. */ export type WindowRect = { left: number; top: number; width: number; height: number }; export type ResizeEdge = 'n' | 's' | 'e' | 'w' | 'ne' | 'nw' | 'se' | 'sw'; export const windowResizeEdges: ResizeEdge[] = ['n', 's', 'e', 'w', 'ne', 'nw', 'se', 'sw']; type Bounds = { width: number; height: number }; const clamp = (value: number, min: number, max: number) => Math.max(min, Math.min(max, value)); export function fitWindow(rect: WindowRect, bounds: Bounds): WindowRect { const width = clamp(rect.width, Math.min(320, bounds.width - 16), Math.max(1, bounds.width - 16)); const height = clamp(rect.height, Math.min(200, bounds.height - 16), Math.max(1, bounds.height - 16)); return { width, height, left: clamp(rect.left, 8, Math.max(8, bounds.width - width - 8)), top: clamp(rect.top, 8, Math.max(8, bounds.height - height - 8)) }; } export function resizeWindow(rect: WindowRect, edge: ResizeEdge, dx: number, dy: number, bounds: Bounds): WindowRect { const r = fitWindow(rect, bounds), right = r.left + r.width, bottom = r.top + r.height; const minWidth = Math.min(320, bounds.width - 16), minHeight = Math.min(200, bounds.height - 16); const left = edge.includes('w') ? clamp(r.left + dx, 8, right - minWidth) : r.left; const top = edge.includes('n') ? clamp(r.top + dy, 8, bottom - minHeight) : r.top; const endX = edge.includes('e') ? clamp(right + dx, left + minWidth, bounds.width - 8) : right; const endY = edge.includes('s') ? clamp(bottom + dy, top + minHeight, bounds.height - 8) : bottom; return { left, top, width: endX - left, height: endY - top }; }