diff --git a/crates/ui-contract/src/runtime/ime.rs b/crates/ui-contract/src/runtime/ime.rs index b474abc46..edbfed635 100644 --- a/crates/ui-contract/src/runtime/ime.rs +++ b/crates/ui-contract/src/runtime/ime.rs @@ -159,6 +159,23 @@ impl Engine { self.observed.map(|(target, _)| target).filter(|target| target.owner == owner) } + /// Hands the IME to `owner`. The old owner, if it was composing, keeps its preedit for its adapter and is + /// interrupted. The tracker starts the new owner's context when the end hook observes it. + fn set_owner(&mut self, owner: Option) { + if self.owner == owner { + return; + } + if self.composing + && let Some(old) = self.owner + { + self.leaving = Some((old, std::mem::take(&mut self.preedit))); + self.interrupt(old); + } + self.composing = false; + self.edits.clear(); + self.owner = owner; + } + /// Ends the composition of `owner` here and has the end hook interrupt the OS composition; its late events are /// dropped until the reset completes. fn interrupt(&mut self, owner: Id) { @@ -203,18 +220,7 @@ pub(crate) fn take_discard(ctx: &Context, viewport: ViewportId) -> bool { /// The begin hook, after pending focus moves are applied: resolves the owner and turns the queue into edits. pub(crate) fn begin(ctx: &Context, owner: Option) { current(ctx, |engine| { - if engine.owner != owner { - if engine.composing - && let Some(old) = engine.owner - { - engine.leaving = Some((old, std::mem::take(&mut engine.preedit))); - engine.interrupt(old); - } - engine.composing = false; - engine.edits.clear(); - // The tracker starts the new owner's context when the end hook observes it. - engine.owner = owner; - } + engine.set_owner(owner); for (raw, place) in std::mem::take(&mut engine.queue) { match raw { // A completion of an earlier request is stale: the latest request still fences. @@ -404,9 +410,19 @@ pub(crate) fn end_pass(ctx: &Context) { let viewport = pass::current_viewport(ctx); let (frame_nr, pass_nr) = (ctx.cumulative_frame_nr_for(viewport), ctx.cumulative_pass_nr_for(viewport)); let (owner, interrupt, caret, update, published) = with(ctx, viewport, |engine| { - if !std::mem::take(&mut engine.ran) { + let ran = std::mem::take(&mut engine.ran); + if !ran { engine.edits.clear(); } + // An owner that neither took its edits nor noted its caret was not shown this pass (a replay notes the caret + // again). egui keeps its focus until after this hook, so it loses the IME here: the interrupt goes out in the + // pass it vanished, not at the next begin. + if let Some(owner) = engine.owner + && !ran + && engine.caret.is_none_or(|(id, ..)| id != owner) + { + engine.set_owner(None); + } // The fence has no request the integration can answer: the owner's output asks again. if engine.stale && engine.reset.is_none() diff --git a/crates/ui-contract/tests/it/text/editor.rs b/crates/ui-contract/tests/it/text/editor.rs index 3c7db0a92..c3246338f 100644 --- a/crates/ui-contract/tests/it/text/editor.rs +++ b/crates/ui-contract/tests/it/text/editor.rs @@ -542,6 +542,32 @@ fn an_owner_change_mid_composition_interrupts_it_and_drops_its_late_input() { ); } +/// An editor that stops being shown while it composes is interrupted in the pass it vanished in. egui keeps its focus +/// until the end of that pass, so waiting for the owner change at the next begin interrupted it a frame late. Guards +/// against the interrupt depending on a second pass of the frame. +#[trial] +fn an_editor_that_vanishes_while_composing_is_interrupted_in_that_pass() { + let log = RefCell::new(Vec::new()); + let shown = Cell::new(true); + let mut ui = |ui: &mut egui::Ui| { + if shown.get() { + Probe::new(&log, true).label("Source").id_salt("code").show_at(ui, AT); + } + }; + let mut frames = Frames::new(); + click(&mut frames, AT.center(), &mut ui); + frames.frame(vec![preedit("k")], &mut ui); + assert!(matches!(log.take().as_slice(), [EditorInput::Preedit { .. }]), "the editor composes"); + + shown.set(false); + let passes = frames.frame(Vec::new(), &mut ui); + let seen = frames.ime().cloned(); + assert!(seen.as_ref().is_some_and(|ime| ime.interrupt && ime.reset.is_some()), "the pass the editor vanished in interrupts it: {seen:?}"); + assert_eq!(passes, 1, "the interrupt needs no second pass"); + frames.frame(Vec::new(), &mut ui); + assert!(frames.ime().is_none(), "with the editor gone, no one owns the IME: {:?}", frames.ime()); +} + /// An editor losing focus while it composes asks the system once to discard the marked text: later frames and a retired /// request asked again add no discard. #[trial]