import { slotOf, tileElement, type Board } from '../board.svelte'; import { shown, type Direction } from './focus'; import { addTab, childrenOf, find, paneTrail, shape, splitBeside, update, type Side, type Tile } from './tree'; // The layout moves a key makes (⌥⇧ with an arrow): a pane (a window, a // section, a canvas) steps past its neighbour in a split, into a tabs block or // a split that way, or out one level at an edge. One placed alone on a canvas // has no layout to step through. export type Move = { target: string; where: Side | 'tab'; index?: number }; export const sideOf: Record = { left: 'left', right: 'right', up: 'top', down: 'bottom' }; const across = (side: Side) => side === 'left' || side === 'right'; const before = (side: Side) => side === 'left' || side === 'top'; function rectOf(tile: Tile): DOMRect | undefined { return (tile.kind === 'leaf' ? slotOf(tile.window) : tileElement(tile.id))?.getBoundingClientRect(); } /** Where pane `id` goes when moved toward `side`; null when it has nowhere to go. */ export function moveToward(root: Tile, id: string, side: Side): Move | null { const path = paneTrail(root, id); if (!path || path.length < 2) return null; const own = path[path.length - 1]; const parent = path[path.length - 2]; const step = before(side) ? -1 : 1; if (parent.kind === 'canvas') return null; if (parent.kind === 'tabs') { const next = parent.children.findIndex((tab) => tab.id === own.id) + step; if (across(side) && next >= 0 && next < parent.children.length) return resolved(root, own, { target: parent.id, where: 'tab', index: next }); return resolved(root, own, { target: parent.id, where: side }); } let child: Tile = own; for (let at = path.length - 2; at >= 0; at--) { const holder = path[at]; if (holder.kind === 'canvas') break; if (holder.kind === 'split' && across(side) === (holder.axis === 'row')) { const sibling = holder.children[holder.children.indexOf(child) + step]; if (sibling) { if (child !== own) return resolved(root, own, { target: child.id, where: side }); if (sibling.kind === 'tabs') return resolved(root, own, { target: sibling.id, where: 'tab', index: sibling.children.length }); if (sibling.kind !== 'split') return resolved(root, own, { target: sibling.id, where: side }); return resolved(root, own, into(sibling, own)); } } child = holder; } return resolved(root, own, { target: child.id, where: side }); } /** Into a split across the way: beside its child level with the moving window, on the nearer side. */ function into(split: Extract, own: Tile): Move { const row = split.axis === 'row'; const from = rectOf(own); const centre = from ? (row ? from.left + from.width / 2 : from.top + from.height / 2) : 0; const start = (rect: DOMRect | undefined) => (rect ? (row ? rect.left : rect.top) : 0); const target = split.children.findLast((child) => start(rectOf(child)) <= centre) ?? split.children[0]; const rect = rectOf(target); const middle = rect ? (row ? rect.left + rect.width / 2 : rect.top + rect.height / 2) : 0; const first = centre < middle; return { target: target.id, where: row ? (first ? 'left' : 'right') : first ? 'top' : 'bottom' }; } /** * The move with its target named as it is once `own` has left: a tile that * held only it and one other is replaced by that other, and a split so freed * folds into its parent, leaving its first or last child to stand beside. * Null when the move would change nothing. */ function resolved(root: Tile, own: Tile, move: Move): Move | null { const after = update(root, own.id, () => null); let target = move.target; if (!find(after, target)) { const rest = childrenOf(find(root, target)!).filter((child) => !find(child, own.id)); if (rest.length !== 1) return null; target = rest[0].id; if (!find(after, target)) { const inner = childrenOf(rest[0]); target = (move.where !== 'tab' && before(move.where) ? inner[0] : inner[inner.length - 1]).id; } } const next = move.where === 'tab' ? addTab(after, target, own, move.index) : splitBeside(after, target, own, move.where); return shape(next) === shape(root) ? null : { ...move, target }; } export type Mark = { letter: string; window: string; rect: DOMRect; tab: boolean }; const LETTERS = 'asdfghjklqwertyuiopzxcvbnm'; /** A letter for every window on screen but notes, in reading order; a hidden tab gets one on its tab. */ export function marks(board: Board): Mark[] { const found: Omit[] = []; for (const [window, rect] of shown(board)) if (board.windows[window]?.type !== 'note') found.push({ window, rect, tab: false }); for (const button of document.querySelectorAll('[role="tab"][data-dock-window]')) { const window = button.dataset.dockWindow ?? ''; if (board.windows[window] && !button.closest('[inert]') && !found.some((mark) => mark.window === window)) found.push({ window, rect: button.getBoundingClientRect(), tab: true }); } found.sort((a, b) => a.rect.top - b.rect.top || a.rect.left - b.rect.left); return found.slice(0, LETTERS.length).map((mark, at) => ({ ...mark, letter: LETTERS[at] })); }