Skip to content

The agent that grows with you

feat(desktop): floating pane geometry

Brooklyn Nicholson6dd1c6c062d7
diff --git a/apps/desktop/src/components/pane-shell/tree/renderer/floating-rect.test.ts b/apps/desktop/src/components/pane-shell/tree/renderer/floating-rect.test.ts
new file mode 100644
index 00000000000..dec110531fe
--- /dev/null
+++ b/apps/desktop/src/components/pane-shell/tree/renderer/floating-rect.test.ts
@@ -0,0 +1,86 @@
+import { describe, expect, it } from 'vitest'
+
+import { anchoredRect, clampFloatingRect, floatingPx, reflowRect } from './floating-rect'
+
+const viewport = { width: 1440, height: 900, top: 34 }
+const size = { width: 240, height: 180 }
+
+describe('anchoredRect', () => {
+ it('spawns in the named corner, inset by the margin', () => {
+ expect(anchoredRect('top-right', size, viewport)).toMatchObject({ x: 1188, y: 46 })
+ expect(anchoredRect('top-left', size, viewport)).toMatchObject({ x: 12, y: 46 })
+ expect(anchoredRect('bottom-right', size, viewport)).toMatchObject({ x: 1188, y: 708 })
+ expect(anchoredRect('bottom-left', size, viewport)).toMatchObject({ x: 12, y: 708 })
+ })
+
+ it('never spawns over the reserved top chrome', () => {
+ for (const anchor of ['top-left', 'top-right', 'bottom-left', 'bottom-right'] as const) {
+ expect(anchoredRect(anchor, size, { width: 320, height: 120, top: 34 }).y).toBeGreaterThanOrEqual(34)
+ }
+ })
+})
+
+describe('clampFloatingRect', () => {
+ it('keeps a grabbable sliver on screen at either horizontal edge', () => {
+ expect(clampFloatingRect({ ...size, x: 5000, y: 100 }, viewport).x).toBe(1392)
+ expect(clampFloatingRect({ ...size, x: -5000, y: 100 }, viewport).x).toBe(-192)
+ })
+
+ it('hard-bounds the top so the drag handle is always reachable', () => {
+ expect(clampFloatingRect({ ...size, x: 100, y: -500 }, viewport).y).toBe(34)
+ expect(clampFloatingRect({ ...size, x: 100, y: 5000 }, viewport).y).toBe(852)
+ })
+
+ it('pins to the top-left rather than inverting when larger than the viewport', () => {
+ const tiny = { width: 200, height: 100, top: 34 }
+ const rect = clampFloatingRect({ x: 0, y: 0, width: 400, height: 400 }, tiny)
+
+ expect(rect.x).toBeLessThanOrEqual(tiny.width)
+ expect(rect.y).toBe(34)
+ })
+
+ it('leaves an in-bounds rect untouched', () => {
+ const rect = { ...size, x: 400, y: 200 }
+
+ expect(clampFloatingRect(rect, viewport)).toEqual(rect)
+ })
+})
+
+describe('reflowRect', () => {
+ const shrunk = { width: 1000, height: 700, top: 34 }
+
+ it('rides the right edge when right-anchored', () => {
+ const start = anchoredRect('top-right', size, viewport)
+
+ expect(reflowRect(start, 'top-right', viewport, shrunk).x).toBe(748)
+ })
+
+ it('rides the bottom edge when bottom-anchored', () => {
+ const start = anchoredRect('bottom-left', size, viewport)
+
+ expect(reflowRect(start, 'bottom-left', viewport, shrunk).y).toBe(508)
+ })
+
+ it('holds position when left/top-anchored, but still clamps into view', () => {
+ const start = { ...size, x: 40, y: 60 }
+
+ expect(reflowRect(start, 'top-left', viewport, shrunk)).toMatchObject({ x: 40, y: 60 })
+ expect(reflowRect({ ...size, x: 1300, y: 60 }, 'top-left', viewport, shrunk).x).toBe(952)
+ })
+
+ it('is identity for an in-bounds rect when the viewport did not change', () => {
+ const rect = { ...size, x: 300, y: 300 }
+
+ expect(reflowRect(rect, 'top-right', viewport, viewport)).toEqual(rect)
+ })
+})
+
+describe('floatingPx', () => {
+ it('accepts authored px strings and raw numbers, falling back otherwise', () => {
+ expect(floatingPx('216px', 240)).toBe(216)
+ expect(floatingPx(300, 240)).toBe(300)
+ expect(floatingPx(undefined, 240)).toBe(240)
+ expect(floatingPx('auto', 240)).toBe(240)
+ expect(floatingPx(Number.NaN, 240)).toBe(240)
+ })
+})
diff --git a/apps/desktop/src/components/pane-shell/tree/renderer/floating-rect.ts b/apps/desktop/src/components/pane-shell/tree/renderer/floating-rect.ts
new file mode 100644
index 00000000000..f09947e8132
--- /dev/null
+++ b/apps/desktop/src/components/pane-shell/tree/renderer/floating-rect.ts
@@ -0,0 +1,109 @@
+/**
+ * Floating-pane geometry — pure, so the clamping/anchoring rules are testable
+ * without a DOM.
+ *
+ * A floating pane is NOT a track in the layout tree: it never takes width from
+ * a zone. It's a fixed-position card the tree renders above itself. Its whole
+ * contract is "stay a sane rect inside the viewport", which is this module.
+ */
+
+/** Corner a floating pane spawns from (and re-anchors to on reset). */
+export type FloatingAnchor = 'bottom-left' | 'bottom-right' | 'top-left' | 'top-right'
+
+export interface FloatingRect {
+ x: number
+ y: number
+ width: number
+ height: number
+}
+
+export interface FloatingViewport {
+ width: number
+ height: number
+ /** Chrome reserved at the top (the 34px titlebar) — panes never cover it. */
+ top: number
+}
+
+/** Keep this much of the card on screen when clamping, so it stays grabbable. */
+const MIN_VISIBLE = 48
+
+/** Gap between a spawned pane and the viewport edge it anchors to. */
+export const FLOATING_MARGIN = 12
+
+/** The one non-tiling placement — see renderer/floating-panes.tsx. */
+export const FLOATING_PLACEMENT = 'floating'
+
+export const clamp = (n: number, lo: number, hi: number): number => Math.min(Math.max(n, lo), hi)
+
+/**
+ * Clamp a rect into the viewport. Horizontal keeps `MIN_VISIBLE` px on screen
+ * from either edge (a card can hang off the right, never vanish); vertical is
+ * hard-bounded by the reserved chrome so the drag handle is always reachable.
+ *
+ * Both axes clamp `lo` before `hi`, so a card wider/taller than the viewport
+ * pins to the top-left rather than inverting.
+ */
+export function clampFloatingRect(rect: FloatingRect, viewport: FloatingViewport): FloatingRect {
+ const maxX = Math.max(MIN_VISIBLE - rect.width, viewport.width - MIN_VISIBLE)
+ const maxY = Math.max(viewport.top, viewport.height - MIN_VISIBLE)
+
+ return {
+ ...rect,
+ x: clamp(rect.x, Math.min(MIN_VISIBLE - rect.width, maxX), maxX),
+ y: clamp(rect.y, viewport.top, maxY)
+ }
+}
+
+/** Spawn position for an anchor — the corner, inset by `FLOATING_MARGIN`. */
+export function anchoredRect(
+ anchor: FloatingAnchor,
+ size: { width: number; height: number },
+ viewport: FloatingViewport
+): FloatingRect {
+ const right = anchor === 'bottom-right' || anchor === 'top-right'
+ const bottom = anchor === 'bottom-left' || anchor === 'bottom-right'
+
+ const rect = {
+ ...size,
+ x: right ? viewport.width - size.width - FLOATING_MARGIN : FLOATING_MARGIN,
+ y: bottom ? viewport.height - size.height - FLOATING_MARGIN : viewport.top + FLOATING_MARGIN
+ }
+
+ return clampFloatingRect(rect, viewport)
+}
+
+/**
+ * Re-clamp on viewport resize. A pane anchored to a right/bottom edge TRACKS
+ * that edge (shrinking the window keeps it in the corner) instead of being
+ * dragged inward only when it would fall off — matching how the pet and every
+ * OS HUD behave.
+ */
+export function reflowRect(
+ rect: FloatingRect,
+ anchor: FloatingAnchor,
+ previous: FloatingViewport,
+ next: FloatingViewport
+): FloatingRect {
+ const right = anchor === 'bottom-right' || anchor === 'top-right'
+ const bottom = anchor === 'bottom-left' || anchor === 'bottom-right'
+
+ return clampFloatingRect(
+ {
+ ...rect,
+ x: right ? rect.x + (next.width - previous.width) : rect.x,
+ y: bottom ? rect.y + (next.height - previous.height) : rect.y
+ },
+ next
+ )
+}
+
+/** Parse an authored CSS px length (`'216px'`, `216`) with a fallback. */
+export function floatingPx(value: number | string | undefined, fallback: number): number {
+ if (typeof value === 'number') {
+ return Number.isFinite(value) ? value : fallback
+ }
+
+ const parsed = Number.parseFloat(value ?? '')
+
+ return Number.isFinite(parsed) ? parsed : fallback
+}