YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix(macos): improve WKWebView keyboard focus recovery
Louis Beaumontb939543fc36e
diff --git a/apps/screenpipe-app-tauri/app/globals.css b/apps/screenpipe-app-tauri/app/globals.cssindex db467bf87..5e15ecae2 100644--- a/apps/screenpipe-app-tauri/app/globals.css+++ b/apps/screenpipe-app-tauri/app/globals.css@@ -232,6 +232,19 @@/* cursor: default; */}+ /* Explicit overrides for form inputs — the :not() exclusion above is not+ sufficient in WebKit when parent elements have user-select:none, which+ can propagate and block keyboard input in WKWebView edge cases. */+ input,+ textarea,+ [contenteditable],+ [contenteditable="true"] {+ -webkit-user-select: text !important;+ -moz-user-select: text !important;+ -ms-user-select: text !important;+ user-select: text !important;+ }+/* Prevent all image and element dragging globally */img, a, [draggable] {-webkit-user-drag: none;diff --git a/apps/screenpipe-app-tauri/app/layout.tsx b/apps/screenpipe-app-tauri/app/layout.tsxindex ca1c5c147..30056dee3 100644--- a/apps/screenpipe-app-tauri/app/layout.tsx+++ b/apps/screenpipe-app-tauri/app/layout.tsx@@ -50,6 +50,14 @@ export default function RootLayout({// Focus recovery for Tauri WKWebView (macOS)// The webview can silently lose focus, making the entire UI unresponsive// to keyboard and sometimes mouse input. Detect and recover by refocusing.+ const callNativeFocusRecovery = () => {+ // Call the Rust-side ensure_webview_focus to re-assert WKWebView+ // as first responder via makeFirstResponder + dispatch_async+ try {+ (window as any).__TAURI_INTERNALS__?.invoke("ensure_webview_focus").catch(() => {});+ } catch {}+ };+const handleWindowFocus = () => {// When the native window regains focus, ensure the webview body is focused// so keyboard events work. Small delay to let Tauri finish its focus cycle.@@ -57,11 +65,12 @@ export default function RootLayout({if (document.activeElement === document.body || !document.activeElement) {document.body.focus();}+ callNativeFocusRecovery();}, 50);};window.addEventListener("focus", handleWindowFocus);- // Safety valve: triple-click on the app background to force-dismiss stuck overlays+ // Safety valve: click on the app background to force-dismiss stuck overlays// by blurring and refocusing — helps when overlays block normal interactionconst handlePointerRecovery = () => {// If there are any fixed z-50 overlays that shouldn't be there,@@ -74,6 +83,30 @@ export default function RootLayout({// Re-check focus on any click — if click reaches window, focus should workwindow.addEventListener("mousedown", handlePointerRecovery, true);+ // Periodic focus watchdog: detect silent focus loss that no event catches.+ // WKWebView can lose first-responder status without firing any JS event+ // (e.g. after native dialog dismiss, tray interaction, or AppKit race).+ // Every 2s, test if a keystroke would reach the webview by checking if+ // the document can receive input. If not, trigger native recovery.+ let lastKeyTime = Date.now();+ const markKeyActivity = () => { lastKeyTime = Date.now(); };+ window.addEventListener("keydown", markKeyActivity, true);++ const focusWatchdog = setInterval(() => {+ // Only check when the window is visible and focused+ if (document.hidden || !document.hasFocus()) return;+ // If we haven't seen a keystroke in 10s and the active element is body+ // (not an input), something may be wrong — but don't interfere if user+ // is just using the mouse. Only recover if user recently clicked (mouse+ // activity without keyboard suggests potential stuck state).+ const now = Date.now();+ const noRecentKeys = now - lastKeyTime > 10_000;+ const activeIsBody = document.activeElement === document.body || !document.activeElement;+ if (noRecentKeys && activeIsBody) {+ callNativeFocusRecovery();+ }+ }, 2_000);+// Auto-reload on IndexedDB disconnect (APP-2E, 27 users on v2.0.379)// WKWebView's IndexedDB server can crash; the page becomes unusable.// PostHog JS SDK uses IndexedDB for session replay — this is a known WebKit bug.@@ -136,7 +169,9 @@ export default function RootLayout({return () => {window.removeEventListener("focus", handleWindowFocus);window.removeEventListener("mousedown", handlePointerRecovery, true);+ window.removeEventListener("keydown", markKeyActivity, true);window.removeEventListener("unhandledrejection", handleUnhandledRejection);+ clearInterval(focusWatchdog);};}, []);diff --git a/apps/screenpipe-app-tauri/src-tauri/src/commands.rs b/apps/screenpipe-app-tauri/src-tauri/src/commands.rsindex 299f0039d..46827960b 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/commands.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/commands.rs@@ -500,9 +500,11 @@ pub async fn show_window(Ok(())}-/// Re-assert the WKWebView as first responder for the current key panel.-/// Called from JS on pointer enter / window focus to ensure trackpad pinch-/// gestures (magnifyWithEvent:) reach the WKWebView for zoom handling.+/// Re-assert the WKWebView as first responder for the current key window/panel.+/// Called from JS on window focus, pointer enter, and periodically by a focus+/// watchdog to recover from silent WKWebView focus loss (tao#208, tauri#11897).+/// Handles both NSPanel-based windows (main overlay, chat) and regular NSWindows+/// (settings) so keyboard input works reliably across all window types.#[tauri::command]#[specta::specta]pub async fn ensure_webview_focus(app_handle: tauri::AppHandle) -> Result<(), String> {@@ -513,12 +515,26 @@ pub async fn ensure_webview_focus(app_handle: tauri::AppHandle) -> Result<(), Stlet app = app_handle.clone();run_on_main_thread_safe(&app_handle, move || {- for label in &["main", "main-window"] {+ // Try NSPanel-based windows first (main overlay, chat)+ for label in &["main", "main-window", "chat"] {if let Ok(panel) = app.get_webview_panel(label) {unsafe { crate::window_api::make_webview_first_responder(&panel); }return;}}+ // Fall back to regular NSWindow (settings, etc.)+ for label in &["settings"] {+ if let Some(window) = app.get_webview_window(label) {+ if let Ok(ns_win) = window.ns_window() {+ unsafe {+ crate::window_api::make_nswindow_webview_first_responder(+ ns_win as tauri_nspanel::cocoa::base::id,+ );+ }+ }+ return;+ }+ }});}Ok(())diff --git a/apps/screenpipe-app-tauri/src-tauri/src/window_api.rs b/apps/screenpipe-app-tauri/src-tauri/src/window_api.rsindex c17722f77..701cc934b 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/window_api.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/window_api.rs@@ -265,7 +265,29 @@ pub unsafe fn make_nswindow_webview_first_responder(ns_win: tauri_nspanel::cocoa}if wk_view != nil {+ // Set first responder immediately (handles the common case)let _: () = msg_send![ns_win, makeFirstResponder: wk_view];++ // Also schedule on the next run-loop tick to win the race against any+ // deferred responder-chain reset triggered by makeKeyAndOrderFront.+ // Same pattern as make_webview_first_responder for NSPanel.+ let win_id = ns_win as usize;+ let wk_id = wk_view as usize;+ extern "C" {+ static _dispatch_main_q: std::ffi::c_void;+ fn dispatch_async_f(queue: *const std::ffi::c_void, context: *mut std::ffi::c_void, work: extern "C" fn(*mut std::ffi::c_void));+ }+ struct Ctx { win_id: usize, wk_id: usize }+ extern "C" fn set_responder(ctx: *mut std::ffi::c_void) {+ unsafe {+ let ctx = Box::from_raw(ctx as *mut Ctx);+ let window: id = ctx.win_id as id;+ let wk: id = ctx.wk_id as id;+ let _: () = msg_send![window, makeFirstResponder: wk];+ }+ }+ let ctx = Box::into_raw(Box::new(Ctx { win_id, wk_id }));+ dispatch_async_f(&_dispatch_main_q, ctx as *mut std::ffi::c_void, set_responder);}}