125 lines
3.5 KiB
TypeScript
125 lines
3.5 KiB
TypeScript
// <PaneTree> — draws a split tree (`agent/terminal/split-layout.ts`): panes
|
|
// in rows and columns that fill their box, with a divider between neighbours
|
|
// that is dragged with the pointer or moved with the arrow keys. It owns the
|
|
// geometry only; what a pane shows is the caller's (`renderPane`).
|
|
import { useRef } from "preact/hooks";
|
|
import { Fragment, type ComponentChildren } from "preact";
|
|
import type { Node } from "../../agent/terminal/split-layout.js";
|
|
import "./PaneTree.css";
|
|
|
|
function Divider({
|
|
dir,
|
|
onMove,
|
|
}: {
|
|
dir: "row" | "column";
|
|
/** Pixels moved along the split's axis since the last event. */
|
|
onMove: (px: number) => void;
|
|
}) {
|
|
const last = useRef<number | null>(null);
|
|
const along = (e: PointerEvent) => (dir === "row" ? e.clientX : e.clientY);
|
|
return (
|
|
<div
|
|
class={`ui-pane-divider ui-pane-divider-${dir}`}
|
|
role="separator"
|
|
aria-orientation={dir === "row" ? "vertical" : "horizontal"}
|
|
tabIndex={0}
|
|
onPointerDown={(e) => {
|
|
(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId);
|
|
last.current = along(e);
|
|
}}
|
|
onPointerMove={(e) => {
|
|
if (last.current === null) return;
|
|
const now = along(e);
|
|
onMove(now - last.current);
|
|
last.current = now;
|
|
}}
|
|
onPointerUp={() => {
|
|
last.current = null;
|
|
}}
|
|
onPointerCancel={() => {
|
|
last.current = null;
|
|
}}
|
|
onKeyDown={(e) => {
|
|
const back = dir === "row" ? "ArrowLeft" : "ArrowUp";
|
|
const fwd = dir === "row" ? "ArrowRight" : "ArrowDown";
|
|
if (e.key !== back && e.key !== fwd) return;
|
|
e.preventDefault();
|
|
// A fixed nudge, turned into a share of the box like a drag.
|
|
onMove(e.key === fwd ? 20 : -20);
|
|
}}
|
|
/>
|
|
);
|
|
}
|
|
|
|
function Branch({
|
|
node,
|
|
path,
|
|
renderPane,
|
|
onResize,
|
|
}: {
|
|
node: Node;
|
|
path: number[];
|
|
renderPane: (agent: string) => ComponentChildren;
|
|
onResize: (path: number[], index: number, delta: number) => void;
|
|
}) {
|
|
const box = useRef<HTMLDivElement>(null);
|
|
if (node.kind === "pane") {
|
|
return (
|
|
<div class="ui-pane-tree-pane" key={node.agent}>
|
|
{renderPane(node.agent)}
|
|
</div>
|
|
);
|
|
}
|
|
const size = () => {
|
|
const el = box.current;
|
|
if (el === null) return 1;
|
|
return Math.max(1, node.dir === "row" ? el.clientWidth : el.clientHeight);
|
|
};
|
|
return (
|
|
<div class={`ui-pane-tree-split ui-pane-tree-${node.dir}`} ref={box}>
|
|
{node.children.map((child, i) => (
|
|
<Fragment key={child.kind === "pane" ? child.agent : `split-${i}`}>
|
|
{i > 0 ? (
|
|
<Divider
|
|
dir={node.dir}
|
|
onMove={(px) => onResize(path, i - 1, px / size())}
|
|
/>
|
|
) : null}
|
|
<div
|
|
class="ui-pane-tree-cell"
|
|
style={{ flex: `${node.sizes[i] ?? 0} 1 0` }}
|
|
>
|
|
<Branch
|
|
node={child}
|
|
path={[...path, i]}
|
|
renderPane={renderPane}
|
|
onResize={onResize}
|
|
/>
|
|
</div>
|
|
</Fragment>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function PaneTree({
|
|
root,
|
|
renderPane,
|
|
onResize,
|
|
}: {
|
|
root: Node;
|
|
renderPane: (agent: string) => ComponentChildren;
|
|
/** The divider after child `index` of the split at `path` moved by `delta`, a share of that split's box. */
|
|
onResize: (path: number[], index: number, delta: number) => void;
|
|
}) {
|
|
return (
|
|
<div class="ui-pane-tree">
|
|
<Branch
|
|
node={root}
|
|
path={[]}
|
|
renderPane={renderPane}
|
|
onResize={onResize}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|