Skip to content

[Cocoa] Commit the IME composition on a click or a focus change - #3876

Open
Jm6CnDDXBwFTakS wants to merge 2 commits into
eclipse-platform:masterfrom
Jm6CnDDXBwFTakS:cocoa-ime-commit-on-click
Open

Jm6CnDDXBwFTakS wants to merge 2 commits into
eclipse-platform:masterfrom
Jm6CnDDXBwFTakS:cocoa-ime-commit-on-click

Conversation

@Jm6CnDDXBwFTakS

Copy link
Copy Markdown
Contributor

Fixes #3875.

On macOS, an input-method composition in a StyledText, or in any Canvas with an inline IME, now ends the way it does in a native text view (TextEdit):

  • A left click outside the composition commits it, and the input method forgets its marked text. The candidate list closes and the next keystroke starts a new composition at the caret. A click inside the composition is left to the input method, which is the same rule as IME.WM_LBUTTONDOWN on Windows. COMPOSITION_OFFSET tells where the click lies.
  • When the canvas resigns as first responder, the composition is committed the same way, because the focus moved to another control. Deactivating the window or the application keeps the composition, as TextEdit does.
  • unmarkText is no longer a stub. It 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 the character only, with no key code, so that a key which made the input method commit is not taken for its own action.
  • IME.selectedRange and IME.attributedSubstringFromRange no longer throw a NullPointerException when no ImeComposition listener sets event.text. This is the first commit.

NSView.inputContext and NSTextInputContext.discardMarkedText are generated with MacGenerator. They are new entries in AppKitFull.bridgesupport.extras, and no native code changes.

How it was tested

By hand on macOS 27.2 (Apple Silicon), with the snippet from #3875 and a test canvas whose IME listener logs every event:

  • Japanese (Romaji):
    • a click outside the composition commits it, with and without the candidate list open;
    • a click on the composition keeps it;
    • moving the focus to a Text commits it;
    • switching to another application keeps it, as TextEdit does;
    • the next keystroke starts a fresh composition;
    • a two-character commit sends two KeyDowns.
  • StyledText: the committed text stays where it was composed, and the next composition starts at the clicked position.
  • 2-Set Korean and Pinyin – Simplified: clicks and focus moves commit, in the same way.
  • A listener that disposes the canvas, or only its IME, in the commit event or in the following KeyDown: no exception.
  • A listener that moves the focus in the commit event: the nested commit is skipped, and the text arrives once.

mvn verify for org.eclipse.swt.tests with the Canvas, Composite, Caret, Text and StyledText tests: 637 tests run. Only Test_org_eclipse_swt_widgets_Text.test_backspaceAndDelete and Test_org_eclipse_swt_custom_StyledText.test_backspaceAndDelete fail. They fail the same way on unchanged master on this machine, because Display.post keys do not arrive without the Accessibility permission.

Not covered

  • Two guards that no tested input method reached.

    • If an input method itself calls unmarkText during a keystroke, Shell.keyInputHappened keeps Composite.keyDown from sending that key a second time.
    • If an input method answers discardMarkedText with insertText:, the text is not inserted again.

    Japanese, Korean and Pinyin do neither, so these paths are defensive.

  • A click inside the composition is not forwarded to the input method. SWT still uses the deprecated NSInputManager for mouse events, not -[NSTextInputContext handleEvent:].

  • Clicks on controls that do not take the focus do not commit, for example a toolbar or a button with full keyboard access off. Nothing makes the canvas resign as first responder then.

Two related problems are left for separate changes:

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

Copy link
Copy Markdown
Contributor

Test Results (macosx)

   364 files  ±0    364 suites  ±0   37m 56s ⏱️ - 4m 22s
 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 5b86245. ± 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

Development

Successfully merging this pull request may close these issues.

[macOS] Click outside an IME composition in StyledText or a Canvas does not commit it; NPE in IME.selectedRange without a listener

1 participant