You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tracking issue for IME / composition support #1497
DS - androidAffects the Android backendDS - appkitAffects the AppKit/macOS backendDS - uikitAffects the UIKit backend (iOS, tvOS, watchOS, visionOS)DS - waylandAffects the Wayland backend, or generally free Unix platformsDS - webAffects the Web backend (WebAssembly/WASM)DS - win32Affects the Win32/Windows backendH - help wantedSomeone please save usS - apiDesign and usabilityS - enhancementWouldn't this be the coolest?
This issue tracks the implementation of IME composition events on each platform, an API initially proposed in #1293. PRs implementing this event should be made against the composition-event branch, which will be merged into master once all implementations are complete.
As a consumer of winit I'd recommend against naming these events CompositionEvent. Applications that deal with display servers will already know that term in connection with window compositing and it could easily be confused with something related to that.
It also does not fall in line with the existing winit naming of functions like set_ime_position.
I think it would be much better to have a more self descriptive name for this event that cannot be confused with anything else related to the winit project. I'd strongly suggest that IME should at least be part of the name.
Oh, I thought CompositionEvent means some kind of compose key thing, I never used those and don't even know how to use compose key, I had only used IME to input characters not in the keyboard.
Ok, I just tested it on a fresh Fedora 37 VM, can confirm that the IME works there on both Wayland and X11 (tested in alacritty). The UX isn't the best, but it appears to be usable. Not sure what Fedora is doing differently, it might just be an issue on my machine then.
I used alacritty-git alacritty 0.13.0-dev (2df8f860) with IME on both x11 and wayland kde on arch linux and it works, and I have no idea what is XIM even though I heard about it, I don't think I touched that. But I remember on wayland some tweaks needs to be done, like set the virtual keyboard https://fcitx-im.org/wiki/FAQ#Non_Gtk.2FQt_Wayland_Application_.28Alacritty.2C_kitty.2C_etc.29.
The issue I had might be specific to the Arch Linux ibus package. The PKGBUILD does not pass --enable-xim to configure. I recompiled the package with --enable-xim and it works now. Note that you also have to start ibus-daemon with -x or --xim.
I managed to add IME support to android and iOS:
Android input works really well, including autocomplete and suggestions:
Video
Screen_Recording_20230708_144153.mp4
iOS supports basic text entry, but no autocomplete /autocorrect yet:
Video
IMG_0149.MP4
The android support is based on @rib's work on android-activity here: rust-mobile/android-activity#24
I had to add a new Event to winit, basically just passing through the TextInputState to egui, handling the logic there.
Implementation in egui is pretty simple, basically
send the initial TextInputState (text + cursor position/selection) to winit when focusing a text field + open the keyboard
update the text in egui whenever a new TextInputState is received, handling the selection and compose region.
The TextInputState struct looks like this:
Expand me
/// This struct holds a span within a region of text from `start` (inclusive) to/// `end` (exclusive).////// An empty span or cursor position is specified with `Some(start) == Some(end)`.////// An undefined span is specified with start = end = `None`.#[derive(Debug,Clone,Copy,PartialEq,Eq)]#[cfg_attr(feature = "serde", derive(Serialize,Deserialize))]pubstructTextSpan{/// The start of the span (inclusive)pubstart:Option<usize>,/// The end of the span (exclusive)pubend:Option<usize>,}#[derive(Debug,Clone,PartialEq,Eq)]#[cfg_attr(feature = "serde", derive(Serialize,Deserialize))]pubstructTextInputState{pubtext:String,/// A selection defined on the text.pubselection:TextSpan,/// A composing region defined on the text.pubcompose_region:TextSpan,}
For iOS text support, I implemented the UIKeyInput api to receive basic key events but it doesn't support autocomplete / autocorrect / character composition yet. There also is UITextInput to support these features, I think it should be possible to write a implementation in winit that provides the same simple TextInputState api to applications, but I don't have the objective c skills to implement this.
If you want to try these in an egui app, you can add the following to your Cargo.toml to try my branches:
So first of all I don't need CompostionDone event, since this is an event when I'd send things downstream, meaning that I don't need CompositionDeleteSurroundingText. And it seems like I don't need DeleteSurroundingText, since I can model it via CompositionPreedit. CompositionCommit(text: String) is indeed needed so the user will know when to insert text.
Hi @kchibisov, I'm wondering why you said that you don't need CompositionDeleteSurroundingText event and how we can model it via CompositionPreedit. Because, the surrounding text is the text around the cursor, even if user committed it to widget or not, as far as I know. I think what you mean is we can modify the preedit, but the problem became a thing if the IME want to delete the surrounding text via text input v3 after the preedit be committed to widget, I think we didn't have a mechanism for doing that yet.
Now I wanna support this feature about surrounding text. Based on the design of GTK for this feature, I need a way for downstream (I'm using egui) to send the surrounding text back to winit whenever it need, through Ime::RetrieveSurrounding event. It would be nice if you have any idea for doing that.
You should add delete surrounding text, just keep in mind that it doesn't exist without setting surrounding text. The issue is how to communicate that cross platform, but it's a technical detail.
Part of that was done in #2993 . IME will also be reworked for 0.31.
I've taken the work in this pull request and got the idea that how the surrounding text be updated in the event loop. I also implement set_ime_surrounding_text for Wayland and call it whenever it changed, especially when text input enabled (and commit it to compositor). But I still haven't receive any DeleteSurroundingText event triggered by compositor. Do you have any idea about what I'm missing?
I understand. But if I reproduce the same situation in the same environment, I should expect the same result right? I've created a text box with gtk and egui (with winit), input the same content and when I go back to change some character, gtk worked fine with surrounding text while winit receive a new Preedit event.
Have you ever got a DeleteSurroundingText event in winit before? I just wanna know if the bug come from the flow that I'm implemented was wrong or something else?
The event is not handled by winit unless you've added it. Use WAYLAND_DEBUG=1 to compare patters and use that knowledge to troubleshoot, since usually it's all pretty deterministic.
DS - androidAffects the Android backendDS - appkitAffects the AppKit/macOS backendDS - uikitAffects the UIKit backend (iOS, tvOS, watchOS, visionOS)DS - waylandAffects the Wayland backend, or generally free Unix platformsDS - webAffects the Web backend (WebAssembly/WASM)DS - win32Affects the Win32/Windows backendH - help wantedSomeone please save usS - apiDesign and usabilityS - enhancementWouldn't this be the coolest?
Activity
chrisduerr commentedon Apr 4, 2020
As a consumer of winit I'd recommend against naming these events
CompositionEvent. Applications that deal with display servers will already know that term in connection with window compositing and it could easily be confused with something related to that.It also does not fall in line with the existing winit naming of functions like
set_ime_position.I think it would be much better to have a more self descriptive name for this event that cannot be confused with anything else related to the winit project. I'd strongly suggest that
IMEshould at least be part of the name.pickfire commentedon Jun 26, 2020
Oh, I thought
CompositionEventmeans some kind of compose key thing, I never used those and don't even know how to use compose key, I had only usedIMEto input characters not in the keyboard.garasubo commentedon Jun 26, 2020
@pickfire This event naming comes from web specification (Ref: https://developer.mozilla.org/en-US/docs/Web/API/Element/compositionstart_event). And IME will create those composition events.
But as @chrisduerr is pointed out, I agree that calling those events as "composition" is confusing.
garasubo commentedon Jul 10, 2020
Proposed the rename these events to
IMEPreeditEventin #1622kaiwk commentedon Aug 20, 2020
For mac, maybe we can use this API? composedStringSeems
setMarkedTextdoes the trick:jakerr commentedon Aug 21, 2020
Just FYI here's a similar problem being solved in the glfw project that might provide some hints / inspiration: glfw/glfw#658
57 remaining items
Isopod commentedon Apr 4, 2023
Ok, I just tested it on a fresh Fedora 37 VM, can confirm that the IME works there on both Wayland and X11 (tested in alacritty). The UX isn't the best, but it appears to be usable. Not sure what Fedora is doing differently, it might just be an issue on my machine then.
pickfire commentedon Apr 4, 2023
I used alacritty-git alacritty 0.13.0-dev (2df8f860) with IME on both x11 and wayland kde on arch linux and it works, and I have no idea what is XIM even though I heard about it, I don't think I touched that. But I remember on wayland some tweaks needs to be done, like set the virtual keyboard https://fcitx-im.org/wiki/FAQ#Non_Gtk.2FQt_Wayland_Application_.28Alacritty.2C_kitty.2C_etc.29.
Isopod commentedon Apr 4, 2023
The issue I had might be specific to the Arch Linux ibus package. The PKGBUILD does not pass
--enable-ximtoconfigure. I recompiled the package with--enable-ximand it works now. Note that you also have to startibus-daemonwith-xor--xim.lucasmerlin commentedon Jul 8, 2023
I managed to add IME support to android and iOS:
Android input works really well, including autocomplete and suggestions:
Video
Screen_Recording_20230708_144153.mp4
iOS supports basic text entry, but no autocomplete /autocorrect yet:
Video
IMG_0149.MP4
The android support is based on @rib's work on android-activity here: rust-mobile/android-activity#24
I had to add a new Event to winit, basically just passing through the TextInputState to egui, handling the logic there.
Implementation in egui is pretty simple, basically
The
TextInputStatestruct looks like this:Expand me
For iOS text support, I implemented the UIKeyInput api to receive basic key events but it doesn't support autocomplete / autocorrect / character composition yet. There also is UITextInput to support these features, I think it should be possible to write a implementation in winit that provides the same simple TextInputState api to applications, but I don't have the objective c skills to implement this.
If you want to try these in an egui app, you can add the following to your Cargo.toml to try my branches:
Or try this example for android: https://github.com/lucasmerlin/rust-android-examples/tree/ime_support_showcase/agdk-eframe.
These are the relevant branches:
https://github.com/lucasmerlin/winit/tree/mobile_ime_support
https://github.com/lucasmerlin/egui/tree/mobile_ime_support
https://github.com/lucasmerlin/android-activity/tree/ime_support (based on the work of @rib)
If there is interest to add this to winit I'm happy to open a draft PR.
kchibisov commentedon Jul 8, 2023
Well, we'd need patches upstream to be merged first, you could open a draft PR if you want to though.
rib commentedon Jul 28, 2023
Sorry for the delay following up here @lucasmerlin - very cool that you got something working here.
I'm hoping to get a chance to look at this soon.
It'll be good to compare the egui / winit changes with the ones I experimented with at the time:
https://github.com/rib/winit/tree/android-activity-ime-events
https://github.com/rib/egui/tree/android-winit-ime-support
It'll be good to test this on GameActivity 2.0.2 once I land this PR: rust-mobile/android-activity#88
phnaharris commentedon Mar 2, 2024
Hi @kchibisov, I'm wondering why you said that you don't need
CompositionDeleteSurroundingTextevent and how we can model it viaCompositionPreedit. Because, the surrounding text is the text around the cursor, even if user committed it to widget or not, as far as I know. I think what you mean is we can modify the preedit, but the problem became a thing if the IME want to delete the surrounding text via text input v3 after the preedit be committed to widget, I think we didn't have a mechanism for doing that yet.Now I wanna support this feature about surrounding text. Based on the design of
GTKfor this feature, I need a way for downstream (I'm usingegui) to send the surrounding text back towinitwhenever it need, throughIme::RetrieveSurroundingevent. It would be nice if you have any idea for doing that.kchibisov commentedon Mar 2, 2024
You should add delete surrounding text, just keep in mind that it doesn't exist without setting surrounding text. The issue is how to communicate that cross platform, but it's a technical detail.
Part of that was done in #2993 . IME will also be reworked for 0.31.
phnaharris commentedon Mar 3, 2024
I've taken the work in this pull request and got the idea that how the surrounding text be updated in the event loop. I also implement
set_ime_surrounding_textfor Wayland and call it whenever it changed, especially when text input enabled (and commit it to compositor). But I still haven't receive anyDeleteSurroundingTextevent triggered by compositor. Do you have any idea about what I'm missing?Thank you .
kchibisov commentedon Mar 3, 2024
it's IME dependent, you'll get it only when IME thinks that you should get it. Also, ensure to
commitwhen setting surrounding text.phnaharris commentedon Mar 3, 2024
I understand. But if I reproduce the same situation in the same environment, I should expect the same result right? I've created a text box with gtk and egui (with winit), input the same content and when I go back to change some character, gtk worked fine with surrounding text while winit receive a new
Preeditevent.Have you ever got a
DeleteSurroundingTextevent in winit before? I just wanna know if the bug come from the flow that I'm implemented was wrong or something else?My environment:
kchibisov commentedon Mar 3, 2024
The event is not handled by winit unless you've added it. Use
WAYLAND_DEBUG=1to compare patters and use that knowledge to troubleshoot, since usually it's all pretty deterministic.phnaharris commentedon Mar 4, 2024
Okay, I got the idea. Thank you for your help.
kchibisov commentedon Sep 6, 2025
No point in keeping meta issue.