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.
This commit is contained in:
dotCooCoo committed 2026-11-29 14:10:29 -08:00
1 parent 744812cbf9
commit 9f649dc80d
2 files changed
+22 -1

No files matched your search

@@ -29,7 +29,7 @@ pub(crate) struct AltCodeEntry {
carrier: Option<Key>,
}
/// 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<Event>, 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<Event>, pages: Alt
Event::WindowFocused(false) => {
entry.code.clear();
entry.swallow_text = false;
entry.echo = None;
entry.carrier = None;
}
_ => {}
@@ -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);