Repository navigation
Don't terminate the IME composition when the window is deactivated - #20964
Open
許家維 (sfy257000) wants to merge 2 commits into
Open
許家維 (sfy257000) wants to merge 2 commits into
許家維 (sfy257000) wants to merge 2 commits into
Conversation
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: There may be pipelines that require an authorized user to comment /azp run to run. |
Member
|
/azp run |
|
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.
Author
|
Pushed a follow-up commit that also fixes the first Bopomofo key being lost after reactivation, without touching |
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 touchingTF_TMAE_CONSOLE.References and Relevant Issues
Closes #20936
Detailed Description of the Pull Request / Additional comments
TermControl::_LostFocusHandler()callsImplementation::Unfocus(), which ends any active composition withITfContextOwnerCompositionServices::TerminateComposition(). That's fine when focus moves somewhere else inside the window (another tab, the search box), butLostFocusis 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.cppto 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:So on our side everything is back in order after reactivation:
_compositionsis 0, no edit session is pending, and our document manager has TSF focus. The keys reachTermControl::_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 becauseUnfocus()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 afterFocus()has set_provideragain, so the text is sent to the shell the normal way. That's also what Notepad and XAMLTextBoxes (e.g. our own search box) do with Bopomofo: the pending text is committed when you come back to the window. The logging confirmed thatGetActiveWindow()isnullptrinLostFocuswhen the window is deactivated, and non-null when focus moves to the search box.conhost and the WPF control use
AssociateFocus()and never callUnfocus(), so this only affectsTermControl.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
ITfKeyTraceEventSinkandITfThreadFocusSinkto see where that happens: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), thenSetFocus(_documentMgr)) inOnSetThreadFocus. It only runs ifUnfocus()left a composition to the IME, and only ifFocus()was called again (_provideris set). Things that did not help, for the record:Focus(), i.e. beforeOnSetThreadFocusGUID_COMPARTMENT_KEYBOARD_OPENCLOSEoff and on afterOnSetThreadFocusITfKeystrokeMgrafterOnSetThreadFocusRemoving
TF_TMAE_CONSOLEalso 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.dllfrom thev1.26.2734.0tag with these changes (Release x64, on GitHub Actions in my fork) and swapped it into the unpackaged build.src/tsf/Implementation.cppandImplementation.hare identical between that tag andmain.你好without committing it (underline still visible).哈(ㄏㄚ).你好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
gksksstill 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.