From 33a389b1bcfbb66b99075449938b0f4b95b583cc Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 22:50:46 +0000 Subject: [PATCH] Keys: keys::keyed reports a pass's key input, withheld presses included; craft-menu's had_input reads it --- crates/menu/src/driver.rs | 9 +++++---- crates/ui-contract/src/runtime/keys.rs | 4 +++- crates/ui-contract/src/runtime/keys/input.rs | 9 +++++++++ crates/ui-contract/tests/it/collection/list.rs | 16 ++++++++++++++++ 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/crates/menu/src/driver.rs b/crates/menu/src/driver.rs index 3e107092e..15e6f0c9c 100644 --- a/crates/menu/src/driver.rs +++ b/crates/menu/src/driver.rs @@ -485,9 +485,10 @@ pub(crate) fn native_ok(c: &Chord) -> bool { c.has_command() } -/// A key press or click this frame. It may have changed state an app's menu hash doesn't list, -/// so apps fold it into the `state` passed to [`NativeMenu::sync_with`], for example as -/// `had_input(ctx).then_some(ctx.cumulative_frame_nr())`. +/// A key press, typed text or click this frame. It may have changed state an app's menu hash +/// doesn't list, so apps fold it into the `state` passed to [`NativeMenu::sync_with`], for +/// example as `had_input(ctx).then_some(ctx.cumulative_frame_nr())`. Keys come from the key +/// stage (`keys::keyed`), which also counts the presses a focused component took from egui. pub fn had_input(ctx: &egui::Context) -> bool { - ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Key { pressed: true, .. } | egui::Event::PointerButton { pressed: false, .. }))) + craft_ui::runtime::keys::keyed(ctx) || ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::PointerButton { pressed: false, .. }))) } diff --git a/crates/ui-contract/src/runtime/keys.rs b/crates/ui-contract/src/runtime/keys.rs index d731f7cf7..09bf53c39 100644 --- a/crates/ui-contract/src/runtime/keys.rs +++ b/crates/ui-contract/src/runtime/keys.rs @@ -27,7 +27,7 @@ pub use claims::take_claimed; /// The types `Intents` reads and returns, for an app that reads the window's intents (`with_window_intents`). pub use craft_behavior::{AccessRequest, Intent, MoveMods, NavMove, PartRef}; #[doc(hidden)] -pub use input::down; +pub use input::{down, keyed}; pub(crate) use input::{input_hook, queue_nav, take_menu_keys, take_nav}; pub use intents::Intents; #[doc(hidden)] @@ -198,6 +198,8 @@ pub(crate) struct Stage { alt: AltTrack, /// The keys down now, as the input hook read them before it withheld any press. down: Vec, + /// The input hook read a key press, typed text or a clipboard event in this pass, withheld ones included. + keyed: bool, /// The keys whose press the strip took and whose release it is still to drop. consumed: Vec, shortcuts: Shortcuts, diff --git a/crates/ui-contract/src/runtime/keys/input.rs b/crates/ui-contract/src/runtime/keys/input.rs index ebea9c8d0..2d57eef12 100644 --- a/crates/ui-contract/src/runtime/keys/input.rs +++ b/crates/ui-contract/src/runtime/keys/input.rs @@ -303,6 +303,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput) -> bool { if press { overlay::note_dropped_press(ctx, viewport); } + with(ctx, viewport, |stage| stage.keyed = false); return removed || trimmed; } let os = crate::runtime::windows::os_of(ctx); @@ -340,9 +341,11 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput) -> bool { }; let mut batch = Batch::default(); let mut visible = None; + stage.keyed = false; input.events.retain(|event| { let read = raw_input::key_event(event, os, &latin); note_down(&mut stage.down, &read); + stage.keyed |= matches!(read, KeyEvent::Press { .. } | KeyEvent::Text(_) | KeyEvent::Clip { .. }); visible = match &read { KeyEvent::PointerPress => Some(false), KeyEvent::Press { .. } | KeyEvent::Text(_) => Some(true), @@ -386,6 +389,12 @@ pub fn down(ctx: &Context) -> Vec { current(ctx, |stage| stage.down.clone()) } +/// Whether the running viewport's input hook read a key press, typed text or a clipboard event in this pass, the ones +/// the key stage took from egui's input included: an app that redraws state after input asks this, not egui's events. +pub fn keyed(ctx: &Context) -> bool { + current(ctx, |stage| stage.keyed) +} + /// Takes the Tab and F6 moves the input hook recorded, for the focus service's begin hook. The pass's root env is /// installed by now, so a bare Tab that passes the chrome gate and has a chrome command bound to it becomes that /// command's toggle for the running viewport; any other Tab moves focus. A repeat of a toggling press is dropped: it diff --git a/crates/ui-contract/tests/it/collection/list.rs b/crates/ui-contract/tests/it/collection/list.rs index dfa1b7f80..855c409dc 100644 --- a/crates/ui-contract/tests/it/collection/list.rs +++ b/crates/ui-contract/tests/it/collection/list.rs @@ -303,6 +303,22 @@ fn a_text_after_a_release_takes_no_press_with_it() { assert!(saw.borrow().contains(&Key::F5), "and the app reads the press that typed nothing"); } +/// A press the list takes from egui's input is still key input of its pass (`keys::keyed`); a pass with only a +/// release has none. +#[trial] +fn a_press_the_list_takes_is_key_input_of_its_pass() { + let words = Words::flat(&labels()); + let seen = Seen::default(); + let mut frames = Frames::new(); + let mut show = |ui: &mut Ui| show_words(ui, List::new(SelectMode::Single), &words, &seen); + frames.idle(2, &mut show); + focus_list(&mut frames, &seen, &mut show); + frames.frame(vec![input::key_press(Key::ArrowDown, Modifiers::NONE)], &mut show); + assert!(keys::keyed(&frames.ctx), "the pass of the press had key input"); + frames.frame(vec![craft_ui::testing::focus::key_release(Key::ArrowDown, Modifiers::NONE)], &mut show); + assert!(!keys::keyed(&frames.ctx), "the pass of the release had none"); +} + /// A list without type-ahead leaves typed letters to the app's tool keys: the lead stays where it is, and the letter's /// press reaches the app. #[trial]