Skip to content

macOS: Fix Korean composing text commit key handling - #4478

Open
jayychoi wants to merge 1 commit into
rust-windowing:masterfrom
jayychoi:fix/korean-ime-commit-handling
Open

jayychoi wants to merge 1 commit into
rust-windowing:masterfrom
jayychoi:fix/korean-ime-commit-handling

Conversation

@jayychoi

@jayychoi jayychoi commented Feb 7, 2026 •

Copy link
Copy Markdown
  • Tested on all platforms changed
  • Added an entry to the changelog module if knowledge of this change could be valuable to users
  • Updated documentation to reflect any user-facing changes, including notes of platform-specific behavior
  • Created or updated an example program if it would help users understand this functionality

Summary

A minimal fix that addresses a major pain point in macOS Korean IME handling.

Fixes two interrelated bugs reported downstream in Alacritty:

  1. Double space input: With Korean IME active and no text being composed, pressing Space once inserts two spaces
    When using CJK input method in MacOS, space is inserted twice per 1 stroke. alacritty/alacritty#8079
  2. Key loss: After committing composed text, trigger keys of ASCII characters were silently dropped.
    Keyboard input does not work with CJK IME (macOS, 0.12.1) alacritty/alacritty#6942

Root Cause

Bug 1: Double Space

When Space is pressed with Korean IME active but no active composition, macOS calls setMarkedText(" ") followed by insertText twice within the same interpretKeyEvents. The first insertText call set ImeState::Committed but did not clear marked_text. The second call still saw hasMarkedText() == true and emitted a duplicate Ime::Commit(" ").

Bug 2: Key Loss

doCommandBySelector had a blanket early-return guard when ImeState == Committed:

if self.ivars().ime_state.get() == ImeState::Committed {
    return;
}

This was originally added to prevent Enter from being sent twice when used to confirm IME input. However, it was too aggressive — it swallowed ASCII key commands after a commit, preventing forward_key_to_app from being set. This caused trigger keys to be silently dropped.

Fix

In insertText:

  • Clear marked_text immediately after committing, so subsequent insertText calls within the same interpretKeyEvents no longer see stale preedit state.
  • Add an else if Committed branch that sets forward_key_to_app = true for post-commit characters, forwarding them as regular key events instead of duplicate IME commits.

In doCommandBySelector:

  • Remove the Committed early-return guard. With marked_text properly cleared in insertText, the double-input issue this guard was meant to prevent no longer occurs.

Test Results (Korean IME)

Tested with the ime example (cargo run --example ime). Below are before/after comparisons for each Korean IME scenario.

Space on empty field (Bug 1: double space)

Before (🔴 double space)After (✅ single space)
Preedit:  , caret Some((1, 1))
Preedit: , caret None
 |                          ← 1st space
Preedit: , caret None
  |                         ← 2nd space
                             (duplicate!)
Preedit:  , caret Some((1, 1))
Preedit: , caret None
 |                          ← 1 space only ✓

"한" + Space commit (already worked)

Before (✅)After (✅)
Preedit: ㅎ, caret Some((3, 3))
Preedit: 하, caret Some((3, 3))
Preedit: 한, caret Some((3, 3))
Preedit: 한 , caret Some((4, 4))
Preedit: , caret None
한 |
Preedit: ㅎ, caret Some((3, 3))
Preedit: 하, caret Some((3, 3))
Preedit: 한, caret Some((3, 3))
Preedit: 한 , caret Some((4, 4))
Preedit: , caret None
한 |

"한" + Enter commit (already worked)

Before (✅)After (✅)
Preedit: ㅎ, caret Some((3, 3))
Preedit: 하, caret Some((3, 3))
Preedit: 한, caret Some((3, 3))
Preedit: , caret None
한|
한                          ← newline ✓
Preedit: ㅎ, caret Some((3, 3))
Preedit: 하, caret Some((3, 3))
Preedit: 한, caret Some((3, 3))
Preedit: , caret None
한|
한                          ← newline ✓

"한" + digit (Bug 2: key loss)

Before (🔴 digit dropped)After (✅ digit forwarded)
Preedit: ㅎ, caret Some((3, 3))
Preedit: 하, caret Some((3, 3))
Preedit: 한, caret Some((3, 3))
Preedit: , caret None
한|                     ← "5" silently dropped!
Preedit: , caret None
Preedit: ㅎ, caret Some((3, 3))
Preedit: 하, caret Some((3, 3))
Preedit: 한, caret Some((3, 3))
Preedit: , caret None
한|
한5|                    ← digit forwarded ✓

"한" + ASCII (Bug 2: key loss)

Before (🔴 ASCII dropped)After (✅ ASCII forwarded)
Preedit: ㅎ, caret Some((3, 3))
Preedit: 하, caret Some((3, 3))
Preedit: 한, caret Some((3, 3))
Preedit: , caret None
한|                     ← ";" silently dropped!
Preedit: , caret None
Preedit: ㅎ, caret Some((3, 3))
Preedit: 하, caret Some((3, 3))
Preedit: 한, caret Some((3, 3))
Preedit: , caret None
한|
한;|                    ← ASCII forwarded ✓

Impact on Other CJK IMEs

These changes only affect the Korean IME code path. Japanese and Chinese IMEs are not affected because:

  • Japanese/Chinese IMEs consume confirmation keys (Enter/Space) internally — they never reach doCommandBySelector or trigger a second insertText within the same interpretKeyEvents call.
  • The Committed state is reset to Ground at the end of each keyDown, so the next keypress always starts from Ground regardless of IME.

Japanese and Chinese IMEs were tested and confirmed no regression (nihon→にほん, nihao→你好, emoji selection, etc.).

Fix space being inserted twice.
Fix ASCII characters being dropped when typed during preedit state.
@jayychoi
jayychoi requested a review from madsmtm as a code owner February 7, 2026 13:50
@madsmtm madsmtm added the DS - appkit Affects the AppKit/macOS backend label Feb 8, 2026
hiking90 added a commit to hiking90/winit that referenced this pull request Feb 22, 2026
Fix space being inserted twice.
Fix ASCII characters being dropped when typed during preedit state.

Cherry-picked from: rust-windowing#4478

Co-Authored-By: Jay Choi <jayyoungchoi22@gmail.com>
zilhak added a commit to zilhak/winit-ime-fix that referenced this pull request Apr 1, 2026
Port of rust-windowing#4478 to v0.30.13.

Bug 1 - Space double input: Clear marked_text immediately after commit
to prevent subsequent insertText calls from being treated as IME commits.

Bug 2 - Trigger key drop (comma, period, etc.): Remove the early return
guard in doCommandBySelector when ImeState::Committed, which was
swallowing ASCII key commands after IME commit.

When upstream PR rust-windowing#4478 is merged and released,
this fork should be replaced with the official winit release.

Reference: rust-windowing#4478
zilhak added a commit to zilhak/tasty that referenced this pull request Apr 1, 2026
winit의 macOS 백엔드 버그로 인해 한글 컴포징 후 트리거 키(콤마, 마침표 등)의
Pressed 이벤트가 누락되고, 스페이스가 이중 입력되던 문제를 애플리케이션 레벨
workaround로 처리하고 있었음.

winit PR #4478의 수정을 v0.30.13에 포팅한 fork를 사용하여 근본 원인을 해결하고,
기존 ime_committed_text 기반 workaround 코드를 전부 제거.

Fork: https://github.com/zilhak/winit-ime-fix (branch: fix-korean-ime)
Upstream PR: rust-windowing/winit#4478
minhyeoky pushed a commit to minhyeoky/alacritty that referenced this pull request Sep 27, 2026
With the built-in Korean IME and no active composition (e.g. right after
punctuation or backspace), one Space press reached the terminal twice:
once as an IME commit and once as a key event.

Patch the vendored winit 0.30.13 with the fix from
rust-windowing/winit#4478:

- insertText: clear marked text on commit so later insertText calls in
  the same interpretKeyEvents pass don't see stale preedit; forward a
  post-commit insertText as a regular key event.
- doCommandBySelector: drop the early return on the Committed state,
  which swallowed keys right after a commit.

Wire it in through [patch.crates-io] and keep vendor/ out of the
workspace.

Refs alacritty#8079

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QGGbnFKGxfaz4n6TzPrQ2F
IrosTheBeggar added a commit to IrosTheBeggar/mstream-terminal-player that referenced this pull request Oct 1, 2026
…ME backport

