diff --git a/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionOptionPickers.test.tsx b/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionOptionPickers.test.tsx index 4584ed2a1ed..dcf326a81c4 100644 --- a/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionOptionPickers.test.tsx +++ b/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionOptionPickers.test.tsx @@ -45,6 +45,7 @@ it('exposes the current selection on keyboard focus and follows the chat snapsho ) const view = render(picker([model, effort])) + fireEvent.keyDown(window, { key: 'Tab' }) fireEvent.focus(screen.getByRole('button')) const tooltip = await screen.findByRole('tooltip') expect(tooltip.textContent).toBe('Chat model ยท Extra high') @@ -105,6 +106,7 @@ it('keeps a working model-only picker inspectable without allowing changes', asy const view = render(picker(true)) const informationalTrigger = screen.getByRole('button', { name: 'Model Chat model' }) expect(informationalTrigger.getAttribute('aria-disabled')).toBe('true') + fireEvent.keyDown(window, { key: 'Tab' }) act(() => informationalTrigger.focus()) expect(document.activeElement).toBe(informationalTrigger) expect((await screen.findByRole('tooltip')).textContent).toBe('Chat model') diff --git a/src/renderer/src/components/ui/tooltip.test.tsx b/src/renderer/src/components/ui/tooltip.test.tsx new file mode 100644 index 00000000000..335a4678083 --- /dev/null +++ b/src/renderer/src/components/ui/tooltip.test.tsx @@ -0,0 +1,77 @@ +// @vitest-environment happy-dom +import { act } from 'react' +import { createRoot, type Root } from 'react-dom/client' +import { afterEach, beforeEach, expect, it, vi } from 'vitest' +import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from './tooltip' + +let root: Root | null = null + +beforeEach(() => { + vi.useFakeTimers() + root = createRoot(document.body.appendChild(document.createElement('div'))) + act(() => + root!.render( + + + + + + Open settings + + + ) + ) +}) + +afterEach(() => { + act(() => root?.unmount()) + root = null + document.body.innerHTML = '' + vi.useRealTimers() +}) + +function trigger(): HTMLButtonElement { + return document.querySelector('button')! +} + +function tooltipOpen(): boolean { + act(() => vi.runAllTimers()) + return document.querySelector('[role="tooltip"]') !== null +} + +it('opens on keyboard focus', () => { + window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Tab' })) + act(() => trigger().focus()) + expect(tooltipOpen()).toBe(true) +}) + +it('stays closed when focus is restored after a pointer interaction', () => { + window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Tab' })) + document.body.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true })) + act(() => trigger().focus()) + expect(tooltipOpen()).toBe(false) +}) + +it.each([ + ['a click', ['pointerdown', 'mousedown', 'pointerup']], + ['a touch that became a scroll', ['pointerdown', 'pointercancel']] +])('opens when assistive tech moves focus after %s', (_, events) => { + for (const type of events) { + document.body.dispatchEvent(new PointerEvent(type, { bubbles: true })) + } + vi.runAllTimers() + // Screen-reader focus arrives as a focus event with no preceding input or script focus() call. + act(() => { + trigger().dispatchEvent(new FocusEvent('focusin', { bubbles: true })) + }) + expect(tooltipOpen()).toBe(true) +}) + +it('opens on hover', () => { + act(() => { + trigger().dispatchEvent( + new PointerEvent('pointermove', { bubbles: true, pointerType: 'mouse' }) + ) + }) + expect(tooltipOpen()).toBe(true) +}) diff --git a/src/renderer/src/components/ui/tooltip.tsx b/src/renderer/src/components/ui/tooltip.tsx index bdbbcb46801..a28d0f214db 100644 --- a/src/renderer/src/components/ui/tooltip.tsx +++ b/src/renderer/src/components/ui/tooltip.tsx @@ -3,6 +3,7 @@ import * as TooltipPrimitive from 'radix-ui/tooltip' import { cva, type VariantProps } from 'class-variance-authority' import { cn } from '@/lib/class-names' +import { isKeyboardOrVirtualFocus } from '@/lib/keyboard/keyboard-input-modality' import { PortalContainerContext, usePortalContainer } from './portal-container-context' function TooltipProvider({ @@ -27,8 +28,24 @@ function Tooltip({ ...props }: React.ComponentProps } -function TooltipTrigger({ ...props }: React.ComponentProps) { - return +function TooltipTrigger({ + onFocus, + ...props +}: React.ComponentProps) { + return ( + { + onFocus?.(event) + // Why: focus restored to the trigger after a mouse-closed dialog/menu/panel must not pop the + // label; preventDefault skips Radix's open-on-focus unless keyboard or assistive tech moved focus. + if (!isKeyboardOrVirtualFocus()) { + event.preventDefault() + } + }} + {...props} + /> + ) } const TAB_PREVIEW_CLASSES = diff --git a/src/renderer/src/lib/keyboard/keyboard-input-modality.test.ts b/src/renderer/src/lib/keyboard/keyboard-input-modality.test.ts new file mode 100644 index 00000000000..ac174f34dc0 --- /dev/null +++ b/src/renderer/src/lib/keyboard/keyboard-input-modality.test.ts @@ -0,0 +1,25 @@ +// @vitest-environment happy-dom +import { expect, it } from 'vitest' +import { isKeyboardOrVirtualFocus, stopFocusModalityTracking } from './keyboard-input-modality' + +it('counts focus from clicking a label as pointer focus', () => { + const input = document.body.appendChild(document.createElement('input')) + window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Tab' })) + for (const type of ['pointerdown', 'mousedown', 'pointerup', 'mouseup', 'click']) { + document.body.dispatchEvent(new PointerEvent(type, { bubbles: true })) + } + // The browser focuses a label's control after the release events, in the same task. + input.dispatchEvent(new FocusEvent('focusin', { bubbles: true })) + expect(isKeyboardOrVirtualFocus()).toBe(false) +}) + +it('stops following input once tracking is stopped', () => { + const button = document.body.appendChild(document.createElement('button')) + window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Tab' })) + expect(isKeyboardOrVirtualFocus()).toBe(true) + + stopFocusModalityTracking() + document.body.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true })) + button.focus() + expect(isKeyboardOrVirtualFocus()).toBe(true) +}) diff --git a/src/renderer/src/lib/keyboard/keyboard-input-modality.ts b/src/renderer/src/lib/keyboard/keyboard-input-modality.ts new file mode 100644 index 00000000000..1f576f2c2d8 --- /dev/null +++ b/src/renderer/src/lib/keyboard/keyboard-input-modality.ts @@ -0,0 +1,101 @@ +// 'virtual' is focus moved by assistive tech (e.g. VoiceOver), which sends no DOM input first. +type FocusModality = 'keyboard' | 'pointer' | 'virtual' + +let modality: FocusModality | null = null +let inputSinceLastFocus = false +let windowBlurredSinceLastFocus = false +let scriptFocusDepth = 0 +let stopTracking: (() => void) | null = null + +function onKeyDown(event: KeyboardEvent): void { + inputSinceLastFocus = true + // Shortcuts like Cmd+, are commands, not keyboard navigation. + if (!event.metaKey && !event.ctrlKey && !event.altKey) { + modality = 'keyboard' + } +} + +function onPointerDown(): void { + inputSinceLastFocus = true + modality = 'pointer' +} + +// A press has moved focus once its release task ends (a