Skip to content

[Cocoa] Send text that an input method commits as plain characters - #3877

Draft
Jm6CnDDXBwFTakS wants to merge 3 commits into
eclipse-platform:masterfrom
Jm6CnDDXBwFTakS:cocoa-ime-committed-keys
Draft

Jm6CnDDXBwFTakS wants to merge 3 commits into
eclipse-platform:masterfrom
Jm6CnDDXBwFTakS:cocoa-ime-committed-keys

Conversation

@Jm6CnDDXBwFTakS

Copy link
Copy Markdown
Contributor

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 Canvas with an inline IME (an ImeComposition listener; StyledText among them), Cocoa calls insertText: with the committed text during that key's interpretKeyEvents. Control.insertText sends the text as KeyDown events and gives the first one that key's key code, so StyledText runs the key's action instead of inserting the text. The key itself, which the input method passes on with doCommandBySelector, is never sent, because keyInputHappened is already set.

Seen with 2-Set Korean (logs of the calls in a canvas with an IME):

Keys Cocoa calls Before After
가나 with 다 composing, ← insertText: [다] with ←'s key code, then moveLeft: 다 is lost and the caret moves 가나다, with the caret before 다
ㅐ composing, Backspace insertText: [ㅐ] with Backspace's key code, then deleteBackward: ㅐ and the character before it are deleted only ㅐ is deleted
가 composing, Enter insertText: [가], then insertNewline: 가, no line break 가 and a line break

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.insertText sends no KeyDown for that event type, so StyledText loses the committed text. After this change, the text is kept.

The change

  • IME.commitText commits, then sends the committed text as KeyDown events that carry the character only, whatever the current event is, as WM_CHAR does on Windows. IME.insertText uses it when it ends a composition, and so does unmarkText from [Cocoa] Commit the IME composition on a click or a focus change #3876.
  • Shell.keyInputCommitted records that the KeyDowns of the current keystroke so far were committed text, not the key. When it is set, doCommandBySelector still sends the key the input method passes on. It is set only for a commit during an NSKeyDown, and it is reset wherever keyInputHappened is reset and right after interpretKeyEvents.
  • A key the input method consumes is still not sent. Japanese Enter is an example: it commits and calls no selector, so there is still no line break.

For other listeners, two things change:

  • The KeyDowns of committed text no longer carry the committing key's key code or state mask.
  • A key passed on after a commit (Korean Enter, an arrow key, Backspace) now arrives as its own 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.

  1. 2-Set Korean:
    • R K S K E K then ←: 가나다, with the caret before 다.
    • H E L L O then Backspace: only the composing jamo is deleted.
    • R K then Enter: 가 and a new line.
    • R K then Ctrl+Space: 가 is kept.
  2. Pinyin – Simplified: n i then Ctrl+Space: "ni" is kept.
  3. Japanese – Romaji:
    • k a then Enter: か, no new line.
    • k a Space 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, Combo and StyledText tests ran with mvn verify. Only tests that fail the same way on unchanged master on this machine fail: test_backspaceAndDelete in Text and StyledText, because Display.post needs the Accessibility permission, and Combo.test_getSelectionIndex.

🤖 Generated with Claude Code

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]>
@github-actions

Copy link
Copy Markdown
Contributor

Test Results (macosx)

   364 files  ±0    364 suites  ±0   34m 13s ⏱️ - 8m 5s
 4 917 tests ±0  4 480 ✅ ±0    437 💤 ±0  0 ❌ ±0 
10 242 runs  ±0  9 030 ✅ ±0  1 212 💤 ±0  0 ❌ ±0 

Results for commit 057d8ee. ± Comparison against base commit 16080ee.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant