/** * The plugin's reusable PERSISTENT floating window: a draggable, resizable, * body-portaled card with its own title bar, close button and scrolling body. * * Why a second window primitive next to {@link AnchoredPopover}: that one is a * POPOVER — it is anchored to an element, sized to its content, and dismissed * by any interaction outside it (outside mousedown, window blur, its anchor * leaving the viewport). That contract is exactly wrong for a surface the * reader WATCHES for minutes: a job's output must survive clicking elsewhere, * must not grow past the viewport, and must keep its place while the work * runs. This component therefore carries the opposite lifecycle — only the * close control and Escape end it — plus the two interactions a watched pane * needs: move it out of the way, and resize it to the log at hand. * * Since S6 it is also the SHELL of the public window host: * - geometry can be controlled (`box` + `onBoxChange`) so the host keeps the * reader's placement across content updates; * - maximize/restore is built in (controlled through `maximized` + * `onMaximizeChange`, or owned internally by the legacy callers); * - `onBeforeClose` is the draft guard for the close button AND Escape; * - z-order and Escape are shared with every other mounted window through * {@link useWindowLayer}: Escape closes only the TOP window, and a pointer * or focus raise brings a window above its siblings; * - the title bar carries identity/navigation/window controls only; resource * actions render on a toolbar row below it, and primary actions stay in the * stable footer. * * Mechanics reuse the two proven patterns of this codebase rather than * inventing a third: the MOVE rides window-level pointer listeners (the * `AnchoredPopover` drag — capture on the container would retarget the * derived click and kill the buttons inside), and the RESIZE rides * `setPointerCapture` on the handle plus a `createFrameBatcher` per-frame * commit (the `EditorHost` docked-panel resize; a per-event setState on a * streamed log is the visible drag lag of #315). * * Skin contract: tokens only (`--dsw-alias-*` / `--dsw-font-*`), the floating * layer uses a stronger 2px edge and 6px corners to separate it from the page. */ import { Fragment, isValidElement, useCallback, useEffect, useMemo, useRef, useState, type PointerEvent as ReactPointerEvent, type ReactNode, type RefObject, } from 'react' import { createPortal } from 'react-dom' import { IconCloseFillRegular, IconFullscreenOutlineRegular } from '@deepseek-ai/dsh-client-ui-primitives' import { ActionButton, IconButton } from './ui/index.ts' import { createFrameBatcher } from './frame-batcher.ts' import { isNarrowWidth } from './breakpoints.ts' import { useViewportInsetTop } from './viewport-inset.ts' import { WINDOW_MIN_HEIGHT, WINDOW_MIN_WIDTH, WINDOW_VIEWPORT_MARGIN, clampNumber, clampWindowBox, maximizedWindowBox, placeWindowBox, resizeWindowFromEdge, type WindowBox, } from './windows/geometry.ts' import { useWindowLayer } from './windows/layer.ts' import { t } from './locales.ts' import design from './ui/design.module.css' import css from './FloatingWindow.module.css' /** Default share of the viewport a fresh (legacy, unchecked) window takes. */ const DEFAULT_WIDTH_RATIO = 0.7 const DEFAULT_HEIGHT_RATIO = 0.6 export type { WindowBox } export interface FloatingWindowProps { /** Title-bar text (also the dialog's accessible name). */ title: string /** Close (the button, or Escape). */ onClose(): void /** Initial size; omitted → 70% × 60% of the viewport, then clamped. */ initialSize?: { width: number; height: number } /** Resize floor; omitted → 320 × 240. */ minSize?: { width: number; height: number } /** * Where the window first appears: just below-right of this element (the * row that opened it). Omitted / off-screen anchors centre it. */ anchor?: HTMLElement | null /** * Toolbar actions (resource actions), left of the window controls. The * legacy `actions` prop is the same row — the title bar itself carries only * identity, navigation and window controls. */ actions?: ReactNode /** Explicit toolbar contents; wins over `actions` when both are given. */ toolbar?: ReactNode /** Bottom action row (a job's follow switch + stop control). */ footer?: ReactNode /** * The scrollable body element, for callers that follow a live tail. The * caller scrolls it; the window owns its size and its overflow. */ bodyRef?: RefObject /** * How the body stacks its children. `fill` (default) stretches content to * the available width and height without shell padding. Explicit `scroll` * flows long logs or tables from the top in the body scroll container. */ bodyLayout?: 'scroll' | 'fill' /** Controlled geometry (the public window host owns the box). */ box?: WindowBox /** Controlled geometry commit (drag/resize). */ onBoxChange?(box: WindowBox): void /** Controlled maximize state; omitted = this component owns it. */ maximized?: boolean /** Controlled maximize toggle. */ onMaximizeChange?(maximized: boolean): void /** Hide the maximize control (a compact confirmation surface). */ maximizable?: boolean /** Controlled z-index; omitted = the unified layer slot. */ zIndex?: number /** Stable layer identity for the window host's `focus()` raise. */ layerKey?: string /** Title-bar leading control (the in-surface back button). */ titleLeading?: ReactNode /** Unsaved-edit marker in the title bar. */ dirty?: boolean /** A source-owned save is in flight (rendered explicitly). */ saving?: boolean /** Draft guard for the close control + Escape. False keeps the window open. */ onBeforeClose?(): boolean | Promise /** Close-button accessible name; defaults to the shared "Close" copy. */ closeLabel?: string /** Stable portal container; moving this node preserves the entire live frame. */ portalTarget?: HTMLElement /** Native sidebar presentation of this very same frame. */ docked?: boolean onDock?(): void onFloat?(): void children: ReactNode } /** Clamp the legacy 70%/60% measurement into the live viewport. */ function initialBox( anchor: HTMLElement | null | undefined, size: { width: number; height: number } | undefined, min: { width: number; height: number }, viewport: { width: number; height: number }, topFloor: number, ): WindowBox { // A caller-provided size (the task window's compact form, a log's explicit // box) is honoured. An unsized window on a NARROW viewport uses the // near-fullscreen form instead of a shrunken 70%/60% box — the same rule the // public host applies to its presets. const narrow = size === undefined && isNarrowWidth(viewport.width) const width = clampNumber( size?.width ?? (narrow ? viewport.width - WINDOW_VIEWPORT_MARGIN * 2 : viewport.width * DEFAULT_WIDTH_RATIO), min.width, Math.max(min.width, viewport.width - WINDOW_VIEWPORT_MARGIN * 2), ) const height = clampNumber( size?.height ?? (narrow ? viewport.height - WINDOW_VIEWPORT_MARGIN * 2 : viewport.height * DEFAULT_HEIGHT_RATIO), min.height, Math.max(min.height, viewport.height - WINDOW_VIEWPORT_MARGIN * 2 - Math.max(0, topFloor - WINDOW_VIEWPORT_MARGIN)), ) return placeWindowBox({ size: { width, height }, viewport, ...(anchor === undefined ? {} : { anchor }), min, topFloor, }) } /** * Whether a React node actually renders something. `null`/`false`/`''` and an * empty array are how callers CLEAR a row; rendering an empty bordered strip * for them is a visible shell bug. */ function hasNode(value: ReactNode): boolean { if (value === null || value === undefined || value === false || value === true || value === '') return false if (Array.isArray(value)) return value.some(hasNode) // An empty fragment renders nothing either: treat it as a cleared row. if (isValidElement(value) && value.type === Fragment) { return hasNode((value.props as { children?: ReactNode }).children) } return true } /** The restore glyph (the host set ships only the fullscreen/maximize one). */ function RestoreIcon({ size = 16 }: { size?: number }): ReactNode { return ( ) } /** The surface-transfer glyph stays visible beside its label at every width. */ function SidebarIcon(): ReactNode { return } /** * Render a persistent floating window. The caller owns WHAT is shown (body, * toolbar, footer); this component owns the frame, the geometry, the two drag * gestures and the dismissal contract (close control + Escape only). */ export function FloatingWindow(props: FloatingWindowProps): ReactNode { const { title, onClose, initialSize, minSize, anchor, actions, toolbar, footer, bodyRef, bodyLayout = 'fill', box: controlledBox, onBoxChange, maximized: controlledMaximized, onMaximizeChange, maximizable = true, zIndex, layerKey, titleLeading, dirty, saving, onBeforeClose, closeLabel, portalTarget, docked = false, onDock, onFloat, children, } = props const min = useMemo(() => ({ width: minSize?.width ?? WINDOW_MIN_WIDTH, height: minSize?.height ?? WINDOW_MIN_HEIGHT, }), [minSize?.width, minSize?.height]) // Desktop shells draw native window controls over the top of the page and // take their pointer events: keep every window's title bar below that band. const insetTop = useViewportInsetTop() const topFloor = WINDOW_VIEWPORT_MARGIN + insetTop const topFloorRef = useRef(topFloor) topFloorRef.current = topFloor const frameRef = useRef(null) const onCloseRef = useRef(onClose) onCloseRef.current = onClose const onBoxChangeRef = useRef(onBoxChange) onBoxChangeRef.current = onBoxChange const onMaximizeChangeRef = useRef(onMaximizeChange) onMaximizeChangeRef.current = onMaximizeChange const beforeCloseRef = useRef(onBeforeClose) beforeCloseRef.current = onBeforeClose const controlledRef = useRef(controlledBox !== undefined) controlledRef.current = controlledBox !== undefined const maximizeControlled = controlledMaximized !== undefined || onMaximizeChange !== undefined const maximizedControlledRef = useRef(maximizeControlled) maximizedControlledRef.current = maximizeControlled /** * The box is measured ONCE, on mount, from the live viewport: a later * re-render (a streamed output frame, a status flip) must never move or * resize a window the reader placed. A controlled box skips that entirely. */ const [ownedBox, setOwnedBox] = useState(null) const [ownedMaximized, setOwnedMaximized] = useState(false) const restoreBoxRef = useRef(null) const currentBox = controlledBox ?? ownedBox const currentBoxRef = useRef(currentBox) currentBoxRef.current = currentBox const maximized = controlledMaximized ?? ownedMaximized const maximizedRef = useRef(maximized) maximizedRef.current = maximized const commitBox = useCallback((next: WindowBox): void => { if (!controlledRef.current) setOwnedBox(next) onBoxChangeRef.current?.(next) }, []) useEffect(() => { if (controlledRef.current) return setOwnedBox(initialBox(anchor, initialSize, min, { width: window.innerWidth, height: window.innerHeight, }, topFloorRef.current)) // Mount-only on purpose: see the comment above. // eslint-disable-next-line react-hooks/exhaustive-deps }, []) /** Keep the box reachable after a viewport change (a rotation, a split pane). */ useEffect(() => { const onResize = (): void => { if (controlledRef.current) return const current = currentBoxRef.current if (current === null) return const viewport = { width: window.innerWidth, height: window.innerHeight } if (maximizedRef.current) { setOwnedBox(maximizedWindowBox(viewport, topFloor)) return } setOwnedBox(clampWindowBox(current, viewport, min, topFloor)) const restore = restoreBoxRef.current if (restore !== null) restoreBoxRef.current = clampWindowBox(restore, viewport, min, topFloor) } window.addEventListener('resize', onResize) return () => { window.removeEventListener('resize', onResize) } }, [min, topFloor]) /** A band that appears/disappears re-clamps the placed window (lift only). */ useEffect(() => { if (controlledRef.current) return const current = currentBoxRef.current if (current === null) return const viewport = { width: window.innerWidth, height: window.innerHeight } if (maximizedRef.current) { setOwnedBox(maximizedWindowBox(viewport, topFloor)) return } setOwnedBox(clampWindowBox(current, viewport, min, topFloor)) }, [min, topFloor]) /** * Close path shared by the button and Escape: run the draft guard first. * The layer's Escape handler calls exactly this method, so a dirty window is * never discarded silently. */ const closingRef = useRef(false) const requestClose = useCallback(async (): Promise => { if (closingRef.current) return closingRef.current = true try { const guard = beforeCloseRef.current if (guard !== undefined) { const allowed = await guard() if (allowed === false) return } onCloseRef.current() } finally { closingRef.current = false } }, []) const requestCloseRef = useRef(requestClose) requestCloseRef.current = requestClose // Unified layer: z-order + Escape (only the top window closes). const layer = useWindowLayer({ enabled: !docked, ...(layerKey === undefined ? {} : { key: layerKey }), close: () => { void requestCloseRef.current() }, }) const toggleMaximize = useCallback((): void => { const next = !maximizedRef.current if (maximizedControlledRef.current) { onMaximizeChangeRef.current?.(next) return } const current = currentBoxRef.current const viewport = { width: window.innerWidth, height: window.innerHeight } if (next) { restoreBoxRef.current = current setOwnedMaximized(true) setOwnedBox(maximizedWindowBox(viewport, topFloor)) } else { setOwnedMaximized(false) setOwnedBox(clampWindowBox( restoreBoxRef.current ?? current ?? maximizedWindowBox(viewport, topFloor), viewport, min, topFloor, )) } }, [min, topFloor]) /** Live drag teardown: a window closed mid-drag must not leave window listeners behind. */ const dragCleanupRef = useRef<(() => void) | null>(null) useEffect(() => () => { dragCleanupRef.current?.() }, []) /** Drag the window by its title bar (buttons inside it keep working). */ const dragBatcher = useRef(createFrameBatcher()).current const pendingDragRef = useRef(null) useEffect(() => () => { dragBatcher.dispose() }, [dragBatcher]) const onTitlePointerDown = useCallback((event: ReactPointerEvent): void => { if (event.button !== 0 || maximizedRef.current) return const target = event.target as HTMLElement // The header's own controls (back, maximize, close) must not start a drag. if (target.closest('button, input, textarea, select, a, [data-window-no-drag]') !== null) return const start = currentBoxRef.current if (start === null) return event.preventDefault() const startX = event.clientX const startY = event.clientY const onMove = (move: PointerEvent): void => { pendingDragRef.current = clampWindowBox({ ...start, left: start.left + (move.clientX - startX), top: start.top + (move.clientY - startY), }, { width: window.innerWidth, height: window.innerHeight }, min, topFloor) dragBatcher.schedule(() => { const pending = pendingDragRef.current if (pending === null) return pendingDragRef.current = null commitBox(pending) }) } const onUp = (): void => { window.removeEventListener('pointermove', onMove) window.removeEventListener('pointerup', onUp) window.removeEventListener('pointercancel', onUp) dragCleanupRef.current = null dragBatcher.flushNow() } dragCleanupRef.current = onUp window.addEventListener('pointermove', onMove) window.addEventListener('pointerup', onUp) window.addEventListener('pointercancel', onUp) }, [dragBatcher, commitBox, min, topFloor]) /** Resize from one edge handle: `axis` picks which dimension the handle owns. */ const resizeBatcher = useRef(createFrameBatcher()).current useEffect(() => () => { resizeBatcher.dispose() }, [resizeBatcher]) const resizeRef = useRef<{ startX: number; startY: number; box: WindowBox; axis: 'x' | 'y' | 'both' } | null>(null) const pendingBoxRef = useRef(null) const onResizeStart = useCallback((axis: 'x' | 'y' | 'both') => (event: ReactPointerEvent): void => { const start = currentBoxRef.current if (start === null || event.button !== 0) return event.preventDefault() event.stopPropagation() // jsdom lacks setPointerCapture — the tests dispatch plain pointer events. event.currentTarget.setPointerCapture?.(event.pointerId) resizeRef.current = { startX: event.clientX, startY: event.clientY, box: start, axis } }, []) const onResizeMove = useCallback((event: ReactPointerEvent): void => { const drag = resizeRef.current if (drag === null) return const growX = drag.axis === 'y' ? 0 : event.clientX - drag.startX const growY = drag.axis === 'x' ? 0 : event.clientY - drag.startY // Origin-preserving resize: growing into the viewport edge caps the size // in place instead of sliding the whole window left/up (the pre-S6 // FloatingWindow behaviour, and what a reader dragging a corner expects). pendingBoxRef.current = resizeWindowFromEdge( drag.box, { width: drag.box.width + growX, height: drag.box.height + growY }, { width: window.innerWidth, height: window.innerHeight }, min, ) resizeBatcher.schedule(() => { const pending = pendingBoxRef.current if (pending !== null) commitBox(pending) }) }, [min, resizeBatcher, commitBox]) const onResizeEnd = useCallback((event: ReactPointerEvent): void => { if (resizeRef.current === null) return // Flush the last frame: a release can land with the final move still // queued (the committed box would otherwise lose it). resizeBatcher.flushNow() resizeRef.current = null pendingBoxRef.current = null event.currentTarget.releasePointerCapture?.(event.pointerId) }, [resizeBatcher]) const resolvedZ = zIndex ?? layer.zIndex const toolbarRow = toolbar ?? actions const closeText = closeLabel ?? t('close') const style = currentBox === null ? { left: -9999, top: -9999, width: WINDOW_MIN_WIDTH, height: WINDOW_MIN_HEIGHT, zIndex: resolvedZ } : { left: currentBox.left, top: currentBox.top, width: currentBox.width, height: currentBox.height, zIndex: resolvedZ } return createPortal(
{ layer.raise() }} onFocusCapture={() => { layer.raise() }} >
{titleLeading !== undefined && ( {titleLeading} )} {!docked && {title}} {dirty === true && ( )} {saving === true && ( {t('windowSaving')} )} {docked ? ( {t('windowFloat')} ) : onDock !== undefined ? ( {t('windowDock')} ) : null} {!docked && maximizable && ( : } label={maximized ? t('windowRestore') : t('windowMaximize')} title={maximized ? t('windowRestore') : t('windowMaximize')} data-window-maximize={maximized ? 'restore' : 'maximize'} onClick={toggleMaximize} /> )} {!docked && ( } label={closeText} title={closeText} onClick={() => { void requestCloseRef.current() }} /> )}
{hasNode(toolbarRow) && (
{toolbarRow}
)}
{children}
{hasNode(footer) &&
{footer}
} {!docked && !maximized && ( <> {/* Three handles rather than eight: the window grows from the bottom-right in practice (a log gets longer), and the two edges keep a single-axis resize possible. They sit above the body's scrollbar as thin strips. */}