From e1d833c5ff85063b5a82ddc7b7055ecee966bea3 Mon Sep 17 00:00:00 2001 From: Martin Ball Date: Thu, 19 Nov 2026 22:13:41 +0000 Subject: [PATCH] fwstub-2: an editor that refuses an input method's edit asks for the interrupt itself (EditorHandle/Cx/ToolPass::ime_interrupt): a batch of a refused deletion and commits ends with nothing composing, so the engine could not see it, and the app fell back to an application change that made the fresh commit Other; the interrupt now goes out beside the pass's InputMethod receipt; trial Co-Authored-By: Claude Opus 5.5 --- crates/ui-canvas/src/canvas/mod.rs | 9 ++++ crates/ui-contract/src/contract/editor.rs | 15 +++++++ crates/ui-contract/src/runtime/ime.rs | 21 +++++++++ crates/ui-contract/tests/it/text/editor.rs | 50 +++++++++++++++++++++- 4 files changed, 94 insertions(+), 1 deletion(-) diff --git a/crates/ui-canvas/src/canvas/mod.rs b/crates/ui-canvas/src/canvas/mod.rs index c29b4e61c..db6195cde 100644 --- a/crates/ui-canvas/src/canvas/mod.rs +++ b/crates/ui-canvas/src/canvas/mod.rs @@ -764,6 +764,15 @@ impl ToolPass { } } + /// Asks for the OS composition to be interrupted this pass because the body refused an input method's edit, as + /// [`EditorHandle::ime_interrupt`]: a commit the body applied after the refusal keeps its `InputMethod` cause. A + /// canvas without an editor part asks nothing. + pub fn ime_interrupt(&self, ctx: &Context) { + if let Some(editor) = &self.editor { + editor.ime_interrupt(ctx); + } + } + /// `view` as the canvas keeps it: inside the document's edges with Overscroll off. The tool host passes a tool's /// view through it. pub(crate) fn clamped(&self, view: CanvasView) -> CanvasView { diff --git a/crates/ui-contract/src/contract/editor.rs b/crates/ui-contract/src/contract/editor.rs index cb22d27c0..d5c1a4127 100644 --- a/crates/ui-contract/src/contract/editor.rs +++ b/crates/ui-contract/src/contract/editor.rs @@ -131,6 +131,14 @@ impl EditorHandle { ime::note_application_change(ctx, self.widget); } + /// Asks for the OS composition to be interrupted this pass, because this editor part refused an input method's + /// edit and the input method's view of the text is stale. The pass's change cause stays what its receipts prove: + /// a commit applied after the refusal is still the input method's. A composition still running is ended and its + /// late events are dropped until the reset completes; with none running, later input is not held back. + pub fn ime_interrupt(&self, ctx: &egui::Context) { + ime::request_interrupt(ctx, self.widget); + } + /// The one editor report path: notes the caret, the composition, the undo state and the surrounding text with the /// IME engine, and returns the text facts when AccessKit reads the part. /// @@ -187,6 +195,13 @@ impl Cx<'_, C> { } } + /// Asks for the OS composition to be interrupted this pass, as [`EditorHandle::ime_interrupt`]. + pub fn ime_interrupt(&self) { + if let Some(handle) = self.editor_handle() { + handle.ime_interrupt(&self.core.ctx); + } + } + /// The editor part's inputs of this pass in the order they arrived; call it in `update`. The editor applies them /// to its own document. pub fn editor_input(&mut self) -> impl Iterator + '_ { diff --git a/crates/ui-contract/src/runtime/ime.rs b/crates/ui-contract/src/runtime/ime.rs index 93657fdf9..90106a8a9 100644 --- a/crates/ui-contract/src/runtime/ime.rs +++ b/crates/ui-contract/src/runtime/ime.rs @@ -273,6 +273,27 @@ pub(crate) fn note_editor(ctx: &Context, owner: Id, composing: Composing, histor }); } +/// The editor `owner` asks for the OS composition to be interrupted this pass: it refused an input method's edit, so +/// the input method's view of the text is stale. The engine folds a frame's events before the editor runs, so a refused +/// deletion followed by commits in one batch ends with nothing composing, and only the editor knows it refused one. +/// +/// The end hook writes the interrupt beside this pass's change cause, so a commit the editor applied after the refusal +/// keeps its receipt (`InputMethod`). A composition still running is ended and fenced as when the editor stops showing +/// it (`note_editor`); with none running there are no late events to drop, so the editor's later input is not fenced. +/// Nothing happens for an editor that does not own the IME. +pub(crate) fn request_interrupt(ctx: &Context, owner: Id) { + current(ctx, |engine| { + if engine.owner != Some(owner) { + return; + } + if engine.composing { + engine.interrupt(owner); + } else { + engine.interrupt = Some(owner); + } + }); +} + /// Whether the editor `owner` had something to undo and to redo at its last report as the IME owner. pub(crate) fn editor_history(ctx: &Context, owner: Id) -> (bool, bool) { current(ctx, |engine| { diff --git a/crates/ui-contract/tests/it/text/editor.rs b/crates/ui-contract/tests/it/text/editor.rs index ea7bbe1c6..c7c240105 100644 --- a/crates/ui-contract/tests/it/text/editor.rs +++ b/crates/ui-contract/tests/it/text/editor.rs @@ -1,7 +1,7 @@ //! The editor contract: a component with an editor part receives one ordered input stream, a read-only editor //! receives only inputs that change no text, and its visible lines become AccessKit run nodes. -use craft_behavior::{EditorInput, Granularity, PartKey, Sense, TextPos}; +use craft_behavior::{EditorInput, Granularity, PartKey, Sense, SurroundingUnit, TextPos}; use craft_ui::contract::{Common, Component, Cx, EditorHandle, PaintUi, PartsCx, Props as _, Role, Show as _}; use craft_ui::primitives::{EditorLine, EditorPurpose, EditorRun, EditorState, EditorSurrounding, ImeStamp}; use craft_ui::runtime::focus::KeyUse; @@ -143,6 +143,9 @@ struct Doc { /// The probe refuses an input method's deletion after the caret, as a word processor refuses one into a locked /// control: the refusal ends the marked text it shows. refuses_after: Cell, + /// The probe refuses an input method's byte-counted deletion, as a word processor refuses one inside an equation: + /// it drops the commit right after it (the refused deletion's replacement) and asks for an interrupt. + refuses_bytes: Cell, } impl Doc { @@ -154,8 +157,16 @@ impl Doc { /// is the application's. fn apply(&self, cx: &Cx<'_, C>, inputs: &[EditorInput]) { let mut by_ime = false; + let mut drop_replacement = false; for input in inputs { match input { + EditorInput::DeleteSurrounding { unit: SurroundingUnit::Bytes, .. } if self.refuses_bytes.get() => { + drop_replacement = true; + self.composing.set(false); + cx.ime_interrupt(); + } + EditorInput::ImeCommit if drop_replacement => {} + EditorInput::Text(_) if std::mem::take(&mut drop_replacement) => {} EditorInput::Preedit { text, .. } => self.composing.set(!text.is_empty()), EditorInput::DeleteSurrounding { after, .. } if *after > 0 && self.refuses_after.get() => self.composing.set(false), EditorInput::ImeCommit => { @@ -505,6 +516,43 @@ fn an_editors_surrounding_text_and_receipts_reach_the_ime_output() { assert_eq!(seen.generation, second, "a pass with no report keeps the target's epoch, so the context stays"); } +fn delete_bytes(before: usize) -> Event { + Event::Ime(ImeEvent::DeleteSurroundingBytes { before_bytes: before, after_bytes: 0 }) +} + +/// An editor that refuses an input method's edit asks for the interrupt itself (`Cx::ime_interrupt`). One raw batch of +/// a refused deletion, its replacement commit and a fresh commit ends with nothing composing, so the engine could not +/// see the refusal: the editor's request interrupts, and the fresh commit it applied stays the input method's change. +/// With no composition running, the next frame's commit is not fenced; with one running, the request ends it and its +/// late events are dropped until the reset completes. +#[trial] +fn an_editor_that_refuses_an_input_method_edit_asks_for_the_interrupt() { + let mut rig = Rig::with(EditorPurpose::Code, Some(Doc::new(TEXT, Some(1))), false); + rig.doc().refuses_bytes.set(true); + rig.focus(); + rig.frame(Vec::new()); + + rig.frame(vec![delete_bytes(1), commit("Z"), commit("W")]); + assert_eq!(*rig.doc().text.borrow(), "fn mainW", "the replacement is dropped and the fresh commit applies"); + let seen = rig.ime(); + assert!(seen.interrupt && seen.reset.is_some(), "the editor's refusal interrupts the input method: {seen:?}"); + assert_eq!(seen.cause, ImeChangeCause::InputMethod, "the fresh commit is still the input method's change"); + + rig.frame(vec![commit("Y")]); + assert_eq!(*rig.doc().text.borrow(), "fn mainWY", "with nothing composing, the next commit is not fenced"); + let seen = rig.ime(); + assert!(!seen.interrupt, "the interrupt is asked once"); + assert_eq!(seen.cause, ImeChangeCause::InputMethod); + + rig.frame(vec![preedit("p")]); + let reset = rig.frame_reset(vec![delete_bytes(1)]).expect("a refusal while composing interrupts and asks for a reset"); + rig.frame(vec![preedit("pq"), commit("pq")]); + assert_eq!(*rig.doc().text.borrow(), "fn mainWY", "the interrupted composition's late events are fenced"); + rig.frame(vec![Event::ImeResetCompleted { request_id: reset }]); + rig.frame(vec![commit("V")]); + assert_eq!(*rig.doc().text.borrow(), "fn mainWYV", "after the reset, input applies again"); +} + /// While the editor composes, and for one guard frame after a commit, Enter, Space, Backspace, Escape and Delete /// never become editor inputs and do not interrupt: the composition goes on. A press in the editor's text does /// interrupt it: the press moves the caret, the editor ends the marked text it showed, and the engine asks for one