Skip to content

Preview: on macOS a page's script cannot take the keyboard - #1194

Merged
Tryanks merged 1 commit into
mainfrom
preview/keyboard-stays
Nov 24, 2026
Merged

Tryanks merged 1 commit into
mainfrom
preview/keyboard-stays

Conversation

@Tryanks

@Tryanks Tryanks commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Summary

On macOS, text input in the composer sometimes stopped (no typing, no input method, while paste still worked) and moving focus did not bring it back. A preview page had taken the keyboard, usually one an agent had opened in the background.

 page script: element.focus() / autofocus
   WebKit PageClientImpl::makeFirstResponder
-    NSWindow.makeFirstResponder(WKWebView)      # hidden or not
-    GPUI still shows the composer focused; its keys go to the page
+    suppressed (_setShouldSuppressFirstResponderChanges:)
 user presses the page
   AppKit makes the web view first responder     # unchanged
 agent.js click / type
-  el.focus()
+  focus(el)     # sends the blur / focus a page without the keyboard holds back
  • Cause: gpui-fast 0.1.19 refuses such a request only from its own NativeViews; gpui-webview puts the page on a composition surface, which that check does not see (read in their sources, not run). The site names the gap.
  • The selector is WebKit's private API. Where it is missing, the preview works as before and logs a warning.
  • Behaviour change: after opening an autofocus page from the address bar, the page needs a press before it takes keys.

Evidence

  • Before: the system log of an affected instance (gpui-fast 0.1.16): the web view, view hidden 1, goes 57->59 (the focused bit set) 50 ms after its first page commits, and every later text input query goes to it at (-20000, -20000). An AppKit + WKWebView harness reproduced it: autofocus and .focus(), at load or later, leave firstResponder=WKWebView with the view hidden.
    After: in the same harness with the suppression, firstResponder stays GPUI's stand-in view, hidden or visible, and a press still moves it to the page.
  • After, in the app (debug build, throwaway profile, page visible and calling .focus() every second): the state stays 29 (not focused), text typed into the composer arrives; a press on the page gives 29->31 and a caret. Input was synthetic; no input method composition was exercised, and the hidden page was checked only in the harness.
  • The page agent in a WKWebView, typing into #a, then #b, then clicking #a:
no suppression, old agent:   a:input a:focus a:blur b:focus b:input b:blur a:focus
suppression, old agent:      a:input b:input
suppression, new agent:      a:focus a:input a:blur b:focus b:input b:blur a:focus
  • cargo fmt --all --check; Clippy --workspace --all-targets -D warnings; tcode-ui and preview-mcp tests: 496 passed, on macOS arm64. The other platforms are CI's.
  • No test is added: the contract needs a real AppKit window and WKWebView, which the suite has no boundary for.

Merge Danger

Door: two-way

Blast Radius: preview

The suppression is macOS only. agent.js is shared by every engine: in a page that has the keyboard it sees the native events and sends nothing; in one that has not, a page's listeners now get blur and focus (untrusted) from an agent's click or type.

WebKit makes a web view the window's first responder when its page focuses
an element, hidden or not. A preview an agent opened in the background then
held the keyboard: the composer still showed as focused but got no text, no
input method worked, and a press in it gave nothing back. Paste went on
working, being a key equivalent.

A page now takes the keyboard when the user presses it. gpui-fast 0.1.19
refuses such requests from its own NativeViews only, and gpui-webview puts
the page on a composition surface.

A page without the keyboard holds back blur and focus, which the agent's
click and type had been getting by taking it; the page agent sends them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Tryanks
Tryanks merged commit d867b29 into main Nov 24, 2026
13 checks passed
@Tryanks
Tryanks deleted the preview/keyboard-stays branch November 24, 2026 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant