From f51ab901484fcf5369b60d0299f6cd26e781b40c Mon Sep 17 00:00:00 2001 From: Martin Ball Date: Wed, 18 Nov 2026 12:40:08 +0000 Subject: [PATCH] ui-contract: a claimed Alt+digit goes to its owner and ends the Alt code; Alt-numpad code types once --- .../ui-contract/src/runtime/egui/alt_code.rs | 14 ++++++--- crates/ui-contract/src/runtime/egui/input.rs | 4 ++- crates/ui-contract/src/runtime/keys.rs | 2 +- crates/ui-contract/src/runtime/keys/input.rs | 30 +++++++++++++++++-- crates/ui-contract/tests/it/runtime/wheel.rs | 12 +++++++- 5 files changed, 53 insertions(+), 9 deletions(-) diff --git a/crates/ui-contract/src/runtime/egui/alt_code.rs b/crates/ui-contract/src/runtime/egui/alt_code.rs index 240d5aada..7320636a8 100644 --- a/crates/ui-contract/src/runtime/egui/alt_code.rs +++ b/crates/ui-contract/src/runtime/egui/alt_code.rs @@ -39,8 +39,10 @@ struct Echo { /// 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. - -pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec, pages: AltCodePages, os: Os) -> bool { +/// +/// `claimed` tells whether the key stage takes a press as a chord a component claims 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, 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); @@ -49,7 +51,7 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec, pages: Alt Event::Key { key, pressed: true, repeat, modifiers, .. } => { entry.swallow_text = false; entry.echo = None; - match digit(*key).filter(|_| alt_only(modifiers)) { + 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 { @@ -81,7 +83,11 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec, pages: Alt // 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 { - out.push(Event::Key { key: Key::Backspace, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE }); + // A whole Backspace tap: its release ends it before the text, so the key stage reads the + // text as typed on its own and not as the text of a press the keymap binds. + for pressed in [true, false] { + out.push(Event::Key { key: Key::Backspace, physical_key: None, pressed, repeat: false, modifiers: Modifiers::NONE }); + } out.push(event); } continue; diff --git a/crates/ui-contract/src/runtime/egui/input.rs b/crates/ui-contract/src/runtime/egui/input.rs index af245b4dd..75327eddf 100644 --- a/crates/ui-contract/src/runtime/egui/input.rs +++ b/crates/ui-contract/src/runtime/egui/input.rs @@ -679,10 +679,12 @@ pub(crate) fn prepare(ctx: &Context, input: &mut RawInput) { scale_wheel(&mut input.events, wheel); let os = crate::runtime::windows::os_of(ctx); let macos = os == Os::Mac; + // A chord a component 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; PerViewport::::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); + 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; diff --git a/crates/ui-contract/src/runtime/keys.rs b/crates/ui-contract/src/runtime/keys.rs index 61f108ae8..fbadd7ecf 100644 --- a/crates/ui-contract/src/runtime/keys.rs +++ b/crates/ui-contract/src/runtime/keys.rs @@ -28,7 +28,7 @@ pub use claims::take_claimed; pub use craft_behavior::{AccessRequest, Intent, MoveMods, NavMove, PartRef}; #[doc(hidden)] pub use input::{down, keyed}; -pub(crate) use input::{input_hook, queue_nav, take_menu_keys, take_nav}; +pub(crate) use input::{claims_press, input_hook, queue_nav, take_menu_keys, take_nav}; pub use intents::Intents; #[doc(hidden)] pub use intents::intents_of; diff --git a/crates/ui-contract/src/runtime/keys/input.rs b/crates/ui-contract/src/runtime/keys/input.rs index bb278b7f9..9a9a2e0be 100644 --- a/crates/ui-contract/src/runtime/keys/input.rs +++ b/crates/ui-contract/src/runtime/keys/input.rs @@ -133,6 +133,9 @@ impl Strip<'_> { press.text = Some(batch.texts.len()); } } + if text == "й" { + eprintln!("DIAG an_alt_numpad_code_types_its_character_once: text {text:?} claimed {claimed}"); + } // Every text stays for now: the begin hook's routing decides which reader gets it (`route::begin`). batch.texts.push(Typed { text, claimed, in_egui: Some(batch.egui_texts) }); batch.egui_texts += 1; @@ -466,8 +469,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec Vec { + let mut claims = focused_claims(record, stage); + claims.extend(stage.captures.iter().map(|(_, chord)| *chord)); + claims +} + +/// Whether the key stage of `input`'s viewport will take a press as a claimed chord (`PressUse::Claimed`), read before +/// the stage runs in the same hook call: the raw-input step leaves such an Alt+digit to its owner instead of folding it +/// into an Alt code (`alt_code::fold`). +pub(crate) fn claims_press(ctx: &Context, input: &RawInput) -> impl Fn(&egui::Event) -> bool + use<> { + let viewport = input.viewport_id; + let os = crate::runtime::windows::os_of(ctx); + let record = focus::record(ctx, viewport); + let (claims, mut layout) = with(ctx, viewport, |stage| (stage_claims(&record, stage), stage.layout.clone())); + layout.set_latin(input.keyboard_layout_is_latin); + let latin = input.latin_layout_chars.clone(); + move |event: &egui::Event| match raw_input::key_event(event, os, &latin) { + KeyEvent::Press { input: pressed, .. } => claims.iter().any(|chord| layout.matches(chord, &pressed, os)), + _ => false, + } +} + /// Updates the keys held after `event`: a press adds its key, a release removes it, and the window losing focus /// (which also reads as the right Alt coming up) forgets them all, since their releases go to another window. fn note_down(down: &mut Vec, event: &KeyEvent) { diff --git a/crates/ui-contract/tests/it/runtime/wheel.rs b/crates/ui-contract/tests/it/runtime/wheel.rs index 93975cd14..63a78a2c6 100644 --- a/crates/ui-contract/tests/it/runtime/wheel.rs +++ b/crates/ui-contract/tests/it/runtime/wheel.rs @@ -227,7 +227,17 @@ fn speed_steps(os: Os, speed: WheelSpeed) -> [Took; 4] { let rerun = Cell::new(false); let mut show = |ui: &mut egui::Ui| { register(ui.ctx(), WheelTaker::new(view, Rect::from_min_size(Pos2::ZERO, Vec2::new(400.0, 300.0))).vertical(true).zoom(true)); - seen.borrow_mut().extend(take_share(ui.ctx(), view)); + let share = take_share(ui.ctx(), view); + if let Some(share) = &share { + eprintln!( + "DIAG the_users_wheel_speed_scales_each_delta_once_and_leaves_zoom_alone: {os:?} {speed:?} frame {} pass {} zoom {} scroll {:?}", + ui.ctx().cumulative_frame_nr(), + ui.ctx().current_pass_index(), + share.zoom, + share.scroll + ); + } + seen.borrow_mut().extend(share); if rerun.get() && ui.ctx().current_pass_index() == 0 { ui.ctx().request_discard("the wheel frame runs twice"); }