// Find in page: the shell's find bar for one pane. It takes the address field's place in that // pane's toolbar while it is open (so the page under it does not move and, in split view, the bar // is plainly the searched pane's). Native code does the searching (`tab_find`, shell-only): the // engine's own find where the build can drive it (WebView2: highlights and a match count, which // arrive as `tab-find` events), else the engine's `window.find` evaluated by native code (next and // previous work, no count). Only what the page has rendered can be found, as in a browser: a // virtualized list (X's timeline) holds the posts near the viewport, not the whole feed. // // The query lives in this component's state while the bar is open and nowhere else: it is not // saved, logged or put in a URL, and closing the bar clears the engine's highlights. // Keys: Enter / F3 next, Shift+Enter / Shift+F3 previous, Esc closes and gives the page the focus. import { useEffect, useRef, useState } from 'react'; import { invoke, listen } from './bridge.ts'; import { describe, diag } from './diag.ts'; import { useT } from './hooks.ts'; import { formatNumber } from './i18n.ts'; import { Icon } from './icons.tsx'; import { findLabel, menuText } from './menuModel.ts'; import { tabAction } from './state.ts'; import './findBar.css'; /** Opens (or focuses) the find bar of web tab `id`. */ export const openFind = (id: string) => window.dispatchEvent(new CustomEvent('bt-find', { detail: id })); /** The next (`1`) or previous (`-1`) match in tab `id`, when its find bar is open (F3 anywhere in the shell). */ export const stepFind = (id: string, direction: 1 | -1) => window.dispatchEvent(new CustomEvent('bt-find-step', { detail: { id, direction } })); const find = (id: string, action: 'start' | 'next' | 'previous' | 'stop', query?: string) => invoke('tab_find', { id, action, ...(query ? { query } : {}) }).catch(() => {}); export function FindBar({ tabId, url, onClose }: { tabId: string; url: string; onClose(): void }) { useT(); const [query, setQuery] = useState(''); // What the engine reported for the query on screen (null: nothing yet, or an engine that does not count). const [counts, setCounts] = useState<{ count: number; index: number } | null>(null); const input = useRef(null); const live = useRef(query); live.current = query; const close = () => { onClose(); // The page gets the keyboard back. void tabAction(tabId, 'focus'); }; const step = (direction: 1 | -1) => { if (live.current) void find(tabId, direction > 0 ? 'next' : 'previous', live.current); }; // The shell takes the keyboard (Ctrl+F was pressed in the page); closing clears the highlights. useEffect(() => { const focusField = () => { if (input.current && document.activeElement !== input.current) input.current.focus(); }; invoke('shell_focus').then(focusField, () => {}); focusField(); diag(`find bar open: hasFocus=${document.hasFocus()} active=${describe(document.activeElement)}`); // The page may keep the keyboard (the keys that opened the bar were pressed in it): if the shell // still does not have it a moment later, ask once more, so typing never lands in the page. const retry = setTimeout(() => { diag(`find bar +250ms: hasFocus=${document.hasFocus()} active=${describe(document.activeElement)}`); if (!document.hasFocus()) invoke('shell_focus').then(() => { focusField(); diag(`find bar refocused: hasFocus=${document.hasFocus()}`); }, () => {}); else focusField(); }, 250); const again = (event: Event) => { if ((event as CustomEvent).detail === tabId) { input.current?.focus(); input.current?.select(); } }; const onStep = (event: Event) => { const detail = (event as CustomEvent<{ id: string; direction: 1 | -1 }>).detail; if (detail.id === tabId) step(detail.direction); }; window.addEventListener('bt-find', again); window.addEventListener('bt-find-step', onStep); let disposed = false; let unlisten: (() => void) | undefined; listen<{ id: string; count: number; index: number }>('tab-find', ({ payload }) => { if (payload.id === tabId && live.current) setCounts({ count: payload.count, index: payload.index }); }).then(dispose => { if (disposed) dispose(); else unlisten = dispose; }).catch(() => {}); return () => { disposed = true; clearTimeout(retry); unlisten?.(); window.removeEventListener('bt-find', again); window.removeEventListener('bt-find-step', onStep); void find(tabId, 'stop'); }; }, [tabId]); // A new query (or a new page under the same query) starts a new search, a beat after the last key. useEffect(() => { setCounts(null); if (!query) { void find(tabId, 'stop'); return; } const timer = setTimeout(() => void find(tabId, 'start', query), 140); return () => clearTimeout(timer); }, [query, url, tabId]); const status = query && counts ? findLabel(counts, value => formatNumber(value)) : ''; return
setQuery(event.target.value.replace(/[\u0000-\u001f\u007f]/g, ''))} onKeyDown={event => { if (event.key === 'Enter' || event.key === 'F3') { event.preventDefault(); event.stopPropagation(); step(event.shiftKey ? -1 : 1); } if (event.key === 'Escape') { event.preventDefault(); event.stopPropagation(); close(); } }}/> {status}
; }