Skip to content

macOS: the first Apple Korean key in a fresh PdfCraft composes (winit#4744) - #680

Merged
echelon merged 3 commits into
storytold:mainfrom
cyfung1031:fix/winit-4744-macos-korean-first-key
Oct 10, 2026
Merged

echelon merged 3 commits into
storytold:mainfrom
cyfung1031:fix/winit-4744-macos-korean-first-key

Conversation

@cyfung1031

@cyfung1031 cyfung1031 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Builds on #676 (macOS IME commit state: pending_commit, in_key_down). This branch contains that PR's commit; please review the last commit only. Once #676 is merged, this diff reduces to the last commit.

Summary

On macOS, the first Korean word typed in a freshly started PdfCraft comes out broken: g k s Space gives ㅎㅏㄴ instead of 한. On the first key, Apple Korean can call insertText with the raw compatibility jamo instead of starting a composition. winit sends it to the app as an ordinary key, and the next keys compose without it. This interprets that one key a second time, synchronously, before it is sent, and only in exactly that situation.

Port of rust-windowing/winit#4744 (reviewed head 5c5c4db9af; related winit#3095), whose author reproduced it with eframe 0.36 on winit 0.30.13.

PdfCraft impact

The first Korean key in Search, a form field or any egui text field after each launch.

Change

  • ime_startup.rs (ImeStartup) is copied from Dynamic XFA: a repeating subform with occur min="2" loses its second instance in scripts once a script writes into the first #4744 with its seven tests: the policy that decides on the retry.

  • view.rs: keyDown: asks the policy before interpretKeyEvents, and retries the same retained event once afterwards only if every condition holds:

    • Apple Korean is the input source, IME is allowed and not yet composing, and there is no pending commit or marked text;
    • no Command, Control or Option, and the key is not an auto-repeat;
    • the view is the key window's first responder;
    • the interpretation's only callback was one insertText of that same single jamo;
    • focus and input source are unchanged afterwards.

    setMarkedText, unmarkText and doCommandBySelector cancel a retry, and allowing or disallowing IME resets the policy.

  • There is no timer, no key queue, no text rewriting, and no RefCell borrow across AppKit calls.

Adapted for winit 0.30

  • ime_allowed stands in for master's ime_capabilities.
  • Command is ModifiersState::SUPER in 0.30 (META on master).
  • The retry runs inside in_key_down, from the PR this builds on.
  • The policy's reset happens where 0.30's set_ime_allowed changes state, where master uses enable_ime/disable_ime.

How the macOS changes were checked

A scratch winit app (not committed) built against the vendored winit, first unpatched, then with each patch. It picks an input source on its own input context (NSTextInputContext.selectedKeyboardInputSource), posts key events made from CGEventCreateKeyboardEvent to itself, so AppKit routes them through keyDown: and interpretKeyEvents, and the real input method composes them. It prints winit's Ime and KeyboardInput events, and AppKit's NSTextInputClient calls from winit's own trace spans. Machine: macOS 14.7.1 (23H222), Apple Korean 2-Set, Japanese (Romaji), Cangjie, ABC. Afterwards the input source was set back to ABC.

The scenarios are: Korean syllables followed by Space, a digit, ;, Return and Backspace; Space with nothing composed; Japanese 日本 and あ; Cangjie 日; ABC typing; a held key with auto-repeat; IME disallowed and allowed again mid-composition; commits made outside a key press; and unmarkText followed by insertText. How egui then uses these events was read from egui-winit 0.36.2 (KeyboardInput text becomes Event::Text; Ime::Commit inserts into the focused TextEdit).

Before → after (a fresh process each run, Apple Korean, g k s Space as the first keys):

Events Text
Before (3 runs of 3) KeyboardInput("ㅎ"), Preedit("ㅏ"), Commit("ㅏ"), Preedit("ㄴ"), Commit("ㄴ ") ㅎㅏㄴ
After (3 runs of 3) Preedit("ㅎ"), Preedit("하"), Preedit("한"), Preedit("한 "), Commit("한 ") 한

Every other scenario (later Korean keys, Japanese, Cangjie, ABC, held keys) gave identical events. ImeStartup's seven tests pass (rustc --test; vendored crates' own tests don't run in the workspace).

apps/pdfcraft/tests/winit_macos_korean_first_key.rs fails if a re-vendored winit loses the patch.

Automated checks

  • The vendored winit built on its own for aarch64-apple-darwin, x86_64-pc-windows-msvc and x86_64-unknown-linux-musl (cargo clippy --lib, lints capped to warnings): the warnings are the same as on main for all three (the one existing clippy::iter_kv_map in linux/x11/event_processor.rs).
  • cargo check --locked -p winit inside the PdfCraft workspace, with PdfCraft's own Cargo.lock, on macOS and on the tree with all nine fixes merged: clean.
  • The new test was compiled and run against this branch (passes) and against main (fails); rustfmt --check and clippy -D warnings on it are clean.
  • Not run locally: the full cargo test --workspace and workspace clippy (the machine is short of disk space for a full build). CI runs them on Linux and Windows.

Risk

The retry needs every condition above. Shortcuts, repeats, other input sources, keys while the canvas has focus (IME disallowed), a composition in progress, or any other callback all fall through to the existing path, which was checked to give identical events.

Revert and removal

This change can be reverted on its own; it needs the macOS IME commit PR. Drop it, together with ime_startup.rs, the test and README item (10), once PdfCraft uses a winit release that includes rust-windowing/winit#4744 or an equivalent fix.

Merging alongside the other winit PRs

This is one of nine fixes to vendor/winit sent together: #672 (Wayland IME after a capability change), #673 (Windows Korean IME caret), #674 (Windows maximized start-up), #675 (X11 input context focus), #676 (macOS IME commit state), #677 (macOS stale composition), and, building on #676, #678 (macOS emoji picker), #679 (macOS Korean double space), #680 (macOS first Korean key). Each adds one numbered item to the single winit row in vendor/README.md, so they conflict with each other on that one line, and nowhere else (each test is in its own file). The item numbers (2)–(10) are fixed in advance: to resolve, keep every item in numeric order. All nine were merged locally in one tree (only that line conflicted); the combined vendored winit builds for macOS, Windows and Linux with the same clippy output as main, and all nine tests pass on it.

🤖 Generated with Claude Code

cyfung1031 and others added 2 commits October 10, 2026 11:06
… (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>
…#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>
@echelon
echelon merged commit 68e91d4 into storytold:main Oct 10, 2026
5 checks passed
@echelon

echelon commented Oct 10, 2026

Copy link
Copy Markdown
Member

Merged, thank you! The first Apple Korean key in a fresh composition now composes (winit#4744). I ran ime_startup.rs's unit tests standalone (7 pass; no CI runs them) and cross-compiled for macOS. When merging, view.rs is main's plus your commit, keeping #677–#679. Patch (10).

If you haven't already, come join us on Discord: https://discord.gg/artcraft

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>
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.

2 participants