macOS: fix CJK punctuation dropped when insertText: called without hasMarkedText()#4576
Open
zhjie wants to merge 2 commits into
Open
macOS: fix CJK punctuation dropped when insertText: called without hasMarkedText()#4576zhjie wants to merge 2 commits into
zhjie wants to merge 2 commits into
Conversation
The macOS Chinese Simplified Pinyin IME (and likely other CJK input methods) converts punctuation keys by calling insertText: directly, without a prior setMarkedText: call. Because hasMarkedText() is false at that point, winit silently dropped the converted character and the raw ASCII key from NSEvent.characters reached the application instead. For example, pressing ',' in Chinese mode should produce the Chinese comma ',', but the application received ',' (ASCII) because ',' was never emitted as Ime::Commit. Fix: emit Ime::Commit whenever is_ime_enabled() is true and the string is not a control character, regardless of hasMarkedText(). When hasMarkedText() is true we still clear the preedit first, preserving the existing preedit-then-commit behaviour for composed characters. ImeState::Committed is always set, so keyDown: treats the key as handled (had_ime_input = true) and does not generate a duplicate KeyboardInput event with the raw ASCII character. Tested with: Apple Chinese Simplified Pinyin, punctuation (,。?! ;:「」), full-width ASCII, and standard pinyin composition (nihao → 你好). No regression observed on Japanese and Korean IMEs (confirmed by inspecting the existing test matrix in PR rust-windowing#4478).
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
The macOS Chinese Simplified Pinyin IME (and likely other CJK input methods) converts punctuation keys by calling
insertText:replacementRange:without a priorsetMarkedText:call. BecausehasMarkedText()isfalseat that point, the previous code silently dropped the converted character and the raw ASCII key fromNSEvent.charactersreached the application.Example: pressing
,in Chinese input mode should produce,(Chinese comma), but the application received,(ASCII) because,was never emitted asIme::Commit.This affects at least:
,。?!;:「」etc.insertText:for non-composition keys.Root Cause
When an IME converts a key directly (no preedit),
hasMarkedText()isfalse, so the committed text was dropped. ThekeyDown:callback then generatedKeyboardInputwith the rawNSEvent.characters(ASCII).Fix
Emit
Ime::Commitwheneveris_ime_enabled()is true, regardless ofhasMarkedText():ImeState::Committedis set in both paths, sokeyDown:treats the key as handled (had_ime_input = true) and does not generate a duplicateKeyboardInputevent with the raw ASCII character.Testing
,。?!;:), full-width ASCII (for), and composed characters (nihao→你好) all work correctly.Ime::Commit.Relation to #4478
PR #4478 fixes Korean-specific double-input and key-loss bugs. This PR is orthogonal: it fixes the case where
insertText:is called without any priorsetMarkedText:, which the Korean PR does not address.