Merge pull request #174 from nwparker/cloud/w2-predict-drag

fix(cloud): predictive wake: any row drag cancels the press wake (m4air finding)
This commit is contained in:
Neil
2026-11-09 21:57:26 -08:00
committed by GitHub
3 changed files with 107 additions and 5 deletions
@@ -143,7 +143,7 @@ export function useWorktreeCardActivationActions({
) {
return
}
noteWorktreeRowPress(worktree.id)
noteWorktreeRowPress(worktree.id, event.clientX, event.clientY)
},
[isActive, isDeleting, isSshDisconnected, worktree.id]
)
@@ -0,0 +1,66 @@
// @vitest-environment happy-dom
import { beforeEach, describe, expect, it, vi } from 'vitest'
const wake = vi.hoisted(() => ({
dragStart: vi.fn(),
dragEnd: vi.fn(),
hoverStart: vi.fn(),
hoverEnd: vi.fn(),
select: vi.fn()
}))
vi.mock('@/lib/orca-cloud/cloud-predictive-wake', () => ({
notePredictiveWakeDragStart: wake.dragStart,
notePredictiveWakeDragEnd: wake.dragEnd,
notePredictiveWakeHoverStart: wake.hoverStart,
notePredictiveWakeHoverEnd: wake.hoverEnd,
notePredictiveWakeSelect: wake.select
}))
vi.mock('../../../terminal/terminal-worktree-prewarm', () => ({
setTerminalPrewarmWorktrees: vi.fn()
}))
import { ROW_DRAG_SLOP_PX, noteWorktreeRowPress } from './worktree-row-hover-prewarm'
const pointer = (type: string, init: { x?: number; y?: number; buttons?: number } = {}) =>
window.dispatchEvent(
new MouseEvent(type, { clientX: init.x ?? 0, clientY: init.y ?? 0, buttons: init.buttons ?? 0 })
)
describe('row press and drag intent', () => {
beforeEach(() => {
for (const fn of Object.values(wake)) {
fn.mockClear()
}
})
it('a press released in place wakes on the release, not the press', () => {
noteWorktreeRowPress('ws-a', 100, 100)
expect(wake.select).not.toHaveBeenCalled()
pointer('pointermove', { x: 101, y: 101, buttons: 1 })
pointer('pointerup', { x: 101, y: 101 })
expect(wake.select).toHaveBeenCalledWith('ws-a')
expect(wake.dragStart).not.toHaveBeenCalled()
})
it('a press that moves past the slop is a drag (any drag code): no wake, and it ends on release', () => {
noteWorktreeRowPress('ws-a', 100, 100)
pointer('pointermove', { x: 100, y: 100 + ROW_DRAG_SLOP_PX + 2, buttons: 1 })
expect(wake.dragStart).toHaveBeenCalledTimes(1)
pointer('pointermove', { x: 100, y: 180, buttons: 1 })
expect(wake.dragStart).toHaveBeenCalledTimes(1)
pointer('pointerup', { x: 100, y: 180 })
expect(wake.dragEnd).toHaveBeenCalledTimes(1)
expect(wake.select).not.toHaveBeenCalled()
})
it('a native drag whose end never reaches the window is cleared by the next buttonless move', () => {
noteWorktreeRowPress('ws-a', 100, 100)
window.dispatchEvent(new Event('dragstart'))
pointer('pointercancel')
expect(wake.dragStart).toHaveBeenCalledTimes(1)
pointer('pointermove', { x: 300, y: 300, buttons: 0 })
expect(wake.dragEnd).toHaveBeenCalledTimes(1)
expect(wake.select).not.toHaveBeenCalled()
})
})
@@ -22,6 +22,13 @@ let predictiveHoverId: string | null = null
// The row under a press. Its wake waits for the release: a press held still and then dragged is a
// move, and a debounce alone can't tell the two apart.
let pressedId: string | null = null
let pressX = 0
let pressY = 0
// Set while a row drag is under way, by whichever drag code noticed it first.
let rowDragActive = false
/** Pointer travel with the button held that makes a press a drag (the sidebar's own drags start
* within a few pixels; every drag implementation moves at least this far). */
export const ROW_DRAG_SLOP_PX = 4
function clearDwell(): void {
if (dwellTimer !== null) {
@@ -74,12 +81,33 @@ function installWindowListeners(): void {
window.addEventListener('dragstart', () => noteWorktreeRowDragStart(), { capture: true })
window.addEventListener('dragend', () => noteWorktreeRowDragEnd(), { capture: true })
window.addEventListener('drop', () => noteWorktreeRowDragEnd(), { capture: true })
// Any drag code (native, the sidebar's pointer drag, the board's) moves the pointer with the
// button held: past the slop, the press is a drag whatever implementation runs it.
window.addEventListener(
'pointermove',
(event) => {
if (rowDragActive && event.buttons === 0) {
// A drag whose end never reached the window (its source unmounted mid-drag).
noteWorktreeRowDragEnd()
return
}
if (
pressedId !== null &&
Math.hypot(event.clientX - pressX, event.clientY - pressY) > ROW_DRAG_SLOP_PX
) {
noteWorktreeRowDragStart()
}
},
{ capture: true, passive: true }
)
window.addEventListener(
'pointerup',
() => {
const released = pressedId
pressedId = null
if (released !== null) {
if (rowDragActive) {
noteWorktreeRowDragEnd()
} else if (released !== null) {
notePredictiveWakeSelect(released)
}
},
@@ -98,11 +126,17 @@ function installWindowListeners(): void {
export function noteWorktreeRowDragStart(): void {
predictiveHoverId = null
pressedId = null
notePredictiveWakeDragStart()
if (!rowDragActive) {
rowDragActive = true
notePredictiveWakeDragStart()
}
}
export function noteWorktreeRowDragEnd(): void {
notePredictiveWakeDragEnd()
if (rowDragActive) {
rowDragActive = false
notePredictiveWakeDragEnd()
}
}
export function noteWorktreeRowHoverStart(worktreeId: string): void {
@@ -119,11 +153,13 @@ export function noteWorktreeRowHoverStart(worktreeId: string): void {
}
/** A press is intent enough to skip the hover dwell; the mount still waits a task so it can't delay the press's focus move or drag start. */
export function noteWorktreeRowPress(worktreeId: string): void {
export function noteWorktreeRowPress(worktreeId: string, x = 0, y = 0): void {
installWindowListeners()
// The press may become a drag: hold the wake (and stop the hover dwell's) until the release.
notePredictiveWakeHoverEnd(worktreeId)
pressedId = worktreeId
pressX = x
pressY = y
if (prewarmedWorktreeId !== worktreeId) {
armDwell(worktreeId, 0)
}