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);