ui-contract: a claimed Alt+digit goes to its owner and ends the Alt code; Alt-numpad code types once

This commit is contained in:
Martin Ball
2026-11-18 12:40:08 +00:00
parent 0d37561828
commit f51ab90148
5 changed files with 53 additions and 9 deletions
@@ -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<Event>, 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<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);
@@ -49,7 +51,7 @@ pub(crate) fn fold(entry: &mut AltCodeEntry, events: &mut Vec<Event>, 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<Event>, 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;
+3 -1
View File
@@ -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::<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);
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;
+1 -1
View File
@@ -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;
+28 -2
View File
@@ -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<Arriv
let latin = &input.latin_layout_chars;
let bound = |chord: &Chord| commands::holder(ctx, chord).is_some();
let (batch, visible, later) = with(ctx, viewport, |stage| {
let mut claims = focused_claims(&record, stage);
claims.extend(stage.captures.iter().map(|(_, chord)| *chord));
let claims = stage_claims(&record, stage);
let edits = |command| keys.is_some_and(|keys| keys.edits.contains(command));
stage.layout.set_latin(input.keyboard_layout_is_latin);
let strip = Strip {
@@ -526,6 +528,30 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec<Arriv
(trimmed || events.len() != before, later)
}
/// The chords the key stage takes as claimed (`PressUse::Claimed`): the focused component's (`focused_claims`) and
/// every capture's.
fn stage_claims(record: &focus::FocusedRecord, stage: &Stage) -> Vec<Chord> {
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<Key>, event: &KeyEvent) {
+11 -1
View File
@@ -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");
}