Skip to content

W40: macOS: Korean input drops characters when the input method replaces text - #32

Open
moneycode11101-svg wants to merge 1 commit into
storytold:craft/0.30.13from
moneycode11101-svg:korean-keytext
Open

moneycode11101-svg wants to merge 1 commit into
storytold:craft/0.30.13from
moneycode11101-svg:korean-keytext

Conversation

@moneycode11101-svg

Copy link
Copy Markdown

Korean input on macOS drops characters when the input method replaces text via insertText:replacementRange:: spaces vanish, the previous syllable is overwritten, and syllables come out shuffled.

Apple's Korean input methods (2-Set and the others) don't use marked text. Every key calls insertText:replacementRange:, replacing the syllable typed so far with the updated one, and a syllable ends with insertText of it followed by the Space or the next jamo.

Cause. In insertText, the PlainInsert::KeyText branch leaves the key's text to the key's KeyboardInput (sent at the end of keyDown:). But keyDown: doesn't send that event when the same key press also committed (committed_in_key_down, W9) or moved IME from Disabled to Ground. The text never reaches the app, while ImeDocument and the input method count it, so the next replacement deletes a character the app does have.

Commit Change
ae9f727 W40: key text dropped by keyDown is committed. Text inserted for the key being handled is kept in deferred_key_text until it reaches the app. replaced deletes only the part of the replaced range that reached the app, and when keyDown: doesn't send the KeyboardInput, it commits the remembered text with Ime::Commit. PATCHES.md row W40.

tests/ime_patches.rs: macos_korean_key_text_survives_a_replacement guards the three places (remembering the text in insertText, the commit in keyDown:, and the reduced deletion in replaced). The char-boundary step avoids str::floor_char_boundary to keep the 1.70 MSRV.

