Skip to content

Commit ea5fa50

Browse files
committed
fix(desktop): clamp floating composer to the thread area, not the whole window
Now that the popped-out composer is fixed to the viewport, clamping against the window let it slide under a pinned sidebar. Confine it to the thread region (data-slot="composer-bounds") instead — its rect already excludes a pinned sidebar and the header — falling back to the full window before it's measured. This subsumes the old titlebar top-margin (the thread rect starts below the header).
1 parent aff5ae6 commit ea5fa50

4 files changed

Lines changed: 42 additions & 21 deletions

File tree

apps/desktop/src/app/chat/composer/hooks/use-popout-drag.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import {
1111
POPOUT_ESTIMATED_HEIGHT,
1212
POPOUT_WIDTH_REM,
13+
readPopoutBounds,
1314
setComposerPopoutPosition,
1415
type PopoutPosition,
1516
type PopoutSize
@@ -147,7 +148,7 @@ export function useComposerPopoutGestures({
147148
const beginFloatDrag = useCallback(
148149
(state: PressState, clientX: number, clientY: number, next: PopoutPosition, size?: PopoutSize) => {
149150
clearTimer()
150-
const clamped = setComposerPopoutPosition(next, { size })
151+
const clamped = setComposerPopoutPosition(next, { area: readPopoutBounds(composerRef.current), size })
151152
liveRef.current = clamped
152153

153154
state.mode = 'float'
@@ -159,7 +160,7 @@ export function useComposerPopoutGestures({
159160

160161
setDragging(true)
161162
},
162-
[clearTimer]
163+
[clearTimer, composerRef]
163164
)
164165

165166
const peelOffFromDock = useCallback(
@@ -265,7 +266,7 @@ export function useComposerPopoutGestures({
265266
bottom: state.startBottom - (pending.y - state.startY),
266267
right: state.startRight - (pending.x - state.startX)
267268
},
268-
{ size }
269+
{ area: readPopoutBounds(composer), size }
269270
)
270271

271272
if (composer) {
@@ -327,7 +328,7 @@ export function useComposerPopoutGestures({
327328
} else {
328329
// Persist the resting position once, on release — never per move.
329330
const size = composer ? { height: composer.offsetHeight, width: composer.offsetWidth } : undefined
330-
setComposerPopoutPosition(liveRef.current, { persist: true, size })
331+
setComposerPopoutPosition(liveRef.current, { area: readPopoutBounds(composer), persist: true, size })
331332
}
332333
}
333334

apps/desktop/src/app/chat/composer/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
$composerPopoutPosition,
4545
$composerPoppedOut,
4646
POPOUT_WIDTH_REM,
47+
readPopoutBounds,
4748
setComposerPoppedOut,
4849
setComposerPopoutPosition
4950
} from '@/store/composer-popout'
@@ -553,7 +554,7 @@ export function ChatBar({
553554
const reclamp = (persist: boolean) => {
554555
const el = composerRef.current
555556
const size = el ? { height: el.offsetHeight, width: el.offsetWidth } : undefined
556-
setComposerPopoutPosition($composerPopoutPosition.get(), { persist, size })
557+
setComposerPopoutPosition($composerPopoutPosition.get(), { area: readPopoutBounds(el), persist, size })
557558
}
558559

559560
reclamp(true)

apps/desktop/src/app/chat/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ export function ChatView({
443443
>
444444
<div
445445
className="relative min-h-0 max-w-full flex-1 overflow-hidden bg-(--ui-chat-surface-background) contain-[layout_paint]"
446+
data-slot="composer-bounds"
446447
{...dropHandlers}
447448
>
448449
<Thread

apps/desktop/src/store/composer-popout.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,28 @@ export interface PopoutSize {
4949
width: number
5050
}
5151

52+
/** Viewport-space rect the floating composer is confined to. Defaults to the
53+
* whole window; pass the thread area so the box can't slide under a pinned
54+
* sidebar or behind the header. */
55+
export interface PopoutBounds {
56+
bottom: number
57+
left: number
58+
right: number
59+
top: number
60+
}
61+
5262
interface SetPositionOptions {
63+
/** Thread-area rect to confine the box to; falls back to the full window. */
64+
area?: PopoutBounds
5365
persist?: boolean
5466
/** Measured box size; falls back to the compact width + a min height so the
5567
* box stays grabbable even when the caller can't measure it. */
5668
size?: PopoutSize
5769
}
5870

59-
// Keep at least this much of every edge between the box and the viewport, so the
71+
// Keep at least this much between the box and every edge of its bounds, so the
6072
// floating composer can never be dragged (or restored) out of reach.
6173
const EDGE_MARGIN = 8
62-
const TITLEBAR_HEIGHT_FALLBACK = 34
63-
const TITLEBAR_CLEARANCE_REM = 0.75
6474
// Height floor used when the real box height is unknown (init / load / peel-off).
6575
export const POPOUT_ESTIMATED_HEIGHT = 56
6676
const MIN_VISIBLE_HEIGHT = POPOUT_ESTIMATED_HEIGHT
@@ -69,24 +79,32 @@ const clampRange = (value: number, lo: number, hi: number) => Math.min(Math.max(
6979

7080
const rootFontSize = () => parseFloat(getComputedStyle(document.documentElement).fontSize) || 16
7181

72-
function titlebarTopMargin() {
73-
const raw = getComputedStyle(document.documentElement).getPropertyValue('--titlebar-height').trim()
74-
const titlebarHeight = Number.parseFloat(raw)
75-
const breathingRoom = TITLEBAR_CLEARANCE_REM * rootFontSize()
82+
/** The thread area's viewport rect (excludes a pinned sidebar + the header), or
83+
* undefined before it mounts — callers then fall back to the full window. */
84+
export function readPopoutBounds(composer: Element | null): PopoutBounds | undefined {
85+
const el = (composer?.parentElement ?? document).querySelector('[data-slot="composer-bounds"]')
86+
87+
if (!el) {
88+
return undefined
89+
}
90+
91+
const { bottom, left, right, top } = el.getBoundingClientRect()
7692

77-
return Math.max(EDGE_MARGIN, (Number.isFinite(titlebarHeight) ? titlebarHeight : TITLEBAR_HEIGHT_FALLBACK) + breathingRoom)
93+
return { bottom, left, right, top }
7894
}
7995

80-
// Bound the bottom-right inset so the WHOLE box stays on-screen — the corner
81-
// anchor alone would let the box's width/height push it past the left/top edges.
82-
function clampPosition({ bottom, right }: PopoutPosition, size?: PopoutSize): PopoutPosition {
96+
// Bound the bottom/right inset so the WHOLE box stays inside `area` (the thread
97+
// region, or the window by default) — the corner anchor alone would let the
98+
// box's width/height push it past the opposite edges.
99+
function clampPosition({ bottom, right }: PopoutPosition, size?: PopoutSize, area?: PopoutBounds): PopoutPosition {
83100
const width = size?.width || POPOUT_WIDTH_REM * rootFontSize()
84101
const height = size?.height || MIN_VISIBLE_HEIGHT
85-
const topMargin = titlebarTopMargin()
102+
const { innerHeight: vh, innerWidth: vw } = window
103+
const a = area ?? { bottom: vh, left: 0, right: vw, top: 0 }
86104

87105
return {
88-
bottom: clampRange(bottom, EDGE_MARGIN, window.innerHeight - height - topMargin),
89-
right: clampRange(right, EDGE_MARGIN, window.innerWidth - width - EDGE_MARGIN)
106+
bottom: clampRange(bottom, vh - a.bottom + EDGE_MARGIN, vh - a.top - height - EDGE_MARGIN),
107+
right: clampRange(right, vw - a.right + EDGE_MARGIN, vw - a.left - width - EDGE_MARGIN)
90108
}
91109
}
92110

@@ -102,8 +120,8 @@ export function setComposerPoppedOut(value: boolean) {
102120
* unless `persist`. Returns the clamped position so callers can sync their live
103121
* ref. Pass the measured `size` for exact bounds; otherwise a fallback keeps it
104122
* on-screen. */
105-
export function setComposerPopoutPosition(position: PopoutPosition, { persist, size }: SetPositionOptions = {}): PopoutPosition {
106-
const next = clampPosition(position, size)
123+
export function setComposerPopoutPosition(position: PopoutPosition, { area, persist, size }: SetPositionOptions = {}): PopoutPosition {
124+
const next = clampPosition(position, size, area)
107125
$composerPopoutPosition.set(next)
108126

109127
if (persist) {

0 commit comments

Comments
 (0)