[Problem/Bug]: IME composition UI detaches to the screen top-left after switching between two WebView2 windows in the same process
What happened?
Two WebView2 controls hosted in two top-level windows of the same process, sharing one user data folder (and therefore one browser process). After clicking back and forth between the two windows, typing with a CJK IME in one of them breaks:
- The composition UI appears detached at the top-left of the screen. In the browser process
(msedgewebview2.exe) an
MSCTFIME Compositionwindow and aCiceroUIWndFramewindow are shown at about (0,0)-(56,31); the candidate UI ("Windows Input Experience") opens at (0,10). - The page never receives
compositionstart; nothing is composed into the focused textarea. Plain ASCII typing still works. - Once a window is in this state it stays broken on every re-activation, while the other window keeps working. Which window breaks varies between runs. Waiting 1.5 s before typing does not help.
Reproduced with Microsoft Japanese IME and Microsoft Pinyin, with the attached minimal WinForms project (Microsoft.Web.WebView2 1.0.3719.77, net8.0-windows). An automated run of 20 alternating clicks (one IME key per visit) gives 10/20 failures: every visit to one of the two windows fails.
What makes it go away:
- A separate user data folder per window (separate browser processes): 0/20.
- Switching between windows of two different applications: 0/20.
- In a Win32 host that calls
ICoreWebView2Controller::MoveFocus(PROGRAMMATIC)synchronously inside the parent window'sWM_SETFOCUS(which is effectively what the WinForms control does inOnGotFocus), posting a message and callingMoveFocusafter the activation has completed reduces the failure rate from 10/20 to 0-1/20.
This suggests that calling MoveFocus while Windows is still processing the activation sequence
leaves the TSF focus for that WebView's HWND associated with the wrong (default) document manager,
so the IME falls back to the legacy composition window.
Not applicable to / not tested in the Edge browser: the scenario needs two hosted WebView2 controls.
Related: #5675 (composition UI at top-left after host resize), #1611, #5570 (same symptom, different triggers); original reports in tauri-apps/wry#1908 and tauri-apps/tauri#16391 (also on Runtime 142.0.3595.69).
Importance
Important. My app's user experience is significantly compromised.
Runtime Channel
Stable release (WebView2 Runtime)
Runtime Version
157.0.4341.42
SDK Version
1.0.3719.77
Framework
Winforms
Operating System
Windows 11
OS Version
10.0.26200
Repro steps
- Build and run the attached WebView2TwoWindowsImeRepro project (dotnet build -c Release). Two windows A and B open, each with a
- Enable Microsoft Japanese IME or Microsoft Pinyin.
- Click the textarea in window A and type with the IME.
- Click the textarea in window B and type.
- Click the textarea in window A again and type. Repeat A -> B -> A a few times.
Expected: composition happens inline in the focused textarea with the candidate window at the caret. Actual: in one of the windows the composition UI appears detached at the top-left of the screen and the "Composition starts" counter under the textarea does not increase.
Running the project with --separate (one user data folder per window) does not reproduce.
WebView2TwoWindowsImeRepro.zip
Repros in Edge Browser
No, issue does not reproduce in the corresponding Edge version
Regression
Don't know
Last working version (if regression)
No response
I captured a Chromium ime trace of the failing state, in case it helps narrow this down.
How the trace was taken
Same two-window setup as in the report (wry host, shared user data folder), with:
WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS=--trace-startup=ime --trace-startup-file=trace.json --trace-startup-duration=24 --trace-startup-format=json
WebView2 Runtime 157.0.4341.46, Windows 11 10.0.26200, Microsoft Japanese IME. The harness alternately clicks the textarea in window A and B and types one key per visit. In this run 8 of 16 visits failed, all of them in window A. The trace covers the first 10 of those visits (5 failing, 5 succeeding).
What the trace shows
On every window switch the browser process makes the same TSF focus calls, whether the visit then fails or succeeds:
ITfThreadMgr::AssociateFocus ITfDocumentMgr=0x0
ITfThreadMgr::SetFocus ITfDocumentMgr=<doc 1>
ITfThreadMgr::SetFocus ITfDocumentMgr=<doc 2>
ITfThreadMgr::SetFocus ITfDocumentMgr=<doc 1>
ITfThreadMgr::SetFocus ITfDocumentMgr=<doc 2>
The order, the pointers and the timing (all within about 3 ms) are identical in both cases. <doc 1> is the document manager that was passed to AssociateFocus when the window was first focused, and <doc 2> is the one that receives the composition in the working window, so they appear to be the empty document and the text document.
What differs is what happens after the key press:
| per visit (one key typed) | succeeding visit (window B) | failing visit (window A) |
|---|---|---|
TSFTextStore::OnStartComposition |
1 | 0 |
TSFTextStore::SetText |
4 | 0 |
TSFTextStore::OnEndEdit |
2 | 0 |
In one failing visit I typed five keys instead of one; the counts stayed at 0. At the same time the MSCTFIME Composition and CiceroUIWndFrame windows appear in the browser process at the top-left of the screen.
So in the failing state WebView2 has asked TSF to focus its text document exactly as it does when things work, but the text service never writes into that document's text store, and the composition is shown in the MSCTFIME composition window instead.
Comparison with the deferred MoveFocus host
I took the same trace with a wry build that posts a message on WM_SETFOCUS and calls MoveFocus(PROGRAMMATIC) after the activation has finished (tauri-apps/wry#1924). The AssociateFocus / SetFocus sequence in the browser process is the same as above, and every traced visit where a key was typed has an OnStartComposition (0 failures in 24 visits). The calls WebView2 makes are the same in both builds; only the point at which the host triggers them differs.
Possibly related
ui/base/ime/win/tsf_bridge.cc in Chromium has this comment on UpdateAssociateFocus:
// TSF runtime monitors some Win32 messages such as WM_ACTIVATE to
// change the focused document manager. This is problematic when
// TSFBridge::SetFocusedClient is called first then the target window
// receives WM_ACTIVATE.
In the trace, AssociateFocus is only ever called with the first document or with null; the text document is focused with SetFocus alone. That would fit the host-side observation: calling MoveFocus synchronously inside the parent's WM_SETFOCUS makes the browser process call SetFocus before the host window's activation has completed.
What I could not confirm
- Whether the focused document manager is actually replaced after the
SetFocuscalls. The trace records the calls, notITfThreadMgr::GetFocusor the return values, and I cannot read that state from outside the browser process. - Why only one of the two windows ends up in this state when the calls are the same for both.
The two trace files are attached (trace_unpatched.json and trace_deferred_movefocus.json; the Windows user name in the file paths was replaced with user, nothing else was changed).