/** * Geometry and persistence for the chat dock window. The dock floats freely on * desktop-width screens: the header drags it anywhere, eight edges and corners * resize it, and dragging near a side edge offers the pinned split, which * reflows the page. On phone-width screens it stays a full-width bottom bar, * riding above the on-screen keyboard. Pure functions live here so the * placement rules are testable without a DOM; ChatDock.vue owns the pointer * and keyboard events. */ export type DockMode = "left" | "float" | "right"; export interface Rect { x: number; y: number; w: number; h: number; } export interface DockLayout extends Rect { mode: DockMode; } export const DOCK_STORAGE_KEY = "archie.chat.dock.v2"; export const LEGACY_DOCK_STORAGE_KEY = "archie.chat.dock"; export const MIN_W = 320; export const MIN_H = 280; export const MAX_W = 720; /** The minimised bar is at least this wide, screen permitting. */ export const BAR_W = 640; /** Margin kept between a floating window and the viewport edge. */ export const INSET = 8; /** Pointer distance from a side edge that offers the snap preview. */ export const EDGE = 64; /** Pointer travel before a press on a drag surface becomes a drag. */ export const DRAG_START = 4; export const KEY_STEP = 10; export const KEY_STEP_FAST = 40; export const RESIZE_DIRS = ["n", "s", "e", "w", "ne", "nw", "se", "sw"] as const; export type ResizeDir = (typeof RESIZE_DIRS)[number]; export function clamp(value: number, min: number, max: number): number { return Math.min(Math.max(value, min), Math.max(min, max)); } export function defaultLayout(vw: number, vh: number): DockLayout { const w = Math.min(440, Math.max(MIN_W, vw - INSET * 2)); const h = Math.min(560, Math.max(MIN_H, vh - INSET * 2)); return { mode: "float", x: Math.max(INSET, vw - w - 24), y: Math.max(INSET, vh - h - 24), w, h }; } /** Largest width a floating window may take in this viewport. */ export function windowWidth(w: number, vw: number): number { return clamp(w, MIN_W, Math.max(MIN_W, Math.min(MAX_W, vw - INSET * 2))); } /** Largest width the pinned split may take, leaving the page room to reflow. */ export function pinnedWidth(w: number, vw: number): number { return clamp(w, MIN_W, Math.max(MIN_W, Math.min(MAX_W, vw - 480))); } /** Clamp a rectangle into the viewport, keeping its size within the minimums. */ export function fitRect(rect: T, vw: number, vh: number, inset = INSET): T { const availW = Math.max(1, vw - inset * 2); const availH = Math.max(1, vh - inset * 2); const w = clamp(rect.w, Math.min(MIN_W, availW), Math.min(MAX_W, availW)); const h = clamp(rect.h, Math.min(MIN_H, availH), availH); return { ...rect, x: clamp(rect.x, inset, vw - w - inset), y: clamp(rect.y, inset, vh - h - inset), w, h, }; } /** Validate a stored value, filling any missing field from the defaults. */ export function normalizeLayout(raw: unknown, vw: number, vh: number): DockLayout { const d = defaultLayout(vw, vh); if (!raw || typeof raw !== "object") return d; const o = raw as Partial>; const mode: DockMode = o.mode === "left" || o.mode === "right" || o.mode === "float" ? o.mode : "float"; const num = (value: unknown, fallback: number): number => typeof value === "number" && Number.isFinite(value) ? value : fallback; return fitRect( { mode, x: num(o.x, d.x), y: num(o.y, d.y), w: num(o.w, d.w), h: num(o.h, d.h) }, vw, vh, ); } /** Resize a rectangle from one of the eight edges/corners by a pointer delta. */ export function resizeRect(origin: Rect, dir: ResizeDir, dx: number, dy: number, vw: number, vh: number): Rect { const rect = { ...origin }; const minW = Math.min(MIN_W, Math.max(1, vw - INSET * 2)); const minH = Math.min(MIN_H, Math.max(1, vh - INSET * 2)); if (dir.includes("e")) rect.w = clamp(origin.w + dx, minW, Math.max(minW, vw - origin.x - INSET)); if (dir.includes("s")) rect.h = clamp(origin.h + dy, minH, Math.max(minH, vh - origin.y - INSET)); if (dir.includes("w")) { rect.x = clamp(origin.x + dx, INSET, origin.x + origin.w - minW); rect.w = origin.x + origin.w - rect.x; } if (dir.includes("n")) { rect.y = clamp(origin.y + dy, INSET, origin.y + origin.h - minH); rect.h = origin.y + origin.h - rect.y; } return rect; } /** The pinned side a pointer near an edge would commit to on release. */ export function snapEdge(pointerX: number, vw: number, edge = EDGE): "left" | "right" | null { if (pointerX <= edge) return "left"; if (pointerX >= vw - edge) return "right"; return null; } /** * Where the minimised bar sits: docked at the bottom of the screen, under the * window's horizontal position or on the side the mode pins to. Returned as a * bottom offset so the bar's own height never has to be known here. */ export function barPlacement(rect: Rect, mode: DockMode, vw: number): { x: number; bottom: number; w: number } { const w = Math.max(rect.w, Math.min(BAR_W, vw - INSET * 2)); const x = mode === "left" ? 0 : mode === "right" ? Math.max(0, vw - w) : clamp(rect.x, INSET, Math.max(INSET, vw - w - INSET)); return { x, bottom: INSET, w }; } /** Pointer travel down on the header that hides the conversation on a phone. */ export const SWIPE = 48; /** * How much of the layout viewport the on-screen keyboard covers, 0 when there * is none. Fixed elements stay anchored to the layout viewport, so the phone * bar offsets by this to ride above the keyboard. */ export function keyboardCover(innerHeight: number, vv: { height: number; offsetTop: number } | null): number { return vv ? Math.max(0, Math.round(innerHeight - vv.height - vv.offsetTop)) : 0; } /** The phone bar's bottom offset: above the keyboard while one is up, else the safe area. */ export function phoneBarBottom(keyboard: number): string { return keyboard > 0 ? `${keyboard + 8}px` : "max(0.5rem, env(safe-area-inset-bottom))"; } /** The old { side, width } dock preference, as a partial new layout. */ export function legacyLayout(raw: unknown): Partial | null { if (!raw || typeof raw !== "object") return null; const o = raw as Record; const partial: Partial = { mode: o.side === "left" || o.side === "right" ? o.side : "float" }; if (typeof o.width === "number" && Number.isFinite(o.width)) partial.w = o.width; return partial; } export interface StorageLike { getItem(key: string): string | null; setItem(key: string, value: string): void; removeItem(key: string): void; } /** * Move the old "archie.chat.dock" preference into the new key (the side becomes * the mode, the width the window width) and drop the old key either way: the * new key deliberately replaces it. */ export function migrateDockStorage(storage: StorageLike, vw: number, vh: number): void { try { const legacy = storage.getItem(LEGACY_DOCK_STORAGE_KEY); if (storage.getItem(DOCK_STORAGE_KEY) === null && legacy !== null) { let parsed: unknown = null; try { parsed = JSON.parse(legacy); } catch { parsed = null; } storage.setItem(DOCK_STORAGE_KEY, JSON.stringify(normalizeLayout(legacyLayout(parsed) ?? {}, vw, vh))); } if (legacy !== null) storage.removeItem(LEGACY_DOCK_STORAGE_KEY); } catch { // Unwritable storage leaves the dock at its defaults for this visit. } } export function keyStep(fast: boolean): number { return fast ? KEY_STEP_FAST : KEY_STEP; } /** The separator orientation announced for a resize handle. */ export function handleOrientation(dir: ResizeDir): "horizontal" | "vertical" { return dir === "e" || dir === "w" ? "vertical" : "horizontal"; } /** The size a resize handle announces: its height on the north/south edges, else its width. */ export function handleValue(dir: ResizeDir, rect: Rect): number { return dir === "n" || dir === "s" ? Math.round(rect.h) : Math.round(rect.w); }