import { useCallback, useState } from "react"; import { token } from "../styled-system/tokens"; import type { Closing, Leaving } from "./closing"; import { departed, movesAsTab, placedOn, sentAway, shutAway, withClosing, } from "./closing"; import { crossed } from "./crossing"; import type { PlacedTab, Placement } from "./placement"; import { contentsOf } from "./placement"; import type { Restack } from "./restacking"; import { restacked } from "./restacking"; import type { Shown } from "./shown"; import { sameBox, tabSwitched } from "./tab-switch"; import type { StripPlace } from "./tree/frames"; import type { ShellWindow } from "./window"; import type { Shuffle, TabFade, WindowMotion } from "./window-motion"; import { arrivalFrom, departureFor } from "./window-motion"; import type { WorkspaceSwitch } from "./workspace-switch"; import { switchedTo } from "./workspace-switch"; /** A window to draw: its placement and motion. */ export type DrawnWindow = { /** * Whether it just moved to another tab group, so it eases there over the * strips it passes. See `crossing.ts`. */ crossing: boolean; /** * Whether the window has keyboard focus. * * Frozen while the window leaves, since closing or switching moves focus and * the bar should not change mid-animation. */ focused: boolean; motion: WindowMotion; /** The window's placement, or `undefined` when it is off screen. */ placement: Placement | undefined; /** The shuffle the window is playing, or `undefined` when not shuffling. */ restack: Restack | undefined; /** * How far into its motion the window starts, in milliseconds. A scratchpad * slide that cuts the other short starts where that one had got to, and a * window placed partway through a workspace switch starts where the switch * has got to. Every other motion starts at 0. */ rewound: number; /** The screen the window is drawn on, or `undefined` for none. */ screen: string | undefined; /** * {@link DrawnWindow.rewound} for the parts drawn only while the window is * shown: its contents and the strip of the group it opens. A tab shown * partway through a workspace switch starts them where the switch has got * to, since its bar has been sliding all along. */ shownRewound: number; window: ShellWindow; }; /** A tab to draw. A tab names a container, not a window. */ export type DrawnTab = { focused: boolean; motion: WindowMotion; /** Its window's shuffle, or `undefined` when not shuffling. */ restack: Restack | undefined; /** The screen it is drawn on. */ screen: string; tab: PlacedTab; }; /** What each screen shows, by screen name. */ type Desk = Readonly>; export type WindowMotions = { /** The windows to draw, in the order they were opened. */ drawn: readonly DrawnWindow[]; /** * Called when a window's `part` on `screen` finishes an animation. `screen` * identifies which workspace switch ended. */ onPlayedOut: ( id: string, motion: WindowMotion, screen: string | undefined, part: Part, ) => void; /** The tabs to draw, including ones sliding off. */ tabs: readonly DrawnTab[]; }; /** Which element of a window reported an animation's end. */ export type Part = "bar" | "contents"; /** A playing shuffle and which of the two animation names it uses. */ type Shuffling = { motion: Shuffle; restack: Restack }; /** A closing window and its screen. */ type ClosingOn = Closing & { screen: string }; /** * When a scratchpad slide began, and how far into its animation, in * milliseconds. See {@link rewoundAfter}. */ type Slid = { at: number; rewound: number }; /** * An open window sent off the screen or shut, how it leaves, and from which * screen. A shut window leaves as a closed one does. */ type SendingOn = Leaving & Slid & { motion: "closing" | "closing-tab" | "sending" | "sending-tab" | "stowing"; screen: string; }; /** * A workspace switch playing on a screen: when it began, and the windows on the * arriving workspace whose parts were first drawn after that. */ type Switching = WorkspaceSwitch & { at: number; late: readonly Late[] }; /** * How far into a workspace switch a window's parts were first drawn, in * milliseconds: `placed` for the parts every placed window draws, `shown` for * those drawn only while it is shown. A part not drawn starts no animation, so * its slide must start where the others have got to. */ type Late = { id: string; placed: number; shown: number }; /** Animations still playing. */ type Playing = { closing: readonly ClosingOn[]; /** * The windows the last change moved to another group. Only a box change * starts a settle, and every box change replaces this. */ crossing: ReadonlySet; /** Windows still sliding down from the scratchpad. */ dropping: readonly (Slid & { id: string })[]; /** Windows still playing their opening animation. */ opening: readonly string[]; /** Floats still shuffling in the stack. */ restacking: readonly Shuffling[]; /** Open windows leaving the screen. */ sending: readonly SendingOn[]; /** * Each window's last shuffle name, kept after it ends so the next uses the * other (see {@link nextShuffle}). */ shuffled: readonly { id: string; motion: Shuffle }[]; /** The workspace each screen is switching away from, by screen. */ switching: Readonly>; /** The windows in a tab switch crossfade. */ tabbing: readonly { id: string; motion: TabFade }[]; }; const NOTHING_PLAYING: Playing = { closing: [], crossing: new Set(), dropping: [], opening: [], restacking: [], sending: [], shuffled: [], switching: {}, tabbing: [], }; /** * Each window's animation state, including windows the desktop has removed but * that are still animating out. * * Closes and workspace switches are detected by comparing the previous and * current desk. This runs during render, not in an effect: an effect would * first commit a render without the closed window, and React would unmount its * ``, blanking the page during the close animation. * * Animations end when the element reports it, so durations live in the * stylesheet (see `movingStyles`). Only the scratchpad slides' is read here, to * start one where the other was cut short. * * One list covers the whole desk, so a float dragged to another screen keeps * its element and its `` does not reload. */ export const useWindowMotion = ( shown: Desk, now: () => number = () => performance.now(), ): WindowMotions => { // One state, not two: React may keep only one of two updates made during // render, which would desync the last desk from the animations it started. const [{ before, playing }, setState] = useState({ before: shown, playing: NOTHING_PLAYING, }); if (moved(before, shown)) { setState({ before: shown, playing: advanced(playing, before, shown, now()), }); } const onPlayedOut = useCallback( ( id: string, motion: WindowMotion, screen: string | undefined, part: Part, ) => { setState((now) => { const next = played(now.playing, id, motion, screen, part); return next === now.playing ? now : { ...now, playing: next }; }); }, [], ); const index = indexOf(shown, playing); return { drawn: withClosing(windowsOf(shown), playing.closing).map((window) => drawnWindow(shown, playing, index, window), ), onPlayedOut, tabs: drawnTabs(shown, playing), }; }; /** Whether anything drawn has changed since the last render. */ const moved = (before: Desk, shown: Desk): boolean => Object.keys(before).length !== Object.keys(shown).length || Object.entries(shown).some(([name, now]) => { const was = before[name]; return ( was === undefined || was.activeId !== now.activeId || was.current !== now.current || was.overview !== now.overview || was.placements !== now.placements || was.scratchpad !== now.scratchpad || was.shut !== now.shut || was.tabs !== now.tabs || was.windows !== now.windows ); }); /** * The desk's open windows. Every screen has the same list; an empty desk has * none. */ const windowsOf = (desk: Desk): readonly ShellWindow[] => Object.values(desk)[0]?.windows ?? []; /** The desk's shut windows. Every screen has the same list. */ const shutOf = (desk: Desk): ReadonlySet => new Set(Object.values(desk)[0]?.shut); /** * What is playing after the desk changes from `before` to `shown` at `time`. */ const advanced = ( playing: Playing, before: Desk, shown: Desk, time: number, ): Playing => { // Only screens present in both renders; a new screen has no prior state. const screens = Object.entries(shown).flatMap(([name, now]) => { const was = before[name]; return was === undefined ? [] : [{ name, now, was }]; }); const windows = windowsOf(shown); const shuffles = screens .flatMap(({ now, was }) => restacked(was, now)) .map((restack) => ({ motion: nextShuffle(playing, restack.id), restack, })); const switched = screens.map(({ now, was }) => tabSwitched(was, now)); // Sets, since every window is checked against them. const open = new Set(windows.map(({ id }) => id)); // A shut window is open but shown as closed, so showing it opens it. const wasShut = shutOf(before); const shut = shutOf(shown); const wasOpen = new Set( windowsOf(before) .map(({ id }) => id) .filter((id) => !wasShut.has(id)), ); const shownIds = placedIds(shown); const fades = [ ...switched.flatMap(({ concealed }) => concealed.map((id) => ({ id, motion: "concealing" as const })), ), ...switched.flatMap(({ revealed }) => revealed.map((id) => ({ id, motion: "revealing" as const })), ), ...switched.flatMap(({ uncovered }) => uncovered.map((id) => ({ id, motion: "uncovering" as const })), ), ]; const fading = new Set(fades.map(({ id }) => id)); const switching = Object.fromEntries( screens.flatMap(({ name, now, was }) => { const next = switchingAfter(playing.switching[name], was, now, time); return next === undefined ? [] : [[name, next]]; }), ); // Windows on screens not switching workspace, the only ones that crossfade. const settled = new Set( Object.entries(shown).flatMap(([name, { placements }]) => switching[name] === undefined ? placements.map(({ id }) => id) : [], ), ); return { closing: [ ...playing.closing, ...screens.flatMap(({ name, now, was }) => departed(was, now.windows).map((closing) => ({ ...closing, screen: name, })), ), ], crossing: crossed(before, shown), // Drop windows no longer shown: they never report the slide finished. dropping: [ ...playing.dropping.filter(({ id }) => shownIds.has(id)), ...windows .filter( ({ id }) => stowed(before, id) && !stowed(shown, id) && shownIds.has(id), ) .map(({ id }) => ({ at: time, id, rewound: rewoundAfter( playing.sending.find( (sending) => sending.window.id === id && sending.motion === "stowing", ), time, ), })), ], // Drop windows closed or hidden while opening: they never report the // opening finished. opening: [ ...playing.opening.filter((id) => shownIds.has(id)), ...windows .filter(({ id }) => !wasOpen.has(id) && !shut.has(id)) .map(({ id }) => id), ], // A window raised again before settling plays only the latest raise. restacking: [ ...playing.restacking.filter( ({ restack }) => open.has(restack.id) && !shuffling(shuffles, restack.id), ), ...shuffles, ], // Drop windows shown again or closed: neither finishes leaving. sending: [ ...playing.sending.filter( ({ window }) => open.has(window.id) && !shownIds.has(window.id), ), ...screens.flatMap(({ name, was }) => shutAway(was, shut).map((closing) => ({ ...closing, at: time, motion: movesAsTab(closing.placement) ? ("closing-tab" as const) : ("closing" as const), rewound: 0, screen: name, })), ), ...screens.flatMap(({ name, now, was }) => (overviewing(was, now) ? [] : sentAway(was, now, Object.values(shown)).filter( ({ window }) => !shut.has(window.id), ) ).map((sending) => { const motion = sendingMotion(shown, sending); return { ...sending, at: time, motion, rewound: motion === "stowing" ? rewoundAfter( playing.dropping.find(({ id }) => id === sending.window.id), time, ) : 0, screen: name, }; }), ), ], shuffled: [ ...playing.shuffled.filter( ({ id }) => open.has(id) && !shuffling(shuffles, id), ), ...shuffles.map(({ motion, restack }) => ({ id: restack.id, motion })), ], switching, // A window switched back mid-fade plays the latest half, which always // differs from the current one, so the browser restarts it. A workspace // switch slides the tabs switched on the workspace arriving, and cuts // short the fades on the one leaving, which never report their end. // // A tab still drawn under the tab it switched to stops being drawn once // another tab is hidden in its box. Otherwise a quick second switch would // fade the next tab in over it rather than over the tab just left. tabbing: [ ...playing.tabbing.filter( ({ id, motion }) => !fading.has(id) && !(motion === "concealing" && reconcealed(shown, fades, id)), ), ...fades, ].filter(({ id }) => settled.has(id)), }; }; /** * Whether the hidden tab `id` shares its box with a tab `fades` newly conceal. */ const reconcealed = ( shown: Desk, fades: readonly { id: string; motion: TabFade }[], id: string, ): boolean => { const placements = Object.values(shown).flatMap( ({ placements }) => placements, ); const behind = placements.find((placement) => placement.id === id)?.behind; return fades.some( (fade) => fade.motion === "concealing" && fade.id !== id && sameBox( placements.find((placement) => placement.id === fade.id)?.behind, behind, ), ); }; /** * The workspace switch playing on a screen after it changes from `was` to * `now` at `time`: a new one, `playing` with the windows drawn since it began, * or `undefined` for none. */ const switchingAfter = ( playing: Switching | undefined, was: Shown, now: Shown, time: number, ): Switching | undefined => { const started = switchedTo(was, now); if (overviewing(was, now)) { return undefined; } else if (started !== undefined) { return { ...started, at: time, late: [] }; } else if (playing === undefined) { return undefined; } else { return { ...playing, late: lateIn(playing, was, now, time) }; } }; /** * Whether the overview showed the screen before or shows it now. Its windows * ease from tile to tile there, and on into the screen as it closes. */ const overviewing = (was: Shown, now: Shown): boolean => was.overview || now.overview; /** * The windows on `now` whose parts were first drawn after the switch * `playing` began, at `time` or earlier. Parts drawn again restart, so their * slide starts anew from where the switch has got to. */ const lateIn = ( playing: Switching, was: Shown, now: Shown, time: number, ): readonly Late[] => now.placements.flatMap((placement) => { const { id } = placement; const then = was.placements.find((before) => before.id === id); const kept = playing.late.find((late) => late.id === id); const into = time - playing.at; const placed = then === undefined ? into : (kept?.placed ?? 0); const shown = placement.surface !== undefined && then?.surface === undefined ? into : (kept?.shown ?? 0); return placed === 0 && shown === 0 ? [] : [{ id, placed, shown }]; }); /** * How long a scratchpad slide takes, in milliseconds. Matches `dropping` and * `stowing` in `movingStyles`. */ const SLIDE = Number.parseFloat(token("durations.slow")); /** * How far into a scratchpad slide starting at `time` to begin, given the * opposite slide it cuts short, if any. * * Each slide is the other played backwards, so starting at the time the cut * slide has left puts the window where that slide had moved it. */ const rewoundAfter = (cut: Slid | undefined, time: number): number => cut === undefined ? 0 : Math.max(0, SLIDE - (cut.rewound + time - cut.at)); /** Whether the desk has the window `id` in the scratchpad. */ const stowed = (desk: Desk, id: string): boolean => Object.values(desk)[0]?.scratchpad.includes(id) ?? false; /** * How a window leaves the screen: up to the scratchpad, out of a group that * stays, or off to another workspace. */ const sendingMotion = ( desk: Desk, { placement }: Leaving, ): SendingOn["motion"] => { const { strip } = placement; if (stowed(desk, placement.id)) { return "stowing"; } else { return movesAsTab(placement) && strip !== undefined && groupStays(desk, strip) ? "sending-tab" : "sending"; } }; /** Whether a screen in `desk` still shows a window of `strip`'s group. */ const groupStays = (desk: Desk, strip: StripPlace): boolean => strip.group.windows.some((id) => placedOn(Object.values(desk), id)); /** The windows the desk's screens place. */ const placedIds = (desk: Desk): ReadonlySet => new Set( Object.values(desk).flatMap(({ placements }) => placements.map(({ id }) => id), ), ); const shuffling = (shuffles: readonly Shuffling[], id: string): boolean => shuffles.some(({ restack }) => restack.id === id); /** * The shuffle name a window gets next: the one it did not get last. * * Browsers restart an animation only when its name changes. Reusing a name * would play nothing, never report completion, and leave the window stuck. */ const nextShuffle = (playing: Playing, id: string): Shuffle => playing.shuffled.find((shuffled) => shuffled.id === id)?.motion === "restacking" ? "restacking-again" : "restacking"; /** * What is left playing after `id` on `screen` finishes `motion`. * * Returns the same object when nothing changed so React skips the re-render. * Every element of an animating window reports, and the first matters, except * that a leaving tab's drawn contents outlast its bar (see `movingStyles`). */ const played = ( playing: Playing, id: string, motion: WindowMotion, screen: string | undefined, part: Part, ): Playing => { switch (motion) { case "arriving-from-end": case "arriving-from-start": case "leaving-to-end": case "leaving-to-start": { // The first report ends the switch on its screen: all its animations // start together and run equally long. return screen === undefined || playing.switching[screen] === undefined ? playing : { ...playing, switching: Object.fromEntries( Object.entries(playing.switching).filter( ([name]) => name !== screen, ), ), }; } case "closing": case "closing-tab": { const closing = playing.closing.filter( ({ placement, window }) => window.id !== id || outlastsBar(placement, motion, part), ); // A shut window closes from `sending`; see `SendingOn`. const sending = playing.sending.filter( ({ placement, window }) => window.id !== id || outlastsBar(placement, motion, part), ); return closing.length === playing.closing.length && sending.length === playing.sending.length ? playing : { ...playing, closing, sending }; } case "sending": case "sending-tab": case "stowing": { const sending = playing.sending.filter( ({ placement, window }) => window.id !== id || outlastsBar(placement, motion, part), ); return sending.length === playing.sending.length ? playing : { ...playing, sending }; } case "dropping": { const dropping = playing.dropping.filter((shown) => shown.id !== id); return dropping.length === playing.dropping.length ? playing : { ...playing, dropping }; } case "opening": case "opening-tab": { const opening = playing.opening.filter((arriving) => arriving !== id); return opening.length === playing.opening.length ? playing : { ...playing, opening }; } case "restacking": case "restacking-again": { // Match the current shuffle only, not one it was raised out of. const restacking = playing.restacking.filter( (shuffle) => shuffle.restack.id !== id || shuffle.motion !== motion, ); return restacking.length === playing.restacking.length ? playing : { ...playing, restacking }; } case "concealing": case "revealing": case "uncovering": { const tabbing = playing.tabbing.filter( (fade) => fade.id !== id || fade.motion !== motion, ); return tabbing.length === playing.tabbing.length ? playing : { ...playing, tabbing }; } case "resting": { // A resting window has nothing to finish. These events come from chrome // animations, such as the address bar spinner, bubbling up. return playing; } } }; /** * Whether a report from `part` leaves the window playing `motion`: its bar * finished, but its contents are drawn and fade later. */ const outlastsBar = ( placement: Placement, motion: WindowMotion, part: Part, ): boolean => part === "bar" && (motion === "closing-tab" || motion === "sending-tab") && contentsOf(placement, motion) !== undefined; /** Where each window is, by id, built once per render. */ type Index = { closing: ReadonlyMap; /** The departing workspace's placement of each window sliding off. */ leaving: ReadonlyMap< string, { left: WorkspaceSwitch; placement: Placement; screen: string } >; /** * Each shown window's placement. At most one screen shows it: a workspace * is on one screen, and a window on one workspace. */ placed: ReadonlyMap; sending: ReadonlyMap; }; /** * Keys each list by window. Reversed, so the first entry for a window wins, as * `find` would. */ const indexOf = (shown: Desk, playing: Playing): Index => ({ closing: new Map( playing.closing.toReversed().map((gone) => [gone.window.id, gone]), ), leaving: new Map( Object.entries(playing.switching) .flatMap(([screen, left]) => left.placements.map( (placement) => [placement.id, { left, placement, screen }] as const, ), ) .toReversed(), ), placed: new Map( Object.entries(shown) .flatMap(([screen, { placements }]) => placements.map( (placement) => [placement.id, { placement, screen }] as const, ), ) .toReversed(), ), sending: new Map( playing.sending.toReversed().map((gone) => [gone.window.id, gone]), ), }); const drawnWindow = ( shown: Desk, playing: Playing, index: Index, window: ShellWindow, ): DrawnWindow => { const closing = index.closing.get(window.id); const placed = index.placed.get(window.id); if (closing !== undefined) { return { crossing: false, focused: closing.focused, // A tab closes within its strip instead of shrinking with its window, and // a shown tab fades to the tab replacing it. motion: movesAsTab(closing.placement) ? "closing-tab" : "closing", placement: closing.placement, restack: undefined, rewound: 0, screen: closing.screen, shownRewound: 0, window, }; } else if (placed === undefined) { const sending = index.sending.get(window.id); return sending === undefined ? leavingWindow(index, window) : { crossing: false, focused: sending.focused, motion: sending.motion, placement: sending.placement, restack: undefined, rewound: sending.rewound, screen: sending.screen, shownRewound: sending.rewound, window, }; } else { const motion = arriving(playing, placed.screen, placed.placement); return { crossing: playing.crossing.has(window.id), focused: shown[placed.screen]?.activeId === window.id, motion, placement: placed.placement, restack: playing.restacking.find( (shuffle) => shuffle.restack.id === window.id && shuffle.motion === motion, )?.restack, ...rewoundIn(playing, placed.screen, window.id), screen: placed.screen, window, }; } }; /** * How far into its motion each part of window `id` on `screen` starts. * * A workspace switch outranks a drop (see {@link arriving}), so it takes the * switch's. Only a slide reads it, so a drop outranked by another motion can * keep it. One not dropping starts at the beginning. */ const rewoundIn = ( playing: Playing, screen: string, id: string, ): Pick => { const switching = playing.switching[screen]; if (switching === undefined) { const rewound = playing.dropping.find((dropping) => dropping.id === id)?.rewound ?? 0; return { rewound, shownRewound: rewound }; } else { const late = switching.late.find((window) => window.id === id); return { rewound: late?.placed ?? 0, shownRewound: late?.shown ?? 0 }; } }; /** * The motion of a window on `screen`. * * Arrival outranks a raise: a raise does not end a window's arrival. */ const arriving = ( playing: Playing, screen: string, placement: Placement, ): WindowMotion => { const { id } = placement; const switching = playing.switching[screen]; if (switching !== undefined) { return arrivalFrom(switching.towards); } else if (playing.opening.includes(id)) { return movesAsTab(placement) ? "opening-tab" : "opening"; } else if (playing.dropping.some((dropping) => dropping.id === id)) { return "dropping"; } else { return ( playing.tabbing.find((fade) => fade.id === id)?.motion ?? playing.restacking.find((shuffle) => shuffle.restack.id === id)?.motion ?? "resting" ); } }; /** * A window not on screen: either sliding off with its workspace or not drawn. * * Hidden windows (behind a tab, in the scratchpad, on an unseen workspace) stay * mounted to keep their surface and page alive. Windows under a fullscreen * window are not in this group: they keep their box and are covered. */ const leavingWindow = (index: Index, window: ShellWindow): DrawnWindow => { const leaving = index.leaving.get(window.id); if (leaving === undefined) { return { crossing: false, focused: false, motion: "resting", placement: undefined, restack: undefined, rewound: 0, screen: undefined, shownRewound: 0, window, }; } else { return { crossing: false, focused: leaving.left.activeId === window.id, motion: departureFor(leaving.left.towards), placement: leaving.placement, restack: undefined, rewound: 0, screen: leaving.screen, shownRewound: 0, window, }; } }; /** * The tabs on every screen, plus those of the workspace each screen is leaving. * * Tabs never open or close; they only slide with a workspace, or shuffle with * the float they are in, as the window naming them does. */ const drawnTabs = (shown: Desk, playing: Playing): readonly DrawnTab[] => Object.entries(shown).flatMap(([screen, { activeId, tabs }]) => { const left = playing.switching[screen]; return [ ...tabs.map((tab) => { const shuffle = left === undefined ? playing.restacking.find(({ restack }) => restack.id === tab.id) : undefined; return { focused: activeId === tab.id, motion: left === undefined ? (shuffle?.motion ?? "resting") : arrivalFrom(left.towards), restack: shuffle?.restack, screen, tab, }; }), ...(left === undefined ? [] : left.tabs.map((tab) => ({ focused: left.activeId === tab.id, motion: departureFor(left.towards), restack: undefined, screen, tab, }))), ]; });