mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 10:48:15 +01:00
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.
This commit is contained in:
1 parent
9f649dc80d
commit
4b1d442bb9
2 files changed
+66
-18
No files matched your search
@@ -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<Key>,
|
||||
}
|
||||
|
||||
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::<u32>().ok();
|
||||
let byte_code = value.is_some_and(|value| value <= 255);
|
||||
let mut carriers: Vec<Key> = 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<Event>, 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<Event>, 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<Event>, 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<Event>, 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<Event>, 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<Event>, 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<Event>, 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<Event>, 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<Event>, 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<char> {
|
||||
let n = match key {
|
||||
Key::Num0 => 0,
|
||||
|
||||
@@ -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<Event> {
|
||||
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<Event> {
|
||||
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<Vec<Event>> {
|
||||
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<Vec<Event>> {
|
||||
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<Vec<Event>> {
|
||||
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.
|
||||
|
||||
Reference in new issue
Block a user