From a3a58be575021d853c9f74c6366e6fab8c36d11a Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 24 Nov 2026 22:33:51 +0000 Subject: [PATCH] IME: the fence follows the app's reset requests asked through egui The app's reset of a pass is the one egui publishes: Context::pending_ime_reset, else the IME output's own field. The interrupt of an app editor reuses it, the engine records it, and a request the app publishes while the fence is up becomes the one the fence waits for. A completion answers its request and every older one. --- .../ui-contract/src/runtime/egui/fixture.rs | 23 +++-- crates/ui-contract/src/runtime/egui/ime.rs | 14 ++- crates/ui-contract/src/runtime/ime.rs | 30 ++++--- .../src/testing/host_key_trials.rs | 86 ++++++++++++++----- 4 files changed, 112 insertions(+), 41 deletions(-) diff --git a/crates/ui-contract/src/runtime/egui/fixture.rs b/crates/ui-contract/src/runtime/egui/fixture.rs index e1064759df..63be1d9b0d 100644 --- a/crates/ui-contract/src/runtime/egui/fixture.rs +++ b/crates/ui-contract/src/runtime/egui/fixture.rs @@ -526,17 +526,23 @@ pub(crate) fn host_ime_editor(ui: &Ui, id: egui::Id, rect: Rect, text: &mut Stri pub(crate) struct ResettingEditor { pub(crate) text: String, pub(crate) composing: bool, - /// The reset the editor asked for at a press during its composition, until its completion arrives; the editor - /// takes no IME event meanwhile. + /// The reset the editor asked for last, until its completion arrives; the editor takes no IME event meanwhile. pub(crate) reset: Option, + /// The editor also writes its request into its IME output's `reset_request`. Otherwise it leaves that field to egui, + /// which publishes the request asked through `Context::request_ime_reset`, as EffectCraft's editor does. + pub(crate) in_output: bool, + /// Ask for a reset at the next pass, as EffectCraft's editor does when its caret moves after an outside press. + pub(crate) ask: bool, } /// A host's own text editor of `id` at `rect` that fences its own interrupted composition, as EffectCraft's viewer text -/// editor does: a primary press while it composes ends the composition and asks the platform for a reset in its own IME -/// output, and the editor ignores IME events until that reset's completion. Otherwise as [`host_ime_editor`]. +/// editor does: a primary press on it while it composes ends the composition and asks the platform for a reset, and the +/// editor ignores IME events until that reset's completion. A press elsewhere asks for nothing. Otherwise as +/// [`host_ime_editor`]. #[cfg(feature = "suite")] pub(crate) fn host_resetting_ime_editor(ui: &Ui, id: egui::Id, rect: Rect, editor: &mut ResettingEditor) { let _ = ui.interact(rect, id, egui::Sense::click()); + let pressed = ui.input(|input| input.pointer.primary_pressed() && input.pointer.press_origin().is_some_and(|at| rect.contains(at))); for event in ui.input(|input| input.events.clone()) { match event { Event::ImeResetCompleted { request_id } if editor.reset == Some(request_id) => editor.reset = None, @@ -555,11 +561,14 @@ pub(crate) fn host_resetting_ime_editor(ui: &Ui, id: egui::Id, rect: Rect, edito } } let mut ime = egui::output::IMEOutput::new(id, 1, egui::IMEPurpose::Normal, rect, rect); - if editor.composing && ui.input(|input| input.pointer.primary_pressed()) { + let interrupted = editor.composing && pressed; + if interrupted || std::mem::take(&mut editor.ask) { editor.composing = false; editor.reset = ui.ctx().request_ime_reset(ui.ctx().viewport_id()); - ime.should_interrupt_composition = true; - ime.reset_request = editor.reset; + ime.should_interrupt_composition = interrupted; + if editor.in_output { + ime.reset_request = editor.reset; + } } ui.ctx().output_mut(|output| output.ime = Some(ime)); } diff --git a/crates/ui-contract/src/runtime/egui/ime.rs b/crates/ui-contract/src/runtime/egui/ime.rs index 71fd378600..9c9542d310 100644 --- a/crates/ui-contract/src/runtime/egui/ime.rs +++ b/crates/ui-contract/src/runtime/egui/ime.rs @@ -109,12 +109,18 @@ pub(crate) fn write(ctx: &Context, viewport: ViewportId, out: Output) -> Option< reset } +/// The reset request the app asked for this pass, as egui publishes it at the end of the pass: one asked through +/// `Context::request_ime_reset`, which replaces the IME output's own `reset_request`, or else the one the app wrote +/// there. +pub(crate) fn app_reset(ctx: &Context, viewport: ViewportId) -> Option { + ctx.pending_ime_reset(viewport).or_else(|| ctx.output(|output| output.ime.as_ref().and_then(|ime| ime.reset_request))) +} + /// Interrupts the composition of the IME output the app's own editor wrote this pass, in its context, and asks for a -/// reset. An editor that asked for its own reset in that output keeps its request, so the platform answers the app's -/// id and the app gets its completion. Returns the reset request the output carries, as `write` does. +/// reset. An editor that asked for its own reset this pass (`app_reset`) keeps its request, so the platform answers the +/// app's id and the app gets its completion. Returns the reset request the output carries, as `write` does. pub(crate) fn interrupt_written(ctx: &Context, viewport: ViewportId) -> Option { - let written = ctx.output(|output| output.ime.as_ref().and_then(|ime| ime.reset_request)); - let reset = written.or_else(|| ctx.request_ime_reset(viewport)); + let reset = app_reset(ctx, viewport).or_else(|| ctx.request_ime_reset(viewport)); ctx.output_mut(|output| { if let Some(ime) = &mut output.ime { ime.should_interrupt_composition = true; diff --git a/crates/ui-contract/src/runtime/ime.rs b/crates/ui-contract/src/runtime/ime.rs index 20c6509eed..4870f17ebd 100644 --- a/crates/ui-contract/src/runtime/ime.rs +++ b/crates/ui-contract/src/runtime/ime.rs @@ -53,9 +53,9 @@ struct Engine { editor: Option, /// The app's own editor that wrote the IME output of the last pass while no framework editor owned the IME. host: Option, - /// The reset requests the app's own editor wrote in its IME output, unanswered yet: their completions and - /// retirements are the app's and stay in egui's input for it, fence or not. One of them is the fence's `reset` - /// when the app asked for its reset in the pass the engine interrupted its composition. + /// The reset requests the app's own editor published with its IME output, oldest first, unanswered yet: their + /// completions and retirements are the app's and stay in egui's input for it, fence or not. The latest one the app + /// published while the fence is up is the fence's `reset`, from the pass the engine interrupted its composition on. host_resets: Vec, } @@ -201,13 +201,17 @@ impl Engine { self.discard = true; } - /// Notes an answer to one of the app's own reset requests: the request is answered, and a completion of the one the - /// fence waits for ends the fence; a retirement leaves the end hook to ask again. + /// Notes an answer to one of the app's own reset requests. A completion answers that request and every older one, + /// as completions arrive in request order, and ends the fence when it waits for one of them; a retirement answers + /// only its own request and, when the fence waits for it, leaves the end hook to ask again. fn host_answered(&mut self, event: &Event) { let (Event::ImeResetCompleted { request_id: id } | Event::ImeResetRetired { request_id: id }) = event else { return }; let Some(at) = self.host_resets.iter().position(|own| own == id) else { return }; - self.host_resets.remove(at); - if self.reset == Some(*id) { + let answered: Vec<_> = match event { + Event::ImeResetCompleted { .. } => self.host_resets.drain(..=at).collect(), + _ => vec![self.host_resets.remove(at)], + }; + if self.reset.is_some_and(|reset| answered.contains(&reset)) { self.reset = None; if matches!(event, Event::ImeResetCompleted { .. }) { self.stale = false; @@ -490,8 +494,9 @@ pub(crate) fn note_caret(ctx: &Context, owner: Id, field: Rect, caret: Rect, pas 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 app_ime = ctx.output(|output| output.ime.as_ref().map(|ime| (ime.owner, ime.context_generation, ime.reset_request))); - let app_wrote = app_ime.map(|(owner, generation, _)| (owner, generation)); + let app_wrote = ctx.output(|output| output.ime.as_ref().map(|ime| (ime.owner, ime.context_generation))); + // egui publishes the app's request only in a pass that has an IME output. + let app_reset = app_wrote.and(os::app_reset(ctx, viewport)); let (owner, interrupt, caret, update, published, host) = with(ctx, viewport, |engine| { let ran = std::mem::take(&mut engine.ran); if !ran { @@ -510,10 +515,15 @@ pub(crate) fn end_pass(ctx: &Context) { let last_host = engine.host; let kept = engine.host.filter(|host| app_wrote.is_some_and(|(owner, _)| owner == host.owner)).is_some_and(|host| host.composing); engine.host = app_wrote.filter(|_| engine.owner.is_none()).map(|(owner, generation)| HostIme { owner, generation, composing: kept }); - if let Some(id) = app_ime.filter(|_| engine.owner.is_none()).and_then(|(.., reset)| reset) + if let Some(id) = app_reset.filter(|_| engine.owner.is_none()) && !engine.host_resets.contains(&id) { engine.host_resets.push(id); + // Completions arrive in request order: the latest request's completion ends the interrupted composition, + // whichever request the fence waited for. + if engine.stale { + engine.reset = Some(id); + } } // The fence has no request the integration can answer: the owner's output asks again, a framework editor's // or the app's own. diff --git a/crates/ui-contract/src/testing/host_key_trials.rs b/crates/ui-contract/src/testing/host_key_trials.rs index c09af46180..e65e8c1cd3 100644 --- a/crates/ui-contract/src/testing/host_key_trials.rs +++ b/crates/ui-contract/src/testing/host_key_trials.rs @@ -215,12 +215,11 @@ fn a_press_during_an_app_editor_s_composition_fences_its_late_ime_events() { assert_eq!(*editor.borrow(), "abX", "the commit after the completion reaches the editor"); } -/// An app editor that asks for its own reset at the press that interrupts its composition keeps that request: the -/// platform is asked to answer the app's id, the late events stay fenced, and the completion reaches the editor, which -/// takes the events after it and composes again. -#[trial] -fn an_app_editor_s_own_reset_at_the_press_reaches_it_and_it_composes_again() { - let editor = Rc::new(RefCell::new(fixture::ResettingEditor::default())); +/// The app's own resetting editor at its rect, the show closure that draws it, and the frames after two idle ones, +/// a commit of "ab" and a preedit of "z": the editor composes. `in_output`: the editor writes its reset request into +/// its IME output too, instead of leaving it to egui as EffectCraft's editor does. +fn composing_resetting_editor(in_output: bool) -> (Rc>, impl FnMut(&mut Ui), Frames) { + let editor = Rc::new(RefCell::new(fixture::ResettingEditor { in_output, ..Default::default() })); let held = Rc::clone(&editor); let rect = Rect::from_min_size(Pos2::new(100.0, 100.0), egui::vec2(200.0, 30.0)); let mut show = move |ui: &mut Ui| fixture::host_resetting_ime_editor(ui, host_editor_id(), rect, &mut held.borrow_mut()); @@ -228,25 +227,72 @@ fn an_app_editor_s_own_reset_at_the_press_reaches_it_and_it_composes_again() { frames.idle(2, &mut show); frames.frame(vec![Event::Ime(ImeEvent::Commit("ab".to_owned()))], &mut show); frames.frame(vec![Event::Ime(ImeEvent::Preedit { text: "z".to_owned(), active_range_chars: None })], &mut show); - let at = Pos2::new(150.0, 115.0); - let late = vec![ + (editor, show, frames) +} + +/// The press at `at` with the composition's late deletion and commit after it, in one frame. +fn press_with_late_ime(at: Pos2) -> Vec { + vec![ moved(at), press(at), Event::Ime(ImeEvent::DeleteSurrounding { before_chars: 1, after_chars: 0 }), Event::Ime(ImeEvent::Commit("Q".to_owned())), - ]; - frames.frame(late, &mut show); - let own = editor.borrow().reset; - assert!(own.is_some(), "the editor asked for its own reset at the press"); - let reset = frames.ime().and_then(|ime| ime.interrupt.then_some(ime.reset).flatten()); - assert_eq!(reset, own, "the interrupt asks the platform to answer the editor's own request"); - let Some(reset) = reset else { return }; - frames.frame(vec![release(at)], &mut show); + ] +} + +/// An app editor that asks for its own reset at the press that interrupts its composition keeps that request: the +/// platform is asked to answer the app's id, the late events stay fenced, and the completion reaches the editor, which +/// takes the events after it and composes again. The editor asks through egui alone, or writes the request into its +/// output too. +#[trial] +fn an_app_editor_s_own_reset_at_the_press_reaches_it_and_it_composes_again() { + for in_output in [false, true] { + let (editor, mut show, mut frames) = composing_resetting_editor(in_output); + let at = Pos2::new(150.0, 115.0); + frames.frame(press_with_late_ime(at), &mut show); + let own = editor.borrow().reset; + assert!(own.is_some(), "the editor asked for its own reset at the press"); + let reset = frames.ime().and_then(|ime| ime.interrupt.then_some(ime.reset).flatten()); + assert_eq!(reset, own, "the interrupt asks the platform to answer the editor's own request (in_output {in_output})"); + let Some(reset) = reset else { return }; + frames.frame(vec![release(at)], &mut show); + frames.frame(Vec::new(), &mut show); + assert_eq!(editor.borrow().text, "ab", "the composition's late deletion and commit never reach the editor"); + frames.frame(vec![Event::ImeResetCompleted { request_id: reset }, Event::Ime(ImeEvent::Commit("X".to_owned()))], &mut show); + assert_eq!(editor.borrow().reset, None, "the completion of its own request reaches the editor"); + assert_eq!(editor.borrow().text, "abX", "the commit after it reaches the editor"); + frames.frame(vec![Event::Ime(ImeEvent::Preedit { text: "y".to_owned(), active_range_chars: None })], &mut show); + assert!(editor.borrow().composing, "the editor composes again"); + } +} + +/// A press outside the app's editor fences its composition with the framework's own reset request; a reset the app +/// asks for in a later pass becomes the one the fence waits for. Its completion ends the fence, the commit after it +/// reaches the editor, and the framework's request is never answered. +#[trial] +fn an_app_editor_s_later_reset_ends_the_fence() { + let (editor, mut show, mut frames) = composing_resetting_editor(false); + let outside = Pos2::new(500.0, 400.0); + frames.frame(press_with_late_ime(outside), &mut show); + let fence = frames.ime().and_then(|ime| ime.interrupt.then_some(ime.reset).flatten()); + assert!(fence.is_some() && editor.borrow().reset.is_none(), "the framework fences with its own request; the editor asks nothing"); + frames.frame(vec![release(outside)], &mut show); + editor.borrow_mut().ask = true; frames.frame(Vec::new(), &mut show); - assert_eq!(editor.borrow().text, "ab", "the composition's late deletion and commit never reach the editor"); - frames.frame(vec![Event::ImeResetCompleted { request_id: reset }, Event::Ime(ImeEvent::Commit("X".to_owned()))], &mut show); - assert_eq!(editor.borrow().reset, None, "the completion of its own request reaches the editor"); - assert_eq!(editor.borrow().text, "abX", "the commit after it reaches the editor"); + let later = editor.borrow().reset; + assert!(later.is_some() && later != fence, "the editor asks for its own reset later"); + assert_eq!(frames.ime().and_then(|ime| ime.reset), later, "the pass publishes the editor's request"); + let Some(later) = later else { return }; + frames.frame( + vec![ + Event::Ime(ImeEvent::Commit("Q".to_owned())), + Event::ImeResetCompleted { request_id: later }, + Event::Ime(ImeEvent::Commit("X".to_owned())), + ], + &mut show, + ); + assert_eq!(editor.borrow().reset, None, "the completion of its later request reaches the editor"); + assert_eq!(editor.borrow().text, "abX", "the late commit is fenced and the commit after the completion reaches the editor"); frames.frame(vec![Event::Ime(ImeEvent::Preedit { text: "y".to_owned(), active_range_chars: None })], &mut show); assert!(editor.borrow().composing, "the editor composes again"); }