From 9f649dc80d85129459f35fcfc83788238c0ca5e3 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 29 Nov 2026 14:10:29 -0800 Subject: [PATCH] ui-contract: a lost carrier release and a focus loss end an Alt code's state A key press clears the carrier, so a carrier whose release never came does not take the release of a key pressed after it. A focus loss clears the echo as well as the carrier, so a key with no physical key pressed after focus returns is not taken for the platform's carrier. --- crates/ui-contract/src/runtime/egui/alt_code.rs | 8 +++++++- crates/ui-contract/tests/it/runtime/alt_code.rs | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/ui-contract/src/runtime/egui/alt_code.rs b/crates/ui-contract/src/runtime/egui/alt_code.rs index 985d77e5a9..3b8b1b5d4c 100644 --- a/crates/ui-contract/src/runtime/egui/alt_code.rs +++ b/crates/ui-contract/src/runtime/egui/alt_code.rs @@ -29,7 +29,7 @@ pub(crate) struct AltCodeEntry { carrier: Option, } -/// The character an Alt code typed, kept until the next key or pointer press. +/// The character an Alt code typed, kept until the next key or pointer press or a focus loss. struct Echo { typed: String, /// The code names a byte of the code pages (1–255): the platform's own character for it wins over ours, since it @@ -88,6 +88,11 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec, pages: Alt Event::Key { key, pressed: true, repeat, modifiers, .. } => { entry.swallow_text = false; entry.echo = None; + // A carrier whose release never came (the platform dropped it with the held physical key's) does not + // take the release of a key pressed after it. + if !*repeat { + entry.carrier = None; + } match digit(*key).filter(|_| alt_only(modifiers) && !claimed(&event)) { Some(d) => { // A held digit's auto-repeat adds nothing, as on Windows. @@ -139,6 +144,7 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec, pages: Alt Event::WindowFocused(false) => { entry.code.clear(); entry.swallow_text = false; + entry.echo = None; entry.carrier = None; } _ => {} diff --git a/crates/ui-contract/tests/it/runtime/alt_code.rs b/crates/ui-contract/tests/it/runtime/alt_code.rs index 2c7d94eb7d..2c4b4a5a52 100644 --- a/crates/ui-contract/tests/it/runtime/alt_code.rs +++ b/crates/ui-contract/tests/it/runtime/alt_code.rs @@ -341,6 +341,21 @@ fn a_composed_character_on_a_key_press_of_its_own_is_typed_once() { assert_eq!(of(keys, Key::A), [], "the carrier of the byte a code above 255 reduces to does not reach egui"); let (_, keys) = send(&mut frames, &mut show, unmapped()); assert_eq!(of(keys, Key::Backslash), [(Key::Backslash, true), (Key::Backslash, false)], "another key with no physical key reaches egui"); + // The carrier's release never comes (the platform drops it with a held backslash key's), then the user presses + // that key with no physical key in egui: its release reaches egui with its press. + let mut lost_release = code("0092"); + lost_release.push(composed_on_key(Key::Backslash, "\\")[..2].to_vec()); + lost_release.push(composed_on_key(Key::Backslash, "\\")); + let (_, keys) = send(&mut frames, &mut show, lost_release); + assert_eq!(of(keys, Key::Backslash), [(Key::Backslash, true), (Key::Backslash, false)], "a carrier with no release keeps no later release"); + // A code the platform sends nothing after, then focus leaves the window and comes back: a key with no physical key + // in egui that types the code's character is the user's. + let mut refocused = code("0092"); + refocused.push(vec![Event::WindowFocused(false)]); + refocused.push(vec![Event::WindowFocused(true)]); + refocused.push(composed_on_key(Key::Backslash, "\\")); + let (_, keys) = send(&mut frames, &mut show, refocused); + assert_eq!(of(keys, Key::Backslash), [(Key::Backslash, true), (Key::Backslash, false)], "a focus loss ends the code's echo"); click(&mut frames, AT.center(), &mut show); frames.idle(1, &mut show);