Keys: keys::keyed reports a pass's key input, withheld presses included; craft-menu's had_input reads it

This commit is contained in:
aiongg
2026-11-12 22:54:16 +00:00
parent 9f2fe6923e
commit 33a389b1bc
4 changed files with 33 additions and 5 deletions
+5 -4
View File
@@ -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, .. })))
}
+3 -1
View File
@@ -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<Key>,
/// 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<Key>,
shortcuts: Shortcuts,
@@ -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<Key> {
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
@@ -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]