// Turns window state into what one screen shows: a rectangle and stacking // depth per visible window. // // Screen geometry is an argument because only the chrome knows which display // it is on. import type { Float } from "./floating/float"; import { floatHolds, isBare, onScreen, rectOf } from "./floating/float"; import { floatingGapOf, INNER_GAP, tiledAreaOf } from "./gaps"; import type { Rect } from "./rect"; import { spanning } from "./rect"; import type { FocusBox, Frame, StripPlace, Tab, TabLayout, Tiled, } from "./tree/frames"; import { focusBoxOf, framesOf } from "./tree/frames"; import type { Tiling } from "./tree/tiling"; import { focusedWindowIn } from "./tree/tiling"; import type { WindowMotion } from "./window-motion"; import type { Workspace } from "./workspace"; import { fullscreenOn } from "./workspace"; /** Where one window is drawn, and how it stacks against the others. */ export type Placement = { /** Its title bar, or its tab in a tabbed container. */ bar: Rect; /** * Whether it draws no title bar: a bare float's window, whose client draws * its own frame over the whole box (see `isBare`), or a window whose page or * client is fullscreen (see `pageFullscreened`). */ bare: boolean; /** * For a hidden tab, where a tab switch draws it under the shown one, at * {@link COVERED}. `undefined` when `surface` is set. See `Frame.behind`. */ behind: Rect | undefined; /** * The window's `z-index`. * * The page's compositor stacks client surfaces by it, and the pointer * hit-tests by it, so no separate stacking is reported. */ depth: number; /** * The bar and contents together; just the bar for a hidden tab. * * Open and close animations scale both elements about this box's center. * Scaling each about its own center would pull them apart. */ frame: Rect; id: string; /** Whether it is in the container `focus parent` selected. See `Frame.selected`. */ selected: boolean; /** Whether it is its container's only tab. See `Frame.soleTab`. */ soleTab: boolean; /** Its place in its tab strip. See `Frame.strip`. */ strip: StripPlace | undefined; /** Where its contents go, or `undefined` for a hidden tab. */ surface: Rect | undefined; /** The direction of the tab row its bar is in. See `Frame.tabbed`. */ tabbed: TabLayout | undefined; }; /** The rectangles of one screen. */ export type Geometry = { /** The bounding box of every display, which `fullscreen global` fills. */ desktop: Rect; /** The screen's name, which selects its workspace. */ name: string; /** The whole screen, which `fullscreen` fills. */ screen: Rect; /** The screen below the top bar, where windows go. */ workspace: Rect; }; /** A container's tab, stacked with its float, if any. */ export type PlacedTab = Tab & { depth: number }; /** A focus box, stacked with the windows inside it. */ export type PlacedFocusBox = FocusBox & { depth: number }; /** The windows and container tabs a screen shows. */ export type Screenful = { /** * The box around the window or group commands target, which the chrome * lights. `undefined` on an empty workspace or under a fullscreen window. */ focusBox: PlacedFocusBox | undefined; placements: readonly Placement[]; tabs: readonly PlacedTab[]; }; /** * The depth a tab switch draws the tab it hides at, under every tiled window. * See {@link Placement.behind}. * * The wallpaper and tiled wells share this depth but come first in the * document, so they stay underneath. `windowConcealing` then holds the tab at * -1 while the new one fades in. */ const COVERED = -2; /** The `z-index` all tiled windows share. */ export const TILED = 0; /** * The depth of a tiled window moving to another group, while it eases there: * over every tiled window, strip and open tab it passes, under every float. * See `crossing.ts`. */ export const CROSSING = 2; /** * The lowest floating `z-index`, above a {@link CROSSING} window and its open * tab. Floats are four apart, leaving each one's {@link raised} and * {@link leaving} depths free. */ const FLOATING = 4; /** * The depth a group's well is drawn at, under the windows at `depth`. A tiled * well sits at {@link COVERED}, under the tab a switch inside it hides, which * would otherwise vanish under the well instead of fading out. See * `GroupWell`. */ export const wellDepthOf = (depth: number): number => depth === TILED ? COVERED : depth; /** * The depth of a window's open tab and popups: over the window, under the next * window up. The open tab slides over its neighbors when tabs swap, since the * whole strip shares the window's depth. */ export const raised = (depth: number): number => depth + 1; /** * The depth of a window at `depth` while it closes: over the windows at its * depth and their open tabs, under the next window up. * * Its neighbors grow into its space during the close animation and would * otherwise cover it. Rising any higher would draw it over the floats above * it. See `closing.ts` for why it is not just moved to the end of the * document. */ export const leaving = (depth: number): number => depth + 2; /** * The depth of a fullscreen window, over everything else, as in sway. * * A closing fullscreen window clears its workspace's fullscreen (see * `workspace.ts`), so it is never hidden itself. */ export const FULLSCREEN = 2000; /** * The depth of the sheet that shows the cursor while a window moves, over * every window. See `GrabbingSheet`. */ export const GRABBING = 3000; /** * Everything a screen showing `workspace` shows, fullscreen included. * * A fullscreen window or group is placed at {@link FULLSCREEN} over the normal * layout, which stays in place. That lets it animate from its tiled box and * back (see `settlingStyles`) while the other windows stay drawn underneath. * * @param below - how many floats other screens stack under this screen's. All * screens share one stacking order, and a float can hang onto the next one. */ export const placementsOf = ( workspace: Workspace, geometry: Geometry, below: number, ): Screenful => { const { frames, tabs } = framesOf( workspace.tiling, tiledAreaOf(geometry.workspace), INNER_GAP, ); // Floats go above, in workspace stacking order, each tree laid out in its box. const floating = workspace.floats .map((float) => onScreen(float, geometry.screen)) .map((float, at) => ({ depth: FLOATING + 4 * (below + at), float, ...framesOf(float, rectOf(float), floatingGapOf(float)), })); const laidOut = [ ...frames.map((frame) => placed(frame, TILED)), ...floating.flatMap(({ depth, float, frames: inFloat }) => inFloat.map((frame) => isBare(float) ? placedBare(frame, rectOf(float), depth) : placed(frame, depth), ), ), ]; const full = fullscreenOn(workspace); const filled = fullscreenIn(workspace, geometry); // Replace the fullscreen windows' rectangles instead of adding a second. const covered = filled.frames.map(({ id }) => id); return { focusBox: full === undefined ? focusBoxIn(workspace, geometry, floating) : fullscreenSelectionIn(full, geometry), placements: [ ...laidOut.filter(({ id }) => !covered.includes(id)), ...filled.frames.map((frame) => workspace.fullscreen?.bare === true ? placedBare( frame, fullscreenArea(workspace.fullscreen, geometry), FULLSCREEN, ) : placed(frame, FULLSCREEN), ), ], tabs: [ // Tabs are keyed by window, so the strips a fullscreen group draws again // over itself go. ...[ ...tabs.map((tab) => ({ ...tab, depth: TILED })), ...floating.flatMap(({ depth, tabs: inFloat }) => inFloat.map((tab) => ({ ...tab, depth })), ), ].filter(({ strip }) => strip.group.windows.some((id) => !covered.includes(id)), ), ...filled.tabs.map((tab) => ({ ...tab, depth: FULLSCREEN })), ], }; }; /** * The focus box of the focused float, at its depth, or else of the tiling. A * scratchpad window gets none: the box stays on the window under it. * * Throws if the focused float is not among `floating`: both come from the * same workspace. */ const focusBoxIn = ( workspace: Workspace, geometry: Geometry, floating: readonly { depth: number; float: Float }[], ): PlacedFocusBox | undefined => { const { floatFocus, tiling } = workspace; if (floatFocus === undefined) { const box = focusBoxOf(tiling, tiledAreaOf(geometry.workspace), INNER_GAP); return box === undefined ? undefined : { ...box, depth: TILED }; } else { const focused = floating.find(({ float }) => floatHolds(float, floatFocus)); if (focused === undefined) { throw new Error(`placement: no float holds focused ${floatFocus}`); } else if (focused.float.scratchpad) { return focusBoxIn( { ...workspace, floatFocus: focusUnder(floating) }, geometry, floating, ); } else { const { depth, float } = focused; const box = focusBoxOf(float, rectOf(float), floatingGapOf(float)); return box === undefined ? undefined : { ...box, depth }; } } }; /** * The focus box of a fullscreen group `focus parent` selected, so its * selection still shows. `undefined` when a window inside it is focused, as * the fullscreen covers the screen and there is nothing to set apart. */ const fullscreenSelectionIn = ( full: { global: boolean; tiling: Tiling }, geometry: Geometry, ): PlacedFocusBox | undefined => { const box = focusBoxOf( full.tiling, fullscreenArea(full, geometry), INNER_GAP, ); return box?.selection === undefined ? undefined : { ...box, depth: FULLSCREEN }; }; /** * The window a scratchpad window sits over: the front other float's, else the * tiling's (`undefined`). */ const focusUnder = ( floating: readonly { float: Float }[], ): string | undefined => { const under = floating.filter(({ float }) => !float.scratchpad).at(-1); return under === undefined ? undefined : focusedWindowIn(under.float.root); }; /** * Where a window's contents are drawn and at what depth: its `surface`, or * `undefined` when not drawn. * * A hidden tab is drawn only while it is `concealing` (see `tab-switch.ts`), * or `captured`, as a screenshot or screen cast of its page alone is, at its * `behind` rect under the tab taking its place. Otherwise it has no box, so * the compositor stops its client drawing, and a page stops drawing too. */ export const contentsOf = ( placement: Placement | undefined, motion: WindowMotion, captured = false, ): { depth: number; rect: Rect } | undefined => { if (placement?.surface !== undefined) { return { depth: placement.depth, rect: placement.surface }; } else if ( placement?.behind === undefined || (motion !== "concealing" && !captured) ) { return undefined; } else { return { depth: COVERED, rect: placement.behind }; } }; /** * The fullscreen window or group laid out over the screen, or the desktop for * `fullscreen global`. Nothing when none is fullscreen. */ const fullscreenIn = (workspace: Workspace, geometry: Geometry): Tiled => { const full = fullscreenOn(workspace); return full === undefined ? { frames: [], tabs: [] } : framesOf(full.tiling, fullscreenArea(full, geometry), INNER_GAP); }; /** The screen a fullscreen fills, or the desktop for `fullscreen global`. */ const fullscreenArea = ({ global }: { global: boolean }, geometry: Geometry) => global ? geometry.desktop : geometry.screen; /** * One window's frame with no bar, its contents filling `box`: a bare float, or * a window whose page or client is fullscreen, such as a video, as in a * browser. See `pageFullscreened`. */ const placedBare = (frame: Frame, box: Rect, depth: number): Placement => ({ ...placed({ ...frame, surface: box }, depth), bare: true, }); /** * One window's frame, stacked at `depth`. The only place a {@link Placement} * is built, so `frame` is computed one way. */ const placed = ( { bar, behind, id, selected, soleTab, strip, surface, tabbed }: Frame, depth: number, ): Placement => ({ bar, bare: false, behind, depth, frame: surface === undefined ? bar : spanning(bar, surface), id, selected, soleTab, strip, surface, tabbed, });