IME: a press during an app editor's composition interrupts and fences it

The engine fenced only framework editors. An app's own editor (an egui
reader that writes the pass's IME output) saw the late deletion and
commit of a composition a click ended arrive in the next frame (the
one-press rule holds them), with no composition active, so they edited
the text at the new caret. The engine now records the editor from the
IME output it wrote, follows its composition through the IME events
left to it, and on a pointer press during it interrupts in the
editor's own context and drops the IME events until the reset
completes; events after the completion reach the editor.
This commit is contained in:
aiongg
2026-11-24 20:41:30 +00:00
parent f0b97bdc73
commit d2281d2eeb
4 changed files with 146 additions and 8 deletions
@@ -500,6 +500,26 @@ pub(crate) fn host_field(ui: &mut Ui, id: egui::Id, text: &mut String) -> Rect {
ui.add(egui::TextEdit::singleline(text).id(id)).rect
}
/// A host's own text editor of `id` at `rect`, as a canvas text tool draws it: it reads the IME events from egui's
/// input into `text` (a commit appends, a deletion before the caret removes characters from its end) and writes the
/// pass's IME output, context generation 1.
#[cfg(feature = "suite")]
pub(crate) fn host_ime_editor(ui: &Ui, id: egui::Id, rect: Rect, text: &mut String) {
let _ = ui.interact(rect, id, egui::Sense::click());
for event in ui.input(|input| input.events.clone()) {
match event {
Event::Ime(egui::ImeEvent::Commit(committed)) => text.push_str(&committed),
Event::Ime(egui::ImeEvent::DeleteSurrounding { before_chars, .. }) => {
for _ in 0..before_chars {
text.pop();
}
}
_ => {}
}
}
ui.ctx().output_mut(|output| output.ime = Some(egui::output::IMEOutput::new(id, 1, egui::IMEPurpose::Normal, rect, rect)));
}
/// 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) {
@@ -45,6 +45,12 @@ pub(crate) fn take_events(events: &mut Vec<Arrived>, take: bool, drop: impl Fn(&
taken
}
/// The IME event `event` is, as the engine reads it, for the engine to follow an app's own editor through the events
/// it leaves to egui.
pub(crate) fn read(event: &Event) -> Option<Raw> {
raw_of(event).flatten()
}
/// What the IME engine reads from `event`: `None` for an event that is not the engine's, `Some(None)` for one it takes
/// and ignores.
fn raw_of(event: &Event) -> Option<Option<Raw>> {
@@ -103,6 +109,19 @@ pub(crate) fn write(ctx: &Context, viewport: ViewportId, out: Output) -> Option<
reset
}
/// 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.
pub(crate) fn interrupt_written(ctx: &Context, viewport: ViewportId) -> Option<egui::ImeResetId> {
let reset = ctx.request_ime_reset(viewport);
ctx.output_mut(|output| {
if let Some(ime) = &mut output.ime {
ime.should_interrupt_composition = true;
ime.reset_request = reset;
}
});
reset
}
/// 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);
+67 -8
View File
@@ -51,6 +51,17 @@ struct Engine {
discard: bool,
/// What the editor that owns the IME reported at its last pass.
editor: Option<EditorFacts>,
/// The app's own editor that wrote the IME output of the last pass while no framework editor owned the IME.
host: Option<HostIme>,
}
/// An app's own editor holding the IME: the owner and context generation of the IME output it wrote, and whether it
/// is composing, as the IME events left to it in egui's input say.
#[derive(Clone, Copy)]
struct HostIme {
owner: Id,
generation: u64,
composing: bool,
}
impl Stored for Engine {}
@@ -185,6 +196,27 @@ impl Engine {
self.interrupt = Some(owner);
self.discard = true;
}
/// 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
/// commits until the reset completes.
fn watch_host(&mut self, events: &[Arrived]) {
let Some(host) = self.host.as_mut() else { return };
for arrived in events {
match (os::read(&arrived.event), &arrived.event) {
(Some(os::Raw::Preedit { text, .. }), _) => host.composing = !text.is_empty(),
(Some(os::Raw::Commit(_)), _) => host.composing = false,
(None, Event::PointerButton { pressed: true, .. }) if host.composing => {
host.composing = false;
let owner = host.owner;
self.interrupt(owner);
return;
}
_ => {}
}
}
}
}
fn with<R>(ctx: &Context, viewport: ViewportId, f: impl FnOnce(&mut Engine) -> R) -> R {
@@ -197,10 +229,17 @@ fn current<R>(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.
/// 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`).
pub(crate) fn input_hook(ctx: &Context, events: &mut Vec<Arrived>, viewport: ViewportId) {
with(ctx, viewport, |engine| {
let take = engine.owner.is_some() || engine.reset.is_some();
let completed =
|id: egui::ImeResetId| move |arrived: &Arrived| matches!(arrived.event, Event::ImeResetCompleted { request_id } if request_id == id);
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)),
_ => 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, .. })
@@ -208,6 +247,14 @@ pub(crate) fn input_hook(ctx: &Context, events: &mut Vec<Arrived>, viewport: Vie
let taken = os::take_events(events, take, |event| guarded && ends_composition(event));
engine.queue.extend(taken);
engine.guard = engine.guard.saturating_sub(1);
if engine.owner.is_none() {
match &mut after {
Some(after) => engine.watch_host(after),
None if !take => engine.watch_host(events),
None => {}
}
}
events.extend(after.into_iter().flatten());
});
}
@@ -405,11 +452,14 @@ pub(crate) fn note_caret(ctx: &Context, owner: Id, field: Rect, caret: Rect, pas
}
/// The end hook: observes the owner on the tracker, then 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.
/// IME or an interrupt is due. Otherwise the output is egui's: an app's own `TextEdit` or IME reader writes it, and the
/// engine records that editor (`Engine::host`). An interrupt of the app's editor rides on the output it wrote this
/// pass, so its context does not change.
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| {
let app_wrote = ctx.output(|output| output.ime.as_ref().map(|ime| (ime.owner, ime.context_generation)));
let (owner, interrupt, caret, update, published, host) = with(ctx, viewport, |engine| {
let ran = std::mem::take(&mut engine.ran);
if !ran {
engine.edits.clear();
@@ -423,20 +473,28 @@ pub(crate) fn end_pass(ctx: &Context) {
{
engine.set_owner(None);
}
// The fence has no request the integration can answer: the owner's output asks again.
// The app's editor as the last pass recorded it: an interrupt in a pass it wrote nothing names its 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 });
// 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
&& engine.reset.is_none()
&& let Some(owner) = engine.owner
&& let Some(owner) = engine.owner.or(engine.host.map(|host| host.owner))
{
engine.interrupt.get_or_insert(owner);
}
let caret = engine.caret.take();
let password = caret.filter(|(id, ..)| Some(*id) == engine.owner).map(|(.., password)| password);
let update = engine.observe(viewport, frame_nr, pass_nr, password);
(engine.owner, engine.interrupt.take(), caret, update, engine.published)
(engine.owner, engine.interrupt.take(), caret, update, engine.published, last_host)
});
let host_interrupt = interrupt.filter(|old| owner.is_none() && app_wrote.is_some_and(|(id, _)| id == *old));
let written = if owner.is_none() && interrupt.is_none() {
None
} else if host_interrupt.is_some() {
Some((os::interrupt_written(ctx, viewport), None))
} else {
os::clear(ctx);
match (caret, interrupt, update) {
@@ -455,14 +513,15 @@ pub(crate) fn end_pass(ctx: &Context) {
Some((os::write(ctx, viewport, out), Some(generation)))
}
// The old owner still gets an output in the pass it lost focus, or the OS never interrupts. It names the
// context the OS composes in, the last one published.
// context the OS composes in: the last one published, or the one the app's editor wrote last.
(_, Some(old), _) => {
let generation = host.filter(|host| host.owner == old).map_or(published.unwrap_or_default(), |host| host.generation);
let out = os::Output {
owner: old,
field: Rect::NOTHING,
caret: Rect::NOTHING,
interrupt: true,
generation: published.unwrap_or_default(),
generation,
password: false,
surrounding: IMESurroundingText::Unavailable,
cause: ImeChangeCause::Other,
@@ -172,6 +172,46 @@ fn a_focused_egui_field_takes_the_ime() {
assert_eq!(host.borrow().text, "世", "the commit reaches the field");
}
fn host_editor_id() -> Id {
named_id("host editor")
}
/// A window with the app's own IME editor (`fixture::host_ime_editor`), and the text it holds.
fn host_editor() -> (Rc<RefCell<String>>, impl FnMut(&mut Ui)) {
let text = Rc::new(RefCell::new(String::new()));
let held = Rc::clone(&text);
let rect = Rect::from_min_size(Pos2::new(100.0, 100.0), egui::vec2(200.0, 30.0));
(text, move |ui: &mut Ui| fixture::host_ime_editor(ui, host_editor_id(), rect, &mut held.borrow_mut()))
}
/// A press during the app's own editor's composition interrupts it, in the editor's own IME context, and fences the
/// composition's late deletion and commit, which the one-press rule holds for the next frame, until the reset
/// completes; the events after the completion reach the editor.
#[trial]
fn a_press_during_an_app_editor_s_composition_fences_its_late_ime_events() {
let (editor, mut show) = host_editor();
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 ime = frames.ime().expect("the editor writes the pass's IME output");
assert!(ime.owner == host_editor_id() && ime.interrupt, "the press interrupts the editor's composition in its own context");
let reset = ime.reset.expect("the interrupt asks for a reset");
frames.frame(vec![release(at)], &mut show);
frames.frame(Vec::new(), &mut show);
assert_eq!(*editor.borrow(), "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(), "abX", "the commit after the completion reaches the editor");
}
#[trial]
fn a_focused_egui_field_keeps_tab_escape_and_the_clipboard() {
let (host, mut show) = host_window();