diff --git a/crates/preview-mcp/src/page/agent.js b/crates/preview-mcp/src/page/agent.js index 233a0ad6d..7bf47352c 100644 --- a/crates/preview-mcp/src/page/agent.js +++ b/crates/preview-mcp/src/page/agent.js @@ -412,6 +412,29 @@ el.dispatchEvent(event); }; + const activeElement = () => { + let el = document.activeElement; + while (el && el.shadowRoot && el.shadowRoot.activeElement) el = el.shadowRoot.activeElement; + return el === document.body ? null : el; + }; + + // A page without the keyboard moves its active element but holds back + // `blur` and `focus`, which its own listeners wait for. + const focus = (el, options) => { + const before = activeElement(); + let told = false; + const note = () => { + told = true; + }; + for (const type of ["blur", "focus"]) window.addEventListener(type, note, true); + el.focus(options); + for (const type of ["blur", "focus"]) window.removeEventListener(type, note, true); + const after = activeElement(); + if (told || after === before) return; + if (before) before.dispatchEvent(new FocusEvent("blur", { composed: true, relatedTarget: after })); + if (after) after.dispatchEvent(new FocusEvent("focus", { composed: true, relatedTarget: before })); + }; + const setValue = (el, value) => { const proto = el instanceof HTMLTextAreaElement ? HTMLTextAreaElement.prototype : HTMLInputElement.prototype; const setter = Object.getOwnPropertyDescriptor(proto, "value"); @@ -449,7 +472,7 @@ const y = rect.top + rect.height / 2; pointer(el, "pointerdown", x, y); pointer(el, "mousedown", x, y); - if (typeof el.focus === "function") el.focus({ preventScroll: true }); + if (typeof el.focus === "function") focus(el, { preventScroll: true }); pointer(el, "pointerup", x, y); pointer(el, "mouseup", x, y); if (typeof el.click === "function") el.click(); @@ -461,7 +484,7 @@ type: (args) => { const el = resolve(args); const text = String(args.text == null ? "" : args.text); - el.focus(); + focus(el); if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) { setValue(el, text); el.dispatchEvent(new InputEvent("input", { bubbles: true, composed: true, data: text, inputType: "insertText" })); diff --git a/crates/ui/src/preview_panel/lifecycle.rs b/crates/ui/src/preview_panel/lifecycle.rs index b191ae8c2..4804956f0 100644 --- a/crates/ui/src/preview_panel/lifecycle.rs +++ b/crates/ui/src/preview_panel/lifecycle.rs @@ -1262,6 +1262,8 @@ mod platform { } #[cfg(target_os = "ios")] super::super::ios::adopt(webview.read(cx).raw()); + #[cfg(target_os = "macos")] + keep_scripts_off_the_keyboard(webview.read(cx).raw()); webview.update(cx, |view, _| set_webview_visible(view, false)); #[cfg(any(target_os = "macos", target_os = "ios"))] let remote = lifecycle @@ -1279,6 +1281,28 @@ mod platform { ); Availability::Ready(webview) } + + /// A page takes the keyboard when the user presses it, never because its + /// script focused an element. WebKit otherwise makes the web view the + /// window's first responder for that, hidden or not, and GPUI's fields + /// then get no text while GPUI still shows them focused. + /// + /// Gap: GPUI Fast refuses such a request only from its own `NativeView`s, + /// and gpui-webview puts the page on a composition surface instead. + #[cfg(target_os = "macos")] + fn keep_scripts_off_the_keyboard(raw: &wry::WebView) { + use objc2::runtime::NSObjectProtocol as _; + use wry::WebViewExtMacOS as _; + + let webview = raw.webview(); + // WebKit's private API: a macOS without it would raise on the call. + if !webview.respondsToSelector(objc2::sel!(_setShouldSuppressFirstResponderChanges:)) { + log::warn!("preview: a page's script can take the keyboard on this macOS"); + return; + } + let () = + unsafe { objc2::msg_send![&*webview, _setShouldSuppressFirstResponderChanges: true] }; + } } /// gpui-webview releases a webview by destroying its X11 composition