Skip to content

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

Merged
echelon merged 4 commits into
storytold:mainfrom
cyfung1031:fix/winit-4478-macos-korean-postcommit
Oct 10, 2026
Merged

echelon merged 4 commits into
storytold:mainfrom
cyfung1031:fix/winit-4478-macos-korean-postcommit

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 with Apple Korean, Space types two spaces in PdfCraft when no syllable is in progress: a second space, or a space after a digit or punctuation. This is the double space of alacritty#8079. With this change a key whose text was committed through IME is not also sent as KeyboardInput.

Related upstream PR: rust-windowing/winit#4478 (reviewed head dfb23bff7d), which aims at the same double space. Its changes don't reach the call order seen here, so this is a narrow re-implementation of its goal on top of the commit state from the macOS IME commit PR. It is not a copy of #4478 (see below), and it should go upstream with #4478.

PdfCraft impact

Typing Korean with spaces in any egui text field on macOS.

Root cause

AppKit's calls for that Space (traced on macOS 14.7.1) are setMarkedText(" "), insertText(" ") (winit sends Ime::Commit(" ") and enters the committed state), then setMarkedText(""). The last call returns winit's state to the ground state, so keyDown: sees no IME input and also sends KeyboardInput with text " ". egui-winit turns that text into Event::Text, and the field gets two spaces.

Change

keyDown: records whether the key it is handling committed IME text (committed_in_key_down, set in insertText:). It then doesn't also send the key as KeyboardInput, unless AppKit forwards it as a command through doCommandBySelector:. So Enter after a syllable still commits the syllable and sends Enter. keyDown: consumes the record for every key (Cell::replace), so keys typed after IME is disallowed are unaffected.

Not included from #4478, and why

  • Clearing the marked text on commit and forwarding a later insertText as a key: with the commit state from the PR this builds on, the key after a syllable (한 then 5) is already committed instead of lost. fix(sign): let a writer raise the catalog /Version after signing #4478's forwarding would send it again as a key.
  • Removing doCommandBySelector:'s guard for the committed state: on this call order the state is already back to ground when doCommandBySelector: runs, and Enter already works.

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, compared with the macOS IME commit PR it builds on (the only events that changed):

Scenario Before After
Korean Space, Space with nothing composed each Space: Commit(" ") and KeyboardInput(Space " ") each Space: Commit(" ")
Korean 한 Space Space second Space: Commit(" ") and KeyboardInput(Space " ") Commit(" ")

Unchanged: 한 + Return (Commit("한") then KeyboardInput(Enter)), 한 + 5/;, Korean Backspace, Japanese, Cangjie, ABC, held keys, and keys typed after IME is disallowed (KeyboardInput as before).

apps/pdfcraft/tests/winit_macos_korean_space.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

It only changes keys whose keyDown: emitted an Ime::Commit and whose state ended neither committed nor composing, which means an input method that cleared its marked text after committing. Those keys are now reported only as the commit. Keys AppKit routes as commands are still sent.

Revert and removal

This change can be reverted on its own; it needs the macOS IME commit PR. Drop it, together with the test and README item (9), once PdfCraft uses a winit release that fixes the double space (rust-windowing/winit#4478 or an equivalent).

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>
…pace (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>
# Conflicts:
#	vendor/README.md
#	vendor/winit/src/platform_impl/macos/view.rs
@echelon
echelon merged commit cf9f58f into storytold:main Oct 10, 2026
5 checks passed
h-a-n-a-s-h-i pushed a commit to cyfung1031/pdfcraft that referenced this pull request Oct 10, 2026
@echelon

echelon commented Oct 10, 2026

Copy link
Copy Markdown
Member

Merged, thank you! Space in Apple Korean with nothing composed types one space now. When merging I rebuilt view.rs as main's plus your commit 54b3d3c, because a straight merge would have dropped #678's emoji-picker branch. Patch (9), PdfCraft's own fix, to propose upstream (winit#4478).

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