Skip to content

Don't terminate the IME composition when the window is deactivated - #20964

Open
許家維 (sfy257000) wants to merge 2 commits into
microsoft:mainfrom
sfy257000:fix/20936-ime-deactivate-composition
Open

許家維 (sfy257000) wants to merge 2 commits into
microsoft:mainfrom
sfy257000:fix/20936-ime-deactivate-composition

Conversation

@sfy257000

@sfy257000 許家維 (sfy257000) commented Nov 25, 2026 •

Copy link
Copy Markdown

Summary of the Pull Request

If the window is deactivated (Alt+Tab, clicking another app) while an IME composition is in progress, Microsoft Bopomofo stops working in that tab after the window is reactivated: the IME indicator still says Chinese, but every key is typed as a plain latin character. This PR makes Unfocus() leave the composition to the IME when the whole window was deactivated, instead of terminating it. It also fixes the first Bopomofo key after reactivation being lost in that case, without touching TF_TMAE_CONSOLE.

References and Relevant Issues

Closes #20936

Detailed Description of the Pull Request / Additional comments

TermControl::_LostFocusHandler() calls Implementation::Unfocus(), which ends any active composition with ITfContextOwnerCompositionServices::TerminateComposition(). That's fine when focus moves somewhere else inside the window (another tab, the search box), but LostFocus is also raised when the whole window is deactivated, and by then the thread has already lost TSF focus. Terminating the composition in that state leaves Microsoft Bopomofo unable to compose in our context again.

I added temporary file logging to src/tsf/Implementation.cpp to see what happens. This is the sequence with the current code (trimmed): type a few Bopomofo keys without committing them, Alt+Tab away, Alt+Tab back, keep typing:

Unfocus enter comps=1 pending=0
OnEndComposition comps(before)=1 provider=0x0
Unfocus TerminateComposition hr=0x0
Unfocus leave comps=0 pending=1
OnStartComposition comps=1 provider=0x0      <- the IME starts and ends another composition
OnEndComposition comps(before)=1 provider=0x0
_doCompositionUpdate finalized=4 active=0 provider=0x0
Focus enter comps=0 pending=0
Focus leave comps=0 pending=0 focusOurs=1    <- our ITfDocumentMgr has TSF focus again
Key reached TermControl comps=0 focusOurs=1
Key reached TermControl comps=0 focusOurs=1
... (every following key, no composition callbacks at all)

So on our side everything is back in order after reactivation: _compositions is 0, no edit session is pending, and our document manager has TSF focus. The keys reach TermControl::_KeyHandler(), but Bopomofo no longer processes any of them, so they go to the shell as plain keys. Pressing Shift to toggle the input mode doesn't help either. The workaround in the issue (open a new tab, type something there, switch back) works because Unfocus() is then called while the window is active, and terminating the composition in that state is harmless.

With this change, Unfocus() only terminates the composition if the thread still has an active window, i.e. focus moved within the window. If the window was deactivated, the composition is left alone. Bopomofo then commits it by itself as soon as the window is reactivated. This happens after Focus() has set _provider again, so the text is sent to the shell the normal way. That's also what Notepad and XAML TextBoxes (e.g. our own search box) do with Bopomofo: the pending text is committed when you come back to the window. The logging confirmed that GetActiveWindow() is nullptr in LostFocus when the window is deactivated, and non-null when focus moves to the search box.

conhost and the WPF control use AssociateFocus() and never call Unfocus(), so this only affects TermControl.

The first key after reactivation

With only the change above, the first key that starts a composition after reactivation was still lost. Bopomofo commits the leftover composition as soon as the window is reactivated, and the next composition it starts is ended right away with no text. I logged ITfKeyTraceEventSink and ITfThreadFocusSink to see where that happens:

