The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): coalesce zone-editor split preview to one frame
brooklyn!421f9592b3af
diff --git a/apps/desktop/src/components/pane-shell/tree/zone-editor-split-preview.test.tsx b/apps/desktop/src/components/pane-shell/tree/zone-editor-split-preview.test.tsxnew file mode 100644index 00000000000..90104eade7f--- /dev/null+++ b/apps/desktop/src/components/pane-shell/tree/zone-editor-split-preview.test.tsx@@ -0,0 +1,209 @@+import { act, cleanup, fireEvent, render } from '@testing-library/react'+import { Profiler } from 'react'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'++import { $layoutTree } from './store'+import { $zoneEditorOpen, ZoneEditor } from './zone-editor'++// Split preview follows the pointer before a click. Pointermove outruns a frame,+// and a repeated event in the same snap cell must not write again. Shift flips+// the line on the next frame while the pointer is still in the canvas — including+// while the button is still down — and the grid is not written until that click+// commits.++type Frame = { cb: FrameRequestCallback; id: number }++let queued: Frame[] = []+let seq = 1+let commits = 0++function rect(): DOMRect {+ return {+ bottom: 1000,+ height: 1000,+ left: 0,+ right: 1000,+ toJSON: () => ({}),+ top: 0,+ width: 1000,+ x: 0,+ y: 0+ } as DOMRect+}++function installFrames() {+ queued = []+ seq = 1+ vi.stubGlobal('requestAnimationFrame', (cb: FrameRequestCallback) => {+ const id = seq+++ queued.push({ cb, id })++ return id+ })+ vi.stubGlobal('cancelAnimationFrame', (id: number) => {+ queued = queued.filter(frame => frame.id !== id)+ })+}++function flushFrames() {+ const batch = queued.splice(0, queued.length)++ act(() => {+ for (const frame of batch) {+ frame.cb(0)+ }+ })+}++function zoneLabels(canvas: HTMLElement) {+ return [...canvas.querySelectorAll('span')]+ .map(node => node.textContent ?? '')+ .filter(text => /^zone \d+$/.test(text))+}++function previewLine(canvas: HTMLElement): HTMLElement | null {+ return (+ [...canvas.querySelectorAll<HTMLElement>(':scope > div')].find(el => {+ return el.className.includes('pointer-events-none') && (el.style.width === '2px' || el.style.height === '2px')+ }) ?? null+ )+}++function mount() {+ $zoneEditorOpen.set(true)++ const view = render(+ <Profiler+ id="zone-editor"+ onRender={() => {+ commits += 1+ }}+ >+ <ZoneEditor />+ </Profiler>+ )++ const canvas = view.container.querySelector('.cursor-crosshair') as HTMLDivElement+ canvas.getBoundingClientRect = () => rect()++ return canvas+}++beforeEach(() => {+ commits = 0+ $layoutTree.set(null)+ $zoneEditorOpen.set(false)+ installFrames()+})++afterEach(() => {+ act(() => {+ $zoneEditorOpen.set(false)+ })+ cleanup()+ vi.unstubAllGlobals()+})++describe('zone editor split preview', () => {+ it('coalesces pointer moves to one preview update per frame', () => {+ const canvas = mount()+ const layout = $layoutTree.get()+ const painted = commits++ fireEvent.pointerMove(canvas, { clientX: 500, clientY: 400 })+ fireEvent.pointerMove(canvas, { clientX: 450, clientY: 420 })+ fireEvent.pointerMove(canvas, { clientX: 480, clientY: 410 })++ expect(previewLine(canvas)).toBeNull()+ expect(commits).toBe(painted)+ expect(queued).toHaveLength(1)+ expect(zoneLabels(canvas)).toHaveLength(3)+ expect($layoutTree.get()).toBe(layout)++ flushFrames()++ expect(commits).toBe(painted + 1)+ expect(previewLine(canvas)?.style.left).toBe('48%')+ expect(previewLine(canvas)?.style.width).toBe('2px')+ expect(zoneLabels(canvas)).toHaveLength(3)+ expect($layoutTree.get()).toBe(layout)+ })++ it('does not write when later pointer events snap to the same preview', () => {+ const canvas = mount()++ fireEvent.pointerMove(canvas, { clientX: 500, clientY: 500 })+ flushFrames()+ expect(previewLine(canvas)?.style.left).toBe('50%')++ const painted = commits++ fireEvent.pointerMove(canvas, { clientX: 501, clientY: 500 })+ fireEvent.pointerMove(canvas, { clientX: 502, clientY: 499 })+ flushFrames()+ fireEvent.pointerMove(canvas, { clientX: 500, clientY: 501 })+ flushFrames()++ expect(commits).toBe(painted)+ expect(previewLine(canvas)?.style.left).toBe('50%')+ expect(previewLine(canvas)?.style.width).toBe('2px')+ expect(zoneLabels(canvas)).toHaveLength(3)+ })++ it('recomputes orientation on Shift while the pointer remains in the canvas', () => {+ const canvas = mount()+ const layout = $layoutTree.get()++ fireEvent.pointerMove(canvas, { clientX: 500, clientY: 500 })+ flushFrames()+ expect(previewLine(canvas)?.style.width).toBe('2px')++ fireEvent.keyDown(window, { key: 'Shift' })++ // Shift state may render immediately; the line waits for the next frame.+ expect(previewLine(canvas)?.style.width).toBe('2px')+ expect(zoneLabels(canvas)).toHaveLength(3)+ const afterKey = commits++ flushFrames()++ expect(previewLine(canvas)?.style.height).toBe('2px')+ expect(commits).toBe(afterKey + 1)+ expect(previewLine(canvas)?.style.top).toBe('50%')+ expect(zoneLabels(canvas)).toHaveLength(3)+ expect($layoutTree.get()).toBe(layout)++ fireEvent.keyUp(window, { key: 'Shift' })+ flushFrames()++ expect(previewLine(canvas)?.style.width).toBe('2px')+ expect(previewLine(canvas)?.style.left).toBe('50%')+ expect(zoneLabels(canvas)).toHaveLength(3)+ })++ it('recomputes orientation on Shift while the pointer is still down and commits the grid only on release', () => {+ const canvas = mount()+ const layout = $layoutTree.get()++ fireEvent.pointerMove(canvas, { clientX: 500, clientY: 500 })+ flushFrames()++ fireEvent.pointerDown(canvas, { button: 0, clientX: 500, clientY: 500 })+ fireEvent.keyDown(window, { key: 'Shift' })+ flushFrames()++ expect(previewLine(canvas)?.style.height).toBe('2px')+ expect(zoneLabels(canvas)).toHaveLength(3)+ expect($layoutTree.get()).toBe(layout)++ fireEvent.pointerUp(window, { button: 0, clientX: 500, clientY: 500 })++ expect(zoneLabels(canvas)).toHaveLength(4)+ expect(+ [...canvas.querySelectorAll<HTMLElement>(':scope > div')].some(+ el => /^zone \d+$/.test(el.textContent ?? '') && el.style.height === '50%'+ )+ ).toBe(true)+ expect($layoutTree.get()).toBe(layout)+ })+})diff --git a/apps/desktop/src/components/pane-shell/tree/zone-editor.tsx b/apps/desktop/src/components/pane-shell/tree/zone-editor.tsxindex fe40ca2c8bb..4eddc7abbcf 100644--- a/apps/desktop/src/components/pane-shell/tree/zone-editor.tsx+++ b/apps/desktop/src/components/pane-shell/tree/zone-editor.tsx@@ -26,6 +26,7 @@ import { registry } from '@/contrib/registry'import { useI18n } from '@/i18n'import { ESCAPE_PRIORITY, isTopEscapeLayer, pushEscapeLayer } from '@/lib/escape-layers'import { startPointerDrag } from '@/lib/pointer-drag'+import { rafCoalesce } from '@/lib/raf-coalesce'import { cn } from '@/lib/utils'import {@@ -62,6 +63,18 @@ interface SplitPreview {position: number}+function sameSplitPreview(a: SplitPreview | null, b: SplitPreview | null) {+ if (a === b) {+ return true+ }++ if (!a || !b) {+ return false+ }++ return a.zoneIndex === b.zoneIndex && a.orientation === b.orientation && a.position === b.position+}+interface SelectBox {x0: numbery0: number@@ -151,30 +164,104 @@ export function ZoneEditor() {}}, [])- if (!open) {- return null- }+ const modelRef = useRef(model)+ modelRef.current = model+ const shiftRef = useRef(shift)+ shiftRef.current = shift+ const openRef = useRef(open)+ openRef.current = open+ const previewRef = useRef<SplitPreview | null>(null)+ const insideRef = useRef(false)+ const lastPointRef = useRef<{ x: number; y: number } | null>(null)+ const generationRef = useRef(0)++ const publishPreview = useCallback((next: SplitPreview | null) => {+ if (sameSplitPreview(previewRef.current, next)) {+ return+ }++ previewRef.current = next+ setSplitPreview(next)+ }, [])++ const computePreview = useCallback(+ (clientX: number, clientY: number) => {+ if (!openRef.current || !canvasRef.current) {+ return+ }- const zoneAt = (x: number, y: number) =>- zones.find(z => x >= z.left && x < z.right && y >= z.top && y < z.bottom) ?? null+ const p = toModelPoint(clientX, clientY)+ const zonesNow = modelToZones(modelRef.current) ?? []+ const zone = zonesNow.find(z => p.x >= z.left && p.x < z.right && p.y >= z.top && p.y < z.bottom) ?? null- const updateSplitPreview = (clientX: number, clientY: number) => {- const p = toModelPoint(clientX, clientY)- const zone = zoneAt(p.x, p.y)+ if (!zone) {+ publishPreview(null)- if (!zone) {- setSplitPreview(null)+ return+ }++ const orientation = shiftRef.current ? 'horizontal' : 'vertical'+ const raw = orientation === 'horizontal' ? p.y : p.x+ const position = Math.round(raw / SPLIT_SNAP) * SPLIT_SNAP++ publishPreview(+ canSplit(modelRef.current, zone.index, position, orientation)+ ? { zoneIndex: zone.index, orientation, position }+ : null+ )+ },+ [publishPreview, toModelPoint]+ )+ const computeRef = useRef(computePreview)+ computeRef.current = computePreview++ const coalesceRef = useRef<ReturnType<typeof rafCoalesce<{ gen: number; x: number; y: number }>> | null>(null)++ if (coalesceRef.current === null) {+ // pointermove outruns a frame; keep the latest point and paint once.+ coalesceRef.current = rafCoalesce(point => {+ if (point.gen !== generationRef.current) {+ return+ }++ computeRef.current(point.x, point.y)+ })+ }++ // Shift flips orientation on the next frame while the pointer is still inside,+ // including while the button is held. Preview only — the grid waits for the click.+ useEffect(() => {+ const point = lastPointRef.current++ if (!open || !insideRef.current || !point) {return}- const orientation = shift ? 'horizontal' : 'vertical'- const raw = orientation === 'horizontal' ? p.y : p.x- const position = Math.round(raw / SPLIT_SNAP) * SPLIT_SNAP+ coalesceRef.current?.push({ gen: generationRef.current, x: point.x, y: point.y })+ }, [open, shift])- setSplitPreview(- canSplit(model, zone.index, position, orientation) ? { zoneIndex: zone.index, orientation, position } : null- )+ if (!open) {+ if (insideRef.current || lastPointRef.current) {+ generationRef.current += 1+ insideRef.current = false+ lastPointRef.current = null+ }++ return null+ }++ const onCanvasPointerMove = (e: ReactPointerEvent<HTMLDivElement>) => {+ insideRef.current = true+ lastPointRef.current = { x: e.clientX, y: e.clientY }+ coalesceRef.current?.push({ gen: generationRef.current, x: e.clientX, y: e.clientY })+ }++ const onCanvasPointerLeave = () => {+ generationRef.current += 1+ insideRef.current = false+ lastPointRef.current = null+ publishPreview(null)}const onCanvasPointerDown = (e: ReactPointerEvent<HTMLDivElement>) => {@@ -183,6 +270,7 @@ export function ZoneEditor() {}e.preventDefault()+ coalesceRef.current?.finish()setMergeAt(null)setSelection([])@@ -216,13 +304,17 @@ export function ZoneEditor() {}startPointerDrag(onMove, ev => {+ coalesceRef.current?.finish()setSelectBox(null)if (!dragged) {- // Plain click: split at the previewed line.- if (splitPreview) {- setModel(m => splitZone(m, splitPreview.zoneIndex, splitPreview.position, splitPreview.orientation))- setSplitPreview(null)+ // Plain click: split at the previewed line. Read the ref — Shift may+ // have flipped it while the button was down, after this closure was made.+ const preview = previewRef.current++ if (preview) {+ setModel(m => splitZone(m, preview.zoneIndex, preview.position, preview.orientation))+ publishPreview(null)}setSelection([])@@ -353,8 +445,8 @@ export function ZoneEditor() {<divclassName="relative min-h-0 flex-1 cursor-crosshair overflow-hidden rounded-lg border border-(--ui-stroke-secondary)"onPointerDown={onCanvasPointerDown}- onPointerLeave={() => setSplitPreview(null)}- onPointerMove={e => updateSplitPreview(e.clientX, e.clientY)}+ onPointerLeave={onCanvasPointerLeave}+ onPointerMove={onCanvasPointerMove}ref={canvasRef}>{zones.map(zone => {