Skip to content

The agent that grows with you

fix(desktop): refocus find bar input on repeated Cmd-F

brooklyn!e7552113274e
diff --git a/apps/desktop/src/app/chat/sidebar/connection-switcher.test.tsx b/apps/desktop/src/app/chat/sidebar/connection-switcher.test.tsx
index fff77bccbfe..e0362e004b4 100644
--- a/apps/desktop/src/app/chat/sidebar/connection-switcher.test.tsx
+++ b/apps/desktop/src/app/chat/sidebar/connection-switcher.test.tsx
@@ -76,7 +76,7 @@ afterEach(() => {
$connectionsRegistry.set(null)
$activeConnectionId.set('local')
$pendingConnectionId.set(null)
- $findInPage.set({ active: false, query: '', matchOrdinal: 0, matchCount: 0 })
+ $findInPage.set({ active: false, query: '', matchOrdinal: 0, matchCount: 0, focusRequest: 0 })
})
describe('ConnectionSwitcher', () => {
@@ -185,7 +185,7 @@ describe('ConnectionSwitcher', () => {
fireEvent.keyDown(search, { key: 'ArrowDown' })
expect(globalThis.document.activeElement).toBe(result)
- $findInPage.set({ active: true, query: '', matchOrdinal: 0, matchCount: 0 })
+ $findInPage.set({ active: true, query: '', matchOrdinal: 0, matchCount: 0, focusRequest: 0 })
result.focus()
fireEvent.keyDown(result, { key: 'f', metaKey: true })
expect(globalThis.document.activeElement).toBe(search)
diff --git a/apps/desktop/src/components/find-bar.test.tsx b/apps/desktop/src/components/find-bar.test.tsx
index b3eae8b4b9c..4f70f8357a8 100644
--- a/apps/desktop/src/components/find-bar.test.tsx
+++ b/apps/desktop/src/components/find-bar.test.tsx
@@ -72,7 +72,7 @@ function installBridge() {
}
function resetStore() {
- $findInPage.set({ active: false, query: '', matchOrdinal: 0, matchCount: 0 })
+ $findInPage.set({ active: false, query: '', matchOrdinal: 0, matchCount: 0, focusRequest: 0 })
}
// Zero the bridge refcount so a leaked subscription can't bleed between tests.
@@ -231,7 +231,32 @@ describe('find-in-page store', () => {
plantSurface()
openFindBar()
- expect($findInPage.get()).toEqual({ active: true, query: '', matchOrdinal: 0, matchCount: 0 })
+ expect($findInPage.get()).toEqual({ active: true, query: '', matchOrdinal: 0, matchCount: 0, focusRequest: 0 })
+ })
+
+ it('a repeat open keeps the typed query and asks for focus again', () => {
+ const surface = plantSurface()
+ surface.textContent = 'needle haystack'
+ openFindBar()
+ setFindQuery('needle')
+
+ openFindBar()
+
+ expect($findInPage.get().query).toBe('needle')
+ expect($findInPage.get().active).toBe(true)
+ expect($findInPage.get().focusRequest).toBe(1)
+ })
+
+ it('close then reopen starts a fresh focus request', () => {
+ plantSurface()
+ openFindBar()
+ openFindBar()
+ closeFindBar()
+ openFindBar()
+
+ expect($findInPage.get().focusRequest).toBe(0)
+ expect($findInPage.get().query).toBe('')
+ expect($findInPage.get().active).toBe(true)
})
it('closing clears state and tears down the scoped highlights', () => {
@@ -585,7 +610,7 @@ describe('FindBar', () => {
// Counter appears once a query + results exist.
expect(screen.queryByText('3/12')).toBeNull()
- actStore(() => $findInPage.set({ active: true, query: 'two', matchOrdinal: 1, matchCount: 1 }))
+ actStore(() => $findInPage.set({ active: true, query: 'two', matchOrdinal: 1, matchCount: 1, focusRequest: 0 }))
await waitFor(() => expect(screen.getByText('1/1')).toBeTruthy())
})
@@ -599,6 +624,34 @@ describe('FindBar', () => {
await waitFor(() => expect(document.activeElement).toBe(input))
})
+ it('refocuses the input on a repeat open and keeps the typed query', async () => {
+ const surface = plantSurface()
+ surface.textContent = 'needle haystack'
+ openFindBar()
+ renderFindBar()
+
+ const input = (await screen.findByRole('searchbox', { name: /find in page/i })) as HTMLInputElement
+ // eslint-disable-next-line no-restricted-globals -- asserting real focus requires the live document
+ await waitFor(() => expect(document.activeElement).toBe(input))
+
+ fireEvent.change(input, { target: { value: 'needle' } })
+ actStore(() => {
+ void setFindQuery('needle')
+ })
+ input.blur()
+ // eslint-disable-next-line no-restricted-globals -- asserting real focus requires the live document
+ expect(document.activeElement).not.toBe(input)
+
+ actStore(openFindBar)
+
+ // eslint-disable-next-line no-restricted-globals -- asserting real focus requires the live document
+ await waitFor(() => expect(document.activeElement).toBe(input))
+ expect(input.value).toBe('needle')
+ expect($findInPage.get().query).toBe('needle')
+ expect(input.selectionStart).toBe(0)
+ expect(input.selectionEnd).toBe('needle'.length)
+ })
+
it('debounces typing into a single scoped find', async () => {
vi.useFakeTimers()
const surface = plantSurface()
@@ -688,8 +741,7 @@ describe('FindBar', () => {
it('Enter dispatches next and Shift+Enter dispatches previous', () => {
const surface = plantSurface()
surface.textContent = 'needle needle needle'
- $findInPage.set({ active: true, query: 'needle', matchOrdinal: 1, matchCount: 3 })
- // Manually replay the open+query path so the marks exist for stepping.
+ // Open the bar fresh so the scope is captured against the planted surface.
openFindBar()
setFindQuery('needle')
@@ -831,7 +883,7 @@ describe('FindBar', () => {
// Bar gone, state reset, and the highlights stripped — stale marks
// must not survive a session switch.
await waitFor(() => expect(screen.queryByRole('search')).toBeNull())
- expect($findInPage.get()).toEqual({ active: false, query: '', matchOrdinal: 0, matchCount: 0 })
+ expect($findInPage.get()).toEqual({ active: false, query: '', matchOrdinal: 0, matchCount: 0, focusRequest: 0 })
expect(surface.querySelectorAll('mark.find-hit').length).toBe(0)
})
diff --git a/apps/desktop/src/components/find-bar.tsx b/apps/desktop/src/components/find-bar.tsx
index bdfcfce0c34..d02508c1720 100644
--- a/apps/desktop/src/components/find-bar.tsx
+++ b/apps/desktop/src/components/find-bar.tsx
@@ -36,7 +36,7 @@ import {
*/
export function FindBar() {
const { t } = useI18n()
- const { active, query, matchOrdinal, matchCount } = useStore($findInPage)
+ const { active, query, matchOrdinal, matchCount, focusRequest } = useStore($findInPage)
const inputRef = useRef<HTMLInputElement>(null)
const nativeSearchRequestRef = useRef(0)
const [localQuery, setLocalQuery] = useState('')
@@ -57,18 +57,33 @@ export function FindBar() {
return () => closeFindBar()
}, [pathname])
- // Focus input when find bar opens.
+ // Focus the input when the bar opens, and again when ⌘F repeats while it is
+ // already visible. focusRequest is the only thing that changes on a repeat
+ // open; active stays true, so it cannot be the sole dependency.
useEffect(() => {
if (active) {
- setLocalQuery('')
+ // A repeat open must keep the typed query. Clearing here would wipe the
+ // input the store just preserved.
+ if (focusRequest === 0) {
+ setLocalQuery('')
+ }
+
// Small delay so the DOM paints the input before we focus.
- const id = requestAnimationFrame(() => inputRef.current?.focus())
+ const id = requestAnimationFrame(() => {
+ inputRef.current?.focus()
+
+ // Select the existing query so the next keystroke replaces it, matching
+ // Chrome / Safari / VS Code. A first open has nothing to select.
+ if (focusRequest > 0) {
+ inputRef.current?.select()
+ }
+ })
return () => cancelAnimationFrame(id)
}
return undefined
- }, [active])
+ }, [active, focusRequest])
// The files pane (right sidebar, `aside[aria-label="Right sidebar"]`) is a
// floating right rail. The find bar is `fixed right-4` by default, which
diff --git a/apps/desktop/src/store/find-in-page.ts b/apps/desktop/src/store/find-in-page.ts
index 9f5a6e4ffb3..0e8a017ea8a 100644
--- a/apps/desktop/src/store/find-in-page.ts
+++ b/apps/desktop/src/store/find-in-page.ts
@@ -7,9 +7,12 @@ export interface FindInPageState {
query: string
matchOrdinal: number
matchCount: number
+ /** Bumped when openFindBar() is called while the bar is already visible, so
+ * FindBar can refocus without treating the chord as a fresh open. */
+ focusRequest: number
}
-const EMPTY: FindInPageState = { active: false, query: '', matchOrdinal: 0, matchCount: 0 }
+const EMPTY: FindInPageState = { active: false, query: '', matchOrdinal: 0, matchCount: 0, focusRequest: 0 }
export const $findInPage = atom<FindInPageState>({ ...EMPTY })
@@ -25,6 +28,17 @@ export const $findInPage = atom<FindInPageState>({ ...EMPTY })
* "current view" predicate (#81726).
*/
export function openFindBar(): void {
+ const prev = $findInPage.get()
+
+ if (prev.active) {
+ // Already visible: keep the typed query and the captured scope. A fresh
+ // EMPTY write would clear the query, and captureFindScope() would tear
+ // down the highlight observer.
+ $findInPage.set({ ...prev, focusRequest: prev.focusRequest + 1 })
+
+ return
+ }
+
$findInPage.set({ ...EMPTY, active: true })
captureFindScope()
}