Two crates the window stands on need patches upstream has not released:
ratatui-wgpu 0.6.0 (surface format, face index, wide-glyph shaping,
failed presents — the next lane's work) and winit 0.30.13 (macOS drops
an ASCII key typed right after an input method's commit and doubles a
Space typed with no composition; rust-windowing/winit#4478, open against
master). Both now live under vendor/ through [patch.crates-io], examples
and test assets trimmed, each with a VENDORED.md naming the version and
every change. The winit backport is applied to src/platform_impl/macos/
view.rs, hunk for hunk against the upstream diff.

What the lane also settled: the Japanese "swallowed first Enter" was
never a winit defect. A plain AppKit text view, fed the same posted
keys, behaves identically — after Space opens Kotoeri's candidate list
the first Enter closes it and the second commits; without Space one
Enter commits — and stock winit matches the backport. The record, the
checklist and the Windows brief say so now. Korean, the backport's real
target, is untested here (no Korean source enabled).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
echelon added a commit to storytold/pdfcraft that referenced this pull request Oct 10, 2026
…pace (winit#4478) (#679)

* macOS: IME text committed after the marked text is cleared isn't lost (winit#4650)

winit 0.30.13 reports insertText as Ime::Commit only while marked text is
left, and after any commit it swallows the next key. Real paths where that
loses text:

- input methods that end a composition with unmarkText, then insertText
  (rust-windowing/winit#3925): the committed text was dropped;
- Apple Korean 2-Set, for the key typed after a syllable: on 한 then 5 it
  calls setMarkedText("한"), insertText("한"), setMarkedText(""),
  insertText("5"), so the 5 (or ;, or any key that ends the syllable) was
  dropped;
- a commit made outside a key press (a candidate clicked with the mouse,
  dictation): no key follows to reset the state, so the next key (for
  example Backspace) was swallowed.

vendor/winit now commits when a composition was started (pending_commit,
set in setMarkedText, cleared by the commit and when IME is disallowed);
other insertText calls still reach the app as KeyboardInput. A commit
outside keyDown clears the marked text and returns to the ground state.
validAttributesForMarkedText lists the underline and marked-clause
attributes, which some input methods need before they compose.

This is rust-windowing/winit#4650, the 0.30 backport of #4651 (merged on
winit master), plus the part of #4748 that fixes #4651's out-of-key-press
case (#4748's other parts are not included). One correction: upstream
spells the underline attribute "NSUnderlineStyle", but AppKit's
NSUnderlineStyleAttributeName is "NSUnderline" (printed on macOS 14), so
the AppKit value is used.

Checked with the real input methods on macOS 14.7.1 (a scratch winit app fed
key events through AppKit): with the patch 한+5 and 한+; commit the 5 and ;,
unmarkText-then-insertText commits, and Backspace after a commit outside a
key press is delivered; ABC typing, held keys, Japanese (日本, あ), Cangjie
(日) and Korean composition give the same events as before. A test fails if
a re-vendored winit loses the patch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* macOS: Space in Apple Korean with no syllable in progress types one space (winit#4478)

With Apple Korean 2-Set on macOS 14, Space with no syllable in progress
(a second space, or a space after a digit or punctuation) makes AppKit
call setMarkedText(" "), insertText(" "), then setMarkedText(""). winit
reports the insertText as Ime::Commit(" "), but the trailing empty
setMarkedText returns its state to the ground state, so keyDown also sends
the key as KeyboardInput with text " ". egui inserts both, and PdfCraft's
text fields got two spaces (the double space of alacritty/alacritty#8079).

vendor/winit now records that the key being handled committed IME text and
then doesn't also send it as KeyboardInput, unless AppKit forwards it as a
command through doCommandBySelector (Enter after a syllable still commits
the syllable and sends Enter). keyDown consumes the record on every key,
so keys typed after IME is disallowed are unaffected.

rust-windowing/winit#4478 aims at the same double space, but its changes
(clearing the marked text on commit, forwarding a later insertText, and
dropping doCommandBySelector's guard) don't reach this call order, and the
key loss it also fixes is already fixed by the previous change
(pending_commit). This is the narrow form for the call order observed
here; it should go upstream with #4478.

Checked with the real input methods on macOS 14.7.1 (scratch winit app, on top
of the previous change): the duplicate KeyboardInput Space is gone and
nothing else changes (Korean syllables, 한+Enter, 한+5, Japanese, Cangjie,
ABC, held keys, keys after IME is disallowed). A test fails if a
re-vendored winit loses the patch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Keep #678's emoji-picker branch: view.rs is main's plus this PR's Space fix (54b3d3c)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Brandon Thomas <echelon@gmail.com>
krichelj pushed a commit to krichelj/pdfcraft that referenced this pull request Oct 11, 2026
…pace (winit#4478) (storytold#679)

* macOS: IME text committed after the marked text is cleared isn't lost (winit#4650)

winit 0.30.13 reports insertText as Ime::Commit only while marked text is
left, and after any commit it swallows the next key. Real paths where that
loses text:

- input methods that end a composition with unmarkText, then insertText
  (rust-windowing/winit#3925): the committed text was dropped;
- Apple Korean 2-Set, for the key typed after a syllable: on 한 then 5 it
  calls setMarkedText("한"), insertText("한"), setMarkedText(""),
  insertText("5"), so the 5 (or ;, or any key that ends the syllable) was
  dropped;
- a commit made outside a key press (a candidate clicked with the mouse,
  dictation): no key follows to reset the state, so the next key (for
  example Backspace) was swallowed.

vendor/winit now commits when a composition was started (pending_commit,
set in setMarkedText, cleared by the commit and when IME is disallowed);
other insertText calls still reach the app as KeyboardInput. A commit
outside keyDown clears the marked text and returns to the ground state.
validAttributesForMarkedText lists the underline and marked-clause
attributes, which some input methods need before they compose.

This is rust-windowing/winit#4650, the 0.30 backport of storytold#4651 (merged on
winit master), plus the part of storytold#4748 that fixes storytold#4651's out-of-key-press
case (storytold#4748's other parts are not included). One correction: upstream
spells the underline attribute "NSUnderlineStyle", but AppKit's
NSUnderlineStyleAttributeName is "NSUnderline" (printed on macOS 14), so
the AppKit value is used.

Checked with the real input methods on macOS 14.7.1 (a scratch winit app fed
key events through AppKit): with the patch 한+5 and 한+; commit the 5 and ;,
unmarkText-then-insertText commits, and Backspace after a commit outside a
key press is delivered; ABC typing, held keys, Japanese (日本, あ), Cangjie
(日) and Korean composition give the same events as before. A test fails if
a re-vendored winit loses the patch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* macOS: Space in Apple Korean with no syllable in progress types one space (winit#4478)

With Apple Korean 2-Set on macOS 14, Space with no syllable in progress
(a second space, or a space after a digit or punctuation) makes AppKit
call setMarkedText(" "), insertText(" "), then setMarkedText(""). winit
reports the insertText as Ime::Commit(" "), but the trailing empty
setMarkedText returns its state to the ground state, so keyDown also sends
the key as KeyboardInput with text " ". egui inserts both, and PdfCraft's
text fields got two spaces (the double space of alacritty/alacritty#8079).

vendor/winit now records that the key being handled committed IME text and
then doesn't also send it as KeyboardInput, unless AppKit forwards it as a
command through doCommandBySelector (Enter after a syllable still commits
the syllable and sends Enter). keyDown consumes the record on every key,
so keys typed after IME is disallowed are unaffected.

rust-windowing/winit#4478 aims at the same double space, but its changes
(clearing the marked text on commit, forwarding a later insertText, and
dropping doCommandBySelector's guard) don't reach this call order, and the
key loss it also fixes is already fixed by the previous change
(pending_commit). This is the narrow form for the call order observed
here; it should go upstream with storytold#4478.

Checked with the real input methods on macOS 14.7.1 (scratch winit app, on top
of the previous change): the duplicate KeyboardInput Space is gone and
nothing else changes (Korean syllables, 한+Enter, 한+5, Japanese, Cangjie,
ABC, held keys, keys after IME is disallowed). A test fails if a
re-vendored winit loses the patch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Keep storytold#678's emoji-picker branch: view.rs is main's plus this PR's Space fix (54b3d3c)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Brandon Thomas <echelon@gmail.com>
richerfu pushed a commit to richerfu/winit that referenced this pull request Nov 2, 2026
Related to rust-windowing#4478; PdfCraft patch, to be proposed upstream.

Originally "M15.1: suppress duplicate macOS Korean spaces" in storytold/wordcraft#787 (same change as storytold/pdfcraft vendor/winit).

This branch has not been deployed

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

Labels

DS - appkit Affects the AppKit/macOS backend

Development

Successfully merging this pull request may close these issues.

2 participants