Repository navigation
[Cocoa] Send text that an input method commits as plain characters - #3877
Draft
Jm6CnDDXBwFTakS wants to merge 3 commits into
Draft
Jm6CnDDXBwFTakS wants to merge 3 commits into
Jm6CnDDXBwFTakS wants to merge 3 commits into
Conversation
The input method asks a Canvas with an IME for the selected range as soon as it starts composing. IME.selectedRange and IME.attributedSubstringFromRange send a COMPOSITION_SELECTION event and then read event.text. When the IME has no ImeComposition listener, or the listener leaves the text unset, that is null, and typing with an input method such as Japanese Romaji throws a NullPointerException. Treat a missing text as empty. Assisted-by: Claude Opus 5.5 <[email protected]>
A Canvas with an inline IME, StyledText among them, never ended an input method composition when the user clicked elsewhere or moved the focus to another control. The input method (for example Japanese Romaji) does not commit by itself, and unmarkText was a stub, so the marked text stayed and the next keystroke joined the old composition, wherever the caret had moved to. Do what NSTextView does: - unmarkText commits the marked text through a COMPOSITION_CHANGED event with a full commit count, then sends the text as KeyDown events, as Control.insertText does for text that a key commits. StyledText inserts the committed text from these. They carry no key code, so that the key that made the input method commit is not taken for its own action. - A left mouse down outside the composition commits it and makes the input method discard its marked text, so that the next keystroke starts a new composition. A click inside the composition is left to the input method, as on Windows. Where the click lies comes from COMPOSITION_OFFSET. - When the canvas resigns as first responder, the composition is committed the same way. Deactivating the window or the application keeps the composition, as in TextEdit. NSView.inputContext and NSTextInputContext.discardMarkedText are generated with MacGenerator. Fixes eclipse-platform#3875 Assisted-by: Claude Opus 5.5 <[email protected]>
When a key ended an input method composition in a Canvas with an inline IME (an ImeComposition listener; StyledText among them), Control.insertText sent the committed text as KeyDown events and gave the first one the key code of that key. StyledText then ran the key's action instead of inserting the text, and the key itself, which the input method passes on with doCommandBySelector, was never sent: - Korean, a syllable composing, Left Arrow: the syllable was lost and the caret moved. - Korean, a jamo composing, Backspace: the jamo and the character before it were deleted. - Korean, Enter: the text was committed but no line break followed. When the input source changes while composing (Ctrl+Space), the input method commits while the current event is a flags-changed event, for which Control.insertText sends no KeyDown at all, so StyledText lost the committed text. IME.commitText now commits and then sends the text as KeyDown events that carry the character only, whatever the current event is, as on Windows. A key that the input method passes on after committing is sent as its own KeyDown: Shell.keyInputCommitted tells doCommandBySelector that the KeyDowns of this keystroke so far were committed text. A key the input method consumes, such as Enter in Japanese input, is still not sent. For other listeners: the KeyDowns of committed text no longer carry the key code or state mask of the key that committed it, and a key passed on after a commit (Korean Enter, an arrow key, Backspace) now arrives as its own KeyDown, as in native text fields. Fixes eclipse-platform#3554 Fixes eclipse-platform#2412 Assisted-by: Claude Opus 5.5 <[email protected]>
Contributor
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.
Fixes #3554.
Fixes #2412.
Depends on #3876, whose two commits this branch contains. Please review only the last commit, "Send text that an input method commits as plain characters". I will rebase onto master once #3876 is merged.
The problem
When a key ends an input-method composition in a
Canvaswith an inline IME (anImeCompositionlistener;StyledTextamong them), Cocoa callsinsertText:with the committed text during that key'sinterpretKeyEvents.Control.insertTextsends the text asKeyDownevents and gives the first one that key's key code, soStyledTextruns the key's action instead of inserting the text. The key itself, which the input method passes on withdoCommandBySelector, is never sent, becausekeyInputHappenedis already set.Seen with 2-Set Korean (logs of the calls in a canvas with an IME):
insertText:[다] with ←'s key code, thenmoveLeft:insertText:[ㅐ] with Backspace's key code, thendeleteBackward:insertText:[가], theninsertNewline:When the input source is switched while composing (Ctrl+Space, Korean or Pinyin – Simplified), the input method commits while the current event is a flags-changed event.
Control.insertTextsends noKeyDownfor that event type, soStyledTextloses the committed text. After this change, the text is kept.The change
IME.commitTextcommits, then sends the committed text asKeyDownevents that carry the character only, whatever the current event is, asWM_CHARdoes on Windows.IME.insertTextuses it when it ends a composition, and so doesunmarkTextfrom [Cocoa] Commit the IME composition on a click or a focus change #3876.Shell.keyInputCommittedrecords that theKeyDowns of the current keystroke so far were committed text, not the key. When it is set,doCommandBySelectorstill sends the key the input method passes on. It is set only for a commit during anNSKeyDown, and it is reset whereverkeyInputHappenedis reset and right afterinterpretKeyEvents.For other listeners, two things change:
KeyDowns of committed text no longer carry the committing key's key code or state mask.KeyDown, as in native text fields.How to test (macOS)
Add 2-Set Korean, Pinyin – Simplified and Japanese – Romaji under System Settings > Keyboard > Input Sources. Then type in a multi-line
StyledText. The keys below are the QWERTY keycaps.R K S K E Kthen ←: 가나다, with the caret before 다.H E L L Othen Backspace: only the composing jamo is deleted.R Kthen Enter: 가 and a new line.R Kthen Ctrl+Space: 가 is kept.n ithen Ctrl+Space: "ni" is kept.k athen Enter: か, no new line.k aSpace Enter Enter: か, no new line.These were tested by hand on macOS 27.2 (Apple Silicon). Before the change, the first three Korean steps and both Ctrl+Space steps reproduce the bugs.
The
Canvas,Composite,Caret,Shell,Spinner,Text,ComboandStyledTexttests ran withmvn verify. Only tests that fail the same way on unchanged master on this machine fail:test_backspaceAndDeleteinTextandStyledText, becauseDisplay.postneeds the Accessibility permission, andCombo.test_getSelectionIndex.🤖 Generated with Claude Code