Find in page: the shell's find bar per pane (Ctrl+F, the app menu, the palette) over the engine's own find

Host: `tab_find {id, action, query}` (shell-only; start, next, previous, stop). On Windows it drives WebView2's own find (`ICoreWebView2Find` from `ICoreWebView2_28`, find options from `ICoreWebView2Environment15`; its built-in find bar suppressed, all matches highlighted) and reports the engine's match count and active match to the shell as `tab-find` events. On macOS, Linux and an older WebView2 Runtime native code evaluates the engine's `window.find` in the tab instead (next and previous work, no count); WKWebView's and WebKitGTK's native find are not wired here because this tree cannot be compiled or run for them on this machine. The query is one typed line (1..=256 characters, no control characters), goes to the engine and is kept nowhere. Tabs gain no command: a tab forwards the existing key name `find` (Ctrl+F) through tab_shortcut.

Frame: FindBar.tsx takes the address field's place in its pane's toolbar while open, so the page does not move and in split view the bar is the searched pane's. "3/17" where the engine counts, "No matches" for none; Enter / F3 next, Shift+Enter / Shift+F3 previous, Esc closes, clears the highlights and gives the page the keyboard. `features.find` (on by default) gates the menu row, the palette entry and the keys. Only what the page has rendered can be found, as in a browser.

Also: this makes `pub mod find` in lib.rs (already in HEAD) resolve again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
CRTkafa 2026-09-30 22:34:48 +07:00
parent 832396801c
commit b31fa6b1d2
17 changed files with 449 additions and 19 deletions

View file

@ -0,0 +1,92 @@
// 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 { 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<HTMLInputElement>(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(() => {
invoke('shell_focus').catch(() => {});
input.current?.focus();
const again = (event: Event) => { if ((event as CustomEvent<string>).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;
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 <div className={`bt-omni bt-omni--edit bt-find${query && counts?.count === 0 ? ' is-none' : ''}`} role="search" title={menuText('findHint')}>
<Icon name="search" className="bt-omni-lead"/>
<input ref={input} type="text" spellCheck={false} autoComplete="off" maxLength={256} value={query} placeholder={menuText('findField')} aria-label={menuText('findField')}
onChange={event => 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(); }
}}/>
<span className="bt-find-count" role="status" aria-live="polite">{status}</span>
<button type="button" className="bt-omni-action" disabled={!query} title={`${menuText('findPrevious')} (Shift+Enter)`} aria-label={menuText('findPrevious')}
onMouseDown={event => event.preventDefault()} onClick={() => step(-1)}><Icon name="chevron" className="bt-find-up"/></button>
<button type="button" className="bt-omni-action" disabled={!query} title={`${menuText('findNext')} (Enter)`} aria-label={menuText('findNext')}
onMouseDown={event => event.preventDefault()} onClick={() => step(1)}><Icon name="chevron"/></button>
<button type="button" className="bt-omni-action" title={`${menuText('findClose')} (Esc)`} aria-label={menuText('findClose')} onClick={close}><Icon name="close"/></button>
</div>;
}