|
| 1 | +/** |
| 2 | + * Free windows (extracted from Sidebar.tsx, behavior identical): the |
| 3 | + * drag-out gesture that floats a tab onto the conversation column, plus the |
| 4 | + * render layer for the floating windows and its drop-zone hint overlay. |
| 5 | + */ |
| 6 | +import { useEffect, useRef, useState, type ReactNode } from 'react' |
| 7 | +import type { Context } from '../../context-types.ts' |
| 8 | +import { |
| 9 | + dockFloat, floatTab, moveFloat, raiseFloat, resizeFloat, |
| 10 | + type FloatWindow, type SidebarStore, type SidebarTab, |
| 11 | +} from '../state.ts' |
| 12 | +import { TAB_DRAG_TYPE, parseDrag } from '../TabBar.tsx' |
| 13 | +import { FreeWindow } from '../FreeWindow.tsx' |
| 14 | +import { t } from '../locales.ts' |
| 15 | +import css from '../sidebar.module.css' |
| 16 | + |
| 17 | +/** The dashed drop-zone hint's geometry (viewport coordinates). */ |
| 18 | +export interface FloatDropHint { |
| 19 | + left: number |
| 20 | + top: number |
| 21 | + width: number |
| 22 | + height: number |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Free windows — drag-out detection. The tab strips already drive HTML5 |
| 27 | + * DnD (payload application/x-dsh-tab) with drops owned by the panes |
| 28 | + * (split/merge); this hook watches the DOCUMENT (capture) for the same |
| 29 | + * drag hovering OUTSIDE the panel host: while the pointer is over the |
| 30 | + * conversation column it arms the drop (preventDefault) and shows a hint |
| 31 | + * overlay there, and the drop floats the tab at the release point. Targets |
| 32 | + * inside the host are ignored here, so pane drops keep their behavior |
| 33 | + * untouched. Only OUR tab drags count (the body flag is the tab strip's; |
| 34 | + * OS file drags and any DSH drags pass through). Narrow viewports skip |
| 35 | + * the gesture — the merged drawer covers the conversation, leaving |
| 36 | + * nothing to drop onto (the tab context menu entry still floats tabs). |
| 37 | + */ |
| 38 | +export function useFloatDragout(input: { |
| 39 | + narrow: boolean |
| 40 | + sessionId: string | undefined |
| 41 | + store: SidebarStore |
| 42 | + /** The AppFrame center column (from useCenterColumn); stable ref object. */ |
| 43 | + centerColRef: { readonly current: HTMLElement | null } |
| 44 | +}): { floatHint: FloatDropHint | null } { |
| 45 | + const { narrow, sessionId, store, centerColRef } = input |
| 46 | + const [floatHint, setFloatHint] = useState<FloatDropHint | null>(null) |
| 47 | + const floatHintRef = useRef(false) |
| 48 | + useEffect(() => { |
| 49 | + if (narrow || sessionId === undefined) return |
| 50 | + const inPanelHost = (target: EventTarget | null): boolean => |
| 51 | + target instanceof Element && target.closest('[data-dsh-panel-host]') !== null |
| 52 | + /** The conversation column's rect when the pointer is over it (and not |
| 53 | + * over our own surfaces); null otherwise. */ |
| 54 | + const overConversation = (event: DragEvent): DOMRect | null => { |
| 55 | + if (inPanelHost(event.target)) return null |
| 56 | + const col = centerColRef.current |
| 57 | + if (col === null || !col.isConnected) return null |
| 58 | + const rect = col.getBoundingClientRect() |
| 59 | + if (rect.width === 0 || rect.height === 0) return null |
| 60 | + const { clientX: x, clientY: y } = event |
| 61 | + if (x < rect.left || x > rect.right || y < rect.top || y > rect.bottom) return null |
| 62 | + return rect |
| 63 | + } |
| 64 | + const onDragOver = (event: DragEvent): void => { |
| 65 | + if (!document.body.hasAttribute('data-dsh-tab-dragging')) return |
| 66 | + const rect = overConversation(event) |
| 67 | + if (rect !== null) { |
| 68 | + // preventDefault on dragover is what makes the browser deliver the |
| 69 | + // drop (and drop the "no" cursor) over the conversation area. |
| 70 | + event.preventDefault() |
| 71 | + setFloatHint((prev) => { |
| 72 | + const next = { left: rect.left, top: rect.top, width: rect.width, height: rect.height } |
| 73 | + if (prev !== null && prev.left === next.left && prev.top === next.top |
| 74 | + && prev.width === next.width && prev.height === next.height) return prev |
| 75 | + return next |
| 76 | + }) |
| 77 | + floatHintRef.current = true |
| 78 | + } else if (floatHintRef.current) { |
| 79 | + floatHintRef.current = false |
| 80 | + setFloatHint(null) |
| 81 | + } |
| 82 | + } |
| 83 | + const onDrop = (event: DragEvent): void => { |
| 84 | + if (!floatHintRef.current) return |
| 85 | + floatHintRef.current = false |
| 86 | + setFloatHint(null) |
| 87 | + const rect = overConversation(event) |
| 88 | + if (rect === null) return |
| 89 | + event.preventDefault() |
| 90 | + event.stopPropagation() |
| 91 | + const payload = parseDrag(event.dataTransfer?.getData(TAB_DRAG_TYPE) ?? '') |
| 92 | + if (payload === null) return |
| 93 | + store.reduce(s => floatTab(s, payload.tabId, event.clientX, event.clientY)) |
| 94 | + } |
| 95 | + const clear = (): void => { |
| 96 | + if (!floatHintRef.current) return |
| 97 | + floatHintRef.current = false |
| 98 | + setFloatHint(null) |
| 99 | + } |
| 100 | + document.addEventListener('dragover', onDragOver, true) |
| 101 | + document.addEventListener('drop', onDrop, true) |
| 102 | + window.addEventListener('dragend', clear, true) |
| 103 | + window.addEventListener('blur', clear) |
| 104 | + return () => { |
| 105 | + document.removeEventListener('dragover', onDragOver, true) |
| 106 | + document.removeEventListener('drop', onDrop, true) |
| 107 | + window.removeEventListener('dragend', clear, true) |
| 108 | + window.removeEventListener('blur', clear) |
| 109 | + } |
| 110 | + // centerColRef is a stable ref object from useCenterColumn (it lost its |
| 111 | + // useRef provenance crossing the hook boundary, so it is listed here). |
| 112 | + }, [narrow, sessionId, store, centerColRef]) |
| 113 | + |
| 114 | + return { floatHint } |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * The free-window render layer: tabs dragged out onto the conversation area |
| 119 | + * (or floated from the tab context menu). They live in the panel host like |
| 120 | + * the panels (viewport coordinates, immune to desktop-shell transforms) but |
| 121 | + * are independent of panel state — a window stays up while panels collapse. |
| 122 | + * The floats array's order is the stacking order; the content reuses the |
| 123 | + * regular tab renderer, so every tab type floats unchanged. Renders a |
| 124 | + * fragment so the DOM is exactly the floats followed by the drag-out hint. |
| 125 | + */ |
| 126 | +export function FreeWindowLayer(props: { |
| 127 | + floats: readonly FloatWindow[] |
| 128 | + hint: FloatDropHint | null |
| 129 | + renderTab: (tab: SidebarTab, active: boolean, paneId: string, placement: 'top' | 'bottom' | 'float') => ReactNode |
| 130 | + getTabIcon: (tab: SidebarTab) => ReactNode |
| 131 | + store: SidebarStore |
| 132 | + ctx: Context |
| 133 | + sessionId: string |
| 134 | + cwd: string | undefined |
| 135 | +}) { |
| 136 | + const { floats, hint, renderTab, getTabIcon, store, ctx, sessionId, cwd } = props |
| 137 | + return ( |
| 138 | + <> |
| 139 | + {floats.map(float => ( |
| 140 | + <FreeWindow |
| 141 | + key={float.id} |
| 142 | + float={float} |
| 143 | + renderTab={(tab, active, paneId) => renderTab(tab, active, paneId, 'float')} |
| 144 | + getTabIcon={getTabIcon} |
| 145 | + onRaise={() => { store.reduce(s => raiseFloat(s, float.id)) }} |
| 146 | + onMove={(x, y) => { store.reduce(s => moveFloat(s, float.id, x, y)) }} |
| 147 | + onResize={(w, h) => { store.reduce(s => resizeFloat(s, float.id, w, h)) }} |
| 148 | + onDock={(paneId) => { store.reduce(s => dockFloat(s, float.id, paneId ?? undefined)) }} |
| 149 | + onClose={() => { ctx.get('betterSidebar')?.closeTab(float.tab.id, sessionId === undefined ? undefined : { sessionId, cwd }) }} |
| 150 | + /> |
| 151 | + ))} |
| 152 | + {/* |
| 153 | + The drag-out hint: while a tab drag hovers the conversation column, |
| 154 | + a dashed overlay marks the drop zone there (pointer-transparent — it |
| 155 | + must not disturb the drag it describes). |
| 156 | + */} |
| 157 | + {hint !== null && ( |
| 158 | + <div |
| 159 | + className={css.floatDropHint} |
| 160 | + style={{ left: hint.left, top: hint.top, width: hint.width, height: hint.height }} |
| 161 | + > |
| 162 | + <span className={css.floatDropHintLabel}>{t('floatDropHint')}</span> |
| 163 | + </div> |
| 164 | + )} |
| 165 | + </> |
| 166 | + ) |
| 167 | +} |
0 commit comments