From 508faa1eb78d53caedd910b4e38a00588b2930f6 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 17:28:04 +0700 Subject: [PATCH] ui-contract: an Alt press an input method types through is typing: the focused text stop that has the IME holds it, so its bound command does not run beside the commit --- crates/ui-contract/src/runtime/frame.rs | 1 + crates/ui-contract/src/runtime/keys.rs | 6 +++- crates/ui-contract/src/runtime/keys/input.rs | 28 +++++++++++++++++-- crates/ui-contract/src/runtime/keys/route.rs | 7 ++++- .../tests/it/runtime/typed_keys.rs | 15 ++++++++++ 5 files changed, 53 insertions(+), 4 deletions(-) diff --git a/crates/ui-contract/src/runtime/frame.rs b/crates/ui-contract/src/runtime/frame.rs index 7771ea1f0d..7c549734a5 100644 --- a/crates/ui-contract/src/runtime/frame.rs +++ b/crates/ui-contract/src/runtime/frame.rs @@ -664,6 +664,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput) { // H3: the key stage strips what the runtime owns; egui decides on a repaint from the events left. The events sent // to the focus are counted first, before the IME and key stages take theirs. keys::count_sent(ctx, viewport, &events); + keys::note_ime_presses(ctx, viewport, &events); ime::input_hook(ctx, &mut events, viewport); let (stripped, later) = keys::input_hook(ctx, input, &mut events); if !later.is_empty() { diff --git a/crates/ui-contract/src/runtime/keys.rs b/crates/ui-contract/src/runtime/keys.rs index 836e585449..2b48d6ab64 100644 --- a/crates/ui-contract/src/runtime/keys.rs +++ b/crates/ui-contract/src/runtime/keys.rs @@ -33,7 +33,7 @@ pub use input::held; #[doc(hidden)] pub use input::keyed; pub use input::textless_paste; -pub(crate) use input::{claims_press, count_sent, input_hook, keys_state, queue_nav, sent, stirred, take_menu_keys, take_nav}; +pub(crate) use input::{claims_press, count_sent, input_hook, keys_state, note_ime_presses, queue_nav, sent, stirred, take_menu_keys, take_nav}; pub use intents::Intents; #[doc(hidden)] pub use intents::intents_of; @@ -89,6 +89,8 @@ struct Press { place: usize, /// The keys down when it went down, itself not among them: what a nudge reads as held with its arrow. held: Vec, + /// An input method committed text right after it: the press typed that text, which a text stop takes. + ime: bool, } /// An arrow press as a nudge of content: its unit direction, its modifiers and the keys down when it went down. @@ -294,6 +296,8 @@ pub(crate) struct Stage { textless_paste: bool, /// How many inputs of the viewport held an event sent to the focus (`count_sent`). sent: u64, + /// The key presses of this input that an input method's commit follows (`note_ime_presses`). + ime_presses: Vec, /// The keys whose press the strip took and whose release it is still to drop. consumed: Vec, shortcuts: Shortcuts, diff --git a/crates/ui-contract/src/runtime/keys/input.rs b/crates/ui-contract/src/runtime/keys/input.rs index af09285086..4d648e1777 100644 --- a/crates/ui-contract/src/runtime/keys/input.rs +++ b/crates/ui-contract/src/runtime/keys/input.rs @@ -68,6 +68,8 @@ struct Strip<'a> { struct Arrival { /// The event's arrival time (`Arrived::at`). at: Option, + /// An input method's commit follows the event: a press it follows typed through the input method. + ime_next: bool, /// A key press follows the event: a text before it is that press's (the web sends a press's text first). press_next: bool, } @@ -134,7 +136,8 @@ impl Strip<'_> { let pushed = batch.presses.len(); let claims = batch.claimed.len(); let menu_keys = batch.menu.len(); - let shortcut_text = self.shortcut_text(&input); + // Text an input method commits for the press is typing, whatever the press's chord binds. + let shortcut_text = self.shortcut_text(&input) && !arrival.ime_next; let kept = self.press(batch, key, &input, repeat, at); // The canvas's Space keeps its text from every field, as a claim does. let canvas = batch.presses.get(pushed).is_some_and(|press| press.canvas); @@ -147,6 +150,7 @@ impl Strip<'_> { // The release key is kept for withheld presses too: the held-over rule matches repeats by it. if let Some(press) = batch.presses.get_mut(pushed) { press.release = input.physical.or(input.typed); + press.ime = arrival.ime_next; press.held = down.iter().copied().filter(|key| Some(*key) != press.release).collect(); if kept { press.in_egui = Some(batch.egui_presses); @@ -396,6 +400,7 @@ impl Strip<'_> { canvas: true, place: batch.place, held: Vec::new(), + ime: false, }); if !repeat { batch.shortcuts.push(Shortcut { @@ -432,6 +437,7 @@ impl Strip<'_> { canvas: false, place: batch.place, held: Vec::new(), + ime: false, }); false } @@ -462,6 +468,7 @@ impl Strip<'_> { canvas: false, place: batch.place, held: Vec::new(), + ime: false, }); if !repeat { batch.shortcuts.push(Shortcut { @@ -658,6 +665,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec = (0..events.len()) .map(|at| matches!(events.get(at + 1).map(|arrived| &arrived.event), Some(egui::Event::Key { pressed: true, .. }))) .collect(); @@ -682,7 +690,11 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec impl Fn(&egui::Ev } } +/// Notes the key presses of `events` that an input method's commit follows, before the IME stage takes the commits out +/// of the input: the key stage reads such a press as typing through the input method (`Press::ime`). +pub(crate) fn note_ime_presses(ctx: &Context, viewport: ViewportId, events: &[Arrived]) { + let commit = |event: &egui::Event| matches!(event, egui::Event::Ime(egui::ImeEvent::Commit(text)) if !text.is_empty()); + let presses = events + .windows(2) + .filter(|pair| matches!(pair[0].event, egui::Event::Key { pressed: true, .. }) && commit(&pair[1].event)) + .map(|pair| pair[0].event.clone()) + .collect(); + with(ctx, viewport, |stage| stage.ime_presses = presses); +} + /// Updates the keys held after `event`: a press adds its key, a release removes it, and the window losing focus /// (which also reads as the right Alt coming up) forgets them all, since their releases go to another window. fn note_down(down: &mut Vec, event: &KeyEvent) { diff --git a/crates/ui-contract/src/runtime/keys/route.rs b/crates/ui-contract/src/runtime/keys/route.rs index 6fa1b36212..b182cf499d 100644 --- a/crates/ui-contract/src/runtime/keys/route.rs +++ b/crates/ui-contract/src/runtime/keys/route.rs @@ -257,7 +257,11 @@ fn owned_shortcuts(ctx: &Context, target: Option<&StopRec>, batch: &Batch, consu .iter() .zip(consumed) // The canvas's Space is never the focused stop's. - .map(|(press, taken)| !press.canvas && holds_press(owner, target, press, *taken == Taken::Yes, press.text.is_some_and(|at| taken_texts[at]))) + .map(|(press, taken)| { + // Text an input method committed for the press goes to the text stop that has the IME. + let typed = press.text.is_some_and(|at| taken_texts[at]) || (press.ime && target.is_some_and(|rec| rec.stop.text)); + !press.canvas && holds_press(owner, target, press, *taken == Taken::Yes, typed) + }) .collect(); // A shortcut without a press of its own (a release, or the clipboard event that stands for ⌘C) follows the owner by // its modifiers, as its press would. @@ -374,6 +378,7 @@ pub fn focus_holds(ctx: &Context, chord: Chord) -> bool { canvas: false, place: 0, held: Vec::new(), + ime: false, }; let driven = Driven::of(ctx, &rec); let route = press_route(ctx, crate::runtime::windows::os_of(ctx), &rec, &press, driven.or_else(|| list_of_field(ctx, &rec))); diff --git a/crates/ui-contract/tests/it/runtime/typed_keys.rs b/crates/ui-contract/tests/it/runtime/typed_keys.rs index ac095091e7..6ce609aba2 100644 --- a/crates/ui-contract/tests/it/runtime/typed_keys.rs +++ b/crates/ui-contract/tests/it/runtime/typed_keys.rs @@ -100,6 +100,21 @@ fn a_second_press_of_a_held_key_runs_no_shortcut_again() { assert!(ran(&mut frames, &mut show, &field, vec![key_press(Key::S, Modifiers::NONE)]), "a press after the release runs it again"); } +/// In a focused field, a bound Alt chord that an input method types through (its commit follows the press) is typing: +/// the field takes the commit and the command does not run. With its key's text the press runs the command. Guards +/// against a key stage that ran the command and let the input method's text through as well. +#[trial] +fn an_alt_chord_an_input_method_types_through_runs_no_command() { + let field = RefCell::new(Field::default()); + let mut frames = frames_for(Chord::new(Mods::ALT, ChordKey::G)); + let mut show = window(&field); + settle(&mut frames, &mut show, true); + let alt_g = + |then: Event| vec![Event::ModifiersChanged(Modifiers::ALT), key_press(Key::G, Modifiers::ALT), then, key_release(Key::G, Modifiers::ALT)]; + assert!(ran(&mut frames, &mut show, &field, alt_g(text("g"))), "with its key's text the press runs the command"); + assert!(!ran(&mut frames, &mut show, &field, alt_g(Event::Ime(ImeEvent::Commit("ğ".into())))), "an input method's commit is typing"); +} + /// A Cut with Cmd and Shift held is Cmd+Shift+X, the app's chord, not the focused field's Cut; a plain Cmd+X Cut stays /// the field's. Guards against a key stage that gave every clipboard event to a field that lists the edit, whatever /// the modifiers.