|
| 1 | +import type { ScopedThreadRef } from "@t3tools/contracts"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Pure helpers for the chat pane layout: a binary tree of splits whose leaves |
| 5 | + * each show one server thread. The tree stays shallow on purpose (a leaf can |
| 6 | + * split at most twice, and only across its parent's axis) so the result is |
| 7 | + * never deeper than a 2x2 grid. Anything beyond that stops being readable. |
| 8 | + */ |
| 9 | + |
| 10 | +export type ChatPaneId = string; |
| 11 | +export type SplitDirection = "horizontal" | "vertical"; |
| 12 | +export type DropZone = "top" | "bottom" | "left" | "right"; |
| 13 | + |
| 14 | +export interface ChatPaneLeaf { |
| 15 | + readonly kind: "leaf"; |
| 16 | + readonly id: ChatPaneId; |
| 17 | + readonly threadRef: ScopedThreadRef; |
| 18 | +} |
| 19 | + |
| 20 | +export interface ChatPaneSplit { |
| 21 | + readonly kind: "split"; |
| 22 | + readonly id: ChatPaneId; |
| 23 | + readonly direction: SplitDirection; |
| 24 | + readonly first: ChatPaneNode; |
| 25 | + readonly second: ChatPaneNode; |
| 26 | + /** Share of the axis given to `first`, clamped to [MIN_RATIO, MAX_RATIO]. */ |
| 27 | + readonly ratio: number; |
| 28 | +} |
| 29 | + |
| 30 | +export type ChatPaneNode = ChatPaneLeaf | ChatPaneSplit; |
| 31 | + |
| 32 | +const MAX_PANE_DEPTH = 2; |
| 33 | +const MIN_PANE_RATIO = 0.2; |
| 34 | +const MAX_PANE_RATIO = 0.8; |
| 35 | +const EDGE_REGION_FRACTION = 1 / 3; |
| 36 | + |
| 37 | +export function clampPaneRatio(ratio: number): number { |
| 38 | + if (!Number.isFinite(ratio)) return 0.5; |
| 39 | + return Math.min(MAX_PANE_RATIO, Math.max(MIN_PANE_RATIO, ratio)); |
| 40 | +} |
| 41 | + |
| 42 | +export function collectLeaves(node: ChatPaneNode): ChatPaneLeaf[] { |
| 43 | + return node.kind === "leaf" |
| 44 | + ? [node] |
| 45 | + : [...collectLeaves(node.first), ...collectLeaves(node.second)]; |
| 46 | +} |
| 47 | + |
| 48 | +export function findLeafByThread( |
| 49 | + node: ChatPaneNode, |
| 50 | + threadRef: ScopedThreadRef, |
| 51 | +): ChatPaneLeaf | null { |
| 52 | + return ( |
| 53 | + collectLeaves(node).find( |
| 54 | + (leaf) => |
| 55 | + leaf.threadRef.environmentId === threadRef.environmentId && |
| 56 | + leaf.threadRef.threadId === threadRef.threadId, |
| 57 | + ) ?? null |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +function findPaneDepth(node: ChatPaneNode, paneId: ChatPaneId): number | null { |
| 62 | + if (node.id === paneId) return 0; |
| 63 | + if (node.kind === "leaf") return null; |
| 64 | + const first = findPaneDepth(node.first, paneId); |
| 65 | + if (first !== null) return first + 1; |
| 66 | + const second = findPaneDepth(node.second, paneId); |
| 67 | + return second === null ? null : second + 1; |
| 68 | +} |
| 69 | + |
| 70 | +function findParent(node: ChatPaneNode, paneId: ChatPaneId): ChatPaneSplit | null { |
| 71 | + if (node.kind === "leaf") return null; |
| 72 | + if (node.first.id === paneId || node.second.id === paneId) return node; |
| 73 | + return findParent(node.first, paneId) ?? findParent(node.second, paneId); |
| 74 | +} |
| 75 | + |
| 76 | +/** A leaf can split while it is above the depth cap and never along the same axis as its parent. */ |
| 77 | +export function canSplitPane( |
| 78 | + root: ChatPaneNode, |
| 79 | + paneId: ChatPaneId, |
| 80 | + direction: SplitDirection, |
| 81 | +): boolean { |
| 82 | + const depth = findPaneDepth(root, paneId); |
| 83 | + if (depth === null || depth >= MAX_PANE_DEPTH) return false; |
| 84 | + const parent = findParent(root, paneId); |
| 85 | + return parent === null || parent.direction !== direction; |
| 86 | +} |
| 87 | + |
| 88 | +export function zoneToSplit(zone: DropZone): { |
| 89 | + direction: SplitDirection; |
| 90 | + side: "first" | "second"; |
| 91 | +} { |
| 92 | + switch (zone) { |
| 93 | + case "left": |
| 94 | + return { direction: "horizontal", side: "first" }; |
| 95 | + case "right": |
| 96 | + return { direction: "horizontal", side: "second" }; |
| 97 | + case "top": |
| 98 | + return { direction: "vertical", side: "first" }; |
| 99 | + case "bottom": |
| 100 | + return { direction: "vertical", side: "second" }; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +export function splitPane( |
| 105 | + root: ChatPaneNode, |
| 106 | + paneId: ChatPaneId, |
| 107 | + zone: DropZone, |
| 108 | + newLeaf: ChatPaneLeaf, |
| 109 | + splitId: ChatPaneId, |
| 110 | +): ChatPaneNode { |
| 111 | + const { direction, side } = zoneToSplit(zone); |
| 112 | + if (!canSplitPane(root, paneId, direction)) return root; |
| 113 | + const replace = (node: ChatPaneNode): ChatPaneNode => { |
| 114 | + if (node.id === paneId) { |
| 115 | + return { |
| 116 | + kind: "split", |
| 117 | + id: splitId, |
| 118 | + direction, |
| 119 | + ratio: 0.5, |
| 120 | + first: side === "first" ? newLeaf : node, |
| 121 | + second: side === "first" ? node : newLeaf, |
| 122 | + }; |
| 123 | + } |
| 124 | + if (node.kind === "leaf") return node; |
| 125 | + const first = replace(node.first); |
| 126 | + const second = replace(node.second); |
| 127 | + return first === node.first && second === node.second ? node : { ...node, first, second }; |
| 128 | + }; |
| 129 | + return replace(root); |
| 130 | +} |
| 131 | + |
| 132 | +/** Removes a leaf. Its sibling takes the parent's slot; the last leaf leaves an empty layout. */ |
| 133 | +export function removePane(root: ChatPaneNode, paneId: ChatPaneId): ChatPaneNode | null { |
| 134 | + if (root.id === paneId) return null; |
| 135 | + if (root.kind === "leaf") return root; |
| 136 | + const first = removePane(root.first, paneId); |
| 137 | + const second = removePane(root.second, paneId); |
| 138 | + if (first === null) return second; |
| 139 | + if (second === null) return first; |
| 140 | + return first === root.first && second === root.second ? root : { ...root, first, second }; |
| 141 | +} |
| 142 | + |
| 143 | +export function setPaneRatio(root: ChatPaneNode, splitId: ChatPaneId, ratio: number): ChatPaneNode { |
| 144 | + if (root.kind === "leaf") return root; |
| 145 | + if (root.id === splitId) return { ...root, ratio: clampPaneRatio(ratio) }; |
| 146 | + const first = setPaneRatio(root.first, splitId, ratio); |
| 147 | + const second = setPaneRatio(root.second, splitId, ratio); |
| 148 | + return first === root.first && second === root.second ? root : { ...root, first, second }; |
| 149 | +} |
| 150 | + |
| 151 | +/** |
| 152 | + * Which edge the pointer is closest to, VS Code style: the outer third of the |
| 153 | + * long axis wins outright, the middle falls back to the short axis. Zones the |
| 154 | + * tree cannot honor are skipped so the preview never promises a split that |
| 155 | + * the drop would refuse. |
| 156 | + */ |
| 157 | +export function resolveDropZone( |
| 158 | + rect: { left: number; top: number; width: number; height: number }, |
| 159 | + clientX: number, |
| 160 | + clientY: number, |
| 161 | + isZoneAllowed: (zone: DropZone) => boolean, |
| 162 | +): DropZone | null { |
| 163 | + if (rect.width <= 0 || rect.height <= 0) return null; |
| 164 | + const relX = (clientX - rect.left) / rect.width; |
| 165 | + const relY = (clientY - rect.top) / rect.height; |
| 166 | + if (relX < 0 || relX > 1 || relY < 0 || relY > 1) return null; |
| 167 | + const horizontal = relX < 0.5 ? (["left", "right"] as const) : (["right", "left"] as const); |
| 168 | + const vertical = relY < 0.5 ? (["top", "bottom"] as const) : (["bottom", "top"] as const); |
| 169 | + const pick = (candidates: readonly DropZone[]) => candidates.find(isZoneAllowed) ?? null; |
| 170 | + const wide = rect.width >= rect.height; |
| 171 | + const [longAxis, longRel, shortAxis] = wide |
| 172 | + ? [horizontal, relX, vertical] |
| 173 | + : [vertical, relY, horizontal]; |
| 174 | + if (longRel < EDGE_REGION_FRACTION || longRel > 1 - EDGE_REGION_FRACTION) { |
| 175 | + const edge = pick([longAxis[0]]); |
| 176 | + if (edge) return edge; |
| 177 | + } |
| 178 | + return pick(shortAxis) ?? pick(longAxis); |
| 179 | +} |
0 commit comments