feat(ui): add docking, viewport controls and composed settings

Add shared dock layouts, navigation cube, range bands, progress actions and comparison surfaces. Extend toolbar menus and loading composition, and align tokens, registry entries, documentation and the living catalog.
This commit is contained in:
Codex
2026-10-29 09:38:25 +03:00
parent a6e6c8f2d9
commit cb534dbe39
51 changed files with 2273 additions and 336 deletions
+128
View File
@@ -0,0 +1,128 @@
/** Serializable split tree. Content identity is independent of its position. */
export type DockTree = { id: string } | { axis: "x" | "y"; ratio: number; first: DockTree; second: DockTree };
export type DockPosition = "center" | "left" | "right" | "top" | "bottom";
export interface DockRect { left: number; top: number; width: number; height: number }
export interface DockMove { activeId: string; overId: string; position: DockPosition }
export function dockIds(tree: DockTree | null): string[] {
return !tree ? [] : "id" in tree ? [tree.id] : [...dockIds(tree.first), ...dockIds(tree.second)];
}
export function decodeDockTree(raw: unknown): DockTree | null {
const seen = new Set<string>();
const read = (value: unknown, depth: number): DockTree | null => {
if (!value || typeof value !== "object" || depth > 128 || seen.size > 128) return null;
const v = value as Record<string, unknown>;
if (typeof v.id === "string" && v.id.length > 0 && v.id.length < 512) {
if (seen.has(v.id)) return null;
seen.add(v.id); return { id: v.id };
}
if ((v.axis !== "x" && v.axis !== "y") || typeof v.ratio !== "number" || !Number.isFinite(v.ratio)) return null;
const first = read(v.first, depth + 1), second = read(v.second, depth + 1);
return first && second ? { axis: v.axis, ratio: Math.max(10, Math.min(90, v.ratio)), first, second } : first ?? second;
};
return read(raw, 0);
}
export function pruneDockTree(tree: DockTree | null, ids: readonly string[]): DockTree | null {
if (!tree) return null;
if ("id" in tree) return ids.includes(tree.id) ? tree : null;
const first = pruneDockTree(tree.first, ids), second = pruneDockTree(tree.second, ids);
return first && second ? { ...tree, first, second } : first ?? second;
}
export function balancedDockTree(ids: readonly string[], axis: "x" | "y" = "x"): DockTree | null {
if (!ids.length) return null;
if (ids.length === 1) return { id: ids[0] };
const mid = Math.ceil(ids.length / 2);
return { axis, ratio: 50, first: balancedDockTree(ids.slice(0, mid), axis === "x" ? "y" : "x")!,
second: balancedDockTree(ids.slice(mid), axis === "x" ? "y" : "x")! };
}
export function reconcileDockTree(tree: DockTree | null, ids: readonly string[]): DockTree | null {
const kept = pruneDockTree(tree, ids), present = dockIds(kept), missing = ids.filter(id => !present.includes(id));
if (!kept) return balancedDockTree(missing);
return missing.length ? { axis: "x", ratio: 50, first: kept, second: balancedDockTree(missing)! } : kept;
}
export function moveDockPanel(tree: DockTree | null, move: DockMove): DockTree | null {
const { activeId, overId, position } = move, ids = dockIds(tree);
if (!tree || activeId === overId || !ids.includes(activeId) || !ids.includes(overId)) return tree;
const swap = (node: DockTree): DockTree => "id" in node
? { id: node.id === activeId ? overId : node.id === overId ? activeId : node.id }
: { ...node, first: swap(node.first), second: swap(node.second) };
if (position === "center") return swap(tree);
const remaining = pruneDockTree(tree, ids.filter(id => id !== activeId))!;
const insert = (node: DockTree): DockTree => {
if (!("id" in node)) return { ...node, first: insert(node.first), second: insert(node.second) };
if (node.id !== overId) return node;
const before = position === "left" || position === "top";
return { axis: position === "left" || position === "right" ? "x" : "y", ratio: 50,
first: before ? { id: activeId } : node, second: before ? node : { id: activeId } };
};
return insert(remaining);
}
export function resizeDockSplit(tree: DockTree, path: string, ratio: number): DockTree {
if ("id" in tree || !Number.isFinite(ratio)) return tree;
if (!path) return { ...tree, ratio: Math.max(10, Math.min(90, ratio)) };
const side = path[0] === "0" ? "first" : "second";
return { ...tree, [side]: resizeDockSplit(tree[side], path.slice(1), ratio) };
}
export function dockRects(tree: DockTree | null, bounds: DockRect): Record<string, DockRect> {
if (!tree) return {};
if ("id" in tree) return { [tree.id]: bounds };
const fraction = tree.ratio / 100;
const first = tree.axis === "x" ? { ...bounds, width: bounds.width * fraction } : { ...bounds, height: bounds.height * fraction };
const second = tree.axis === "x" ? { ...bounds, left: bounds.left + first.width, width: bounds.width - first.width }
: { ...bounds, top: bounds.top + first.height, height: bounds.height - first.height };
return { ...dockRects(tree.first, first), ...dockRects(tree.second, second) };
}
export function dockPosition(rect: DockRect, x: number, y: number): DockPosition | null {
if (rect.width <= 0 || rect.height <= 0 || x < rect.left || y < rect.top || x > rect.left + rect.width || y > rect.top + rect.height) return null;
const u = (x - rect.left) / rect.width, v = (y - rect.top) / rect.height;
const distances: [DockPosition, number][] = [["left", u], ["right", 1 - u], ["top", v], ["bottom", 1 - v]];
const nearest = distances.sort((a, b) => a[1] - b[1])[0];
return nearest[1] <= .23 ? nearest[0] : "center";
}
export interface DockMinimum { width: number; height: number }
export type DockMinimums = Readonly<Record<string, DockMinimum>>;
/** Propagate content limits through every ancestor, not only the nearest split. */
export function dockMinimum(tree: DockTree | null, minimums: DockMinimums = {}): DockMinimum {
if (!tree) return {width: 0, height: 0};
if ("id" in tree) {
const value = minimums[tree.id];
const valid = (v: number | undefined, fallback: number) => typeof v === "number" && Number.isFinite(v) ? Math.max(0, v) : fallback;
return {width: valid(value?.width, 160), height: valid(value?.height, 120)};
}
const a = dockMinimum(tree.first, minimums), b = dockMinimum(tree.second, minimums);
return tree.axis === "x" ? {width: a.width + b.width, height: Math.max(a.height, b.height)}
: {width: Math.max(a.width, b.width), height: a.height + b.height};
}
/** Host expands to the recursive minimum when the whole viewport is too small. */
export function constrainDockTree(tree: DockTree | null, bounds: DockMinimum, minimums: DockMinimums = {}): DockTree | null {
if (!tree || "id" in tree) return tree;
const required = dockMinimum(tree, minimums), size = {width: Math.max(bounds.width, required.width), height: Math.max(bounds.height, required.height)};
const a = dockMinimum(tree.first, minimums), b = dockMinimum(tree.second, minimums), key = tree.axis === "x" ? "width" : "height";
const available = size[key];
const cut = Math.max(a[key], Math.min(available - b[key], available * tree.ratio / 100));
const ratio=available>0?cut/available*100:50;
return {...tree, ratio:Math.abs(ratio-tree.ratio)<1e-9?tree.ratio:ratio,
first: constrainDockTree(tree.first, {...size, [key]: cut}, minimums)!,
second: constrainDockTree(tree.second, {...size, [key]: available - cut}, minimums)!};
}
/** Initial preferred sizes apply only until the product persists a user layout. */
export function preferDockSizes(tree:DockTree|null,bounds:DockMinimum,preferred:DockMinimums):DockTree|null {
if(!tree||'id' in tree)return tree;
const a=dockIds(tree.first).some(id=>id in preferred),b=dockIds(tree.second).some(id=>id in preferred);
const key=tree.axis==='x'?'width':'height';
let ratio=tree.ratio;
if(a!==b){const wanted=dockMinimum(a?tree.first:tree.second,preferred)[key];ratio=(a?wanted:bounds[key]-wanted)/bounds[key]*100;}
ratio=Math.max(1,Math.min(99,Number.isFinite(ratio)?ratio:50));
return {...tree,ratio,first:preferDockSizes(tree.first,{...bounds,[key]:bounds[key]*ratio/100},preferred)!,second:preferDockSizes(tree.second,{...bounds,[key]:bounds[key]*(1-ratio/100)},preferred)!};
}
+22 -3
View File
@@ -1,4 +1,4 @@
export type FloatingPlacement = "bottom-start" | "bottom-end" | "top-start" | "top-end";
export type FloatingPlacement = "bottom-start" | "bottom-end" | "top-start" | "top-end" | "left-start" | "right-start";
export interface RectLike {
top: number;
@@ -15,6 +15,7 @@ export interface FloatingPositionOptions {
surfaceHeight: number;
placement?: FloatingPlacement;
offset?: number;
flip?: boolean;
viewportWidth?: number;
viewportHeight?: number;
viewportPadding?: number;
@@ -24,6 +25,7 @@ export interface FloatingPosition {
top: number;
left: number;
maxHeight: number;
maxWidth?: number;
resolvedPlacement: FloatingPlacement;
}
@@ -33,6 +35,7 @@ export function computeFloatingPosition({
surfaceHeight,
placement = "bottom-start",
offset = 8,
flip = true,
viewportWidth = typeof window === "undefined" ? 1920 : window.innerWidth,
viewportHeight = typeof window === "undefined" ? 1080 : window.innerHeight,
viewportPadding = 8,
@@ -41,6 +44,22 @@ export function computeFloatingPosition({
0,
viewportHeight - viewportPadding * 2,
);
if (placement.startsWith("left") || placement.startsWith("right")) {
const roomLeft = Math.max(0, anchor.left - offset - viewportPadding);
const roomRight = Math.max(0, viewportWidth - anchor.right - offset - viewportPadding);
const leftRequested = placement.startsWith("left");
const switched = flip && (leftRequested ? surfaceWidth > roomLeft && roomRight > roomLeft : surfaceWidth > roomRight && roomLeft > roomRight);
const toLeft = switched ? !leftRequested : leftRequested;
const maxWidth = toLeft ? roomLeft : roomRight;
const actualWidth = Math.min(surfaceWidth, maxWidth);
const visibleHeight = Math.min(surfaceHeight, viewportAvailableHeight);
return {
left: toLeft ? anchor.left - offset - actualWidth : anchor.right + offset,
top: Math.max(viewportPadding, Math.min(anchor.top, viewportHeight - viewportPadding - visibleHeight)),
maxHeight: viewportAvailableHeight, maxWidth,
resolvedPlacement: toLeft ? "left-start" : "right-start",
};
}
const roomBelow = Math.min(
viewportAvailableHeight,
Math.max(0, viewportHeight - anchor.bottom - offset - viewportPadding),
@@ -50,9 +69,9 @@ export function computeFloatingPosition({
Math.max(0, anchor.top - offset - viewportPadding),
);
const requestedTop = placement.startsWith("top");
const shouldFlip = requestedTop
const shouldFlip = flip && (requestedTop
? surfaceHeight > roomAbove && roomBelow > roomAbove
: surfaceHeight > roomBelow && roomAbove > roomBelow;
: surfaceHeight > roomBelow && roomAbove > roomBelow);
const resolvedPlacement = shouldFlip
? (`${requestedTop ? "bottom" : "top"}-${placement.endsWith("end") ? "end" : "start"}` as FloatingPlacement)
: placement;
+8
View File
@@ -4,3 +4,11 @@ export * from "./glass.js";
export * from "./theme.js";
export * from "./toolbar.js";
export * from "./environment.js";
export * from "./dockTree.js";
export {dockMinimum, constrainDockTree, type DockMinimum, type DockMinimums} from "./dockTree.js";
export {preferDockSizes} from "./dockTree.js";
export * from "./menuAim.js";
+14
View File
@@ -0,0 +1,14 @@
import type { RectLike } from "./floating.js";
export type MenuPoint = {x:number;y:number};
/** Delay sibling activation only while the pointer advances through the corridor to the open menu. */
export function movingToMenu(origin:MenuPoint, previous:MenuPoint, current:MenuPoint, surface:RectLike):boolean {
const horizontal = origin.x < surface.left || origin.x > surface.right;
const edge = horizontal ? (origin.x < surface.left ? surface.left : surface.right) : (origin.y < surface.top ? surface.top : surface.bottom);
const a = horizontal ? {x:edge,y:surface.top-6} : {x:surface.left-6,y:edge};
const b = horizontal ? {x:edge,y:surface.bottom+6} : {x:surface.right+6,y:edge};
const cross=(p:MenuPoint,q:MenuPoint,v:MenuPoint)=>(q.x-p.x)*(v.y-p.y)-(q.y-p.y)*(v.x-p.x);
const signs=[cross(origin,a,current),cross(a,b,current),cross(b,origin,current)];
const inside=signs.every(v=>v>=-0.01)||signs.every(v=>v<=0.01);
const axis=horizontal?"x":"y";
return inside && Math.abs(current[axis]-edge)<Math.abs(previous[axis]-edge)-0.1;
}
+1 -1
View File
@@ -1,4 +1,4 @@
export type ToolbarPlacement = "left" | "right" | "bottom";
export type ToolbarPlacement = "left" | "right" | "top" | "bottom";
export interface ToolbarSettings {
minSize: number;