fix(tabs): keep tab clicks while an agent clicks in a browser page (#42663)

An agent's click in a browser page moves window focus into that page's
guest and Orca's focus guard moves it back. A tab press held across that
bounce read it as the user leaving the window and was dropped.
This commit is contained in:
Kelvin Amoaba
2026-11-15 08:29:30 +00:00
committed by GitHub
parent e9d8eb25e4
commit af63dfa22b
3 changed files with 56 additions and 2 deletions
@@ -21,6 +21,11 @@ function settle(): void {
}
}
/** Agent input bounces window blur/focus through its guest while a guard is open; the user did not leave the window. */
export function isAgentInputMovingFocus(): boolean {
return inFlight > 0
}
function isUserComposing(): boolean {
return composingElement !== null && composingElement === document.activeElement
}
@@ -4,6 +4,12 @@ import { act, renderHook } from '@testing-library/react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { useTabStripPointerActivation } from './tab-strip-pointer-activation'
import {
beginAgentInputFocusGuard,
clearAgentInputFocusGuards,
endAgentInputFocusGuard
} from '../browser-pane/host-guest/input/agent-input-focus-guard'
import { webviewRegistry } from '../browser-pane/host-guest/webview-registry'
function pointerDownEvent(clientX: number, clientY: number, button = 0): React.PointerEvent {
return { button, clientX, clientY } as unknown as React.PointerEvent
@@ -16,6 +22,13 @@ function firePointer(type: string, clientX: number, clientY: number): void {
}
afterEach(() => {
clearAgentInputFocusGuards()
if (vi.isFakeTimers()) {
vi.runAllTimers()
}
vi.useRealTimers()
webviewRegistry.clear()
document.body.replaceChildren()
vi.clearAllMocks()
})
@@ -68,4 +81,26 @@ describe('useTabStripPointerActivation', () => {
expect(onActivate).not.toHaveBeenCalled()
guest.remove()
})
// An agent's click in another page moves window focus into its guest and back; the user never left (#42119).
it('keeps a press across the window focus bounce of agent input', () => {
vi.useFakeTimers()
const onActivate = vi.fn()
const agentPage = document.createElement('webview')
document.body.append(agentPage)
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the guard only reads focus state from the element.
webviewRegistry.set('agent-page', agentPage as unknown as Electron.WebviewTag)
const { result } = renderHook(() => useTabStripPointerActivation({ onActivate }))
act(() => result.current.onPointerDown(pointerDownEvent(10, 10)))
expect(beginAgentInputFocusGuard('agent-page', 'agent-click')).toBe('ready')
act(() => window.dispatchEvent(new Event('blur')))
endAgentInputFocusGuard('agent-click')
vi.runAllTimers()
// The restored focus can reach the window after the guard has settled.
act(() => window.dispatchEvent(new Event('focus')))
firePointer('pointerup', 11, 11)
expect(onActivate).toHaveBeenCalledTimes(1)
})
})
@@ -1,6 +1,7 @@
import { useCallback, useEffect, useRef } from 'react'
import { TAB_DRAG_ACTIVATION_DISTANCE_PX } from '../tab-group/useTabDragSplit'
import { beginTabStripPointerGesture } from './tab-strip-pointer-gesture'
import { isAgentInputMovingFocus } from '../browser-pane/host-guest/input/agent-input-focus-guard'
/** Activate from release displacement, not asynchronously cleared drag state, so drags stay inactive and post-drag clicks still work. */
function isGuestHoldingKeyboard(): boolean {
@@ -48,7 +49,7 @@ export function useTabStripPointerActivation({
window.removeEventListener('pointerup', onPointerUp)
window.removeEventListener('pointercancel', onPointerCancel)
window.removeEventListener('contextmenu', onPointerCancel)
window.removeEventListener('blur', onPointerCancel)
window.removeEventListener('blur', onWindowBlur)
window.removeEventListener('focus', onWindowFocus)
releaseTabStripPointerGesture()
cleanupRef.current = null
@@ -66,7 +67,20 @@ export function useTabStripPointerActivation({
const onPointerCancel = (): void => {
cleanup()
}
// Pair an agent-caused blur with its focus, which can arrive after the guard settles.
let agentBlurPending = false
const onWindowBlur = (): void => {
if (isAgentInputMovingFocus()) {
agentBlurPending = true
return
}
cleanup()
}
const onWindowFocus = (): void => {
if (agentBlurPending || isAgentInputMovingFocus()) {
agentBlurPending = false
return
}
if (pendingGuestFocusHandoff) {
pendingGuestFocusHandoff = false
return
@@ -77,7 +91,7 @@ export function useTabStripPointerActivation({
window.addEventListener('pointerup', onPointerUp)
window.addEventListener('pointercancel', onPointerCancel)
window.addEventListener('contextmenu', onPointerCancel)
window.addEventListener('blur', onPointerCancel)
window.addEventListener('blur', onWindowBlur)
window.addEventListener('focus', onWindowFocus)
cleanupRef.current = cleanup
},