Unfocus composition left to the IME
OnKillThreadFocus comps=1
Focus enter comps=1
Focus after SetFocus comps=1
OnEndComposition comps(before)=1              <- Bopomofo commits the leftover composition
OnStartComposition comps=1
OnEndComposition comps(before)=1
_doCompositionUpdate finalized=2 active=0
OnSetThreadFocus comps=0                      <- ~100 ms later
KeyTraceDown vk=0x43 comps=0                  <- first Bopomofo key (ㄏ)
OnStartComposition comps=1
OnEndComposition comps(before)=1              <- ended immediately, the key is lost
_doCompositionUpdate finalized=0 active=0
KeyTraceDown vk=0x43 comps=0                  <- the same key again works normally
OnStartComposition comps=1

The key does reach TSF, and it's specifically the first key that starts a composition: Backspaces typed first went through normally, and the first Bopomofo key after them was still lost. Note that Bopomofo commits the leftover composition before OnSetThreadFocus, i.e. while the thread doesn't have TSF focus yet.

What fixed it was moving TSF focus away from our document manager and back (SetFocus(nullptr), then SetFocus(_documentMgr)) in OnSetThreadFocus. It only runs if Unfocus() left a composition to the IME, and only if Focus() was called again (_provider is set). Things that did not help, for the record:

  • the same focus bounce in Focus(), i.e. before OnSetThreadFocus
  • toggling GUID_COMPARTMENT_KEYBOARD_OPENCLOSE off and on after OnSetThreadFocus
  • sending Escape to the IME via ITfKeystrokeMgr after OnSetThreadFocus

Removing TF_TMAE_CONSOLE also avoided the lost key in my earlier testing, but this PR keeps it as is, given #20481.

Validation Steps Performed

Windows 10 22H2 (19045), Microsoft Bopomofo, unpackaged Windows Terminal Preview 1.26.2734.0. I couldn't build the full solution locally, so I built Microsoft.Terminal.Control.dll from the v1.26.2734.0 tag with these changes (Release x64, on GitHub Actions in my fork) and swapped it into the unpackaged build. src/tsf/Implementation.cpp and Implementation.h are identical between that tag and main.

  1. Switch Bopomofo to Chinese mode and type 你好 without committing it (underline still visible).
  2. Alt+Tab to another app, then Alt+Tab back.
  3. Type more Chinese, e.g. 哈 (ㄏㄚ).
  • Before: only latin characters are typed until another tab is opened and used.
  • After: 你好 is committed when the window is reactivated, Bopomofo keeps working, and the first key (ㄏ) isn't lost.

Also verified with these changes: switching tabs and typing into the search box still work, Alt+Tab without a pending composition behaves as before, and Korean gksks still produces 하난.

PR Checklist

No test is added. Reproducing this needs a real IME and a real window activation change, which the unit tests can't set up.

When the window loses activation (Alt+Tab, clicking another app) while
an IME composition is in progress, TermControl's LostFocus handler calls
Unfocus(), which terminates the composition. At that point the thread has
already lost TSF focus, and Microsoft Bopomofo never recovers from having
its composition terminated in that state: after reactivating the window
it stops processing keys entirely and only latin characters are typed.

Only terminate the composition if focus moved elsewhere within the window.
If the whole window was deactivated, leave the composition to the IME,
which finishes it on its own once the window is reactivated, like it does
in Notepad or a XAML TextBox.

Closes microsoft#20936
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@lhecker

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
1 pipeline(s) were filtered out due to trigger conditions.

…he IME

Microsoft Bopomofo commits the composition that Unfocus() left to it before the thread gets TSF focus back, and then drops the first key that starts the next composition. Moving TSF focus away from our document and back in OnSetThreadFocus avoids that.
@sfy257000

Copy link
Copy Markdown
Author

Pushed a follow-up commit that also fixes the first Bopomofo key being lost after reactivation, without touching TF_TMAE_CONSOLE: it refocuses our document in OnSetThreadFocus if Unfocus() left a composition to the IME. I've updated the description with the log and what I tried.

@sfy257000

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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.

Microsoft Bopomofo IME stops accepting Chinese input after switching windows during composition

2 participants