mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 10:48:15 +01:00
alt codes: only keypad digits enter a code
egui names a keypad digit and the main row's alike (Key::Num1), so the Alt-code fold took an unclaimed main-row Alt+1 for a code and typed its OEM 437 character where an app's Alt+digit shortcut belonged. The fold now reads the keypad presses the platform reports (RawInput::numpad_keys, from winit's key location and the DOM's): only those digits enter a code. A main-row Alt+digit ends the entry and goes on to the key stage; its own text stays untyped, as before. A press the platform says nothing about (injected input) is no keypad digit. Frames::on_numpad marks a frame's keypad presses for trials. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ee820581ca
commit
c064eb6166
5 files changed
+87
-22
No files matched your search
@@ -1,6 +1,6 @@
|
||||
//! Alt+numpad code entry, folded into the input events in the raw-input step.
|
||||
//!
|
||||
//! Digits typed with Alt as the only modifier held collect into a decimal code; letting go of Alt types the character
|
||||
//! Keypad digits typed with Alt as the only modifier held collect into a decimal code; letting go of Alt types the character
|
||||
//! the code names (`craft_codepage::alt_code_char`: `0233` is é in Windows-1252, `233` is Θ in OEM 437). The digits'
|
||||
//! presses and their own text (Linux sends it with Alt held) reach nothing. The step runs before the IME and key
|
||||
//! stages, so every field and the canvas read the result as typed text, and only the first pass of a frame gets the
|
||||
@@ -35,7 +35,10 @@ struct Echo {
|
||||
byte_code: bool,
|
||||
}
|
||||
|
||||
/// Folds Alt+numpad entry out of `events` and into text events, decoding with `pages`. `os` is the platform the events
|
||||
/// Folds Alt+numpad entry out of `events` and into text events, decoding with `pages`. `numpad` lists the keys the
|
||||
/// platform reports pressed on the keypad in this input (`RawInput::numpad_keys`): only those digits enter a code, so a
|
||||
/// main-row Alt+digit stays a shortcut, and a press the platform says nothing about (injected input) is no keypad
|
||||
/// digit. `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
|
||||
/// 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.
|
||||
@@ -43,7 +46,14 @@ struct Echo {
|
||||
/// `claimed` tells whether the key stage takes a press as a chord the key target claims (the focused component, or with
|
||||
/// nothing focused the window's document canvas) or a capture holds: such an Alt+digit is its owner's, as an app's
|
||||
/// shortcut goes before Alt-code entry, and it ends the entry like any other key.
|
||||
pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec<Event>, pages: AltCodePages, os: Os, claimed: &dyn Fn(&Event) -> bool) -> bool {
|
||||
pub(crate) fn fold(
|
||||
entry: &mut AltCodeEntry,
|
||||
events: &mut Vec<Event>,
|
||||
numpad: &[Key],
|
||||
pages: AltCodePages,
|
||||
os: Os,
|
||||
claimed: &dyn Fn(&Event) -> bool,
|
||||
) -> bool {
|
||||
let composes = os == Os::Windows;
|
||||
let mut ended = false;
|
||||
let mut out = Vec::with_capacity(events.len() + 1);
|
||||
@@ -53,7 +63,7 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec<Event>, pages: Alt
|
||||
entry.swallow_text = false;
|
||||
entry.echo = None;
|
||||
match digit(*key).filter(|_| alt_only(modifiers) && !claimed(&event)) {
|
||||
Some(d) => {
|
||||
Some(d) if numpad.contains(key) => {
|
||||
// A held digit's auto-repeat adds nothing, as on Windows.
|
||||
if !*repeat && entry.code.len() < MAX_DIGITS {
|
||||
entry.code.push(d);
|
||||
@@ -62,6 +72,12 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec<Event>, pages: Alt
|
||||
entry.swallow_text = true;
|
||||
continue;
|
||||
}
|
||||
// A main-row digit is the app's shortcut and ends the entry: its press goes on to the key stage,
|
||||
// and its own text (Linux and Windows send it with Alt held) is the chord's, typed nowhere.
|
||||
Some(_) => {
|
||||
entry.code.clear();
|
||||
entry.swallow_text = true;
|
||||
}
|
||||
// Another key (Alt itself, a letter, a digit with Ctrl or Shift) ends the entry unfinished.
|
||||
None => {
|
||||
entry.code.clear();
|
||||
@@ -127,8 +143,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 names keypad and main-row digits alike (`Key::Num0`…`Num9`); `fold` tells them
|
||||
/// apart by the keypad presses the platform reports.
|
||||
fn digit(key: Key) -> Option<char> {
|
||||
let n = match key {
|
||||
Key::Num0 => 0,
|
||||
|
||||
@@ -228,8 +228,9 @@ impl ImeResets {
|
||||
}
|
||||
|
||||
/// Runs one pass of `viewport` at `time` seconds with `events` and `files`, on a window of `native_pixels_per_point`
|
||||
/// (egui's default when `None`). A viewport other than the root is a child window of the root, which stays live; the
|
||||
/// child stays open in every later frame, the root's included, until it is closed.
|
||||
/// (egui's default when `None`); `numpad_keys` are the keys of `events` pressed on the numeric keypad. A viewport other
|
||||
/// than the root is a child window of the root, which stays live; the child stays open in every later frame, the root's
|
||||
/// included, until it is closed.
|
||||
#[expect(clippy::too_many_arguments, reason = "the arguments are the facts of one frame, and every caller passes them all")]
|
||||
pub(crate) fn run_frame(
|
||||
ctx: &Context,
|
||||
@@ -238,6 +239,7 @@ pub(crate) fn run_frame(
|
||||
native_pixels_per_point: Option<f32>,
|
||||
viewport: egui::ViewportId,
|
||||
mut events: Vec<Event>,
|
||||
numpad_keys: Vec<egui::Key>,
|
||||
files: OsFiles,
|
||||
ui: impl FnMut(&mut Ui),
|
||||
) -> FrameResult {
|
||||
@@ -269,6 +271,7 @@ pub(crate) fn run_frame(
|
||||
viewport_id: viewport,
|
||||
focused,
|
||||
events,
|
||||
numpad_keys,
|
||||
hovered_files,
|
||||
dropped_files,
|
||||
viewports,
|
||||
|
||||
@@ -753,10 +753,10 @@ pub(crate) fn prepare(ctx: &Context, input: &mut RawInput) {
|
||||
let macos = os == Os::Mac;
|
||||
// A chord the key target claims or a capture holds goes to its owner before Alt-code entry.
|
||||
let claimed = crate::runtime::keys::claims_press(ctx, input);
|
||||
let events = &mut input.events;
|
||||
let (events, numpad) = (&mut input.events, &input.numpad_keys);
|
||||
PerViewport::<RawState>::with(ctx, input.viewport_id, |state| {
|
||||
// On macOS Option+digit types its own characters (¡ ™ £…): there is no Alt code there.
|
||||
state.alt_code_ended = !macos && super::alt_code::fold(&mut state.alt_code, events, pages, os, &claimed);
|
||||
state.alt_code_ended = !macos && super::alt_code::fold(&mut state.alt_code, events, numpad, pages, os, &claimed);
|
||||
});
|
||||
cap_text(&mut input.events);
|
||||
let events = &mut input.events;
|
||||
|
||||
@@ -42,6 +42,8 @@ pub struct Frames {
|
||||
told: egui::Modifiers,
|
||||
/// The OS files hovering over the window in every following frame.
|
||||
files: fixture::OsFiles,
|
||||
/// The keys the next frame's events press on the numeric keypad.
|
||||
numpad: Vec<egui::Key>,
|
||||
/// The size of the egui screen rect: the window every press has to land in.
|
||||
screen: Vec2,
|
||||
/// The window's pixels per point; egui's default when `None`.
|
||||
@@ -140,6 +142,7 @@ impl Frames {
|
||||
modifiers: egui::Modifiers::NONE,
|
||||
told: egui::Modifiers::NONE,
|
||||
files: fixture::OsFiles::default(),
|
||||
numpad: Vec::new(),
|
||||
screen: Self::SCREEN,
|
||||
pixels_per_point: None,
|
||||
paint: None,
|
||||
@@ -230,6 +233,12 @@ impl Frames {
|
||||
self.modifiers
|
||||
}
|
||||
|
||||
/// The next frame's presses of `keys` are on the numeric keypad, as the platform reports them
|
||||
/// (`RawInput::numpad_keys`); egui names a keypad digit and the main row's alike.
|
||||
pub fn on_numpad(&mut self, keys: &[egui::Key]) {
|
||||
self.numpad = keys.to_vec();
|
||||
}
|
||||
|
||||
/// Files named `names` are dragged over the window from the OS, in this frame and every following one until they
|
||||
/// drop or leave.
|
||||
pub fn hover_files(&mut self, names: &[&str]) {
|
||||
@@ -328,7 +337,8 @@ impl Frames {
|
||||
read: std::mem::take(&mut self.files.read),
|
||||
given: std::mem::take(&mut self.files.given),
|
||||
};
|
||||
let mut result = fixture::run_frame(&self.ctx, self.time, self.screen, self.pixels_per_point, viewport, events, files, ui);
|
||||
let numpad = std::mem::take(&mut self.numpad);
|
||||
let mut result = fixture::run_frame(&self.ctx, self.time, self.screen, self.pixels_per_point, viewport, events, numpad, files, ui);
|
||||
match &mut self.paint {
|
||||
Some(Recorded(paint)) => {
|
||||
paint.textures_delta.append(std::mem::take(&mut result.textures));
|
||||
|
||||
@@ -129,13 +129,26 @@ impl Session {
|
||||
self.frames.idle(1, &mut show);
|
||||
}
|
||||
|
||||
/// Plays `frames` of input, each frame running two passes, then a frame with none; returns what they typed.
|
||||
/// Plays `frames` of input with their digits on the keypad, each frame running two passes, then a frame with none;
|
||||
/// returns what they typed.
|
||||
fn send(&mut self, frames: Vec<Vec<Event>>) -> Typed {
|
||||
self.play(frames, true)
|
||||
}
|
||||
|
||||
/// `send` with the digits on the main row.
|
||||
fn send_main_row(&mut self, frames: Vec<Vec<Event>>) -> Typed {
|
||||
self.play(frames, false)
|
||||
}
|
||||
|
||||
fn play(&mut self, frames: Vec<Vec<Event>>, keypad: bool) -> Typed {
|
||||
let mut show = self.show();
|
||||
take_output(&self.frames.ctx);
|
||||
self.typed.borrow_mut().clear();
|
||||
self.rerun.set(true);
|
||||
for events in frames {
|
||||
if keypad {
|
||||
self.frames.on_numpad(&digit_presses(&events));
|
||||
}
|
||||
let passes = self.frames.frame(events, &mut show);
|
||||
assert_eq!(passes, 2, "each input frame runs a second pass, which gets none of the frame's events");
|
||||
}
|
||||
@@ -147,12 +160,25 @@ impl Session {
|
||||
|
||||
const ALT: Modifiers = Modifiers::ALT;
|
||||
|
||||
/// The keypad digit key of `digit`; egui names keypad and top-row digits alike.
|
||||
const DIGITS: [Key; 10] = [Key::Num0, Key::Num1, Key::Num2, Key::Num3, Key::Num4, Key::Num5, Key::Num6, Key::Num7, Key::Num8, Key::Num9];
|
||||
|
||||
/// The digit key of `digit`; egui names keypad and main-row digits alike, and the platform tells them apart
|
||||
/// (`Frames::on_numpad`).
|
||||
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]
|
||||
}
|
||||
|
||||
/// The digit keys `events` press, for the platform to report them pressed on the keypad.
|
||||
fn digit_presses(events: &[Event]) -> Vec<Key> {
|
||||
events
|
||||
.iter()
|
||||
.filter_map(|event| match event {
|
||||
Event::Key { key, pressed: true, .. } if DIGITS.contains(key) => Some(*key),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn key_release(key: Key, modifiers: Modifiers) -> Event {
|
||||
Event::Key { key, physical_key: Some(key), pressed: false, repeat: false, modifiers }
|
||||
}
|
||||
@@ -203,6 +229,9 @@ fn an_alt_numpad_code_types_its_character_once() {
|
||||
assert_eq!(lone, Typed { text: String::new(), lone_alt: true }, "{os:?}: a lone Alt in the field alerts");
|
||||
|
||||
assert_eq!(session.send(code("0233")), typed("é"), "{os:?}: a leading 0 reads the ANSI page, the digits type nothing");
|
||||
// Alt with main-row digits is a shortcut, never a code: egui names those digits as it names the keypad's.
|
||||
let main_row = session.send_main_row(code("0233"));
|
||||
assert!(main_row.text.is_empty(), "{os:?}: main-row digits enter no code and type nothing: {main_row:?}");
|
||||
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.
|
||||
@@ -255,7 +284,8 @@ const ALT_3: craft_keymap::Chord = craft_keymap::Chord::new(craft_keymap::Mods::
|
||||
|
||||
/// An app's Alt+digit shortcut on Windows and Linux: the app's own egui canvas (`host_canvas`) claims Alt+3. With
|
||||
/// nothing focused the canvas is the window's key target, so Alt-code entry leaves the claimed press and egui's input
|
||||
/// keeps it for the app, while an unclaimed Alt+5 still folds into a code. With the canvas focused, the claimed press
|
||||
/// keeps it for the app, while an unclaimed keypad Alt+5 still folds into a code; a main-row Alt+5 is never a code, so
|
||||
/// it reaches the app claimed or not (egui names both digits `Num5`). With the canvas focused, the claimed press
|
||||
/// still reaches egui. A focused text field types Alt codes again, with the claimed digit among them.
|
||||
#[trial]
|
||||
fn an_alt_digit_a_host_canvas_claims_reaches_the_app_and_a_field_still_types_codes() {
|
||||
@@ -286,29 +316,35 @@ fn an_alt_digit_a_host_canvas_claims_reaches_the_app_and_a_field_still_types_cod
|
||||
craft_ui::testing::windows::set_os(&frames.ctx, os);
|
||||
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 Alt presses that reached egui and the field's text.
|
||||
let send = |frames: &mut Frames, show: &mut dyn FnMut(&mut egui::Ui), sequence: Vec<Vec<Event>>| {
|
||||
// Plays `sequence` with its digits on the keypad, or on the main row, then a frame with none; returns the Alt
|
||||
// presses that reached egui and the field's text.
|
||||
let play = |frames: &mut Frames, show: &mut dyn FnMut(&mut egui::Ui), sequence: Vec<Vec<Event>>, keypad: bool| {
|
||||
reached.borrow_mut().clear();
|
||||
typed.borrow_mut().clear();
|
||||
for events in sequence {
|
||||
if keypad {
|
||||
frames.on_numpad(&digit_presses(&events));
|
||||
}
|
||||
frames.frame(events, &mut *show);
|
||||
}
|
||||
frames.frame(Vec::new(), &mut *show);
|
||||
(reached.take(), typed.take())
|
||||
};
|
||||
|
||||
let (keys, _) = send(&mut frames, &mut show, code("3"));
|
||||
let (keys, _) = play(&mut frames, &mut show, code("3"), true);
|
||||
assert!(keys.contains(&Key::Num3), "{os:?}: with nothing focused, the canvas's claimed Alt+3 reaches egui: {keys:?}");
|
||||
let (keys, _) = send(&mut frames, &mut show, code("5"));
|
||||
assert!(!keys.contains(&Key::Num5), "{os:?}: an unclaimed Alt+5 folds into an Alt code: {keys:?}");
|
||||
let (keys, _) = play(&mut frames, &mut show, code("5"), true);
|
||||
assert!(!keys.contains(&Key::Num5), "{os:?}: an unclaimed keypad Alt+5 folds into an Alt code: {keys:?}");
|
||||
let (keys, _) = play(&mut frames, &mut show, code("5"), false);
|
||||
assert!(keys.contains(&Key::Num5), "{os:?}: an unclaimed main-row Alt+5 is no code and reaches egui: {keys:?}");
|
||||
|
||||
request(&frames.ctx, FocusTarget::Id(canvas));
|
||||
let (keys, _) = send(&mut frames, &mut show, code("3"));
|
||||
let (keys, _) = play(&mut frames, &mut show, code("3"), true);
|
||||
assert!(keys.contains(&Key::Num3), "{os:?}: the focused canvas's claimed Alt+3 reaches egui: {keys:?}");
|
||||
|
||||
click(&mut frames, AT.center(), &mut show);
|
||||
frames.idle(1, &mut show);
|
||||
let (keys, text) = send(&mut frames, &mut show, code("0233"));
|
||||
let (keys, text) = play(&mut frames, &mut show, code("0233"), true);
|
||||
assert_eq!(text, "é", "{os:?}: the focused field types the code, its 3 included");
|
||||
assert!(keys.is_empty(), "{os:?}: no digit of the code reaches egui: {keys:?}");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user