mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 10:48:15 +01:00
alt codes: a digit enters a code only when its physical key is on the keypad (Key::Numpad*, storytold/egui#48)
Replaces the RawInput::numpad_keys list from the unpublished egui change with the physical key egui#48 reports on Event::Key, so the fork carries one numpad mechanism. The trials send keypad digits as key events with a Numpad physical key; both fail when main-row digits also compose. The LOCAL egui override now points at a local branch of b9770908 plus egui#48's commits. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8355e93985
commit
8577b382a1
5 files changed
+47
-48
No files matched your search
@@ -35,10 +35,9 @@ struct Echo {
|
||||
byte_code: bool,
|
||||
}
|
||||
|
||||
/// 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
|
||||
/// Folds Alt+numpad entry out of `events` and into text events, decoding with `pages`. Only a digit whose physical key
|
||||
/// is on the keypad (`Key::Numpad0`…`Numpad9`) enters a code, so a main-row Alt+digit stays a shortcut, and a press
|
||||
/// with no physical key (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.
|
||||
@@ -46,24 +45,17 @@ 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>,
|
||||
numpad: &[Key],
|
||||
pages: AltCodePages,
|
||||
os: Os,
|
||||
claimed: &dyn Fn(&Event) -> bool,
|
||||
) -> bool {
|
||||
pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec<Event>, 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);
|
||||
for event in events.drain(..) {
|
||||
match &event {
|
||||
Event::Key { key, pressed: true, repeat, modifiers, .. } => {
|
||||
Event::Key { key, physical_key, pressed: true, repeat, modifiers } => {
|
||||
entry.swallow_text = false;
|
||||
entry.echo = None;
|
||||
match digit(*key).filter(|_| alt_only(modifiers) && !claimed(&event)) {
|
||||
Some(d) if numpad.contains(key) => {
|
||||
Some(d) if physical_key.is_some_and(on_keypad) => {
|
||||
// A held digit's auto-repeat adds nothing, as on Windows.
|
||||
if !*repeat && entry.code.len() < MAX_DIGITS {
|
||||
entry.code.push(d);
|
||||
@@ -143,8 +135,25 @@ pub(crate) fn fold(
|
||||
ended
|
||||
}
|
||||
|
||||
/// 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.
|
||||
/// Whether `physical` is a digit key of the numeric keypad.
|
||||
fn on_keypad(physical: Key) -> bool {
|
||||
matches!(
|
||||
physical,
|
||||
Key::Numpad0
|
||||
| Key::Numpad1
|
||||
| Key::Numpad2
|
||||
| Key::Numpad3
|
||||
| Key::Numpad4
|
||||
| Key::Numpad5
|
||||
| Key::Numpad6
|
||||
| Key::Numpad7
|
||||
| Key::Numpad8
|
||||
| Key::Numpad9
|
||||
)
|
||||
}
|
||||
|
||||
/// The digit `key` adds to a code. egui names keypad and main-row digits alike as logical keys (`Key::Num0`…`Num9`);
|
||||
/// `fold` tells them apart by the physical key (`on_keypad`).
|
||||
fn digit(key: Key) -> Option<char> {
|
||||
let n = match key {
|
||||
Key::Num0 => 0,
|
||||
|
||||
@@ -228,9 +228,8 @@ 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`); `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.
|
||||
/// (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.
|
||||
#[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,
|
||||
@@ -239,7 +238,6 @@ 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 {
|
||||
@@ -271,7 +269,6 @@ 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, numpad) = (&mut input.events, &input.numpad_keys);
|
||||
let events = &mut input.events;
|
||||
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, numpad, pages, os, &claimed);
|
||||
state.alt_code_ended = !macos && super::alt_code::fold(&mut state.alt_code, events, pages, os, &claimed);
|
||||
});
|
||||
cap_text(&mut input.events);
|
||||
let events = &mut input.events;
|
||||
|
||||
@@ -42,8 +42,6 @@ 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`.
|
||||
@@ -142,7 +140,6 @@ 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,
|
||||
@@ -233,12 +230,6 @@ 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]) {
|
||||
@@ -337,8 +328,7 @@ impl Frames {
|
||||
read: std::mem::take(&mut self.files.read),
|
||||
given: std::mem::take(&mut self.files.given),
|
||||
};
|
||||
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);
|
||||
let mut result = fixture::run_frame(&self.ctx, self.time, self.screen, self.pixels_per_point, viewport, events, files, ui);
|
||||
match &mut self.paint {
|
||||
Some(Recorded(paint)) => {
|
||||
paint.textures_delta.append(std::mem::take(&mut result.textures));
|
||||
|
||||
@@ -146,9 +146,7 @@ impl Session {
|
||||
self.typed.borrow_mut().clear();
|
||||
self.rerun.set(true);
|
||||
for events in frames {
|
||||
if keypad {
|
||||
self.frames.on_numpad(&digit_presses(&events));
|
||||
}
|
||||
let events = if keypad { on_keypad(events) } else { 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");
|
||||
}
|
||||
@@ -162,19 +160,26 @@ const ALT: Modifiers = Modifiers::ALT;
|
||||
|
||||
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`).
|
||||
/// The digit key of `digit`; egui names keypad and main-row digits alike as logical keys, and the physical key tells
|
||||
/// them apart (`on_keypad`).
|
||||
fn digit_key(digit: char) -> Key {
|
||||
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> {
|
||||
const KEYPAD: [Key; 10] =
|
||||
[Key::Numpad0, Key::Numpad1, Key::Numpad2, Key::Numpad3, Key::Numpad4, Key::Numpad5, Key::Numpad6, Key::Numpad7, Key::Numpad8, Key::Numpad9];
|
||||
|
||||
/// `events` with each digit key on the numeric keypad: its physical key is the keypad's, as egui-winit and the web
|
||||
/// backend report a keypad digit.
|
||||
fn on_keypad(events: Vec<Event>) -> Vec<Event> {
|
||||
events
|
||||
.iter()
|
||||
.filter_map(|event| match event {
|
||||
Event::Key { key, pressed: true, .. } if DIGITS.contains(key) => Some(*key),
|
||||
_ => None,
|
||||
.into_iter()
|
||||
.map(|event| match event {
|
||||
Event::Key { key, pressed, repeat, modifiers, .. } if DIGITS.contains(&key) => {
|
||||
let at = DIGITS.iter().position(|d| *d == key).expect("a digit key");
|
||||
Event::Key { key, physical_key: Some(KEYPAD[at]), pressed, repeat, modifiers }
|
||||
}
|
||||
other => other,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@@ -322,9 +327,7 @@ fn an_alt_digit_a_host_canvas_claims_reaches_the_app_and_a_field_still_types_cod
|
||||
reached.borrow_mut().clear();
|
||||
typed.borrow_mut().clear();
|
||||
for events in sequence {
|
||||
if keypad {
|
||||
frames.on_numpad(&digit_presses(&events));
|
||||
}
|
||||
let events = if keypad { on_keypad(events) } else { events };
|
||||
frames.frame(events, &mut *show);
|
||||
}
|
||||
frames.frame(Vec::new(), &mut *show);
|
||||
|
||||
Reference in new issue
Block a user