import { useGT } from "gt-react"; import { Button } from "~/components/ui/button"; import { scopedThreadKey } from "@t3tools/client-runtime/environment"; import { memo, useDeferredValue, useEffect, useLayoutEffect, useRef, useState } from "react"; import { kickBrowserSurfaceTracking } from "../browser/browserSurfaceTracking"; import { ThreadRouteView } from "../components/ThreadRouteView"; import { launcherFocusTarget } from "../components/SurfaceLauncher"; import { observeResize } from "../lib/observeResize"; import { holdSurfaceFocusOrigin } from "../lib/workspaceFocus"; import { SidebarInset } from "../components/ui/sidebar"; import { StatusDot } from "../components/ui/status-dot"; import { useMediaQuery } from "../hooks/useMediaQuery"; import { useScrollingWorkspacesEnabled } from "../hooks/useSettings"; import { cn, isMacPlatform } from "../lib/utils"; import { scrollTabStripFromRow, sidewaysWheelScrollsStrip, wheelDeltaPx } from "../motion/gesture"; import { noteStripScroll, PaneMotionSnapshot, usePaneMotion } from "../motion/paneMotion"; import { ChatPanelAnimationSuppressionProvider, usePanelNavigationSuppression, } from "../panelAnimations"; import { endUserStripScroll, moveStripCamera, noteUserStripScroll, settleStripCamera, userStripScrollAge, } from "../motion/stripCamera"; import { applyWorkspaceStackTransform, attachWorkspaceSwitch, rememberWorkspaceCamera, useWorkspaceStack, workspaceCamera, workspaceStackCamera, type WorkspaceStack, } from "../motion/workspaceSwitch"; import { LayoutResizeHandles } from "./ColumnResizeHandle"; import { LayoutStripHint } from "./LayoutHint"; import { LayoutOnboarding } from "./LayoutOnboarding"; import { LayoutKeyboard } from "./LayoutShortcutsOverlay"; import { LayoutDropZones } from "./DropZones"; import { LayoutOverview, OverviewStageOverlay, onOverviewBackgroundPointerDown, overviewSlotStyle, overviewZoomTransition, selectOverviewStage, selectOverviewStartPaneIds, useOverviewPhase, useOverviewStage, } from "./Overview"; import { useLayoutBehavior } from "./layoutBehavior"; import { useSyncLayoutBehavior } from "./useSyncLayoutBehavior"; import { ChatPaneStandIn } from "./ChatPaneStandIn"; import { DeferredChatContext, chatHoldsLiveWork, queueChatRender, registerWaitingChat, renderWaitingChatNow, } from "./chatRenderGate"; import { useThreeFingerSwipe } from "./useThreeFingerSwipe"; import { ThreadSurfaceServices, ThreadSurfaceTab } from "../components/ThreadSurfaceTab"; import { findThreadChat, useLayoutSurfaceSync } from "./surfaceSync"; import { isLayoutPageContent, sameLayoutSpan, selectFocusedPane, selectFocusedTab, selectFocusedWorkspace, selectLayoutPaneSlots, selectLayoutStripSize, selectWindowStartPaneId, type LayoutColumn, type LayoutSpan, type LayoutSurfaceContent, type LayoutLauncherContent, type LayoutPaneSlot, type LayoutState, type LayoutTab, type LayoutViewport, type LayoutWorkspace, } from "./layoutModel"; import { holdFocusedPaneKeyboard, LayoutPaneContext, type LayoutPaneState, } from "./layoutPaneContext"; import { LayoutLauncherPane } from "./NewTabLauncher"; import { PaneThumbnail, PaneThumbnailServices } from "./PaneThumbnail"; import { useThreadStatus } from "../state/entities"; import { setLayoutStripViewportWidth, useLayoutStore } from "./layoutStore"; import { LayoutPageBody } from "./PageSurface"; /** * A pane counts as on screen (and its thread highlights in the sidebar) when * at least this share of its width is inside the window; a column peeking * at the edge does not. */ const ON_SCREEN_MIN_SHARE = 0.5; /** * In the overview, a chat not rendered yet renders once its thumbnail is at * least this wide; smaller ones draw their thumbnail (PaneThumbnail). */ const OVERVIEW_RENDER_MIN_PX = 120; /** * Besides the chats near the camera and the focused one, this many of the * most recently near stay rendered; the rest unmount. Each rendered chat * holds a ChatView's DOM and memory, and every focus move re-renders them. */ const RENDERED_RECENT_CHATS = 8; /** An off-screen chat past the cap unmounts once it has stayed past it this long. */ const UNRENDER_DELAY_MS = 1500; /** Set once the first chat pane of this page load has committed. */ let documentFirstFrameDrawn = false; /** A modal dialog is open; dialogs portal out of their chat, so any one holds the cap. */ const OPEN_DIALOG_SELECTOR = '[aria-modal="true"]:is([role="dialog"], [role="alertdialog"]), [data-slot$="dialog-popup"][data-open]'; /** The strip reports what is on screen once its scroll has settled this long. */ const ON_SCREEN_SCROLL_SETTLE_MS = 120; /** * A user's sideways gesture has ended once their wheel, swipe or drag has * been quiet this long; a busy frame can stall it longer than a settle. */ const USER_SCROLL_END_MS = 250; /** * A settled scroll is the user's when their gesture moved the strip this * recently; a fling keeps scrolling a while after the touch ends. */ const USER_SCROLL_SETTLE_WINDOW_MS = 2000; /** The first off-screen pane an agent opened on each side of the window. */ interface UnseenPeeks { left: string | null; right: string | null; } /** * A dot on the peeking edge for a pane an agent opened off screen (pen * uQRYP). Clicking it brings the pane in; scrolling to it clears it. */ function UnseenPeekDot({ side, tabId }: { side: "left" | "right"; tabId: string }) { const gt = useGT(); return ( ); } /** Pen R49C1s: neighboring panes share an 8px gutter, independent of the window inset. */ const HALF_PANE_GAP = "4px"; const FULL_RECT = { left: 0, top: 0, width: 1, height: 1 }; const FULL_SLOT: LayoutPaneSlot = { paneId: "", activeTabId: null, rect: FULL_RECT, hasLeft: false, hasRight: false, hasAbove: false, hasBelow: false, }; /** * A closed tab's pane sliding away where it was (motion/paneMotion.ts): a * chat or a launcher still drawn, or (a thread's surface, Settings, whose * content leaves with them) the flat surface they paint over their pane. */ type ClosingTab = { placed: { slot: LayoutPaneSlot; style: React.CSSProperties } } & ( | { kind: "chat"; tab: LayoutTab; paneId: string; target: Extract["target"]; } | { kind: "launcher"; context: LayoutLauncherContent["context"] } | { kind: "bare" } ); const NO_GHOSTS: ReadonlyMap = new Map(); /** * Pane edges land on whole pixels, so borders and text stay crisp at any split. * Both panes round the same boundary, so the gap between them stays exact. */ function slotStyle(slot: LayoutPaneSlot, stripPx?: number): React.CSSProperties { const edge = (has: boolean) => (has ? HALF_PANE_GAP : "0px"); const at = (fraction: number, offset: string) => `round(calc(${fraction * 100}% + ${offset}), 1px)`; const across = (fraction: number, offset: string) => stripPx === undefined ? at(fraction, offset) : `round(calc(${fraction * stripPx}px + ${offset}), 1px)`; const left = across(slot.rect.left, edge(slot.hasLeft)); const right = across(slot.rect.left + slot.rect.width, `-1 * ${edge(slot.hasRight)}`); const top = at(slot.rect.top, edge(slot.hasAbove)); const bottom = at(slot.rect.top + slot.rect.height, `-1 * ${edge(slot.hasBelow)}`); return { left, top, width: `calc(${right} - ${left})`, height: `calc(${bottom} - ${top})`, }; } /** One workspace in the stack a switch moves (see motion/workspaceSwitch.ts). */ interface StackRow { id: string; workspace: LayoutWorkspace | null; offset: number; slots: LayoutPaneSlot[]; stripWidth: number; /** Its horizontal scroll, in px. */ camera: number; } /** * `slotStyle` for a pane in a stacked row: the stage is one window wide, * the row sits `offset` rows (and gaps) down and shifts left by its camera. * At rest on a row it lands on exactly the strip's pixels. */ function stackSlotStyle(row: StackRow, slot: LayoutPaneSlot, gap: number): React.CSSProperties { const edge = (has: boolean) => (has ? HALF_PANE_GAP : "0px"); const across = (fraction: number, offset: string) => `round(calc(${fraction * row.stripWidth * 100}% - ${row.camera}px + ${offset}), 1px)`; const down = (fraction: number, offset: string) => `round(calc(${(row.offset + fraction) * 100}% + ${row.offset * gap}px + ${offset}), 1px)`; const left = across(slot.rect.left, edge(slot.hasLeft)); const right = across(slot.rect.left + slot.rect.width, `-1 * ${edge(slot.hasRight)}`); const top = down(slot.rect.top, edge(slot.hasAbove)); const bottom = down(slot.rect.top + slot.rect.height, `-1 * ${edge(slot.hasBelow)}`); return { left, top, width: `calc(${right} - ${left})`, height: `calc(${bottom} - ${top})` }; } /** * The panes a workspace shows: all of them, the maximized one alone, or * below md only its focused one, full size. */ function selectShownSlots( workspace: LayoutWorkspace, viewport: LayoutViewport, narrow: boolean, maximizedTabId: string | null, ): { slots: LayoutPaneSlot[]; stripWidth: number; stripSize: { share: number; minPx: number }; unitWidth: number; paneCount: number; } { const all = selectLayoutPaneSlots(workspace, viewport); const paneCount = all.slots.length; // A maximized pane fills the strip (pen O3SkD); below md only the focused one shows. const maximizedSlot = maximizedTabId === null ? null : (all.slots.find((slot) => slot.activeTabId === maximizedTabId) ?? null); if (paneCount < 2 || (!narrow && !maximizedSlot)) { return { ...all, paneCount, stripSize: selectLayoutStripSize(workspace, viewport.whenNoRoom) }; } const onlySlot = (narrow ? null : maximizedSlot) ?? all.slots.find((slot) => slot.paneId === workspace.focusedPaneId) ?? all.slots[0]!; return { paneCount: narrow ? 1 : paneCount, slots: [ { ...onlySlot, rect: FULL_RECT, hasLeft: false, hasRight: false, hasAbove: false, hasBelow: false, }, ], stripWidth: 1, stripSize: { share: 1, minPx: 0 }, unitWidth: viewport.width, }; } function selectStackRows( stack: WorkspaceStack, layout: LayoutState, viewport: LayoutViewport, narrow: boolean, maximizedTabId: string | null, ): StackRow[] { return stack.rows.map(({ id, offset }) => { // A row's workspace can be gone (an empty one left behind): it shows as empty. const workspace = layout.workspaces.find((candidate) => candidate.id === id) ?? null; if (!workspace || workspace.columns.length === 0) { return { id, workspace, offset, slots: [], stripWidth: 1, camera: 0 }; } const shown = selectShownSlots(workspace, viewport, narrow, maximizedTabId); const stripPx = shown.stripWidth * viewport.width; return { id, workspace, offset, slots: shown.slots, stripWidth: shown.stripWidth, camera: workspaceStackCamera(workspace, shown.slots, stripPx, viewport.width), }; }); } /** * A split pane narrower than this opens the right panel as a sheet, as a * window under the base's 980px breakpoint does. */ const LAYOUT_INLINE_RIGHT_PANEL_MIN_PX = 981; /** The strip's visible width in px, taken once a window or sidebar resize has settled. */ function useSettledWidth(ref: React.RefObject): number { const [width, setWidth] = useState(() => (typeof window === "undefined" ? 0 : window.innerWidth)); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Observes the strip DOM width; pane geometry follows a resize in CSS and React learns the width a frame after it stops. useLayoutEffect(() => { const element = ref.current; if (!element) return; setWidth(element.clientWidth); let resized = false; let frame: number | null = null; const settle = () => { if (resized) { resized = false; frame = window.requestAnimationFrame(settle); return; } frame = null; setWidth(element.clientWidth); }; const stopObserving = observeResize(element, () => { resized = true; if (frame === null) frame = window.requestAnimationFrame(settle); }); return () => { stopObserving(); if (frame !== null) window.cancelAnimationFrame(frame); }; }, [ref]); return width; } /** The focused pane's span across the strip, in px. */ interface StripSpan { left: number; width: number; } /** Preserve screen positions when the strip edge shifts, except at its start. Reveal focused panes * after its end moves without exposing trailing chrome. */ function useStripResizeScroll( scrollerRef: React.RefObject, stageRef: React.RefObject, focusedSpanRef: React.RefObject, active: boolean, ): void { // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Observe strip resizes and preserve its DOM scroll position before paint. useLayoutEffect(() => { const scroller = scrollerRef.current; if (!scroller || !active) return; let target = scroller.scrollLeft; let landed = target; let offset = 0; let owed = 0; let owedStage = 0; let { left, right, width } = scroller.getBoundingClientRect(); const content = () => stageRef.current?.getBoundingClientRect().width ?? scroller.scrollWidth; let slack = 0; const measureSlack = () => { slack = Math.max(0, target - (content() - width)); }; measureSlack(); const place = (next: number) => { if (next === offset) return; offset = next; scroller.style.left = next === 0 ? "" : `${next}px`; }; const adopt = () => { target = landed = scroller.scrollLeft; owed = 0; measureSlack(); place(0); }; const moved = () => Math.abs(scroller.scrollLeft - landed) >= 1; const onScroll = () => { // The clamp of a resize the observer has not seen yet: it starts from before it. if (scroller.getBoundingClientRect().width !== width) return; if (moved()) adopt(); }; // Runs after layout and before paint. const stopObserving = observeResize(scroller, () => { const box = scroller.getBoundingClientRect(); const boxLeft = box.left - offset; const boxRight = box.right - offset; if (box.width === width && boxLeft === left) return; const shift = boxLeft - left; const endMoved = Math.abs(boxRight - right) >= 0.5; const grew = box.width > width; left = boxLeft; right = boxRight; width = box.width; const now = scroller.scrollLeft; // Grown past the strip's end, the browser clamped the scroll before this ran. const clamped = grew && now < landed && now >= scroller.scrollWidth - scroller.clientWidth - 1; if (moved() && !clamped) adopt(); const stage = content(); if (owed > 0 && stage !== owedStage) owed = 0; const room = Math.max(0, stage - width) + slack; const wanted = target > 0 || owed > 0 ? target - owed + shift : 0; let next = Math.min(Math.max(0, wanted), room); owed = Math.max(0, -wanted); owedStage = stage; const focused = endMoved ? focusedSpanRef.current : null; if (focused) owed = 0; if (focused && focused.left < next - 1) next = focused.left; else if (focused && focused.left + focused.width > next + width + 1) { next = Math.min(focused.left, focused.left + focused.width - width); } target = next; if (Math.abs(next - scroller.scrollLeft) >= 0.01) { settleStripCamera(); scroller.scrollLeft = next; noteStripScroll(scroller); } landed = scroller.scrollLeft; const rest = landed - next; place(Math.abs(rest) < 0.01 ? 0 : rest); }); scroller.addEventListener("scroll", onScroll, { passive: true }); return () => { stopObserving(); scroller.removeEventListener("scroll", onScroll); place(0); }; }, [active, focusedSpanRef, scrollerRef, stageRef]); } /** Pure placement derivations stay separate from the host's retained transition state. */ function useSurfacePlacement( shown: LayoutState, workspace: LayoutWorkspace, viewportWidth: number, whenNoRoom: LayoutViewport["whenNoRoom"], narrow: boolean, maximizedTabId: string | null, windowWidth: number, ) { const overviewPhase = useOverviewPhase(); const workspaceStack = useWorkspaceStack(); const viewport = { width: viewportWidth, whenNoRoom }; const { slots, stripWidth, stripSize, unitWidth, paneCount } = selectShownSlots( workspace, viewport, narrow, maximizedTabId, ); const chatTabsById = (() => { const byId = new Map(); for (const candidate of shown.workspaces) { for (const column of candidate.columns) { for (const pane of column.panes) { for (const tab of pane.tabs) { if (tab.content.kind === "chat") byId.set(tab.id, { tab, paneId: pane.id }); } } } } return byId; })(); const launcherTabsById = (() => { const byId = new Map(); for (const candidate of shown.workspaces) { for (const column of candidate.columns) { for (const pane of column.panes) { for (const tab of pane.tabs) { if (tab.content.kind === "launcher") byId.set(tab.id, tab.content); } } } } return byId; })(); const surfaceTabsById = (() => { const byId = new Map(); for (const candidate of shown.workspaces) { for (const column of candidate.columns) { for (const pane of column.panes) { for (const tab of pane.tabs) { if (tab.content.kind === "surface") byId.set(tab.id, tab.content); } } } } return byId; })(); // The overview lays every workspace out on one stage (see Overview.tsx), // so the panes it shows are the live ones, zoomed out. const staged = overviewPhase !== "closed"; // The strip's width follows every window resize, so it keys the window's too. const stage = selectOverviewStage(shown, viewport, windowWidth); // A workspace switch in motion stacks the workspaces it moves between. const stacked = !staged && workspaceStack !== null; const stackRows = workspaceStack === null ? [] : selectStackRows(workspaceStack, shown, viewport, narrow, maximizedTabId); const slotByTabId = (() => { const byId = new Map(); if (stacked && workspaceStack) { for (const row of stackRows) { for (const slot of row.slots) { if (slot.activeTabId === null) continue; byId.set(slot.activeTabId, { slot, style: stackSlotStyle(row, slot, workspaceStack.gap), }); } } return byId; } if (staged) { for (const row of stage.rows) { for (const slot of row.slots) { if (slot.activeTabId === null) continue; byId.set(slot.activeTabId, { slot, style: overviewSlotStyle(row, slot, stage.gap, HALF_PANE_GAP), }); } } return byId; } for (const slot of slots) { if (slot.activeTabId !== null) byId.set(slot.activeTabId, { slot, style: slotStyle(slot) }); } return byId; })(); return { slots, stripWidth, stripSize, unitWidth, paneCount, chatTabsById, launcherTabsById, surfaceTabsById, overviewPhase, staged, stage, workspaceStack, stacked, stackRows, slotByTabId, }; } /** Keep chat tabs in one stable sibling layer. Moving a tab changes only its position, preserving * ChatView, terminals, drafts and scroll state. */ export function LayoutSurfaceHost({ layout, active = true, background = false, pagePlane, }: { layout: LayoutState; active?: boolean; background?: boolean; pagePlane?: React.ReactNode; }) { // A resize drag previews here and commits to the store once on release. const [preview, setPreview] = useState<{ layout: LayoutState; heldStripPx: number } | null>(null); const shown = preview?.layout ?? layout; const workspace = selectFocusedWorkspace(shown); const narrow = useMediaQuery("max-md"); useSyncLayoutBehavior(); const scrollingWorkspaces = useScrollingWorkspacesEnabled(); useThreeFingerSwipe(active && scrollingWorkspaces); useLayoutSurfaceSync(); const scrollerRef = useRef(null); const lastWorkspaceFocus = useRef(null); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Restore focus to the retained workspace element when the workspace returns. useLayoutEffect(() => { const element = lastWorkspaceFocus.current; if (active && element?.isConnected && element.closest('[data-layout-pane-focused="true"]')) { element.focus({ preventScroll: true }); } }, [active]); const viewportWidth = useSettledWidth(scrollerRef); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Publish the measured desktop strip width for imperative pane placement. useEffect(() => { // Below md one pane fills the window; columns stay arranged for the last // wide one. The first render's guess (the window's width) is not reported. if (narrow || scrollerRef.current?.clientWidth !== viewportWidth) return; setLayoutStripViewportWidth(viewportWidth); }, [narrow, viewportWidth]); const focusedPaneId = selectFocusedPane(shown)?.id ?? null; const maximizedTabId = useLayoutStore((state) => state.maximizedTabId); const { whenNoRoom } = useLayoutBehavior(); const { slots, stripWidth, stripSize, unitWidth, paneCount, chatTabsById, launcherTabsById, surfaceTabsById, overviewPhase, staged, stage, workspaceStack, stacked, stackRows, slotByTabId, } = useSurfacePlacement( shown, workspace, viewportWidth, whenNoRoom, narrow, maximizedTabId, window.innerWidth, ); // One pane is focused. A focused surface tab's chat is only related: it // answers its thread's pane toggles (cmd+J, alt+cmd+B) and holds the // composer the surface inserts into, without layout or DOM focus. const focusedTab = active ? selectFocusedTab(shown) : null; const focusedTabId = focusedTab?.id ?? null; const relatedChatTabId = focusedTab?.content.kind === "surface" ? (findThreadChat(shown, focusedTab.content.threadRef)?.tab.id ?? null) : null; // Mount newly visible tabs in the same render, then keep them mounted until close so workspace // switches preserve terminals, drafts and scroll. const [shownOrder, setShownOrder] = useState([]); const [windowStartPaneId, setWindowStartPaneId] = useState(null); const overviewStartPaneIds = staged ? selectOverviewStartPaneIds(stage, viewportWidth) : null; if (overviewStartPaneIds) { const landingStartPaneId = stage.focusedRow.slots.find((slot) => overviewStartPaneIds.has(slot.paneId))?.paneId ?? null; if (landingStartPaneId !== windowStartPaneId) setWindowStartPaneId(landingStartPaneId); } const atWindowStart = (paneId: string | undefined) => overviewStartPaneIds ? paneId !== undefined && overviewStartPaneIds.has(paneId) : windowStartPaneId === null ? undefined : paneId === windowStartPaneId; // A focused surface's chat mounts too, wherever it sits, so it answers the // surface's thread keys (cmd+J, alt+cmd+B) after a reload as before one. const visibleChatTabIds = [ ...new Set( [...slotByTabId.keys(), relatedChatTabId].flatMap((id) => id !== null && chatTabsById.has(id) ? [id] : [], ), ), ]; const visibleChatTabIdSet = new Set(visibleChatTabIds); const hiddenKept = shownOrder.filter( (id) => chatTabsById.has(id) && !visibleChatTabIdSet.has(id), ); const nextShownOrder = [...visibleChatTabIds, ...hiddenKept]; if ( nextShownOrder.length !== shownOrder.length || nextShownOrder.some((id, index) => id !== shownOrder[index]) ) { setShownOrder(nextShownOrder); } // Render in a stable order so a focus change never reorders DOM siblings. const mountedTabIds = [...nextShownOrder].toSorted(); // Size long stages in pixels; React learns viewport changes a frame late, and a proportional // stage would stretch panes and fling the camera. const stripPx = stripWidth * viewportWidth; const focusedSlot = slots.find((slot) => slot.paneId === focusedPaneId) ?? null; const focusedLeft = focusedSlot ? Math.round(focusedSlot.rect.left * stripPx) : null; const focusedWidth = focusedSlot ? Math.round(focusedSlot.rect.width * stripPx) : null; const focusedStart = focusedSlot?.rect.left ?? null; const focusedShare = focusedSlot?.rect.width ?? null; const dragging = preview !== null; // Where the strip's camera is, in half windows: it changes a couple of // times per window of scroll, not per frame, so scrolling re-renders the // host rarely. const [cameraStep, setCameraStep] = useState(0); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Subscribe to native scroll and schedule bounded render-window updates. useEffect(() => { const scroller = scrollerRef.current; if (!scroller) return; let frame: number | null = null; const update = () => { frame = null; setCameraStep(Math.floor(scroller.scrollLeft / Math.max(1, viewportWidth / 2))); }; const onScroll = () => { if (frame === null) frame = window.requestAnimationFrame(update); }; update(); scroller.addEventListener("scroll", onScroll, { passive: true }); return () => { if (frame !== null) window.cancelAnimationFrame(frame); scroller.removeEventListener("scroll", onScroll); }; }, [viewportWidth]); // The chats whose panes are within a window of the camera. A chat renders // the first time it comes near, so a strip of a hundred columns renders the // few on screen. const nearTabIds = (() => { const near = new Set(); const mark = (slot: LayoutPaneSlot) => { if (slot.activeTabId !== null) near.add(slot.activeTabId); }; if (staged) { // The overview renders the chats it shows big enough to read. for (const row of stage.rows) { for (const slot of row.slots) { const width = slot.rect.width * row.stripWidth * viewportWidth * stage.scale; if (width >= OVERVIEW_RENDER_MIN_PX) mark(slot); } } return near; } const add = (slot: LayoutPaneSlot, rowPx: number, camera: number) => { const left = slot.rect.left * rowPx; const right = left + slot.rect.width * rowPx; if (right < camera - viewportWidth || left > camera + 2 * viewportWidth) return; mark(slot); }; if (stacked) { for (const row of stackRows) { for (const slot of row.slots) add(slot, row.stripWidth * viewportWidth, row.camera); } return near; } for (const slot of slots) add(slot, stripPx, (cameraStep * viewportWidth) / 2); // Focus can land past the camera before the scroll reports it. if (focusedSlot) add(focusedSlot, stripPx, focusedSlot.rect.left * stripPx); return near; })(); // The chats near the camera, most recently first; the cap keeps the first // few that have left. Derived during render, as `shownOrder` is. const [recentChatIds, setRecentChatIds] = useState([]); const nextRecentChatIds = [ ...[...nearTabIds].filter((id) => chatTabsById.has(id)), ...recentChatIds.filter((id) => chatTabsById.has(id) && !nearTabIds.has(id)), ]; if ( nextRecentChatIds.length !== recentChatIds.length || nextRecentChatIds.some((id, index) => id !== recentChatIds[index]) ) { setRecentChatIds(nextRecentChatIds); } const keptTabIds = (() => { const kept = new Set(nearTabIds); if (focusedTabId !== null) kept.add(focusedTabId); if (relatedChatTabId !== null) kept.add(relatedChatTabId); let recent = 0; for (const id of recentChatIds) { if (recent >= RENDERED_RECENT_CHATS) break; if (kept.has(id)) continue; kept.add(id); recent += 1; } return kept; })(); const unseenTabIds = useLayoutStore((state) => state.unseenTabIds); const [unseenPeeks, setUnseenPeeks] = useState({ left: null, right: null }); const stageRef = useRef(null); const rootRef = useRef(null); // The switch's stack and the strip trade places: entering, the stage spans // one window and each row carries its own scroll; leaving, the strip // scrolls to where its row was drawn. Before any other effect reads the scroll. const wasStacked = useRef(false); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Transfer DOM scroll and transforms when entering or leaving the workspace stack. useLayoutEffect(() => { const scroller = scrollerRef.current; const element = stageRef.current; if (!scroller || !element) return; if (stacked) { for (const row of stackRows) rememberWorkspaceCamera(row.id, row.camera); if (!wasStacked.current) scroller.scrollLeft = 0; wasStacked.current = true; applyWorkspaceStackTransform(); return; } if (!wasStacked.current) return; wasStacked.current = false; // The overview took the stage mid-switch and places it itself. if (staged) return; element.style.transform = ""; scroller.scrollLeft = workspaceCamera(workspace.id) ?? 0; }, [stackRows, stacked, staged, workspace.id]); // Kept current for the switch, which reads them between renders. const stagedRef = useRef(staged); const backgroundRef = useRef(background); const stageGapRef = useRef(stage.gap); const stripShown = !staged && !stacked && !narrow && maximizedTabId === null && !dragging; const stripShownRef = useRef(stripShown); const focusedSpanRef = useRef(null); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Publish committed geometry and modal ownership for native gesture callbacks. useLayoutEffect(() => { stagedRef.current = staged; backgroundRef.current = background; stageGapRef.current = stage.gap; stripShownRef.current = stripShown; focusedSpanRef.current = focusedLeft === null || focusedWidth === null ? null : { left: focusedLeft, width: focusedWidth }; }); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Attach workspace gestures to the mounted strip DOM and release them on cleanup. useEffect(() => { const root = rootRef.current; const scroller = scrollerRef.current; const element = stageRef.current; if (!scrollingWorkspaces || !root || !scroller || !element) return; return attachWorkspaceSwitch({ root, scroller, stage: element, gap: () => stageGapRef.current, blocked: () => stagedRef.current || backgroundRef.current, stripShown: () => stripShownRef.current, }); }, [backgroundRef, scrollingWorkspaces, stageGapRef, stagedRef, stripShownRef]); // Before the reveal below: leaving the overview hands the strip its scroll // back first, so the reveal finds the focused pane in place, not a strip // still at 0 that it would spring sideways. useOverviewStage(overviewPhase, stage, scrollerRef, stageRef); const revealedSpan = useRef(null); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Reveal the committed focused pane with an imperative camera move after measuring its DOM. useLayoutEffect(() => { const scroller = scrollerRef.current; const element = stageRef.current; if (!scroller || !element || staged || stacked || dragging || focusedPaneId === null) { revealedSpan.current = null; return; } if ( focusedLeft === null || focusedWidth === null || focusedStart === null || focusedShare === null ) { return; } const span = { paneId: focusedPaneId, left: focusedStart, width: focusedShare }; // The first render sizes the strip for the window's width, before the // scroller is measured, and a scroll then clamps short of the pane. The // render with the measured width reveals it. if (scroller.clientWidth !== viewportWidth) return; if (revealedSpan.current && sameLayoutSpan(revealedSpan.current, span)) return; const left = focusedLeft; const right = left + focusedWidth; return afterPressRelease(() => { revealedSpan.current = span; const viewLeft = scroller.scrollLeft; const viewRight = viewLeft + scroller.clientWidth; let target: number | null = null; if (left < viewLeft - 1) target = left; else if (right > viewRight + 1) target = Math.min(left, right - scroller.clientWidth); if (target === null) return; // The scroll lands at once and the stage springs there on the compositor, // so the composer's caret scroll, right after, finds the pane in place. moveStripCamera(scroller, element, target); }); }, [ dragging, focusedLeft, focusedPaneId, focusedShare, focusedStart, focusedWidth, stacked, staged, viewportWidth, ]); useStripResizeScroll( scrollerRef, stageRef, focusedSpanRef, !staged && !stacked && !narrow && maximizedTabId === null, ); // alt+cmd+C: center the focused column in the window (a one-shot scroll). const centerRequest = useLayoutStore((state) => state.centerRequest); const handledCenterRequest = useRef(centerRequest); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Scroll the DOM strip in response to an external center request. useLayoutEffect(() => { if (handledCenterRequest.current === centerRequest) return; handledCenterRequest.current = centerRequest; const scroller = scrollerRef.current; const element = stageRef.current; if (!scroller || !element || focusedLeft === null || focusedWidth === null) return; const center = focusedLeft + focusedWidth / 2; const target = Math.max( 0, Math.min(stripPx - scroller.clientWidth, center - scroller.clientWidth / 2), ); if (Math.abs(target - scroller.scrollLeft) < 1) return; moveStripCamera(scroller, element, target); }, [centerRequest, focusedLeft, focusedWidth, stripPx]); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Extend strip wheel input into the window chrome above the workspace. useEffect(() => { const root = rootRef.current; const scroller = scrollerRef.current; const stage = stageRef.current; if (!active || staged || stacked || !root || !scroller || !stage) return; const chrome = document.createElement("div"); chrome.dataset.layoutStripChrome = ""; chrome.setAttribute("aria-hidden", "true"); chrome.className = "drag-region fixed top-0 hidden md:block"; const update = () => { const box = scroller.getBoundingClientRect(); chrome.style.left = `${box.left}px`; chrome.style.width = `${box.width}px`; chrome.style.height = `${Math.max(0, box.top)}px`; chrome.toggleAttribute( "data-tab-row-overflow", (scrollingWorkspaces && scroller.scrollWidth > scroller.clientWidth) || root.querySelector("[data-tab-row-overflow]") !== null, ); }; const wheel = (event: WheelEvent) => { if (event.ctrlKey || event.metaKey || event.altKey) return; const delta = event.shiftKey && event.deltaX === 0 ? event.deltaY : event.deltaX; if (!delta || (!event.shiftKey && Math.abs(delta) <= Math.abs(event.deltaY))) return; const header = [...scroller.querySelectorAll("[data-pane-header]")].find( (row) => { const box = row.getBoundingClientRect(); return ( box.height > 0 && box.top <= scroller.getBoundingClientRect().top + 1 && box.left <= event.clientX && box.right > event.clientX ); }, ); const tabs = header?.querySelector("[data-layout-tab-strip]"); if (tabs) scrollTabStripFromRow(event, tabs); if ( event.defaultPrevented || !scrollingWorkspaces || scroller.scrollWidth <= scroller.clientWidth ) return; event.preventDefault(); settleStripCamera(); noteUserStripScroll(); scroller.scrollLeft += wheelDeltaPx(delta, event); }; root.parentElement?.append(chrome); const stop = observeResize([root, scroller, stage], update); const observer = new MutationObserver(update); observer.observe(root, { subtree: true, attributes: true, attributeFilter: ["data-tab-row-overflow"], }); chrome.addEventListener("wheel", wheel, { passive: false }); update(); return () => { stop(); observer.disconnect(); chrome.remove(); }; }, [active, scrollingWorkspaces, staged, stacked]); // Hold alt+cmd and the strip follows the wheel sideways and a middle-button // drag, wherever the pointer is (pen ZlxmK). Up and down switch workspaces // (motion/workspaceSwitch.ts). Any sideways scroll of the user's takes the // camera over from a spring in flight. // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Own wheel and swipe subscriptions on the mounted workspace scroller. useEffect(() => { const scroller = scrollerRef.current; if (!scrollingWorkspaces || !scroller) return; const mac = isMacPlatform(navigator.platform); const held = (event: MouseEvent) => mac ? event.metaKey && event.altKey : event.ctrlKey && event.altKey; const moveStrip = (event: WheelEvent, delta: number) => { event.preventDefault(); settleStripCamera(); noteUserStripScroll(); scroller.scrollLeft += wheelDeltaPx(delta, event); }; const onWheel = (event: WheelEvent) => { if (held(event) && Math.abs(event.deltaX) > Math.abs(event.deltaY)) moveStrip(event, event.deltaX); }; const onSidewaysWheel = (event: WheelEvent) => { if (event.defaultPrevented) return; const shifted = event.shiftKey && event.deltaX === 0; const delta = shifted ? event.deltaY : event.deltaX; if ( sidewaysWheelScrollsStrip(event.composedPath(), scroller, delta, shifted ? 0 : event.deltaY) ) { moveStrip(event, delta); } }; const onPointerDown = (event: PointerEvent) => { if (event.button !== 1 || !held(event)) return; event.preventDefault(); settleStripCamera(); const startX = event.clientX; const startLeft = scroller.scrollLeft; const move = (moveEvent: PointerEvent) => { noteUserStripScroll(); scroller.scrollLeft = startLeft - (moveEvent.clientX - startX); }; const end = () => { window.removeEventListener("pointermove", move, true); window.removeEventListener("pointerup", end, true); window.removeEventListener("pointercancel", end, true); }; window.addEventListener("pointermove", move, true); window.addEventListener("pointerup", end, true); window.addEventListener("pointercancel", end, true); }; // A finger drags the scroll itself, so it lands the camera where it shows. const onTouchMove = () => { settleStripCamera(); noteUserStripScroll(); }; scroller.addEventListener("wheel", onWheel, { passive: false, capture: true }); scroller.addEventListener("wheel", onSidewaysWheel, { passive: false }); scroller.addEventListener("pointerdown", onPointerDown, true); scroller.addEventListener("touchmove", onTouchMove, { passive: true }); return () => { scroller.removeEventListener("wheel", onWheel, true); scroller.removeEventListener("wheel", onSidewaysWheel); scroller.removeEventListener("pointerdown", onPointerDown, true); scroller.removeEventListener("touchmove", onTouchMove); }; }, [scrollingWorkspaces]); // The pane each column focuses when focus follows the view back to it. const lastFocusedPaneByColumn = useRef(new Map()); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Remember the committed focused pane for later native scroll focus restoration. useEffect(() => { if (focusedPaneId === null) return; const column = workspace.columns.find((candidate) => candidate.panes.some((pane) => pane.id === focusedPaneId), ); if (column) lastFocusedPaneByColumn.current.set(column.id, focusedPaneId); }, [focusedPaneId, workspace]); // The sidebar keeps every thread on screen from receding. Derived from // the slots and the strip's settled scroll position, never per frame. // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to settled strip scrolling and publishes the DOM-visible panes to the shared layout registry useEffect(() => { if (!active) { useLayoutStore.getState().setOnScreenThreadKeys(new Set()); useLayoutStore.getState().setScrolledOffTabIds(new Set()); return; } // Staged and stacked panes do not sit on a scrolled strip. if (staged || stacked) useLayoutStore.getState().setScrolledOffTabIds(new Set()); if (dragging || staged || stacked) return; const scroller = scrollerRef.current; // Focus follows the view, as in niri: once a sideways scroll of the // user's settles with the focused pane under half on screen, the column // most in view takes focus, with the pane it last focused. const followView = (viewStart: number, viewEnd: number) => { const store = useLayoutStore.getState(); const { columns, focusedPaneId: focusedId } = selectFocusedWorkspace(store.layout); const slotByPane = new Map(slots.map((slot) => [slot.paneId, slot])); const inside = (slot: LayoutPaneSlot) => Math.min(slot.rect.left + slot.rect.width, viewEnd) - Math.max(slot.rect.left, viewStart); const focused = focusedId === null ? undefined : slotByPane.get(focusedId); if (!focused) return; const share = Math.min(focused.rect.width, viewEnd - viewStart) * ON_SCREEN_MIN_SHARE; if (inside(focused) >= share) return; let best: LayoutColumn | null = null; let bestInside = 0; for (const column of columns) { const slot = column.panes[0] ? slotByPane.get(column.panes[0].id) : undefined; const shown = slot ? inside(slot) : 0; if (shown > bestInside) { best = column; bestInside = shown; } } if (!best) return; const remembered = lastFocusedPaneByColumn.current.get(best.id); const fromColumn = columns.find((column) => column.panes.some((pane) => pane.id === focusedId), ); const index = fromColumn?.panes.findIndex((pane) => pane.id === focusedId) ?? 0; const pane = best.panes.find((candidate) => candidate.id === remembered) ?? best.panes[Math.min(Math.max(index, 0), best.panes.length - 1)]!; store.focusPane(pane.id); }; const compute = (settled = false) => { // In shares of the strip, which the window may clip on the right. const measured = scroller !== null && stripPx > 0; const viewStart = measured ? scroller.scrollLeft / stripPx : 0; const viewEnd = measured ? (scroller.scrollLeft + scroller.clientWidth) / stripPx : 1; if (settled && measured && userStripScrollAge() <= USER_SCROLL_SETTLE_WINDOW_MS) { endUserStripScroll(); followView(viewStart, viewEnd); } const keys = new Set(); const scrolledOff = new Set(); const seen: string[] = []; const peeks: UnseenPeeks = { left: null, right: null }; for (const slot of slots) { if (slot.activeTabId === null) continue; const inside = Math.min(slot.rect.left + slot.rect.width, viewEnd) - Math.max(slot.rect.left, viewStart); const unseen = unseenTabIds.has(slot.activeTabId); if (inside < slot.rect.width * ON_SCREEN_MIN_SHARE) { scrolledOff.add(slot.activeTabId); if (unseen) { const side = slot.rect.left < viewStart ? "left" : "right"; if (peeks[side] === null) peeks[side] = slot.activeTabId; } continue; } if (unseen) seen.push(slot.activeTabId); // A surface tab on screen counts for its thread, as its chat would. const surface = surfaceTabsById.get(slot.activeTabId); const chat = chatTabsById.get(slot.activeTabId); if (surface) keys.add(scopedThreadKey(surface.threadRef)); else if (chat?.tab.content.kind === "chat" && chat.tab.content.target.kind === "server") { keys.add(scopedThreadKey(chat.tab.content.target.threadRef)); } } useLayoutStore.getState().setOnScreenThreadKeys(keys); useLayoutStore.getState().setScrolledOffTabIds(scrolledOff); useLayoutStore.getState().markTabsSeen(seen); setUnseenPeeks((current) => current.left === peeks.left && current.right === peeks.right ? current : peeks, ); setWindowStartPaneId(selectWindowStartPaneId(slots, viewStart * stripPx, stripPx)); }; compute(); if (!scroller) return; let timer: number | null = null; const settle = () => { // The user's gesture may still be going between its scroll events. const age = userStripScrollAge(); if (age < USER_SCROLL_END_MS) { timer = window.setTimeout(settle, USER_SCROLL_END_MS - age); return; } timer = null; compute(true); }; const onScroll = () => { if (timer !== null) window.clearTimeout(timer); timer = window.setTimeout(settle, ON_SCREEN_SCROLL_SETTLE_MS); }; scroller.addEventListener("scroll", onScroll, { passive: true }); return () => { if (timer !== null) window.clearTimeout(timer); scroller.removeEventListener("scroll", onScroll); }; }, [ active, chatTabsById, surfaceTabsById, dragging, slots, stacked, staged, stripPx, unseenTabIds, ]); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- clears the shared on-screen thread registry when the strip unmounts useEffect( () => () => { useLayoutStore.getState().setOnScreenThreadKeys(new Set()); useLayoutStore.getState().setScrolledOffTabIds(new Set()); }, [], ); const surfaceSlots = [...slotByTabId].flatMap(([tabId, placed]) => { const content = surfaceTabsById.get(tabId); return content ? [{ placed, tabId, content }] : []; }); const launcherSlots = [...slotByTabId].flatMap(([tabId, placed]) => { const content = launcherTabsById.get(tabId); return content ? [{ placed, tabId, content }] : []; }); // Mid-switch the panes of the workspace being left are staged. const focusedWorkspacePaneIds = new Set( workspace.columns.flatMap((column) => column.panes.map((pane) => pane.id)), ); const still = (paneId: string | undefined) => staged || (stacked && (paneId === undefined || !focusedWorkspacePaneIds.has(paneId))); // Panes open, close and shift with niri's window motion (motion/paneMotion.ts) // on the plain strip; a resize drag, a switch, the overview, filling the // window and the window resizing move them 1:1. const paneMotionKey = staged || stacked || dragging || narrow ? null : `${workspace.id}:${maximizedTabId !== null}`; const [ghosts, setGhosts] = useState>(NO_GHOSTS); const presentPanes = shown.workspaces.flatMap((candidate) => candidate.columns.flatMap((column) => column.panes), ); const presentTabIds = new Set(presentPanes.flatMap((pane) => pane.tabs.map((tab) => tab.id))); const presentPaneIds = new Set(presentPanes.map((pane) => pane.id)); const tabIdsKey = [...presentTabIds].join(); // The tabs and their places as last drawn; a resize drag leaves them be. const [seen, setSeen] = useState({ key: tabIdsKey, chats: chatTabsById, launchers: launcherTabsById, placed: slotByTabId, stripPx, motionKey: paneMotionKey, framed: paneCount > 1 || maximizedTabId !== null, }); if ( !dragging && (seen.key !== tabIdsKey || seen.placed !== slotByTabId || seen.motionKey !== paneMotionKey) ) { setSeen({ key: tabIdsKey, chats: chatTabsById, launchers: launcherTabsById, placed: slotByTabId, stripPx, motionKey: paneMotionKey, framed: paneCount > 1 || maximizedTabId !== null, }); // A pane closed from the strip leaves a ghost behind; the window's lone // pane has nothing beside it to slide under, it just goes. if ( seen.key !== tabIdsKey && seen.framed && paneMotionKey !== null && seen.motionKey === paneMotionKey ) { const closed: [string, ClosingTab][] = []; for (const [tabId, was] of seen.placed) { if (presentTabIds.has(tabId) || presentPaneIds.has(was.slot.paneId) || ghosts.has(tabId)) { continue; } const placed = { slot: was.slot, style: slotStyle(was.slot, seen.stripPx) }; const chat = seen.chats.get(tabId); const launcher = seen.launchers.get(tabId); if (chat?.tab.content.kind === "chat") { closed.push([tabId, { kind: "chat", ...chat, target: chat.tab.content.target, placed }]); } else if (launcher) { closed.push([tabId, { kind: "launcher", context: launcher.context, placed }]); } else { closed.push([tabId, { kind: "bare", placed }]); } } if (closed.length > 0) setGhosts((current) => new Map([...current, ...closed])); } } if (paneMotionKey === null && ghosts.size > 0) setGhosts(NO_GHOSTS); const dropGhost = (tabId: string) => setGhosts((current) => { if (!current.has(tabId)) return current; const next = new Map(current); next.delete(tabId); return next; }); // Maximized, the lone pane keeps its split look so its tabs stay on the chrome. // Stacked mid-switch, every pane draws its own, as in the overview. While // a closed pane slides off, the one left keeps it too, until it rests. const split = paneCount > 1 || maximizedTabId !== null || stacked || ghosts.size > 0; usePaneMotion( stageRef, paneMotionKey, paneCount > 1 || maximizedTabId !== null || ghosts.size > 0, dropGhost, [slotByTabId, ghosts], viewportWidth, ); // Browser webviews float over their slots: after anything that may move a // pane (a layout change, a switch, the overview, pane motion), they follow, // from this very frame. After the motion above, so they start where it does. // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Reposition external browser surfaces after committed pane geometry and transforms change. useLayoutEffect(() => kickBrowserSurfaceTracking({ now: true })); const paneStyleById = new Map([ ...shown.workspaces.flatMap((candidate) => { if (candidate.columns.length === 0) return []; const own = selectShownSlots( candidate, { width: viewportWidth, whenNoRoom }, narrow, maximizedTabId, ); return own.slots.map((slot) => { const { width, height } = slotStyle(slot, own.stripWidth * viewportWidth); return [slot.paneId, { width, height }] as const; }); }), ...[...slotByTabId.values()].map( ({ slot, style }) => [slot.paneId, { width: style.width, height: style.height }] as const, ), ]); const renderedTabIds = [ ...new Set([ ...mountedTabIds, ...[...ghosts].flatMap(([id, ghost]) => ghost.kind === "chat" && !chatTabsById.has(id) ? [id] : [], ), ]), ].toSorted(); const goneRows = stacked && workspaceStack ? stackRows .filter((row) => row.slots.length === 0) .map((row) => ({ id: row.id, style: stackSlotStyle(row, FULL_SLOT, workspaceStack.gap), })) : []; const pageTabs = shown.workspaces.flatMap((candidate) => candidate.columns.flatMap((column) => column.panes.flatMap((pane) => pane.tabs.filter((tab) => isLayoutPageContent(tab.content))), ), ); return (
{ if (active && event.target instanceof HTMLElement) { lastWorkspaceFocus.current = event.target; } }} onPointerDown={staged ? onOverviewBackgroundPointerDown : undefined} className={cn( "relative min-h-0 flex-1 overscroll-x-none", !active && "pointer-events-none absolute inset-0 invisible", // Panes round their own cuts against the scrolling strip (PaneCuts); // a workspace switch rounds the rows it slides past the window. split && stacked && "rounded-pane", // Stacked, nothing may scroll it: a composer taking focus in the // row below would scroll that row into view under the motion. stacked ? "overflow-clip" : stripWidth > 1 && !staged ? "overflow-x-auto overflow-y-hidden [scrollbar-width:none] [&::-webkit-scrollbar]:hidden" : "overflow-hidden", )} > {staged ? ( // The window chrome behind the zoomed-out stage (pen rg38s).
) : null}
{renderedTabIds.map((tabId) => { const entry = chatTabsById.get(tabId); const ghost = entry ? null : ghosts.get(tabId); if (ghost?.kind === "chat") { return ( ); } if (!entry || entry.tab.content.kind !== "chat") return null; return ( ); })} {pageTabs.map((tab) => isLayoutPageContent(tab.content) ? ( ) : null, )} {launcherSlots.map(({ placed, tabId, content }) => ( ))} {[...ghosts].map(([tabId, ghost]) => ghost.kind === "launcher" ? ( ) : ghost.kind === "bare" ? (
) : null, )} {surfaceSlots.map(({ placed, tabId, content }) => ( ))} {goneRows.map((row) => ( ))} {staged ? ( ) : null} {split && slots.length > 1 && !staged && !stacked ? ( ) : null}
{preview ? (
) : null}
{pagePlane} {active ? ( <> {scrollingWorkspaces ? : null} {scrollingWorkspaces ? : null} {unseenPeeks.left && !stacked ? ( ) : null} {unseenPeeks.right && !stacked ? ( ) : null} ) : null} ); } function LayoutChatSurface({ tab, paneId, target, placed, paneStyle, near, kept, focused, background = false, related, atWindowStart, split, staged, overview, narrow, closing = false, active = true, }: { tab: LayoutTab; paneId: string; target: Extract["target"]; placed: { slot: LayoutPaneSlot; style: React.CSSProperties } | null; paneStyle?: React.CSSProperties | undefined; /** Within a window of the camera: a chat not rendered yet renders now. */ near: boolean; /** Near, focused or recently near: a rendered chat stays rendered. */ kept: boolean; focused: boolean; background?: boolean; /** A surface tab of its thread has focus (see `LayoutPaneState.related`). */ related: boolean; /** Whether this pane starts at the window's left edge; unset reads the slot. */ atWindowStart: boolean | undefined; split: boolean; /** Under the overview or off the focused workspace mid-switch: draws its own pane. */ staged: boolean; /** Under the overview: inert, and a chat not rendered yet draws its thumbnail. */ overview: boolean; narrow: boolean; /** Closed: a ghost sliding away, gone once its animation ends. */ closing?: boolean; active?: boolean; }) { const slot = placed?.slot ?? null; // Focus that lands here from the keyboard (alt+cmd arrows, closing a // surface tab) moves the caret into the composer. A press already put it // where the user wanted. const surfaceRef = useRef(null); // The palette puts the caret back itself, through the composer handle. usePaneKeyboard(surfaceRef, focused, chatKeyboardTarget, false); const visible = slot !== null; const leadingEdge = slot ? (atWindowStart ?? (!slot.hasLeft && !slot.hasAbove)) : true; const trailingEdge = slot ? !slot.hasRight && !slot.hasAbove : true; const paneState = { tabId: tab.id, paneId, // Staged, every pane draws its own; a window-fixed header cluster would // land in the stage's corner, over whichever workspace sits on top. split: visible && (split || staged), narrow: visible && narrow, leadingEdge, trailingEdge, focused: focused && !background, related: related && !background, visible: active && visible, } satisfies LayoutPaneState; // A press inside a pane makes it the focused pane; the URL follows. Focus // events are left alone so a pane's own autofocus cannot steal focus. const claimFocus = (event: React.PointerEvent) => { if (!(event.target instanceof Node) || !event.currentTarget.contains(event.target)) return; claimPaneFocus(tab.id); renderWaitingChatNow(tab.id); }; return ( ); } /** * Memoized so a resize drag, which restyles every surface each frame, does * not re-render the ChatViews inside them. */ const LayoutChatSurfaceBody = memo(function LayoutChatSurfaceBody({ paneState, tab, target, background, near, kept, thumbnail, }: { paneState: LayoutPaneState; tab: LayoutTab; target: Extract["target"]; background: boolean; near: boolean; kept: boolean; /** Under the overview: a chat not rendered yet draws its thumbnail at once. */ thumbnail: boolean; }) { const [paintedTarget, setPaintedTarget] = useState(target); const [request, setRequest] = useState({ target, deferred: false }); if (request.target !== target) { setRequest({ target, deferred: paintedTarget !== request.target, }); } // An isolated switch renders immediately; only a chat already behind navigation yields. const deferredTarget = useDeferredValue(target); const [inputTarget, setInputTarget] = useState(target); // Draft handoffs retain their synchronous editor lifetime. const shownTarget = !request.deferred || inputTarget === target || target.kind === "draft" || deferredTarget.kind === "draft" ? target : deferredTarget; // Panel motion holds off for this chat's own first frame and thread swaps. // The window-wide suppression flips on every navigation, and every focus // move navigates, so it re-rendered every mounted chat twice per keypress. const panelAnimationsSuppressed = usePanelNavigationSuppression( shownTarget.kind === "draft" ? `draft:${shownTarget.draftId}` : scopedThreadKey(shownTarget.threadRef), ); // Paint the pane before rendering ChatView in a separate task. Avoid transitions, whose // interruption can destroy the composer editor beneath the pane. The first frame after a // load has no pane to hold steady, so the focused chat renders in it. const wanted = paneState.visible && near; const [rendered, setRendered] = useState( () => !documentFirstFrameDrawn && wanted && (paneState.focused || paneState.related), ); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- A committed chat must reach a browser frame before the next jump counts as isolated. useLayoutEffect(() => { if (!rendered) return; const frame = window.requestAnimationFrame(() => setPaintedTarget(shownTarget)); return () => window.cancelAnimationFrame(frame); }, [rendered, shownTarget]); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Mark the document's first frame as committed so later panes keep the stand-in. useEffect(() => { documentFirstFrameDrawn = true; }, []); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Schedule live chat mounting through the shared render queue and cancel stale requests. useEffect(() => { if (rendered || !wanted) return; return queueChatRender( () => setRendered(true), paneState.focused || paneState.related ? "focused" : "normal", ); }, [paneState.focused, paneState.related, rendered, wanted]); // Past the cap, it unmounts once the user has moved on. Its draft, scroll // position, side pane and terminal live outside it and come back with it. // Never while it has the keyboard, live work or an open dialog. const { tabId } = paneState; // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Delay offscreen chat unmounting while DOM focus, dialogs or provider work still hold it. useEffect(() => { if (kept || !rendered) return; let timer = 0; const unrender = () => { const typing = document.activeElement?.closest("[data-layout-tab-id]"); const busy = typing?.getAttribute("data-layout-tab-id") === tabId || document.querySelector(OPEN_DIALOG_SELECTOR) !== null || chatHoldsLiveWork(target); if (busy) timer = window.setTimeout(unrender, UNRENDER_DELAY_MS * 4); else setRendered(false); }; timer = window.setTimeout(unrender, UNRENDER_DELAY_MS); return () => window.clearTimeout(timer); }, [kept, rendered, tabId, target]); // Prepare hidden thumbnails before overview opens. Keep them over chats until their own thread // loads, preventing empty or borrowed timelines. const [drawn, setDrawn] = useState(false); if (!rendered && thumbnail && !drawn) setDrawn(true); if (rendered && !thumbnail && drawn) setDrawn(false); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Schedule offscreen thumbnail construction through the bounded render queue. useEffect(() => { if (rendered || drawn || near) return; return queueChatRender(() => setDrawn(true), "later"); }, [drawn, near, rendered]); const covering = rendered && drawn; const threadStatus = useThreadStatus( covering && target.kind === "server" ? target.threadRef : null, ); const loaded = target.kind === "draft" || (threadStatus !== "empty" && threadStatus !== "synchronizing"); // A turn after it loads, so the chat has painted its timeline under it. // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Release the thumbnail cover only after the loaded chat has had a turn to paint. useEffect(() => { if (!covering || !loaded) return; return queueChatRender(() => setDrawn(false), "normal"); }, [covering, loaded]); // Input must land on the selected thread even before its deferred render commits. // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Publish the synchronous input gate before any key or pointer can reach the previous chat. useLayoutEffect(() => { if ((rendered && shownTarget === target) || tabId === null) return; return registerWaitingChat(tabId, () => { setInputTarget(target); setRendered(true); }); }, [rendered, shownTarget, tabId, target]); return ( {rendered ? ( ) : null} {/* Near on the strip it is about to render: its stand-in, as before. */} {drawn && (thumbnail || !near) ? (