Skip to content

Tracking issue for IME / composition support #1497

Closed

Description

@murarth

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.

Activity

  1. added
    DS - android
    DS - uikit
    DS - appkit
    DS - wayland
    DS - web
    DS - win32
    H - help wanted
    S - api
    S - enhancement
    on Mar 6, 2020
  2. chrisduerr commented on Apr 4, 2020

    @chrisduerr
    Contributor

    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.

  3. pickfire commented on Jun 26, 2020

    @pickfire

    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.

  4. garasubo commented on Jun 26, 2020

    @garasubo
    Contributor

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

  5. garasubo commented on Jul 10, 2020

    @garasubo
    Contributor

    Proposed the rename these events to IMEPreeditEvent in #1622

  6. kaiwk commented on Aug 20, 2020

    @kaiwk
    Contributor

    For mac, maybe we can use this API? composedString

    Seems setMarkedText does the trick:

  7. jakerr commented on Aug 21, 2020

    @jakerr

    Just FYI here's a similar problem being solved in the glfw project that might provide some hints / inspiration: glfw/glfw#658

  8. 57 remaining items

  9. Isopod commented on Apr 4, 2023

    @Isopod

    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.

  10. pickfire commented on Apr 4, 2023

    @pickfire

    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.

  11. Isopod commented on Apr 4, 2023

    @Isopod

    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.

  12. lucasmerlin commented on Jul 8, 2023

    @lucasmerlin
    Contributor

    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))]
    pub struct TextSpan {
        /// The start of the span (inclusive)
        pub start: Option<usize>,
    
        /// The end of the span (exclusive)
        pub end: Option<usize>,
    }
    
    #[derive(Debug, Clone, PartialEq, Eq)]
    #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
    pub struct TextInputState {
        pub text: String,
        /// A selection defined on the text.
        pub selection: TextSpan,
        /// A composing region defined on the text.
        pub compose_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:

    [patch.crates-io]
    winit = { git = "https://github.com/lucasmerlin/winit", branch = "v0.28.x_ime_support" }
    egui = { git = "https://github.com/lucasmerlin/egui", branch = "mobile_ime_support"}
    eframe = { git = "https://github.com/lucasmerlin/egui", branch = "mobile_ime_support"}
    egui-wgpu = { git = "https://github.com/lucasmerlin/egui", branch = "mobile_ime_support"}
    android-activity = { git = "https://github.com/lucasmerlin/android-activity", branch = "ime_support"}

    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.

  13. kchibisov commented on Jul 8, 2023

    @kchibisov
    Member

    Well, we'd need patches upstream to be merged first, you could open a draft PR if you want to though.

  14. rib commented on Jul 28, 2023

    @rib
    Contributor

    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

  15. phnaharris commented on Mar 2, 2024

    @phnaharris

    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.

  16. kchibisov commented on Mar 2, 2024

    @kchibisov
    Member

    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.

  17. phnaharris commented on Mar 3, 2024

    @phnaharris

    it doesn't exist without setting surrounding text

    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?

    Thank you .

  18. kchibisov commented on Mar 3, 2024

    @kchibisov
    Member

    But I still haven't receive any DeleteSurroundingText event triggered by compositor. Do you have any idea about what I'm missing?

    it's IME dependent, you'll get it only when IME thinks that you should get it. Also, ensure to commit when setting surrounding text.

  19. phnaharris commented on Mar 3, 2024

    @phnaharris

    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?

    My environment:

    • Compositor: Hyprland.
    • IME: Fcitx5.
    • Toolkit: GTK and egui + winit.
  20. kchibisov commented on Mar 3, 2024

    @kchibisov
    Member

    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.

  21. phnaharris commented on Mar 4, 2024

    @phnaharris

    Okay, I got the idea. Thank you for your help.

  22. kchibisov commented on Sep 6, 2025

    @kchibisov
    Member

    No point in keeping meta issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DS - androidDS - appkitDS - uikitDS - waylandDS - webDS - win32H - help wantedS - apiS - enhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions