Keys: the hold of a key a command fired for ends at any release of the key

The key stage now keeps fired_down: it drops a key at any release of it, whatever has focus, at a press of it that is no repeat (the earlier release went to another window) and when the window loses focus. unclaimed only reads the hold as it stood before the pass's events, so a release that lands in a focused field or in another window no longer swallows the next unrelated release of that key. Trial: a_key_a_command_fired_for_in_a_native_float_and_released_elsewhere_forwards_its_next_press_and_release.
This commit is contained in:
skylartaylor committed 2026-11-24 14:57:32 +01:00
1 parent 74193afa18
commit ebd722399c
5 files changed
+100 -13

No files matched your search

+6 -1
View File
@@ -350,8 +350,13 @@ pub(crate) struct Stage {
/// The presses the shortcut dispatcher fired a command for in this pass (`note_fired`), each `true` when the command
/// fired for an auto-repeat.
fired: Vec<(KeyInput, bool)>,
/// The keys whose press a command fired and whose release has not come yet: the release is the command's too.
/// The keys whose press a command fired and whose release has not come yet: the release is the command's too. The
/// key stage drops a key at any release of it, whatever has focus, at a press of it that is no repeat (the earlier
/// release went to another window) and when the window loses focus.
fired_down: Vec<Key>,
/// The keys of `fired_down` held over from earlier passes, as `unclaimed` reads this pass's events by them:
/// `fired_down` before this pass's events, less each key pressed again (no repeat) in this pass.
fired_before: Vec<Key>,
/// The paste the focused component reads through its clipboard handle.
pasted: Option<(Id, ClipContent)>,
/// Edit commands fired from the queue for the focused component.
@@ -639,6 +639,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec<Arriv
stage.keyed = false;
stage.stirred = false;
stage.textless_paste = false;
stage.fired_before.clone_from(&stage.fired_down);
});
return (removed || trimmed, Vec::new());
}
@@ -697,6 +698,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec<Arriv
stage.keyed = false;
stage.stirred = false;
stage.textless_paste = false;
stage.fired_before.clone_from(&stage.fired_down);
let mut place = 0;
let ime_presses = std::mem::take(&mut stage.ime_presses);
let press_next: Vec<bool> = (0..events.len())
@@ -739,6 +741,11 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec<Arriv
};
let down = stage.down.clone();
note_down(&mut stage.down, &read);
note_fired_up(&mut stage.fired_down, &mut stage.fired_before, &read);
if batch.focus_lost {
// The releases of the keys held now go to another window.
stage.fired_down.clear();
}
let arrival = Arrival {
at: arrived.at,
ime_next: ime_presses.contains(&arrived.event),
@@ -823,6 +830,21 @@ fn note_down(down: &mut Vec<Key>, event: &KeyEvent) {
}
}
/// Ends the hold of a key a command fired for (`Stage::fired_down`) at any release of the key, whatever has focus, and
/// at a press of it that is no repeat, as the release of the earlier press went to another window: the next release
/// of that key is no command's. Such a press ends it for this pass's events too (`Stage::fired_before`).
fn note_fired_up(fired_down: &mut Vec<Key>, fired_before: &mut Vec<Key>, event: &KeyEvent) {
match event {
KeyEvent::Press { input, repeat: false, .. } => {
let pressed = input.physical.or(input.typed);
fired_down.retain(|key| Some(*key) != pressed);
fired_before.retain(|key| Some(*key) != pressed);
}
KeyEvent::Release { input } => fired_down.retain(|key| Some(*key) != input.physical.or(input.typed)),
_ => {}
}
}
/// The keys down in the running viewport this pass: the key stage's `down` (withheld presses included), plus each key
/// automation holds there that is not already listed. It gives the same answer on every pass of a frame, and is a
/// guard, never an event.
+27 -11
View File
@@ -1,7 +1,7 @@
//! The begin hook's routing: key presses, shortcuts and AccessKit requests to the intents of their owners.
use super::input::chrome_toggle_of;
use super::{Batch, EscapePress, Item, MenuKey, Node, Nudge, Press, Routed, Shortcuts, Source, Taken, current, with};
use super::{Batch, EscapePress, Item, LeftClip, MenuKey, Node, Nudge, Press, Routed, Shortcuts, Source, Taken, current, with};
use crate::runtime::commands::{self, ClipContent, FirePhase};
use crate::runtime::egui::input::{Action, ActionKind, Leftover, leftover};
use crate::runtime::focus::{self, StopRec};
@@ -555,7 +555,9 @@ pub(crate) fn note_fired(ctx: &Context, input: &KeyInput, repeat: bool) {
/// command for it (`note_fired`) or for the press of a key still held, whose auto-repeats are the command's too, whether
/// or not they fire it again. A press the dispatcher declined is left over, and so are its repeats that fire nothing. Call
/// it once the viewport's content has run, for a host that runs its own shortcuts on the input of a window the
/// framework gives it no commands for: an OS window of a dock, whose keys egui hands to that window only.
/// framework gives it no commands for: an OS window of a dock, whose keys egui hands to that window only. The release of
/// a key a command took is the command's only while that press holds it: the hold ends at any release of the key,
/// wherever focus is, so the next press and release of the key are left over.
///
/// A clipboard chord the key stage turns into a shortcut press, which no component lists the command of, comes back
/// as the `Copy`, `Cut` or `Paste` event (`PasteImage` for a pasted image). Empty while a component has focus, as that component owns the keys then.
@@ -567,12 +569,14 @@ pub fn unclaimed(ctx: &Context) -> Vec<Event> {
return Vec::new();
}
let platform = craft_keymap::egui::platform(ctx);
// The keys whose press a command took in an earlier pass and that are still held as this pass's events begin. The
// key stage keeps the hold itself (`note_fired_up`), so this reads it and leaves it as it is.
let (fired, clips, passed, mut down) =
current(ctx, |stage| (stage.fired.clone(), stage.left_clips.clone(), stage.passed.clone(), std::mem::take(&mut stage.fired_down)));
current(ctx, |stage| (stage.fired.clone(), stage.left_clips.clone(), stage.passed.clone(), stage.fired_before.clone()));
let key_of = |input: &KeyInput| input.physical.or(input.typed);
let same = |input: &KeyInput, other: &KeyInput| key_of(input) == key_of(other) && input.mods == other.mods;
// The clipboard events whose press no command took, in the order they arrived.
let mut clips = clips.into_iter().filter(|clip| !fired.iter().any(|(fired, by_repeat)| !by_repeat && same(fired, &clip.input))).peekable();
let took = |input: &KeyInput| fired.iter().any(|(fired, by_repeat)| !by_repeat && same(fired, input));
let mut clips = clips.into_iter().peekable();
let events = ctx.input(|input| input.events.clone());
let mut left = Vec::with_capacity(events.len());
// The place in `passed` after the last event found there.
@@ -588,12 +592,16 @@ pub fn unclaimed(ctx: &Context) -> Vec<Event> {
None => from,
};
while let Some(clip) = clips.next_if(|clip| clip.at <= place) {
left.push(clip.event);
leave_clip(clip, &took, &mut down, &mut left);
}
match leftover(&event) {
Leftover::Key { pressed, repeat } => {
let Some(input) = KeyInput::from_egui(&event, platform) else { continue };
if pressed && fired.iter().any(|(fired, by_repeat)| *by_repeat == repeat && same(fired, &input)) {
// A press a command took holds its key until its release.
if let Some(key) = key_of(&input).filter(|key| !repeat && !down.contains(key)) {
down.push(key);
}
continue;
}
// A later pass's repeat of a key a command took at its press is the command's whether or not the repeat
@@ -611,14 +619,22 @@ pub fn unclaimed(ctx: &Context) -> Vec<Event> {
Leftover::Other => {}
}
}
left.extend(clips.map(|clip| clip.event));
current(ctx, |stage| {
// What this pass did not release stays down for a later pass's release.
stage.fired_down = down;
});
for clip in clips {
leave_clip(clip, &took, &mut down, &mut left);
}
left
}
/// Puts `clip` in `left` in [`unclaimed`] when no command took the press it stands for (`took`); a press a command
/// took holds its key in `down` until its release, as a key's own press does.
fn leave_clip(clip: LeftClip, took: impl Fn(&KeyInput) -> bool, down: &mut Vec<Key>, left: &mut Vec<Event>) {
if !took(&clip.input) {
left.push(clip.event);
} else if let Some(key) = clip.input.physical.or(clip.input.typed).filter(|key| !down.contains(key)) {
down.push(key);
}
}
/// Asks the focused component of the running viewport to take `command` as `Intent::Edit` in the next pass.
/// `false` when it does not list the command. A paste asks the platform for the clipboard text, which arrives as
/// the paste event that becomes the intent. Focus is read as it is now: a menu pick returns focus in its pass
@@ -1506,6 +1506,50 @@ fn a_held_key_a_command_fired_for_in_a_native_float_forwards_none_of_its_repeats
}
}
/// The hold of a key a command fired for in a native float's own OS window ends at the key's release wherever it lands:
/// released in the main window, it leaves the float's next press and release of that key (with Shift, which no command
/// is bound to) forwarded, as they are no command's.
#[trial]
fn a_key_a_command_fired_for_in_a_native_float_and_released_elsewhere_forwards_its_next_press_and_release() {
let windows = Windows::new();
windows.root_info(info(true, ROOT_AT));
windows.child_info(info(true, PALETTE_AT));
let binding = Binding::parse("F1").expect("the trial's chord parses");
let help = CommandDef::new("nativeKeysTrial.help", "Help", Scope::app()).with_default(binding);
let catalog = AppCatalog::new(vec![help], 1).expect("the trial's catalog is valid");
windows.commands(CommandInput::new().catalog(catalog));
let mut workspace = Arc::new(Workspace::new(Layout::new(None, vec![float()])));
let mut frame = |root: Vec<Event>, child: Vec<Event>| {
let mut forwarded = Vec::new();
windows.frame(root, child, |ui| {
let config = DockConfig::default().native_windows(true);
forwarded = DockArea::new("Native dock", Bind::bound(&mut workspace), &Specs::with(Permissions::ALL), &mut Palettes, config)
.show(ui)
.forwarded_events;
});
let fired = take_output(&windows.ctx).fired.len();
// The key and whether it went down: egui may still count the key down in the float, and read its next press as
// a repeat.
let keys: Vec<(Key, bool)> =
forwarded.iter().filter_map(|event| if let Event::Key { key, pressed, .. } = event { Some((*key, *pressed)) } else { None }).collect();
(keys, fired)
};
for _ in 0..5 {
frame(vec![], vec![]);
}
take_output(&windows.ctx);
let none: Vec<(Key, bool)> = Vec::new();
assert_eq!(frame(vec![], vec![key_press(Key::F1, Modifiers::NONE)]), (none.clone(), 1), "the command fires, and its press is its own");
frame(vec![key_release(Key::F1, Modifiers::NONE)], vec![]);
assert_eq!(
frame(vec![], vec![key_press(Key::F1, Modifiers::SHIFT)]),
(vec![(Key::F1, true)], 0),
"the release in the main window ended the hold: the next press of the key is forwarded"
);
assert_eq!(frame(vec![], vec![key_release(Key::F1, Modifiers::SHIFT)]), (vec![(Key::F1, false)], 0), "and so is its release");
}
/// A clipboard chord no command takes in a native float's own OS window is forwarded as its clipboard event in its place
/// among the window's other leftover events: a Paste and a typed text in one pass are forwarded in the order they
/// arrived, whichever comes first.
+1 -1
View File
@@ -80,7 +80,7 @@ A panel in a column collapsed to icons shows in its flyout (`Layout::opens_in_fl
`DockAreaOutput` holds the `actions` taken, the bodies' `requested` actions, the `events` for the host, the `focused` and `picked` panels, `edit`, `float_space`, `columns`, the OS `windows` the floating windows showed in (by a panel of each), `window_files`, `forwarded_events` and `forwarded_modifiers`, and `error`: an `InvalidGeometry` when the workspace fails `validate`, in which case the area draws nothing.
`forwarded_events` holds the key, text and clipboard events (`Key`, `Text`, `Copy`, `Cut`, `Paste`, `PasteImage`) of the floating windows that show in OS windows of their own and that nothing took this pass, and `forwarded_modifiers` the modifiers held in the window that forwarded the last of them. egui gives each OS window only its own input, so a host that runs shortcuts from its main window's input runs them on these as well, to keep them working while a floating window has the focus. The key stage's own keys (Escape, Tab and focus moves, claimed and edit chords) and what the window's widgets read are not in them, and the key of a command the shortcut dispatcher fired is not forwarded too: neither its press nor, while it is held, its repeats (whether or not `RootEnv::shortcuts_repeat` runs the command again on them), nor its release. A press that starts a hold binding's wait is the dispatcher's in the same way. A press the dispatcher declines, a chord whose commands are all disabled, is forwarded, and its repeats that run nothing are too. A window where a component has keyboard focus forwards nothing: its keys are that component's. Floating windows in the app window forward nothing, as their keys are the app window's.
`forwarded_events` holds the key, text and clipboard events (`Key`, `Text`, `Copy`, `Cut`, `Paste`, `PasteImage`) of the floating windows that show in OS windows of their own and that nothing took this pass, and `forwarded_modifiers` the modifiers held in the window that forwarded the last of them. egui gives each OS window only its own input, so a host that runs shortcuts from its main window's input runs them on these as well, to keep them working while a floating window has the focus. The key stage's own keys (Escape, Tab and focus moves, claimed and edit chords) and what the window's widgets read are not in them, and the key of a command the shortcut dispatcher fired is not forwarded too: neither its press nor, while it is held, its repeats (whether or not `RootEnv::shortcuts_repeat` runs the command again on them), nor its release. The hold ends at any release of the key, wherever it lands (in a focused field, or in another window), so the window's next press and release of that key are forwarded. A press that starts a hold binding's wait is the dispatcher's in the same way. A press the dispatcher declines, a chord whose commands are all disabled, is forwarded, and its repeats that run nothing are too. A window where a component has keyboard focus forwards nothing: its keys are that component's. Floating windows in the app window forward nothing, as their keys are the app window's.
The host handles these `DockEvent`s: