From a0b1c35a8f1b053400a9806bfbeaa9aed43756e6 Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 24 Nov 2026 21:46:04 +0000 Subject: [PATCH] IME: an app editor's own reset survives the host fence A press during the app's own editor's composition interrupts it, and the engine wrote its own reset request over the one the editor wrote in the same output: the platform answered only the engine's id, so the app never got the completion its own fence waits for, and the fence (which took every IME event) dropped it anyway. The interrupt keeps a reset request the app's output already carries, and the engine waits for that one. The app's reset requests are recorded from its IME output; their completions and retirements stay in egui's input for the app, and a completion of the request the fence waits for ends the fence. Late events of the interrupted composition stay fenced, and an editor that asks for no reset is fenced by the engine's own request as before. --- .../ui-contract/src/runtime/egui/fixture.rs | 44 ++++++++++++++++++ crates/ui-contract/src/runtime/egui/ime.rs | 16 ++++--- crates/ui-contract/src/runtime/ime.rs | 46 +++++++++++++++++-- .../src/testing/host_key_trials.rs | 36 +++++++++++++++ 4 files changed, 131 insertions(+), 11 deletions(-) diff --git a/crates/ui-contract/src/runtime/egui/fixture.rs b/crates/ui-contract/src/runtime/egui/fixture.rs index 0a6f26448f..e1064759df 100644 --- a/crates/ui-contract/src/runtime/egui/fixture.rs +++ b/crates/ui-contract/src/runtime/egui/fixture.rs @@ -520,6 +520,50 @@ pub(crate) fn host_ime_editor(ui: &Ui, id: egui::Id, rect: Rect, text: &mut Stri ui.ctx().output_mut(|output| output.ime = Some(egui::output::IMEOutput::new(id, 1, egui::IMEPurpose::Normal, rect, rect))); } +/// What [`host_resetting_ime_editor`] holds between passes. +#[cfg(feature = "suite")] +#[derive(Default)] +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. + pub(crate) reset: Option, +} + +/// 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`]. +#[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()); + for event in ui.input(|input| input.events.clone()) { + match event { + Event::ImeResetCompleted { request_id } if editor.reset == Some(request_id) => editor.reset = None, + _ if editor.reset.is_some() => {} + Event::Ime(egui::ImeEvent::Preedit { text, .. }) => editor.composing = !text.is_empty(), + Event::Ime(egui::ImeEvent::Commit(committed)) => { + editor.text.push_str(&committed); + editor.composing = false; + } + Event::Ime(egui::ImeEvent::DeleteSurrounding { before_chars, .. }) => { + for _ in 0..before_chars { + editor.text.pop(); + } + } + _ => {} + } + } + let mut ime = egui::output::IMEOutput::new(id, 1, egui::IMEPurpose::Normal, rect, rect); + if editor.composing && ui.input(|input| input.pointer.primary_pressed()) { + editor.composing = false; + editor.reset = ui.ctx().request_ime_reset(ui.ctx().viewport_id()); + ime.should_interrupt_composition = true; + ime.reset_request = editor.reset; + } + ui.ctx().output_mut(|output| output.ime = Some(ime)); +} + /// Gives the host's own egui widget `id` key focus, as the host's code does. #[cfg(feature = "suite")] pub(crate) fn host_focus(ctx: &Context, id: egui::Id) { diff --git a/crates/ui-contract/src/runtime/egui/ime.rs b/crates/ui-contract/src/runtime/egui/ime.rs index 32ee328e8c..71fd378600 100644 --- a/crates/ui-contract/src/runtime/egui/ime.rs +++ b/crates/ui-contract/src/runtime/egui/ime.rs @@ -27,14 +27,14 @@ pub(crate) enum Raw { ResetRetired(egui::ImeResetId), } -/// Removes the IME events from `events`, and the other events `drop` names, and returns the IME events in order, each -/// with its place: how many of the events left in `events` arrived before it. The key stage numbers the events it -/// reads by the same places (`keys::input_hook`), so an editor gets the two in the order they arrived. -pub(crate) fn take_events(events: &mut Vec, take: bool, drop: impl Fn(&Event) -> bool) -> Vec<(Raw, usize)> { +/// Removes the IME events `take` names from `events`, and the other events `drop` names, and returns the IME events +/// in order, each with its place: how many of the events left in `events` arrived before it. The key stage numbers the +/// events it reads by the same places (`keys::input_hook`), so an editor gets the two in the order they arrived. +pub(crate) fn take_events(events: &mut Vec, take: impl Fn(&Event) -> bool, drop: impl Fn(&Event) -> bool) -> Vec<(Raw, usize)> { let mut taken = Vec::new(); let mut kept = 0; events.retain(|Arrived { event, .. }| { - if let Some(raw) = take.then(|| raw_of(event)).flatten() { + if let Some(raw) = take(event).then(|| raw_of(event)).flatten() { taken.extend(raw.map(|raw| (raw, kept))); return false; } @@ -110,9 +110,11 @@ pub(crate) fn write(ctx: &Context, viewport: ViewportId, out: Output) -> Option< } /// Interrupts the composition of the IME output the app's own editor wrote this pass, in its context, and asks for a -/// reset. Returns the reset request it published, as `write` does. +/// 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. pub(crate) fn interrupt_written(ctx: &Context, viewport: ViewportId) -> Option { - let reset = ctx.request_ime_reset(viewport); + 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)); 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 dbb537f3eb..20c6509eed 100644 --- a/crates/ui-contract/src/runtime/ime.rs +++ b/crates/ui-contract/src/runtime/ime.rs @@ -53,6 +53,10 @@ 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. + host_resets: Vec, } /// An app's own editor holding the IME: the owner and context generation of the IME output it wrote, and whether it @@ -197,6 +201,20 @@ 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. + 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) { + self.reset = None; + if matches!(event, Event::ImeResetCompleted { .. }) { + self.stale = false; + } + } + } + /// Follows the composition of the app's own editor through the IME events left to it in egui's input, and /// interrupts it at a pointer press, as a press ends a framework editor's composition. The one-press rule (H0) /// holds the events after the press for the next frame, where the fence drops the composition's late deletions and @@ -230,23 +248,37 @@ fn current(ctx: &Context, f: impl FnOnce(&mut Engine) -> R) -> R { /// 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. With no framework editor, the events after the reset's completion are -/// the app's own editor's again, and the hook follows that editor's composition (`Engine::watch_host`). +/// the app's own editor's again, and the hook follows that editor's composition (`Engine::watch_host`). The answers to +/// the app's own reset requests always stay in egui's input for it; when the fence waits for one of them, its +/// completion ends the fence too. pub(crate) fn input_hook(ctx: &Context, events: &mut Vec, viewport: ViewportId) { with(ctx, viewport, |engine| { let take = engine.owner.is_some() || engine.reset.is_some(); + let host_resets = engine.host_resets.clone(); + let host_answer = |event: &Event| match event { + Event::ImeResetCompleted { request_id } | Event::ImeResetRetired { request_id } => host_resets.contains(request_id), + _ => false, + }; let completed = |id: egui::ImeResetId| move |arrived: &Arrived| matches!(arrived.event, Event::ImeResetCompleted { request_id } if request_id == id); + // The events after the completion are the app's; the completion too when it answers the app's own request. let mut after = match engine.reset { - Some(id) if engine.owner.is_none() => events.iter().position(completed(id)).map(|at| events.split_off(at + 1)), + Some(id) if engine.owner.is_none() => { + let own = host_resets.contains(&id); + events.iter().position(completed(id)).map(|at| events.split_off(if own { at } else { at + 1 })) + } _ => None, }; let guarded = engine.composing || engine.guard > 0; let ends_composition = |event: &Event| { matches!(event, Event::Key { key: Key::Enter | Key::Escape | Key::Backspace | Key::Delete | Key::Space, pressed: true, .. }) }; - let taken = os::take_events(events, take, |event| guarded && ends_composition(event)); + let taken = os::take_events(events, |event| take && !host_answer(event), |event| guarded && ends_composition(event)); engine.queue.extend(taken); engine.guard = engine.guard.saturating_sub(1); + for arrived in events.iter().chain(after.iter().flatten()) { + engine.host_answered(&arrived.event); + } if engine.owner.is_none() { match &mut after { Some(after) => engine.watch_host(after), @@ -458,7 +490,8 @@ 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_wrote = ctx.output(|output| output.ime.as_ref().map(|ime| (ime.owner, ime.context_generation))); + 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 (owner, interrupt, caret, update, published, host) = with(ctx, viewport, |engine| { let ran = std::mem::take(&mut engine.ran); if !ran { @@ -477,6 +510,11 @@ 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) + && !engine.host_resets.contains(&id) + { + engine.host_resets.push(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. if engine.stale diff --git a/crates/ui-contract/src/testing/host_key_trials.rs b/crates/ui-contract/src/testing/host_key_trials.rs index 71f62c579b..804d9b08a4 100644 --- a/crates/ui-contract/src/testing/host_key_trials.rs +++ b/crates/ui-contract/src/testing/host_key_trials.rs @@ -215,6 +215,42 @@ 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())); + 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()); + let mut frames = Frames::new(); + 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![ + 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); + 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"); +} + #[trial] fn a_focused_egui_field_keeps_tab_escape_and_the_clipboard() { let (host, mut show) = host_window();