diff --git a/crates/ui-contract/src/runtime/egui/ime.rs b/crates/ui-contract/src/runtime/egui/ime.rs index 7a3c55f69..a925663e4 100644 --- a/crates/ui-contract/src/runtime/egui/ime.rs +++ b/crates/ui-contract/src/runtime/egui/ime.rs @@ -66,7 +66,7 @@ pub(crate) fn write(ctx: &Context, viewport: ViewportId, out: Output) { }); } -/// Drops what `TextEdit` wrote: the engine is the one writer. +/// Drops what `TextEdit` wrote in a pass whose IME output the engine writes. pub(crate) fn clear(ctx: &Context) { ctx.output_mut(|output| output.ime = None); } diff --git a/crates/ui-contract/src/runtime/ime.rs b/crates/ui-contract/src/runtime/ime.rs index 81e79e5ba..f428cce46 100644 --- a/crates/ui-contract/src/runtime/ime.rs +++ b/crates/ui-contract/src/runtime/ime.rs @@ -1,5 +1,6 @@ //! The IME engine (18.4): who owns the IME this frame, the composing flag, preedit and commit delivery, and the -//! caret rect for the OS. It is the one writer of `PlatformOutput.ime`. +//! caret rect for the OS. While a framework editor owns the IME, the engine is the one reader of the IME events and +//! the one writer of `PlatformOutput.ime`; otherwise both stay with egui and the app's own widgets. use crate::runtime::egui::ime as os; use crate::runtime::egui::pass; @@ -51,11 +52,14 @@ fn current(ctx: &Context, f: impl FnOnce(&mut Engine) -> R) -> R { with(ctx, pass::current_viewport(ctx), f) } -/// The input hook: takes every IME event out of raw input and removes the keys that would end the composition. +/// The input hook: takes the IME events out of raw input while a framework editor owns the IME, or while the reset +/// the engine asked for is pending (the late events of the interrupted composition, and the OS's answer), and removes +/// the keys that would end the composition. pub(crate) fn input_hook(ctx: &Context, events: &mut Vec, viewport: ViewportId) { - let taken = os::take_events(events); with(ctx, viewport, |engine| { - engine.queue.extend(taken); + if engine.owner.is_some() || engine.stale { + engine.queue.extend(os::take_events(events)); + } let guarded = engine.composing || engine.guard > 0; if guarded { events.retain(|event| { @@ -168,7 +172,8 @@ pub(crate) fn note_caret(ctx: &Context, owner: Id, field: Rect, caret: Rect, pas current(ctx, |engine| engine.caret = Some((owner, field, caret, password))); } -/// The end hook: writes the pass's IME output. +/// The end hook: writes the pass's IME output while a framework editor owns the IME or an interrupt is due. Otherwise +/// the output is egui's: an app's own `TextEdit` or IME reader writes it. pub(crate) fn end_pass(ctx: &Context) { let viewport = pass::current_viewport(ctx); let (owner, interrupt, caret, generation) = with(ctx, viewport, |engine| { @@ -177,6 +182,9 @@ pub(crate) fn end_pass(ctx: &Context) { } (engine.owner, engine.interrupt.take(), engine.caret.take(), engine.generation) }); + if owner.is_none() && interrupt.is_none() { + return; + } os::clear(ctx); match (caret, interrupt, owner) { (Some((id, field, caret, password)), interrupt, _) if Some(id) == owner => { diff --git a/crates/ui-contract/src/testing/host_key_trials.rs b/crates/ui-contract/src/testing/host_key_trials.rs index d2cac9461..0aec6d5d0 100644 --- a/crates/ui-contract/src/testing/host_key_trials.rs +++ b/crates/ui-contract/src/testing/host_key_trials.rs @@ -6,7 +6,7 @@ use crate::testing::focus::{Stop, egui_focused, named_id, register_stop, widget} use crate::testing::pointer::{moved, press, release}; use crate::testing::{Frames, trial}; use craft_behavior::Input; -use egui::{Id, Rect, Ui}; +use egui::{Event, Id, ImeEvent, Rect, Ui}; use std::cell::RefCell; use std::rc::Rc; @@ -52,3 +52,16 @@ fn a_press_on_the_focused_egui_field_keeps_its_focus() { frames.frame(vec![press(outside)], &mut show); assert_eq!(egui_focused(&frames.ctx), None, "a press no widget takes surrenders the focus"); } + +#[trial] +fn a_focused_egui_field_takes_the_ime() { + let (host, mut show) = host_window(); + let mut frames = Frames::new(); + frames.frame(Vec::new(), &mut show); + fixture::host_focus(&frames.ctx, field()); + frames.frame(Vec::new(), &mut show); + frames.frame(vec![Event::Ime(ImeEvent::Preedit { text: "世".to_owned(), active_range_chars: None })], &mut show); + assert_eq!(frames.ime().map(|ime| ime.owner), Some(field()), "the field writes the pass's IME output"); + frames.frame(vec![Event::Ime(ImeEvent::Commit("世".to_owned()))], &mut show); + assert_eq!(host.borrow().text, "世", "the commit reaches the field"); +} diff --git a/docs/craft-framework.md b/docs/craft-framework.md index 6719967ab..72f167395 100644 --- a/docs/craft-framework.md +++ b/docs/craft-framework.md @@ -153,6 +153,7 @@ An app with its own eframe `App` calls the hooks in `craft::ui::shell::embed`. ` | Escape | Plain Escape (Alt and Shift may be held) never reaches egui. It goes to the focused framework stop and its scopes; with no framework stop focused, it goes to the window as `Intent::Cancel`. | | F10, Alt+letter | Taken while a framework menu bar is registered: F10 focuses the bar, Alt+letter opens a menu (not on macOS). | | Edit chords | Taken while a framework editor is focused. | +| IME | Taken while a framework editor owns the IME: its events and `PlatformOutput.ime` are the runtime's. Otherwise they stay egui's, for the app's own `TextEdit` or IME code. | | Releases | The release of every key the runtime took is taken too. | | Focus surrender | egui's own surrender is off; the runtime moves focus on press. A press a framework component takes moves focus to it; a press no component takes drops egui focus, unless it lands on the widget that holds egui focus (an app's own focused field keeps it). | | Zoom keys | egui's keyboard zoom is off. The interface scale is `RootEnv::scale`. |