From 744812cbf94730c0971c1b5dfb5ac99db037f76c Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 29 Nov 2026 13:31:57 -0800 Subject: [PATCH 1/3] ui-contract: an Alt code types its character once with winit 0dfcec0e The winit patch rev moves from a517b384 to 0dfcec0e, the head of storytold/winit craft/0.30.13. It adds native touchpad pinch and rotation on Wayland (storytold/winit#20), which egui-winit reports as Event::Zoom and Event::Rotate, and the Windows change that reports an Alt + keypad code's character as a key press with that text after the Alt release. That press has no physical key. For a character egui names a key for (backslash, pipe, letters, digits), egui-winit sends a key press before the text, and the Alt-code fold took the press for a key the user pressed: it dropped the echo of the character it had typed, so the character was typed twice, and the press reached egui, where a single-key shortcut could take it. The fold now leaves that press and its release out, so the text that follows meets the echo. Only a press whose key names the code's character, or for a code above 255 the character of the byte Windows reduces it to, counts; another key with no physical key still types. --- Cargo.lock | 2 +- Cargo.toml | 2 +- .../ui-contract/src/runtime/egui/alt_code.rs | 40 ++++++- .../ui-contract/tests/it/runtime/alt_code.rs | 101 ++++++++++++++++++ 4 files changed, 141 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c319ea1631..aab3d2ddd0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14711,7 +14711,7 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winit" version = "0.30.13" -source = "git+https://github.com/storytold/winit.git?rev=a517b3846ee8457d84ee4975110bfda6797c44dc#a517b3846ee8457d84ee4975110bfda6797c44dc" +source = "git+https://github.com/storytold/winit.git?rev=0dfcec0eb62fda5904137a63a6dd0c3d83295f45#0dfcec0eb62fda5904137a63a6dd0c3d83295f45" dependencies = [ "android-activity", "atomic-waker", diff --git a/Cargo.toml b/Cargo.toml index f8bc4506d5..e664d72fdd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -342,7 +342,7 @@ egui_system_font_provider = { git = "https://github.com/storytold/egui.git", rev eframe = { git = "https://github.com/storytold/egui.git", rev = "14c39fed181756be6f357c284354e0ab55b64443" } egui_demo_lib = { git = "https://github.com/storytold/egui.git", rev = "14c39fed181756be6f357c284354e0ab55b64443" } egui_inspection = { git = "https://github.com/storytold/egui.git", rev = "14c39fed181756be6f357c284354e0ab55b64443" } -winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" } +winit = { git = "https://github.com/storytold/winit.git", rev = "0dfcec0eb62fda5904137a63a6dd0c3d83295f45" } # symphonia's FLAC decoder restores LPC samples as `residual + (prediction as i32)`. In a valid # 32-bit stream the prediction can leave the i32 range while the sum does not, so a debug build diff --git a/crates/ui-contract/src/runtime/egui/alt_code.rs b/crates/ui-contract/src/runtime/egui/alt_code.rs index 0639e31fa8..985d77e5a9 100644 --- a/crates/ui-contract/src/runtime/egui/alt_code.rs +++ b/crates/ui-contract/src/runtime/egui/alt_code.rs @@ -25,6 +25,8 @@ pub(crate) struct AltCodeEntry { swallow_text: bool, /// What the last code typed, for the character a platform composes itself and sends after it. echo: Option, + /// The key of the press that carries the platform's composed character, whose release is left out with it. + carrier: Option, } /// The character an Alt code typed, kept until the next key or pointer press. @@ -33,6 +35,26 @@ struct Echo { /// The code names a byte of the code pages (1–255): the platform's own character for it wins over ours, since it /// knows the system's code pages (double-byte ones too) better than the app's locale guess. byte_code: bool, + /// The keys egui names the platform's character by, for the press that can carry it on Windows: the key of our + /// character, and for a code above 255 the key of the character of the byte Windows reduces it to. + carriers: Vec, +} + +impl Echo { + /// The echo of `typed`, the character `code` names in `pages`. + fn new(typed: char, code: &str, pages: AltCodePages) -> Self { + let value = code.trim_start_matches('0').parse::().ok(); + let byte_code = value.is_some_and(|value| value <= 255); + let mut carriers: Vec = Key::from_name(&typed.to_string()).into_iter().collect(); + if let Some(value) = value.filter(|value| *value > 255) { + let byte = value % 256; + let byte_digits = if code.starts_with('0') { format!("0{byte}") } else { byte.to_string() }; + if let Some(key) = craft_codepage::alt_code_char(&byte_digits, pages.ansi, pages.oem).and_then(|c| Key::from_name(&c.to_string())) { + carriers.push(key); + } + } + Echo { typed: typed.to_string(), byte_code, carriers } + } } /// Folds Alt+numpad entry out of `events` and into text events, decoding with `pages`. `os` is the platform the events @@ -49,6 +71,20 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec, pages: Alt let mut out = Vec::with_capacity(events.len() + 1); for event in events.drain(..) { match &event { + // On Windows the composed character can come on a press of its own after the Alt release, with no + // physical key and the key egui names the character by (`\` is `Key::Backslash`). It is no key the user + // pressed: the press and its release are left out, and the text that follows meets the echo. A press of + // another key with no physical key (one egui does not map, such as a JIS key) is the user's. + Event::Key { key, physical_key: None, pressed: true, repeat: false, .. } + if composes && entry.echo.as_ref().is_some_and(|echo| echo.carriers.contains(key)) => + { + entry.carrier = Some(*key); + continue; + } + Event::Key { key, physical_key: None, pressed: false, .. } if entry.carrier == Some(*key) => { + entry.carrier = None; + continue; + } Event::Key { key, pressed: true, repeat, modifiers, .. } => { entry.swallow_text = false; entry.echo = None; @@ -103,6 +139,7 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec, pages: Alt Event::WindowFocused(false) => { entry.code.clear(); entry.swallow_text = false; + entry.carrier = None; } _ => {} } @@ -117,8 +154,7 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec, pages: Alt // composed character or what is typed next. A digit that sent no text must not cost the next one. entry.swallow_text = false; if let Some(c) = craft_codepage::alt_code_char(&code, pages.ansi, pages.oem) { - let byte_code = code.trim_start_matches('0').parse::().is_ok_and(|value| value <= 255); - entry.echo = Some(Echo { typed: c.to_string(), byte_code }); + entry.echo = Some(Echo::new(c, &code, pages)); out.push(Event::Text(c.to_string())); } } diff --git a/crates/ui-contract/tests/it/runtime/alt_code.rs b/crates/ui-contract/tests/it/runtime/alt_code.rs index 560d7cd3a8..2c7d94eb7d 100644 --- a/crates/ui-contract/tests/it/runtime/alt_code.rs +++ b/crates/ui-contract/tests/it/runtime/alt_code.rs @@ -250,6 +250,107 @@ fn an_alt_numpad_code_types_its_character_once() { assert!(!option.lone_alt, "macOS has no lone Alt either"); } +/// The press, text and release that winit on Windows reports a composed Alt code's character with: the press has no +/// physical key, and its key is the one egui names the character by. +fn composed_on_key(key: Key, composed: &str) -> Vec { + let event = |pressed| Event::Key { key, physical_key: None, pressed, repeat: false, modifiers: Modifiers::NONE }; + vec![event(true), text(composed), event(false)] +} + +/// On Windows the platform's character for a code can come on a key press of its own, with no physical key, after +/// the Alt release: in the release's frame or in the next one. For a character egui has a key for (`\` is +/// `Key::Backslash`, `A` is `Key::A`), that press is the platform's carrier of the character, not a key the user +/// pressed: the character is typed once and the press does not reach egui, where a single-key shortcut could take +/// it. Above 255 the carried character is the one of the byte Windows reduces the code to, and ours stays. A key the +/// user presses after a code still types as normal, also one with no physical key in egui. +#[trial] +fn a_composed_character_on_a_key_press_of_its_own_is_typed_once() { + let typed = Rc::new(RefCell::new(String::new())); + let reached = Rc::new(RefCell::new(Vec::<(Key, bool)>::new())); + let mut show = { + let (typed, reached) = (Rc::clone(&typed), Rc::clone(&reached)); + move |ui: &mut egui::Ui| { + let keys: Vec<(Key, bool)> = ui.input(|input| { + input + .events + .iter() + .filter_map(|event| match event { + Event::Key { key, pressed, .. } => Some((*key, *pressed)), + _ => None, + }) + .collect() + }); + reached.borrow_mut().extend(keys); + Probe::new(&typed).label("Text").id_salt("alt_code_field").show_at(ui, AT); + } + }; + let mut frames = Frames::new(); + craft_ui::testing::windows::set_os(&frames.ctx, Os::Windows); + frames.root(RootEnv::default().alt_code_pages(AltCodePages::new(1252, 437))); + frames.idle(3, &mut show); + // Plays `sequence`, then a frame with none; returns the field's text and the key events that reached egui, each + // with whether it is a press. + let send = |frames: &mut Frames, show: &mut dyn FnMut(&mut egui::Ui), sequence: Vec>| { + reached.borrow_mut().clear(); + typed.borrow_mut().clear(); + for events in sequence { + frames.frame(events, &mut *show); + } + frames.frame(Vec::new(), &mut *show); + (typed.take(), reached.take()) + }; + let next_frame = || { + let mut frames = code("0092"); + frames.push(composed_on_key(Key::Backslash, "\\")); + frames + }; + let same_frame = || { + let mut frames = code("65"); + if let Some(release) = frames.last_mut() { + release.extend(composed_on_key(Key::A, "A")); + } + frames + }; + let then_pressed = || { + let mut frames = code("0233"); + frames.push(vec![key_press(Key::A, Modifiers::NONE), text("a"), key_release(Key::A, Modifiers::NONE)]); + frames + }; + // Above 255 Windows reduces the code to a byte: 321 is 65, `A`, while ours is U+0141. + let reduced = || { + let mut frames = code("321"); + frames.push(composed_on_key(Key::A, "A")); + frames + }; + // A key egui has no physical key for (a JIS key typing `\`) pressed after a code whose character is `A`. + let unmapped = || { + let mut frames = code("65"); + frames.push(composed_on_key(Key::Backslash, "\\")); + frames + }; + + // With nothing focused, egui's input holds every key event no component took. The digits' releases reach it too. + let of = |keys: Vec<(Key, bool)>, key: Key| keys.into_iter().filter(|(k, _)| *k == key).collect::>(); + let (_, keys) = send(&mut frames, &mut show, next_frame()); + assert_eq!(of(keys, Key::Backslash), [], "a carrier press and its release in the next frame do not reach egui"); + let (_, keys) = send(&mut frames, &mut show, same_frame()); + assert_eq!(of(keys, Key::A), [], "a carrier press and its release in the release's frame do not reach egui"); + let (_, keys) = send(&mut frames, &mut show, then_pressed()); + assert_eq!(of(keys, Key::A), [(Key::A, true), (Key::A, false)], "a key the user presses after a code reaches egui"); + let (_, keys) = send(&mut frames, &mut show, reduced()); + 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"); + + click(&mut frames, AT.center(), &mut show); + frames.idle(1, &mut show); + assert_eq!(send(&mut frames, &mut show, reduced()).0, "\u{141}", "above 255 ours stays and the carried byte is dropped"); + assert_eq!(send(&mut frames, &mut show, unmapped()).0, "A\\", "another key with no physical key types as normal"); + assert_eq!(send(&mut frames, &mut show, next_frame()).0, "\\", "a carrier press in the next frame types nothing more"); + assert_eq!(send(&mut frames, &mut show, same_frame()).0, "A", "a carrier press in the release's frame types nothing more"); + assert_eq!(send(&mut frames, &mut show, then_pressed()).0, "éa", "a key the user presses after a code types as normal"); +} + /// Alt with the digit 3, which the host canvas of the next trial claims. const ALT_3: craft_keymap::Chord = craft_keymap::Chord::new(craft_keymap::Mods::ALT, craft_keymap::Key::Num3); From 9f649dc80d85129459f35fcfc83788238c0ca5e3 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 29 Nov 2026 14:10:29 -0800 Subject: [PATCH 2/3] 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); From 4b1d442bb97e0b84dcf8ed76988a9d67e52983d6 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 29 Nov 2026 14:34:24 -0800 Subject: [PATCH 3/3] ui-contract: a top-row Alt code leaves the next key press alone on Windows Windows composes Alt codes from keypad digits only. After a top-row code it sends no character of its own, so the next key press with no physical key in egui (a JIS backslash key) and the next text are the user's. The entry notes a digit whose physical key is a top-row one, and only a keypad code filters a carrier press or lets the platform's text replace ours. --- .../ui-contract/src/runtime/egui/alt_code.rs | 41 ++++++++++++------ .../ui-contract/tests/it/runtime/alt_code.rs | 43 ++++++++++++++++--- 2 files changed, 66 insertions(+), 18 deletions(-) diff --git a/crates/ui-contract/src/runtime/egui/alt_code.rs b/crates/ui-contract/src/runtime/egui/alt_code.rs index 3b8b1b5d4c..4d6b7f4730 100644 --- a/crates/ui-contract/src/runtime/egui/alt_code.rs +++ b/crates/ui-contract/src/runtime/egui/alt_code.rs @@ -18,6 +18,9 @@ const MAX_DIGITS: usize = 8; pub(crate) struct AltCodeEntry { /// The digits typed while Alt is held. code: String, + /// A digit of the code came from the top row (its physical key is `Num0`…`Num9`, a keypad digit's is + /// `Numpad0`…`Numpad9`). Windows composes keypad codes only, so it sends no character of its own for this code. + top_row: bool, /// A digit was typed in this Alt hold, also when the code was then dropped (a focus loss): the Alt release is no /// lone Alt, since the key stage never saw the digits. digits_typed: bool, @@ -35,14 +38,18 @@ struct Echo { /// The code names a byte of the code pages (1–255): the platform's own character for it wins over ours, since it /// knows the system's code pages (double-byte ones too) better than the app's locale guess. byte_code: bool, + /// The platform composes this code itself and sends its character after it: Windows, for a code typed on the + /// keypad. + native: bool, /// The keys egui names the platform's character by, for the press that can carry it on Windows: the key of our /// character, and for a code above 255 the key of the character of the byte Windows reduces it to. carriers: Vec, } impl Echo { - /// The echo of `typed`, the character `code` names in `pages`. - fn new(typed: char, code: &str, pages: AltCodePages) -> Self { + /// The echo of `typed`, the character `code` names in `pages`. `native` tells whether the platform composes the + /// code too. + fn new(typed: char, code: &str, pages: AltCodePages, native: bool) -> Self { let value = code.trim_start_matches('0').parse::().ok(); let byte_code = value.is_some_and(|value| value <= 255); let mut carriers: Vec = Key::from_name(&typed.to_string()).into_iter().collect(); @@ -53,12 +60,12 @@ impl Echo { carriers.push(key); } } - Echo { typed: typed.to_string(), byte_code, carriers } + Echo { typed: typed.to_string(), byte_code, native, carriers } } } /// Folds Alt+numpad entry out of `events` and into text events, decoding with `pages`. `os` is the platform the events -/// come from: on Windows (a browser on Windows too) the platform composes the code itself and sends its character +/// come from: on Windows (a browser on Windows too) the platform composes a keypad code itself and sends its character /// after the Alt release. Returns whether an Alt release in `events` ended a hold in which digits were typed (a code, /// or one a focus loss dropped), so the key stage does not take it for a lone Alt. The caller does not call it on macOS, where Option+digit types its own characters. /// @@ -74,9 +81,10 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec, pages: Alt // On Windows the composed character can come on a press of its own after the Alt release, with no // physical key and the key egui names the character by (`\` is `Key::Backslash`). It is no key the user // pressed: the press and its release are left out, and the text that follows meets the echo. A press of - // another key with no physical key (one egui does not map, such as a JIS key) is the user's. + // another key with no physical key (one egui does not map, such as a JIS key) is the user's, and so is any + // such press after a top-row code, which Windows does not compose. Event::Key { key, physical_key: None, pressed: true, repeat: false, .. } - if composes && entry.echo.as_ref().is_some_and(|echo| echo.carriers.contains(key)) => + if entry.echo.as_ref().is_some_and(|echo| echo.native && echo.carriers.contains(key)) => { entry.carrier = Some(*key); continue; @@ -85,7 +93,7 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec, pages: Alt entry.carrier = None; continue; } - Event::Key { key, pressed: true, repeat, modifiers, .. } => { + Event::Key { key, physical_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 @@ -99,6 +107,7 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec, pages: Alt if !*repeat && entry.code.len() < MAX_DIGITS { entry.code.push(d); } + entry.top_row |= physical_key.is_some_and(|physical| digit(physical).is_some()); entry.digits_typed = true; entry.swallow_text = true; continue; @@ -106,6 +115,7 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec, pages: Alt // Another key (Alt itself, a letter, a digit with Ctrl or Shift) ends the entry unfinished. None => { entry.code.clear(); + entry.top_row = false; // A new Alt hold starts clean: a release that went to another window ended the last one. if matches!(key, Key::AltLeft | Key::AltRight) && !*repeat { entry.digits_typed = false; @@ -120,8 +130,8 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec, pages: Alt // The platform composed the same character: it is typed once. continue; } - if composes { - // On Windows the text after a code is the platform's character for it. For a code up to 255 + if echo.native { + // On Windows the text after a keypad code is the platform's character for it. For a code up to 255 // it replaces ours, which was typed just before with no key or click since; above 255 ours // stays (Windows reduces the code to a byte, Word types the Unicode character). if echo.byte_code && text.chars().count() == 1 { @@ -134,7 +144,8 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec, pages: Alt } continue; } - // Linux and the BSDs compose nothing: a different text is what was typed next. + // Linux and the BSDs compose nothing, nor Windows a top-row code: a different text is what was + // typed next. } } Event::PointerButton { pressed: true, .. } => entry.echo = None, @@ -143,6 +154,7 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec, pages: Alt // Focus in another window ends a code unfinished: Alt's release goes there. Event::WindowFocused(false) => { entry.code.clear(); + entry.top_row = false; entry.swallow_text = false; entry.echo = None; entry.carrier = None; @@ -151,16 +163,19 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec, pages: Alt } let released = alt_released(&event); out.push(event); + let mut top_row = false; if released { ended |= std::mem::take(&mut entry.digits_typed); + top_row = std::mem::take(&mut entry.top_row); } if released && !entry.code.is_empty() { let code = std::mem::take(&mut entry.code); + let native = composes && !top_row; // A digit's own text comes with its press, before Alt is let go: text after this is the platform's // composed character or what is typed next. A digit that sent no text must not cost the next one. entry.swallow_text = false; if let Some(c) = craft_codepage::alt_code_char(&code, pages.ansi, pages.oem) { - entry.echo = Some(Echo::new(c, &code, pages)); + entry.echo = Some(Echo::new(c, &code, pages, native)); out.push(Event::Text(c.to_string())); } } @@ -169,8 +184,8 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec, pages: Alt ended } -/// The digit `key` adds to a code. egui reports keypad and top-row digits alike (`Key::Num0`…`Num9`) and carries no -/// key location, so the top-row digits compose codes too; with Alt alone held they type nothing else. +/// The digit `key` adds to a code. egui's logical key is the same for keypad and top-row digits (`Key::Num0`…`Num9`), +/// so the top-row digits compose codes too; with Alt alone held they type nothing else. fn digit(key: Key) -> Option { let n = match key { Key::Num0 => 0, diff --git a/crates/ui-contract/tests/it/runtime/alt_code.rs b/crates/ui-contract/tests/it/runtime/alt_code.rs index 2c4b4a5a52..f3e7d1d801 100644 --- a/crates/ui-contract/tests/it/runtime/alt_code.rs +++ b/crates/ui-contract/tests/it/runtime/alt_code.rs @@ -10,7 +10,7 @@ use craft_ui::runtime::focus::{FocusTarget, KeyUse, host_canvas, request}; use craft_ui::runtime::frame::take_output; use craft_ui::style::StyleCx; use craft_ui::testing::focus::{named_id, widget}; -use craft_ui::testing::input::{key_press, key_repeat, text}; +use craft_ui::testing::input::{key_press, text}; use craft_ui::testing::pointer::click; use craft_ui::testing::{Frames, trial}; use craft_ui_test_kit::contract::PlainStyle; @@ -147,12 +147,20 @@ impl Session { const ALT: Modifiers = Modifiers::ALT; -/// The keypad digit key of `digit`; egui names keypad and top-row digits alike. +/// The logical key of `digit`; egui names keypad and top-row digits alike. fn digit_key(digit: char) -> Key { const DIGITS: [Key; 10] = [Key::Num0, Key::Num1, Key::Num2, Key::Num3, Key::Num4, Key::Num5, Key::Num6, Key::Num7, Key::Num8, Key::Num9]; DIGITS[digit.to_digit(10).expect("a decimal digit") as usize] } +/// A keypad digit's press, auto-repeat or release with Alt held: its physical key is the keypad one. +fn keypad(digit: char, pressed: bool, repeat: bool) -> Event { + const KEYPAD: [Key; 10] = + [Key::Numpad0, Key::Numpad1, Key::Numpad2, Key::Numpad3, Key::Numpad4, Key::Numpad5, Key::Numpad6, Key::Numpad7, Key::Numpad8, Key::Numpad9]; + let physical = KEYPAD[digit.to_digit(10).expect("a decimal digit") as usize]; + Event::Key { key: digit_key(digit), physical_key: Some(physical), pressed, repeat, modifiers: ALT } +} + fn key_release(key: Key, modifiers: Modifiers) -> Event { Event::Key { key, physical_key: Some(key), pressed: false, repeat: false, modifiers } } @@ -162,17 +170,28 @@ fn alt(down: bool) -> Event { Event::ModifiersChanged(if down { ALT } else { Modifiers::NONE }) } -/// The frame of one digit typed with Alt held: its press with the digit's own text (Linux sends it), then its release. +/// The frame of one keypad digit typed with Alt held: its press with the digit's own text (Linux sends it), then its +/// release. fn digit(d: char) -> Vec { + vec![keypad(d, true, false), text(&d.to_string()), keypad(d, false, false)] +} + +/// The frame of one top-row digit typed with Alt held. +fn top_row_digit(d: char) -> Vec { let key = digit_key(d); vec![key_press(key, ALT), text(&d.to_string()), key_release(key, ALT)] } -/// The frames of an Alt code: Alt down, each digit in a frame of its own, then Alt up. +/// The frames of an Alt code: Alt down, each keypad digit in a frame of its own, then Alt up. fn code(digits: &str) -> Vec> { std::iter::once(vec![alt(true)]).chain(digits.chars().map(digit)).chain([vec![alt(false)]]).collect() } +/// `code(digits)` typed on the top row, which Windows does not compose. +fn top_row_code(digits: &str) -> Vec> { + std::iter::once(vec![alt(true)]).chain(digits.chars().map(top_row_digit)).chain([vec![alt(false)]]).collect() +} + /// `code(digits)`, then a frame in which the platform sends `composed`, the character it made of the code itself. fn code_then(digits: &str, composed: &str) -> Vec> { let mut frames = code(digits); @@ -206,7 +225,7 @@ fn an_alt_numpad_code_types_its_character_once() { assert_eq!(session.send(code("233")), typed("Θ"), "{os:?}: no leading 0 reads the OEM page"); assert_eq!(session.send(code("8364")), typed("€"), "{os:?}: above 255 the code is the Unicode character"); // With the repeat counted the code would be 02333, U+091D. - let held = vec![key_press(Key::Num3, ALT), text("3"), key_repeat(Key::Num3, ALT), text("3"), key_release(Key::Num3, ALT)]; + let held = vec![keypad('3', true, false), text("3"), keypad('3', true, true), text("3"), keypad('3', false, false)]; let held_digit = vec![vec![alt(true)], digit('0'), digit('2'), held, digit('3'), vec![alt(false)]]; assert_eq!(session.send(held_digit), typed("é"), "{os:?}: a digit's auto-repeat adds nothing and types nothing"); // Eight digits are kept: 00000233 is é, where all nine (2334) would be U+091E. @@ -226,6 +245,10 @@ fn an_alt_numpad_code_types_its_character_once() { assert_eq!(other_echo, typed("€¬"), "{os:?}: a different text after a code is typed as normal"); } } + // Windows composes keypad codes only: after a top-row code, a different text is what was typed next. + let mut top_row_then = top_row_code("0233"); + top_row_then.push(vec![text("й")]); + assert_eq!(session.send(top_row_then), typed("éй"), "{os:?}: the text after a top-row code is typed as normal"); // Focus going to another window ends the code: its Alt release types nothing and is no lone Alt. let away = vec![vec![alt(true)], digit('0'), digit('2'), vec![Event::WindowFocused(false)], vec![Event::WindowFocused(true), alt(false)]]; @@ -356,6 +379,15 @@ fn a_composed_character_on_a_key_press_of_its_own_is_typed_once() { 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"); + // Windows composes nothing for a top-row code: a key with no physical key in egui that types the code's character + // right after it, with no focus change, is the user's. + let top_row = || { + let mut frames = top_row_code("0092"); + frames.push(composed_on_key(Key::Backslash, "\\")); + frames + }; + let (_, keys) = send(&mut frames, &mut show, top_row()); + assert_eq!(of(keys, Key::Backslash), [(Key::Backslash, true), (Key::Backslash, false)], "a press after a top-row code reaches egui"); click(&mut frames, AT.center(), &mut show); frames.idle(1, &mut show); @@ -364,6 +396,7 @@ fn a_composed_character_on_a_key_press_of_its_own_is_typed_once() { assert_eq!(send(&mut frames, &mut show, next_frame()).0, "\\", "a carrier press in the next frame types nothing more"); assert_eq!(send(&mut frames, &mut show, same_frame()).0, "A", "a carrier press in the release's frame types nothing more"); assert_eq!(send(&mut frames, &mut show, then_pressed()).0, "éa", "a key the user presses after a code types as normal"); + assert_eq!(send(&mut frames, &mut show, top_row()).0, "\\\\", "a key pressed after a top-row code types as normal"); } /// Alt with the digit 3, which the host canvas of the next trial claims.