// Lays out the tree as rectangles: each visible window's frame, and the tabs // of tabbed and stacking containers. // // Windows are positioned boxes, so the whole layout is computed here // instead of by document flow. import { WELL_MARGIN } from "../gaps"; import type { Rect } from "../rect"; import { barOf, surfaceOf, TITLE_BAR, tuckedUnderBar } from "../rect"; import type { Container, LayoutNode } from "./node"; import { Layout, NodeKind, showsOneChild, windowsIn } from "./node"; import type { NodeRef, Path } from "./path"; import { nodeAt } from "./path"; import type { Tiling } from "./tiling"; import { focusChainOf, focusedWindowIn, focusPathOf } from "./tiling"; /** One window's title bar and contents. */ export type Frame = { /** Its title bar, or its tab in a tabbed or stacking container. */ bar: Rect; /** * For a hidden tab, the shown tab's box, which it is drawn in while the * tab hiding it comes in (`tab-switch.ts`). `undefined` when `surface` is * set. * * Otherwise a hidden tab is not drawn, so the compositor stops its client * drawing. */ behind: Rect | undefined; id: string; /** * Whether it is in the container `focus parent` selected, which is * highlighted as a group. */ selected: boolean; /** * Whether it is its container's only tab. It closes with its group, as a * window does, rather than along the strip. */ soleTab: boolean; /** Its place in its tab strip, or `undefined` for a plain title bar. */ strip: StripPlace | undefined; /** Where its contents go, or `undefined` for a hidden tab. */ surface: Rect | undefined; /** * The layout of the container whose tab this bar is: tabbed or stacking. * `undefined` for a plain title bar. */ tabbed: TabLayout | undefined; }; /** Where a tab sits in its container's tab strip. See `TitleBar`. */ export type StripPlace = { /** * Its container's whole box: the strip and the window under it. Popups from * its tabs stay inside it. */ area: Rect; /** Its index in the strip, from the start. */ at: number; /** The whole strip's box. Tabs in one strip share it. */ box: Rect; /** * The empty end of a tabbed strip, past its last tab, which holds the * new-tab button and the group's buttons and drags the group. `undefined` * for a stack, whose bars span the strip. */ end: Rect | undefined; /** * Whether a divider marks the end's start: the last tab is hidden. The open * tab's own edge divides it from the end. */ endDivided: boolean; /** * Whether a divider marks its start: a hidden tab of a tabbed container * after another hidden tab, or any but the first bar of a stack. The open * tab's own edge divides it from its neighbors. */ divided: boolean; /** The container the strip belongs to. */ group: StripGroup; /** * Whether the strip sits in another group's well (see `Tab.well`), which * draws its edges stronger to stand out on the well. */ nested: boolean; /** * Whether its container shows it: the open tab, raised to join its window. * Hidden tabs lie flat on the strip. */ open: boolean; /** How many tabs the strip holds. */ tabs: number; }; /** A tab strip's container, which dragging the strip's empty end moves. */ export type StripGroup = { node: NodeRef; /** * How many wells the strip sits in. A nested group's windows are in the * groups around it too, so this tells their strips apart. */ wells: number; /** Every window inside it, which it cannot be dropped on. */ windows: readonly string[]; }; /** The layouts that give each child a tab instead of a share of the area. */ export type TabLayout = Layout.Stacking | Layout.Tabbed; /** A tab for a nested container, named after a window inside it. */ export type Tab = { /** Whether its container is showing this child. */ active: boolean; /** The layout of the nested container, which the tab shows. */ group: Layout; /** The container's last-focused window, which names the tab. */ id: string; /** The nested container. */ node: NodeRef; rect: Rect; /** Whether it is inside the container `focus parent` selected. */ selected: boolean; /** Its place in its tab strip. */ strip: StripPlace; /** The layout of the container whose tab this is. */ tabbed: TabLayout; /** * The box under the strip that the shown container sits in, with * {@link WELL_MARGIN} around it. `undefined` for a hidden tab. */ well: Rect | undefined; /** The windows the nested container holds. */ windows: readonly string[]; }; /** * The key a container's tab is drawn under. A shown group's own shown group * can be named after the same window, so the depth of its strip tells the two * apart. Counted in wells, which focus moving into the group leaves alone. */ export const tabKeyOf = ({ id, strip }: Tab): string => `${id}-${strip.group.wells.toString()}`; /** The frames and tabs a workspace's tiling puts on screen. */ export type Tiled = { frames: readonly Frame[]; tabs: readonly Tab[]; }; const NOTHING: Tiled = { frames: [], tabs: [] }; /** * The tiling laid out over `area`, with `gap` between neighbors. * * The caller picks the gap, since it is config policy. See `gaps.ts`. */ export const framesOf = (tiling: Tiling, area: Rect, gap: number): Tiled => { const { root } = tiling; return root === undefined ? NOTHING : placed(root, area, gap, focusPathOf(root, tiling.depth), false, 0); }; /** * The box the node at `path` gets, computed as {@link framesOf} does. * * Edge drags use it to turn pixels into shares, since the tree stores shares, * not lengths. */ export const areaOf = ( root: LayoutNode, path: Path, area: Rect, gap: number, ): Rect => { const [index, ...rest] = path; if (index === undefined) { return area; } else if (root.kind === NodeKind.Window) { throw new Error(`layout tree: path ${path.join(".")} runs into a window`); } else { const child = root.children[index]; if (child === undefined) { throw new Error(`layout tree: path ${path.join(".")} leaves the tree`); } else { return areaOf(child, rest, childArea(root, child, area, gap, index), gap); } } }; /** The box focus lights in a tiling, and the windows inside it. */ export type FocusBox = { rect: Rect; /** * The box of the container `focus parent` selected, or `undefined` when a * window is focused. */ selection: Rect | undefined; windows: readonly string[]; }; /** * The box of the node commands target, laid out as {@link framesOf} does, or * `undefined` on an empty tiling. * * A window shown as a tab gives the box of its tab group instead, since the * tabs belong to the group. A group `focus parent` selects keeps its own box, * inside its tab's well. */ export const focusBoxOf = ( tiling: Tiling, area: Rect, gap: number, ): FocusBox | undefined => { const { root } = tiling; if (root === undefined) { return undefined; } else { const focused = focusPathOf(root, tiling.depth); const selected = nodeAt(root, focused).kind === NodeKind.Container; const path = selected ? focused : tabGroupAround(root, focused); const rect = areaOf(root, path, area, gap); return { rect, selection: selected ? rect : undefined, windows: windowsIn(nodeAt(root, path)), }; } }; /** * One node and its descendants laid out in `area`. * * `pointed` is the rest of the focus path below this node, or `undefined` if * the focus is not inside it. `selected` is whether the node is inside the * container `focus parent` selected. `wells` is how many wells it is inside. */ const placed = ( node: LayoutNode, area: Rect, gap: number, pointed: Path | undefined, selected: boolean, wells: number, ): Tiled => { switch (node.kind) { case NodeKind.Window: { return { frames: [ { bar: barOf(area), behind: undefined, id: node.id, selected, soleTab: false, strip: undefined, surface: surfaceOf(area), tabbed: undefined, }, ], tabs: [], }; } case NodeKind.Container: { // A focus path ending at a container means `focus parent` selected it. const inside = selected || pointed?.length === 0; switch (node.layout) { case Layout.SplitH: case Layout.SplitV: { return joined( node.children.map((child, at) => placed( child, sliceOf(node, area, gap, at), gap, within(pointed, at), inside, wells, ), ), ); } case Layout.Stacking: case Layout.Tabbed: { return titled(node, node.layout, area, gap, pointed, inside, wells); } } } } }; /** The box `child`, at `at` in `container`, is laid out in. */ const childArea = ( container: Container, child: LayoutNode, area: Rect, gap: number, at: number, ): Rect => { switch (container.layout) { case Layout.SplitH: case Layout.SplitV: { return sliceOf(container, area, gap, at); } case Layout.Stacking: case Layout.Tabbed: { return child.kind === NodeKind.Container ? insetOf(contentsOf(container, area)) : contentsOf(container, area); } } }; /** The rest of the focus path below child `at`, if it runs through it. */ const within = (pointed: Path | undefined, at: number): Path | undefined => pointed === undefined || pointed[0] !== at ? undefined : pointed.slice(1); /** * The part of `area` child `at` gets: its share of the space left after the * gaps, along the container's axis. * * Each edge rounds to a whole pixel, as a float's does, so a client's box * stays on the pixel grid. Shares stay exact, so a 1px drag moves an edge 1px. */ const sliceOf = ( container: Container, area: Rect, gap: number, at: number, ): Rect => { const gaps = gap * (container.children.length - 1); const shares = container.fractions.slice(0, at); const before = shares.reduce((sum, fraction) => sum + fraction, 0); const fraction = container.fractions[at]; if (fraction === undefined) { throw new Error(`layout tree: no share for child ${at.toString()}`); } else if (container.layout === Layout.SplitV) { const { end, start } = edgesOf( area.y + gap * at, area.height - gaps, before, fraction, ); return { ...area, height: end - start, y: start }; } else { const { end, start } = edgesOf( area.x + gap * at, area.width - gaps, before, fraction, ); return { ...area, width: end - start, x: start }; } }; /** A slice's edges, from `origin`, on whole pixels. */ const edgesOf = ( origin: number, extent: number, before: number, fraction: number, ): { end: number; start: number } => ({ end: Math.round(origin + extent * (before + fraction)), start: Math.round(origin + extent * before), }); /** * Lays out a tabbed or stacking container: a title per child, and the shown * child filling the rest. * * A window child's tab is its title bar. A container child gets a {@link Tab} * named after its last-focused window, and is laid out only when shown. */ const titled = ( container: Container, layout: TabLayout, area: Rect, gap: number, pointed: Path | undefined, selected: boolean, wells: number, ): Tiled => { const contents = contentsOf(container, area); return joined( container.children.map((child, at) => { const bar = titleOf(container, area, at); const showing = at === container.focused; const strip = stripPlaceOf(container, area, at, wells); switch (child.kind) { case NodeKind.Window: { // A window's surface tucks under the bars, as in `surfaceOf`. const surface = tuckedUnderBar(contents); return { frames: [ { bar, behind: showing ? undefined : surface, id: child.id, selected, soleTab: container.children.length === 1, strip, surface: showing ? surface : undefined, tabbed: layout, }, ], tabs: [], }; } case NodeKind.Container: { const tab = { active: showing, group: child.layout, id: focusedWindowIn(child), node: { id: focusedWindowIn(child), up: focusChainOf(child).length, }, rect: bar, selected, strip, tabbed: layout, well: showing ? contents : undefined, windows: windowsIn(child), }; const inside = showing ? placed( child, insetOf(contents), gap, within(pointed, at), selected, wells + 1, ) : NOTHING; return { frames: inside.frames, tabs: [tab, ...inside.tabs], }; } } }), ); }; /** The widest a tab grows, as in a browser. */ const TAB_WIDTH = 240; /** * The empty strip kept past the last tab, so the strip's end always shows. It * fits the new-tab button and the group's buttons (see `StripButtons`). */ const STRIP_END = 72; // Tabs share the top row, each at most `TAB_WIDTH`; a stack gives each child a // full-width bar. The bars meet, so together they draw one tab strip. See // `TitleBar`. const titleOf = (container: Container, area: Rect, at: number): Rect => { if (container.layout === Layout.Stacking) { return { ...barOf(area), y: area.y + TITLE_BAR * at }; } else { const width = tabWidthOf(container, area); return { ...barOf(area), width, x: area.x + width * at }; } }; const tabWidthOf = (container: Container, area: Rect): number => Math.min(TAB_WIDTH, (area.width - STRIP_END) / container.children.length); const stripPlaceOf = ( container: Container, area: Rect, at: number, wells: number, ): StripPlace => { const count = container.children.length; return { area, at, box: { ...area, height: TITLE_BAR * barsOf(container) }, divided: at > 0 && (container.layout === Layout.Stacking || (at !== container.focused && at - 1 !== container.focused)), end: endOf(container, area), endDivided: container.layout !== Layout.Stacking && container.focused !== count - 1, group: { node: { id: focusedWindowIn(container), up: focusChainOf(container).length, }, wells, windows: windowsIn(container), }, nested: wells > 0, open: at === container.focused, tabs: count, }; }; const endOf = (container: Container, area: Rect): Rect | undefined => { if (container.layout === Layout.Stacking) { return undefined; } else { const tabs = tabWidthOf(container, area) * container.children.length; return { ...barOf(area), width: area.width - tabs, x: area.x + tabs }; } }; // The area under the titles. const contentsOf = (container: Container, area: Rect): Rect => { const bars = barsOf(container); return { ...area, height: Math.max(0, area.height - TITLE_BAR * bars), y: area.y + TITLE_BAR * bars, }; }; /** A tab's well, less its margin: the box its container is laid out in. */ const insetOf = (well: Rect): Rect => ({ height: Math.max(0, well.height - 2 * WELL_MARGIN), width: Math.max(0, well.width - 2 * WELL_MARGIN), x: well.x + WELL_MARGIN, y: well.y + WELL_MARGIN, }); /** How many bars tall a strip is: one for tabbed, one per child for stacking. */ const barsOf = (container: Container): number => container.layout === Layout.Stacking ? container.children.length : 1; /** `path`, or its parent's path when the parent shows it as a tab. */ const tabGroupAround = (root: LayoutNode, path: Path): Path => { const parentPath = path.slice(0, -1); const parent = nodeAt(root, parentPath); return path.length > 0 && parent.kind === NodeKind.Container && showsOneChild(parent.layout) ? parentPath : path; }; const joined = (placements: readonly Tiled[]): Tiled => ({ frames: placements.flatMap(({ frames }) => frames), tabs: placements.flatMap(({ tabs }) => tabs), });