diff --git a/src/renderer/src/components/browser-pane/host-guest/input/agent-input-focus-guard.ts b/src/renderer/src/components/browser-pane/host-guest/input/agent-input-focus-guard.ts index 9133e714964..1b08de67685 100644 --- a/src/renderer/src/components/browser-pane/host-guest/input/agent-input-focus-guard.ts +++ b/src/renderer/src/components/browser-pane/host-guest/input/agent-input-focus-guard.ts @@ -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 } diff --git a/src/renderer/src/components/tab-bar/tab-strip-pointer-activation.test.tsx b/src/renderer/src/components/tab-bar/tab-strip-pointer-activation.test.tsx index 7178f9723f8..b4731228e41 100644 --- a/src/renderer/src/components/tab-bar/tab-strip-pointer-activation.test.tsx +++ b/src/renderer/src/components/tab-bar/tab-strip-pointer-activation.test.tsx @@ -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) + }) }) diff --git a/src/renderer/src/components/tab-bar/tab-strip-pointer-activation.ts b/src/renderer/src/components/tab-bar/tab-strip-pointer-activation.ts index d5980159580..91841a1a6de 100644 --- a/src/renderer/src/components/tab-bar/tab-strip-pointer-activation.ts +++ b/src/renderer/src/components/tab-bar/tab-strip-pointer-activation.ts @@ -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 },