Find in page: if the page kept the keyboard when the bar opened, ask for it once more

The keys that open the find bar are often pressed in the page. The bar asks for the keyboard (`shell_focus`) and focuses its field; if the shell still does not have the keyboard 250 ms later, it asks once more and focuses the field again, so typing never lands in the page (seen on macOS: text typed right after Cmd+F was lost). Diagnostics lines record both steps.
This commit is contained in:
CRTkafa 2026-10-01 05:23:16 +07:00
parent 8bbdbecf0d
commit 45f81e7d9a

View file

@ -46,10 +46,17 @@ export function FindBar({ tabId, url, onClose }: { tabId: string; url: string; o
// The shell takes the keyboard (Ctrl+F was pressed in the page); closing clears the highlights. // The shell takes the keyboard (Ctrl+F was pressed in the page); closing clears the highlights.
useEffect(() => { useEffect(() => {
invoke('shell_focus').catch(() => {}); const focusField = () => { if (input.current && document.activeElement !== input.current) input.current.focus(); };
input.current?.focus(); invoke('shell_focus').then(focusField, () => {});
focusField();
diag(`find bar open: hasFocus=${document.hasFocus()} active=${describe(document.activeElement)}`); diag(`find bar open: hasFocus=${document.hasFocus()} active=${describe(document.activeElement)}`);
setTimeout(() => diag(`find bar +300ms: hasFocus=${document.hasFocus()} active=${describe(document.activeElement)}`), 300); // 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<string>).detail === tabId) { input.current?.focus(); input.current?.select(); } }; 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); }; 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', again);
@ -61,6 +68,7 @@ export function FindBar({ tabId, url, onClose }: { tabId: string; url: string; o
}).then(dispose => { if (disposed) dispose(); else unlisten = dispose; }).catch(() => {}); }).then(dispose => { if (disposed) dispose(); else unlisten = dispose; }).catch(() => {});
return () => { return () => {
disposed = true; disposed = true;
clearTimeout(retry);
unlisten?.(); unlisten?.();
window.removeEventListener('bt-find', again); window.removeEventListener('bt-find', again);
window.removeEventListener('bt-find-step', onStep); window.removeEventListener('bt-find-step', onStep);