Skip to content

Fix first Apple Korean key falling through before preedit on macOS - #4744

Open
kijoong-kim wants to merge 1 commit into
rust-windowing:masterfrom
kijoong-kim:codex/macos-first-korean-key
Open

kijoong-kim wants to merge 1 commit into
rust-windowing:masterfrom
kijoong-kim:codex/macos-first-korean-key

Conversation

@kijoong-kim

Copy link
Copy Markdown

On macOS with Apple Korean 2-set, the first key in a fresh app can arrive as
insertText("ㅎ") without a preceding setMarkedText. Winit forwards it as an
ordinary key; subsequent composition starts with the vowel, producing ㅎㅏㄴ
instead of 한 for g/k/s. This patch retries that exact retained event once,
synchronously, before ordinary keyboard dispatch.

Related: #3095 and #4693. PhotoCraft maintainers requested an upstream fix in
storytold/photocraft#606 rather than carrying a vendored dependency. This is an
alternative to #4693's delayed retry approach; it introduces no timer or key
queue. The OS-internal reason for the initial fallback is not established here.

Change

  • Observe only an initial Apple Korean interpretation batch with IME enabled,
    disabled composition state, no marked text or pending commit, a single
    compatibility jamo, and the key window's first responder.
  • Retry only if exactly one insertText matches that original jamo. Preedit,
    unmark, command, multiple/mismatched inserts, repeat, Command/Control/Option,
    nested key dispatch, IME reset or changed source/focus excludes replay.
  • Consume the attempt and drop the policy borrow before re-entering AppKit.
    Reuse the retained event; a failed retry falls through to existing dispatch.
  • Preserve the current pending_commit behavior. No Hangul reconstruction,
    application-text rewriting, added unsafe block or production input logging.

The policy is separate from AppKit callbacks and has seven regression tests.
The existing ime example is sufficient to reproduce and inspect the events.

Reproduce

  1. On macOS, select Apple Korean 2-set.
  2. Build cargo build -p winit --example ime.
  3. Launch it from a fresh app bundle identifier (using a new identifier for
    each control/patched comparison avoids reusing input-client state).
  4. Press physical g, k, s, then Space without deletion/retyping.
  5. Inspect the example's INFO log. It emulates an undisplayed text field.

Native verification

Tested on macOS 27.0.1, Apple Silicon, Apple Korean 2-set, against master
01634067402db15d26a31bfd199f5c69a989813d on 2026-10-07. The two bundles contain
builds of the unmodified ime example, with only this backend patch differing.
The patched binary was built before the final formatting-only pass.

Fresh bundle First g/k/s + Space
Unmodified master raw ㅎ, then ㅏ/ㄴ preedit; final ㅎㅏㄴ
Patched master preedit ㅎ → 하 → 한; final 한

The fresh unmodified control was also run after the patched trial, and still
failed. The patched app additionally produced ㄲ → 까 → 깐 → Backspace →
까, then committed 까 with Space. Earlier minimal eframe 0.36.2 experiments
on winit 0.30.13 independently reproduced control/fix/control behavior.

Automated validation

  • cargo test -p winit-appkit --lib: 19 passed, including 7 new policy tests.
  • cargo clippy -p winit-appkit --all-targets -- -D warnings: passed.
  • cargo build -p winit --example ime: passed for control and patched builds.
  • cargo +nightly fmt --all: passed; changelog updated.

Other macOS versions and other Korean input sources have not been verified.
This targets the observed first-key fallback, not all Korean IME issues.

  • Tested on all platforms changed (macOS; version/input-source limits above)
  • Added an entry to the changelog module
  • Documented reproduction and platform-specific behavior above
  • Used the existing IME example for the native reproduction

@kijoong-kim
kijoong-kim requested a review from madsmtm as a code owner October 7, 2026 14:33
Copilot AI balanced review requested due to automatic review settings October 7, 2026 14:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

echelon added a commit to storytold/pdfcraft that referenced this pull request Oct 10, 2026
…#4744) (#680)

* 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: the first Apple Korean key in a fresh PdfCraft composes (winit#4744)

On the first key after PdfCraft starts, Apple Korean 2-Set can answer
interpretKeyEvents with insertText and the raw compatibility jamo instead of
setMarkedText. winit sends that key to the app as an ordinary key, and the
next keys compose without it: g k s Space typed ㅎㅏㄴ instead of 한 (Search,
form fields, comments; rust-windowing/winit#3095).

vendor/winit now ports rust-windowing/winit#4744: keyDown interprets that
one key a second time, synchronously, before any KeyboardInput is sent, and
only when every condition holds: Apple Korean, IME allowed and not yet
composing, no pending commit or marked text, no Command/Control/Option, not
a repeat, the key window's first responder, the only callback is one
insertText of that same single jamo, and focus and input source unchanged
afterwards. The decision is ImeStartup (ime_startup.rs, copied with its
seven tests); no timer, no key queue, no text rewriting. It sits on top of
the previous macOS IME change (pending_commit, in_key_down); the retry runs
inside in_key_down. Command maps to ModifiersState::SUPER in winit 0.30
(META on winit master).

Checked with the real input method on macOS 14.7.1 (scratch winit app, fresh
process each time): without the patch g k s Space gave ㅎ as a key, then
ㅏ and ㄴ committed separately, 3 runs out of 3; with it, ㅎ → 하 → 한 → 한
committed, 3 out of 3. All other traces (Japanese, Cangjie, ABC, held
keys, later Korean keys) are unchanged. ImeStartup's tests pass with
rustc --test. A test fails if a re-vendored winit loses the patch.

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

---------

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
…#4744) (storytold#680)

* 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: the first Apple Korean key in a fresh PdfCraft composes (winit#4744)

On the first key after PdfCraft starts, Apple Korean 2-Set can answer
interpretKeyEvents with insertText and the raw compatibility jamo instead of
setMarkedText. winit sends that key to the app as an ordinary key, and the
next keys compose without it: g k s Space typed ㅎㅏㄴ instead of 한 (Search,
form fields, comments; rust-windowing/winit#3095).

vendor/winit now ports rust-windowing/winit#4744: keyDown interprets that
one key a second time, synchronously, before any KeyboardInput is sent, and
only when every condition holds: Apple Korean, IME allowed and not yet
composing, no pending commit or marked text, no Command/Control/Option, not
a repeat, the key window's first responder, the only callback is one
insertText of that same single jamo, and focus and input source unchanged
afterwards. The decision is ImeStartup (ime_startup.rs, copied with its
seven tests); no timer, no key queue, no text rewriting. It sits on top of
the previous macOS IME change (pending_commit, in_key_down); the retry runs
inside in_key_down. Command maps to ModifiersState::SUPER in winit 0.30
(META on winit master).

Checked with the real input method on macOS 14.7.1 (scratch winit app, fresh
process each time): without the patch g k s Space gave ㅎ as a key, then
ㅏ and ㄴ committed separately, 3 runs out of 3; with it, ㅎ → 하 → 한 → 한
committed, 3 out of 3. All other traces (Japanese, Cangjie, ABC, held
keys, later Korean keys) are unchanged. ImeStartup's tests pass with
rustc --test. A test fails if a re-vendored winit loses the patch.

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

---------

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
Port of rust-windowing#4744.

Originally "M15.1: retry first Apple Korean key" in storytold/wordcraft#787 (same change as storytold/pdfcraft vendor/winit).
softkr added a commit to softkr/CoShell that referenced this pull request Nov 21, 2026
macOS answers the first key after Apple's Korean input method attaches
with a bare insertText of one jamo, which winit forwards as a plain key:
"한" arrived as "ㅎ", "ㅏ", "ㄴ" in every field and in the terminal.

Vendor winit 0.30.13 with one change that hands that key to the input
method once more (same approach as rust-windowing/winit#4744, not yet
released). See vendor/winit/COSHELL_PATCH.md.

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

None yet

Development

Successfully merging this pull request may close these issues.

3 participants