diff --git a/Cargo.lock b/Cargo.lock index ea03712224..085c1e4bb3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14713,7 +14713,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..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, @@ -25,18 +28,44 @@ 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. +/// 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 /// 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`. `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(); + 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, 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. /// @@ -49,15 +78,36 @@ 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 { - Event::Key { key, pressed: true, repeat, modifiers, .. } => { + // 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, 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 entry.echo.as_ref().is_some_and(|echo| echo.native && 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, 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 + // 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. 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; @@ -65,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; @@ -79,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 { @@ -93,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, @@ -102,23 +154,28 @@ 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; } _ => {} } 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) { - 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, native)); out.push(Event::Text(c.to_string())); } } @@ -127,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 560d7cd3a8..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)]]; @@ -250,6 +273,132 @@ 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"); + // 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"); + // 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); + 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"); + 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. const ALT_3: craft_keymap::Chord = craft_keymap::Chord::new(craft_keymap::Mods::ALT, craft_keymap::Key::Num3);