Reproduce: macOS, input source Korean 2-Set, in an egui text field (egui-winit with W26's take_ime_delete_surrounding), type 이제 대로 이게 됨 (dlwp eofh dlrp ehla). Before: spaces disappear and syllables are overwritten or swapped. After: the text is exactly 이제 대로 이게 됨.

Tested on macOS (Apple Silicon) in FilmCraft C built against this branch:

  1. cargo test --test ime_patches: 45 passed (44 existing + 1 new); cargo test --lib: 25 passed; cargo clippy --all-targets is clean.
  2. Korean 2-Set typing in FilmCraft text fields: 이제 대로 이게 됨 comes out exactly, both in a custom egui TextEdit panel and in FilmCraft's own Markers search field; Backspace then edits the last syllable (됨 → 되)
  3. English hello world, Backspace and Enter: work as before (ABC layout, Markers search field)

Not tested: Korean 3-Set and other replacement-based input methods (Vietnamese), Japanese/Chinese (they use marked text, which this change doesn't touch), dictation. Other platforms are untouched.

🤖 Generated with Claude Code

Apple Korean doesn't use marked text: every key calls
`insertText:replacementRange:`, replacing the syllable typed so far. The
`PlainInsert::KeyText` branch leaves the key's text to its `KeyboardInput`,
but keyDown drops that event when the same key also committed
(`committed_in_key_down`) or enabled IME. The text never reached the app,
while `ImeDocument` and the input method count it, so the next replacement
deleted a character the app does have: spaces vanished and the previous
syllable was overwritten.

Text the app hasn't received is kept in `deferred_key_text`. A replacement
deletes only what reached the app, and keyDown commits the text when it
drops the key event.

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

Copy link
Copy Markdown

Tested with a real keyboard: macOS 27, Apple 2-Set Korean, GridCraft 0.18.0 built against this PR's head (ae9f727). That is the same base GridCraft 0.18.0 pins (a517b38), so the only winit change is this PR. The GridCraft build also had storytold/gridcraft#1632 ported onto 0.18.0.

Before (a517b38): typing 안녕하세요 into a cell gave 하세요. ㄴ and ㅎ never reached the app, and the next replacement deleted 안 and then 녕, as described in #15.

With this PR, all of these came out as typed:

Input Where Result
안녕하세요 + Enter first key after launch, in-cell editor 안녕하세요
이 + Enter after moving with the arrow keys 이
할로윈 + Enter formula bar, appending to a filled cell 할로윈
이제 대로 이게 됨 + Enter in-cell editor 이제 대로 이게 됨

I also traced the ImeStartup retry, the first Korean key after IME is enabled (5 times in this run). In the retry, Apple Korean replaces the raw jamo from the first pass with itself, then inserts it again. With deferred_key_text, the replacement deletes nothing in the app and the key arrives once as KeyboardInput (Text("ㅇ")). There was no doubled jamo. (A local patch of mine that committed the KeyText whenever a replacement came with it did double here, ㅇ이 for 이, so this path is worth keeping in the test.)

Not tested: #15 itself, other Korean layouts (3-Set, Romaja), and anything outside GridCraft.

Comment thread PATCHES.md
| W25 | `eb4007da` | X11/Wayland: `platform::latin_layout::KeyEventExtLatinLayout`, the key on the first Latin layout group for shortcuts on non-Latin layouts | none; to be proposed | upstream release with an equivalent | storytold/winit PR #5; craft-libs#198 (K2) |
| W26 | `2b9cd1cf` | macOS: replaced text is deleted by UTF-8 byte count with the commit (`WindowExtMacOS::take_ime_delete_surrounding`, same shape as W27) | master: `Ime::DeleteSurrounding` (a new `Ime` variant breaks 0.30 matchers) | the egui → 0.31 move | storytold/winit PR #6 (`d103edae`) |
| W27 | `f40c76a2` | Wayland: text-input-v3 surrounding text (`WindowExtWayland::set_ime_surrounding_text`, `set_ime_surrounding_text_with_change_cause`, `clear_ime_surrounding_text`), delete-surrounding attached to the commit (`take_ime_delete_surrounding` → `ImeDeleteSurrounding { before_bytes, after_bytes }`), per-seat preedit ownership and outgoing state waiting for a matching `done` (E21b) | master has both as core API (`Ime::DeleteSurrounding`) | the egui → 0.31 move | storytold/winit PR #7; craft-libs#198 (I7) |
| W40 | `korean-keytext` | macOS: Apple Korean's key text left to a `KeyboardInput` that keyDown drops (same key committed or enabled IME) is committed instead, and replacements don't delete it from the app (spaces vanishing, previous syllable overwritten) | none; to be proposed | upstream fix | storytold/winit PR (this branch); filmcraft C |

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The W40 row names only Apple Korean, but the same patch fixes Apple Vietnamese too (Space after a syllable lost, storytold/wordcraft#3569). Consider naming both, so the row says what to re-test before dropping the patch:

Suggested change
| W40 | `korean-keytext` | macOS: Apple Korean's key text left to a `KeyboardInput` that keyDown drops (same key committed or enabled IME) is committed instead, and replacements don't delete it from the app (spaces vanishing, previous syllable overwritten) | none; to be proposed | upstream fix | storytold/winit PR (this branch); filmcraft C |
| W40 | `korean-keytext` | macOS: key text that Apple Korean or Apple Vietnamese inserts is left to a `KeyboardInput` that keyDown drops (same key committed or enabled IME); it is committed instead, and replacements don't delete it from the app (Korean: spaces vanishing, previous syllable overwritten; Vietnamese: the Space after a syllable lost) | none; to be proposed | upstream fix | storytold/winit PR (this branch); filmcraft C; wordcraft#3569 |

@ducthotran2010

Copy link
Copy Markdown

Tested with Apple's Vietnamese input method: this PR also fixes storytold/wordcraft#3569 (the Space after a Vietnamese syllable is lost).

Apple Vietnamese (Telex) uses marked text. From reading view.rs, on Space after a syllable the input method calls insertText twice in the same key press: first to commit the syllable, which sets committed_in_key_down, then to insert the space. The second call takes the PlainInsert::KeyText branch, which leaves the space to the key's KeyboardInput. After a commit, keyDown: does not send that event, so the space was lost. With this PR, deferred_key_text is committed instead.

Setup: macOS 26.6.2, Apple Silicon, input source Vietnamese – Telex, real keyboard. WordCraft v0.17.0 built with its winit patch pointed at ae9f7270.

Typed (Telex) With this PR (expected)
hieenj ddaau, one Space after each word hiện đâu
the + Space the
hieenj. + Space hiện.
hieenj + Space twice hiện (two spaces)
hieenj + Space, then Backspace hiện (only the space is deleted)

@M1nu0x0

M1nu0x0 commented Nov 11, 2026

Copy link
Copy Markdown

Tested the ImeStartup retry with text already in the field, with a real keyboard: this PR also fixes that case.

Steps: type abc in a field, click another field and back, ⌘→, switch to Apple 2-Set Korean, type g k s.

Before (a517b38): the field ends as 한. The first key's jamo never reaches the app, and the retry's replacement deletes 3 bytes, which are abc:

DeleteSurroundingBytes(3) Commit("")
DeleteSurroundingBytes(3) Commit("하")
DeleteSurroundingBytes(3) Commit("한")
DeleteSurroundingBytes(3) Commit("한")

With this PR's view.rs (ae9f727) on a517b38: abc한.

Text("ㅎ")
DeleteSurroundingBytes(3) Commit("하")
DeleteSurroundingBytes(3) Commit("한")
DeleteSurroundingBytes(3) Commit("한")

That matches the retry trace in the comment above: the key arrives once as Text("ㅎ") and the replacement deletes only that jamo.

Setup: macOS 26.6.2, Apple Silicon, Apple 2-Set Korean, an egui 0.37.0 TextEdit (eframe, the suite's egui rev 6d518fc7), a new process for each run. One run each with a real keyboard. The same steps with Quartz virtual key codes posted to the app gave the same strings: abc한 in 3 of 3 runs with this PR, 한 without it.

Not tested: continuous typing with this PR, #15, anything outside the test app. Written with AI assistance (Claude).

@IT-HONGREAT

Copy link
Copy Markdown

Tested in FilmCraft: v0.20.1 built with the winit pin changed to this PR's head (ae9f727), nothing else changed. macOS 27.2.1, Apple Silicon, Apple 2-Set Korean, real keyboard.

Before (a517b38): typing 입니다 in Marker Properties ▸ Notes (a stock egui::TextEdit) gives 니다, and almost all Korean text loses syllables.
After: 입니다 and other Korean text are entered correctly.

Not tested: other input methods, #38. Reported as storytold/filmcraft#4954.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants