Commit 46e93fe
committed
iOS: Implement UITextInput so multi-stage IMEs (CJK) work
The `WinitView` on iOS previously adopted only `UIKeyInput` (plus
the trait-only `UITextInputTraits`). Per Apple's docs that's enough
for plain ASCII keypresses, but multi-stage input methods — Chinese
pinyin, Japanese kana, Korean hangul, Vietnamese, etc. — require the
view to also adopt the `UITextInput` protocol:
https://developer.apple.com/documentation/uikit/uitextinput
> Multi-stage input methods, such as Chinese, Japanese, Korean, and
> Thai are excluded from classes that adopt only the UIKeyInput
> protocol, but if a class also adopts the UITextInput protocol,
> those input methods are then available.
Without `UITextInput`, the iOS soft keyboard silently drops every
keypress while pinyin / kana / hangul is selected, so users on the
simulator or device see a completely unresponsive text input. This
affects every downstream toolkit using winit on iOS; we noticed it
via slint-ui/slint#4698.
This is the master-branch port of the same fix applied to v0.30.x
in #4592 — same logic, adapted to the `winit-uikit` crate split,
`define_class!`, `#[unsafe(method_id(...))]`, and the `winit-core`
event type layout.
What this PR does:
* Adds a new `winit-uikit/src/ime.rs` module with two small `NSObject`
subclasses `WinitTextPosition` / `WinitTextRange`, each carrying a
character offset (positions are opaque to UIKit, so a simple `i64`
wrapper is enough), and an `ImeState` holding the current marked
text + selection.
* Adopts `UITextInput` on `WinitView`:
- `setMarkedText:selectedRange:` → `WindowEvent::Ime(Ime::Preedit(...))`
- `unmarkText` → `Ime::Commit(prev) + Preedit("", None)` (so that
e.g. typing `n` and then tapping `123` keeps `n` in the field
instead of silently dropping it, matching how UIKit's own
`UITextField` behaves)
- `replaceRange:withText:` (the typical pinyin / kana commit path)
→ `WindowEvent::Ime(Ime::Commit(...))`
- The other 20+ required methods are minimal but correct stubs
that model the document as just the current marked string —
that's sufficient because committed text lives on the
application side and only flows back out via the existing
`Ime::Commit` path.
* Implements `baseWritingDirectionForPosition:inDirection:` and
`setBaseWritingDirection:forRange:` as plain selectors. The
protocol marks them required at runtime even though the Rust
binding for `NSWritingDirection` is gated on the `NSText` feature,
so they're declared outside the `UITextInput` impl block to avoid
pulling that feature in.
Tested in the iPhone 17 Pro simulator with iOS 26.3 (system
zh_Hans-Pinyin keyboard):
* Without the patch: tapping any letter on the pinyin keyboard has
no visible effect; candidate bar stays empty.
* With the patch: tapping letters opens the candidate bar populated
with real hanzi (e.g. tapping `n` yields 你 / 能 / 年 …), the
preedit text shows up inline in the focused text input with the
usual iOS highlight, and selecting a candidate (or pressing the
spacebar to commit the first one) inserts the chosen hanzi into
the text input.
* ASCII input via `UIKeyInput.insertText:` and backspace via
`deleteBackward` continue to work unchanged.1 parent 81b2729 commit 46e93fe
3 files changed
Lines changed: 669 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
| 105 | + | |
105 | 106 | | |
106 | 107 | | |
107 | 108 | | |
| |||
0 commit comments