feat(shortcuts): rework shortcut bindings, focus restore, and input guards

Resolve effective bindings from stored prefs plus defaults with leader/direct collision handling and default leader Ctrl+X; let direct actions pass through in terminals and suppress abort while an overlay is open. Add half-page scroll disengage, verified overlay focus restore, and consolidate prompt reset.
This commit is contained in:
Chris Scott committed 2026-10-08 12:40:04 -04:00
1 parent 7e8443f54f
commit 994a8b82e8
33 files changed
+784 -195

No files matched your search

@@ -9,8 +9,17 @@ import type { Provider } from '@/api/providers'
const mocks = vi.hoisted(() => ({
useModelSelection: vi.fn(),
useModelSections: vi.fn(),
deferredSearchValue: { current: null as string | null },
}))
vi.mock('react', async (importOriginal) => {
const actual = await importOriginal<typeof import('react')>()
return {
...actual,
useDeferredValue: (value: string) => mocks.deferredSearchValue.current ?? value,
}
})
vi.mock('@/hooks/useModelSelection', () => ({
useModelSelection: mocks.useModelSelection,
}))
@@ -151,8 +160,23 @@ function focusDetachedTextarea() {
return textarea
}
const originalInnerWidth = window.innerWidth
function setViewportWidth(width: number) {
Object.defineProperty(window, 'innerWidth', { configurable: true, writable: true, value: width })
}
function mobileModelList() {
const lists = Array.from(document.querySelectorAll<HTMLElement>('.overflow-y-auto.px-4.pb-4'))
const list = lists.find((element) => !element.classList.contains('pt-2'))
if (!list) throw new Error('No mobile model list found')
return list
}
afterEach(() => {
document.querySelectorAll('textarea').forEach((element) => element.remove())
setViewportWidth(originalInnerWidth)
mocks.deferredSearchValue.current = null
})
beforeAll(() => {
@@ -453,4 +477,89 @@ describe('ModelQuickSelect keyboard navigation', () => {
expect(fireEvent.keyDown(textarea, { key: 'ArrowDown' })).toBe(true)
})
it('ignores keyboard navigation when the mobile provider list hides the models', async () => {
const user = userEvent.setup()
const { setModel } = setModelSelection()
setViewportWidth(500)
render(<ModelQuickSelect open />)
await user.click(screen.getByRole('button', { name: /More models/ }))
const textarea = focusDetachedTextarea()
expect(fireEvent.keyDown(textarea, { key: 'ArrowDown' })).toBe(true)
expect(fireEvent.keyDown(textarea, { key: 'Enter' })).toBe(true)
expect(setModel).not.toHaveBeenCalled()
})
it('navigates the provider models that are visible on mobile', async () => {
const user = userEvent.setup()
const { setModel } = setModelSelection()
setViewportWidth(500)
render(<ModelQuickSelect open />)
await user.click(screen.getByRole('button', { name: /More models/ }))
await user.click(screen.getAllByRole('button', { name: /Anthropic 2 models/ })[0])
const textarea = focusDetachedTextarea()
expect(fireEvent.keyDown(textarea, { key: 'ArrowDown' })).toBe(false)
expect(fireEvent.keyDown(textarea, { key: 'Enter' })).toBe(false)
expect(setModel).toHaveBeenCalledWith(selection('anthropic', 'claude-opus-5-5-fast'))
})
it('ignores keydown events while an IME composition is active', () => {
const { setModel } = setModelSelection({ model: selection('opencode-go', 'go-1') })
render(<ModelQuickSelect open />)
const textarea = focusDetachedTextarea()
expect(fireEvent.keyDown(textarea, { key: 'ArrowDown', isComposing: true })).toBe(true)
expect(fireEvent.keyDown(textarea, { key: 'Enter', isComposing: true })).toBe(true)
expect(setModel).not.toHaveBeenCalled()
})
it('does not select while the deferred search query is still catching up', async () => {
const user = userEvent.setup()
const { setModel } = setModelSelection()
render(<ModelQuickSelect open />)
await user.click(screen.getByRole('button', { name: /More models/ }))
const searchInput = screen.getByPlaceholderText('Search models...')
mocks.deferredSearchValue.current = ''
await user.type(searchInput, '5')
expect(fireEvent.keyDown(searchInput, { key: 'Enter' })).toBe(false)
expect(setModel).not.toHaveBeenCalled()
})
it('scrolls the mobile provider model list to follow the active row', async () => {
const user = userEvent.setup()
const clientHeight = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'clientHeight')
Object.defineProperty(HTMLElement.prototype, 'clientHeight', { configurable: true, get: () => 60 })
setViewportWidth(500)
try {
render(<ModelQuickSelect open />)
await user.click(screen.getByRole('button', { name: /More models/ }))
await user.click(screen.getAllByRole('button', { name: /Anthropic 2 models/ })[0])
const list = mobileModelList()
expect(list.scrollTop).toBe(0)
const textarea = focusDetachedTextarea()
fireEvent.keyDown(textarea, { key: 'ArrowDown' })
expect(list.scrollTop).toBe(60)
} finally {
Object.defineProperty(HTMLElement.prototype, 'clientHeight', clientHeight!)
}
})
})
@@ -1,6 +1,7 @@
import { useCallback, useDeferredValue, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
import { Check, ChevronDown, ChevronLeft, ChevronRight, Search, Star, Trash2, X, type LucideIcon } from 'lucide-react'
import { useModelSelection, type ModelSelectionSession } from '@/hooks/useModelSelection'
import { useMobile } from '@/hooks/useMobile'
import { useModelSections } from '@/hooks/useModelSections'
import { useVariants } from '@/hooks/useVariants'
import { modelSelectionRef, type Model } from '@/api/providers'
@@ -14,6 +15,7 @@ import {
} from '@/components/ui/dropdown-menu'
import { Input } from '@/components/ui/input'
import { buildModelSections, filterModelSections, type ModelOption } from '@/lib/modelSections'
import { isTextEntryElement } from '@/lib/domTargets'
interface ModelQuickSelectProps {
directory?: string
@@ -183,6 +185,7 @@ export function ModelQuickSelect({
const [selectedProviderId, setSelectedProviderId] = useState<string | null>(null)
const [activeIndex, setActiveIndex] = useState(0)
const quickListRef = useRef<HTMLDivElement>(null)
const isMobile = useMobile()
const { model, modelString, info, recentModels, favoriteModels, setModel, toggleFavorite, removeRecentModel } = useModelSelection(directory, session)
const { availableVariants, currentVariant, setVariant, clearVariant, hasVariants } = useVariants(directory, session)
@@ -286,10 +289,12 @@ export function ModelQuickSelect({
const navigableItems = useMemo((): ModelListItem[] => {
if (showAllModels) {
return isSearching ? searchResults : browseModels
if (isSearching) return searchResults
if (isMobile && !selectedProviderId) return EMPTY_MODELS
return browseModels
}
return quickSections.flatMap((section) => section.models)
}, [showAllModels, isSearching, searchResults, browseModels, quickSections])
}, [showAllModels, isSearching, isMobile, selectedProviderId, searchResults, browseModels, quickSections])
const quickSectionOffsets = useMemo(() => {
const offsets = new Map<string, number>()
@@ -306,6 +311,10 @@ export function ModelQuickSelect({
navigableItemsRef.current = navigableItems
const modelStringRef = useRef(modelString)
modelStringRef.current = modelString
const searchQueryRef = useRef(searchQuery)
searchQueryRef.current = searchQuery
const deferredSearchQueryRef = useRef(deferredSearchQuery)
deferredSearchQueryRef.current = deferredSearchQuery
useEffect(() => {
const selectedIndex = navigableItemsRef.current.findIndex((item) => item.key === modelStringRef.current)
@@ -341,15 +350,20 @@ export function ModelQuickSelect({
const handleKeyDown = (event: KeyboardEvent) => {
if (event.ctrlKey || event.metaKey || event.altKey) return
if (event.isComposing) return
const target = event.target
if (target instanceof HTMLElement && target.closest('[role="menu"]')) return
const isTextInput =
target instanceof HTMLElement && (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA')
const isTextInput = target instanceof Element && isTextEntryElement(target)
if (event.key === 'Enter') {
if (target instanceof HTMLElement && target.closest('button')) return
if (searchQueryRef.current !== deferredSearchQueryRef.current) {
event.preventDefault()
event.stopPropagation()
return
}
const item = navigableItems[activeIndex]
if (!item) return
event.preventDefault()
@@ -676,6 +690,7 @@ export function ModelQuickSelect({
emptyLabel="No models found"
className="px-4 pb-4"
resetKey={selectedProviderId}
activeIndex={activeIndex}
/>
) : (
<div className="h-full overflow-y-auto px-4 pb-4">