diff --git a/crates/ui-contract/src/runtime/commands/board.rs b/crates/ui-contract/src/runtime/commands/board.rs index 8a826f3e69..9423460061 100644 --- a/crates/ui-contract/src/runtime/commands/board.rs +++ b/crates/ui-contract/src/runtime/commands/board.rs @@ -2251,7 +2251,7 @@ pub(crate) fn begin(ctx: &Context) { } let tap = keys.press(&shortcuts.layout, &shortcut.input, typing, Some((&mut matcher, time, true, &enabled))); if let Some(repeated) = tap.and_then(|tap| press(tap, None, time, Some(shortcut.place))) { - // A host that reads the keys nothing took (`keys::unclaimed`) leaves this repeat to the command. A + // A host that reads the keys nothing took (`keys::leftover_events`) leaves this repeat to the command. A // repeat that fires nothing goes the way of its press: the command's while a fired press holds the // key, else left over, as a declined press is. keys::note_fired(ctx, &shortcut.input, true); @@ -2269,13 +2269,13 @@ pub(crate) fn begin(ctx: &Context) { match (hold, tap) { (Some((key, hold)), tap) => { // The wait fires the tap or the hold for this key in a later event or pass: a host that reads - // the keys nothing took (`keys::unclaimed`) leaves the press, its repeats and its release to it. + // the keys nothing took (`keys::leftover_events`) leaves the press, its repeats and its release to it. keys::note_fired(ctx, &shortcut.input, false); wait = Some(HoldWait { key, hold, since: time, delay, tap, viewport }); } (None, Some(tap)) => { if let Some(pressed) = press(tap, key, time, Some(shortcut.place)) { - // A host that reads the keys nothing took (`keys::unclaimed`) leaves this press to the command. + // A host that reads the keys nothing took (`keys::leftover_events`) leaves this press to the command. keys::note_fired(ctx, &shortcut.input, false); fired.push(pressed); } diff --git a/crates/ui-contract/src/runtime/egui/input.rs b/crates/ui-contract/src/runtime/egui/input.rs index 8c4aa378f2..eb4ad4416c 100644 --- a/crates/ui-contract/src/runtime/egui/input.rs +++ b/crates/ui-contract/src/runtime/egui/input.rs @@ -174,7 +174,7 @@ pub(crate) enum KeyEvent { Other, } -/// A raw event as a host that reads what nothing took (`keys::unclaimed`) sorts it. +/// A raw event as a host that reads what nothing took (`keys::leftover_events`) sorts it. pub(crate) enum Leftover { /// A key press (an auto-repeat when `repeat`) or release. Key { pressed: bool, repeat: bool }, @@ -184,7 +184,7 @@ pub(crate) enum Leftover { Other, } -/// How `unclaimed` sorts `event`. +/// How `leftover_events` sorts `event`. pub(crate) fn leftover(event: &Event) -> Leftover { match event { Event::Key { pressed, repeat, .. } => Leftover::Key { pressed: *pressed, repeat: *repeat }, diff --git a/crates/ui-contract/src/runtime/keys.rs b/crates/ui-contract/src/runtime/keys.rs index 7160329101..868cd39091 100644 --- a/crates/ui-contract/src/runtime/keys.rs +++ b/crates/ui-contract/src/runtime/keys.rs @@ -47,12 +47,10 @@ pub(crate) use intents::{ }; pub use intents::{with_canvas_window_intents, with_window_intents}; pub(crate) use nodes::{redirect_node, register_expanded, register_node}; -#[doc(hidden)] -pub use route::unclaimed; pub(crate) use route::{ activated_member, begin, focus_in_modal_dialog, layout_of, note_fired, owner_at, pasted, pasted_text, queue_edit, routed_to, take_shortcuts, }; -pub use route::{escape_mods, focus_holds}; +pub use route::{escape_mods, focus_holds, leftover_events}; /// A menu-bar key of an in-window bar (spec 9.7). F10 reaches the bar on every OS; Alt keys only off a Mac. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -354,7 +352,7 @@ pub(crate) struct Stage { /// 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, - /// The keys of `fired_down` held over from earlier passes, as `unclaimed` reads this pass's events by them: + /// The keys of `fired_down` held over from earlier passes, as `leftover_events` 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, /// The paste the focused component reads through its clipboard handle. diff --git a/crates/ui-contract/src/runtime/keys/route.rs b/crates/ui-contract/src/runtime/keys/route.rs index a3a061bb34..fa34ccdd4e 100644 --- a/crates/ui-contract/src/runtime/keys/route.rs +++ b/crates/ui-contract/src/runtime/keys/route.rs @@ -534,7 +534,7 @@ pub(crate) fn take_shortcuts(ctx: &Context) -> Shortcuts { } /// Records that the shortcut dispatcher fired a command for the press `input` in the running pass, or holds it for a -/// hold binding's wait, so [`unclaimed`] leaves the press, its auto-repeats until its release, the release and the +/// hold binding's wait, so [`leftover_events`] leaves the press, its auto-repeats until its release, the release and the /// clipboard event that stood for it to the command. With `repeat`, the command fired for an auto-repeat of a press /// the dispatcher left (`RootEnv::shortcuts_repeat`): that repeat alone is the command's, and the press's release goes /// the way of the press. @@ -548,21 +548,25 @@ pub(crate) fn note_fired(ctx: &Context, input: &KeyInput, repeat: bool) { }); } -/// The key, text and clipboard events of the running pass that nothing took: no component of the viewport has -/// keyboard focus (a framework stop or an app's own widget), and the key stage neither stripped an event for itself -/// (Escape, Tab and focus moves, claimed and edit chords, the menu bar's keys) nor handed it to a component -/// (typing, a list's keys), and no widget has read it from egui's input since, nor has the shortcut dispatcher fired a -/// 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. 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. +/// The key, text and clipboard events (`Key`, `Text`, `Copy`, `Cut`, `Paste`, `PasteImage`) of the running pass that +/// nothing took, in the order they arrived, for a host that runs shortcuts of its own 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. Call it once +/// the viewport's content has run. Other events (pointer, IME, window events) are never in it. +/// +/// An event is left over when no component of the viewport has keyboard focus (a framework stop or an app's own +/// widget), the key stage neither took it for itself (Escape, Tab and focus moves, claimed and edit chords, the menu +/// bar's keys) nor handed it to a component (typing, a list's keys), no widget has read it from egui's input since, +/// and the shortcut dispatcher fired no command for it. A press a command fired for holds its key: the key's +/// auto-repeats are the command's too, whether or not they fire it again (`RootEnv::shortcuts_repeat`), and so is its +/// release. The hold ends at any release of the key, wherever it lands, so the next press and release of the key are +/// left over. A press the dispatcher declined (a chord whose commands are all disabled) is left over, and so are its +/// repeats that fire nothing. /// /// 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. -#[doc(hidden)] -pub fn unclaimed(ctx: &Context) -> Vec { +/// as the `Copy`, `Cut` or `Paste` event (`PasteImage` for a pasted image), in its place among the other events. Empty +/// while a component has focus, as that component owns the keys then. The same in every call of a pass: it reads the +/// pass and changes nothing. +pub fn leftover_events(ctx: &Context) -> Vec { let viewport = crate::runtime::egui::pass::current_viewport(ctx); let record = focus::record(ctx, viewport); if record.focused.is_some() || record.foreign || crate::runtime::egui::focus::focused(ctx).is_some() { @@ -625,7 +629,7 @@ pub fn unclaimed(ctx: &Context) -> Vec { left } -/// Puts `clip` in `left` in [`unclaimed`] when no command took the press it stands for (`took`); a press a command +/// Puts `clip` in `left` in [`leftover_events`] 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, left: &mut Vec) { if !took(&clip.input) { diff --git a/crates/ui-contract/tests/it/runtime/leftover_events.rs b/crates/ui-contract/tests/it/runtime/leftover_events.rs new file mode 100644 index 0000000000..af72fe37f9 --- /dev/null +++ b/crates/ui-contract/tests/it/runtime/leftover_events.rs @@ -0,0 +1,40 @@ +//! `runtime::keys::leftover_events`: the keys of a pass that nothing took, for a host that runs shortcuts of its own on +//! a window's input. + +use craft_ui::runtime::commands::{AppCatalog, Binding, CommandDef, CommandInput, Scope}; +use craft_ui::runtime::frame::take_output; +use craft_ui::runtime::keys::leftover_events; +use craft_ui::testing::focus::key_release; +use craft_ui::testing::input::key_press; +use craft_ui::testing::{Frames, trial}; +use egui::{Event, Key, Modifiers, Ui}; +use std::cell::RefCell; + +/// With nothing focused, a host reading the leftover events after the window's content gets the press and release of +/// a key no command is bound to, and neither the press nor the release of a key whose command fired. +#[trial] +fn leftover_events_holds_the_keys_no_command_took() { + let mut frames = Frames::new(); + let binding = Binding::parse("F1").expect("the trial's chord parses"); + let help = CommandDef::new("leftoverTrial.help", "Help", Scope::app()).with_default(binding); + frames.commands(CommandInput::new().catalog(AppCatalog::new(vec![help], 1).expect("the trial's catalog is valid"))); + let left = RefCell::new(Vec::new()); + let mut show = |ui: &mut Ui| { + left.borrow_mut().extend(leftover_events(ui.ctx())); + }; + frames.idle(2, &mut show); + take_output(&frames.ctx); + left.borrow_mut().clear(); + let keys = |events: Vec| -> Vec<(Key, bool)> { + events.into_iter().filter_map(|event| if let Event::Key { key, pressed, .. } = event { Some((key, pressed)) } else { None }).collect() + }; + + frames.frame(vec![key_press(Key::F1, Modifiers::NONE)], &mut show); + frames.frame(vec![key_release(Key::F1, Modifiers::NONE)], &mut show); + assert_eq!(take_output(&frames.ctx).fired.len(), 1, "the command fired"); + assert!(keys(left.take()).is_empty(), "its press and release are the command's"); + + frames.frame(vec![key_press(Key::F4, Modifiers::NONE)], &mut show); + frames.frame(vec![key_release(Key::F4, Modifiers::NONE)], &mut show); + assert_eq!(keys(left.take()), [(Key::F4, true), (Key::F4, false)], "an unbound key's are left over"); +} diff --git a/crates/ui-contract/tests/it/runtime/mod.rs b/crates/ui-contract/tests/it/runtime/mod.rs index 1901607c05..f53d861119 100644 --- a/crates/ui-contract/tests/it/runtime/mod.rs +++ b/crates/ui-contract/tests/it/runtime/mod.rs @@ -11,6 +11,7 @@ pub(crate) mod fingers; pub(crate) mod focus; pub(crate) mod frame; pub(crate) mod latch; +pub(crate) mod leftover_events; pub(crate) mod memory; pub(crate) mod part_request; pub(crate) mod paste; diff --git a/crates/ui-panels/src/docking/floating.rs b/crates/ui-panels/src/docking/floating.rs index 632d049e26..c574d7db59 100644 --- a/crates/ui-panels/src/docking/floating.rs +++ b/crates/ui-panels/src/docking/floating.rs @@ -431,7 +431,7 @@ impl<'a> FloatingGroup<'a> { } let inner = (!minimized).then(|| body(ui, cx)); if native && !ui.is_sizing_pass() && ui.ctx().viewport_id() != viewport { - keys.extend(crate::runtime::keys::unclaimed(ui.ctx())); + keys.extend(crate::runtime::keys::leftover_events(ui.ctx())); if !keys.is_empty() { shown_modifiers = Some(ui.input(|input| input.modifiers)); } diff --git a/docs/contract-api.md b/docs/contract-api.md index 30983c458b..313eabb968 100644 --- a/docs/contract-api.md +++ b/docs/contract-api.md @@ -4157,6 +4157,7 @@ The key stage (spec 9.1, 9.2, 9.3, 9.7): the input hook strips the keys the runt - `craft_ui::runtime::keys::escape_mods`: `pub fn escape_mods(ctx: &Context) -> Option`. The Alt and Shift held at the press of the pass's last Escape that became a `Cancel`; `None` when the pass had none. - `craft_ui::runtime::keys::focus_holds`: `pub fn focus_holds(ctx: &Context, chord: Chord) -> bool`. Whether the focused component of the running viewport holds a press of `chord`, as routing decides for the press: - `craft_ui::runtime::keys::held`: `pub fn held(ctx: &Context) -> Vec`. The keys down in the running viewport this pass: the key stage's `down` (withheld presses included), plus each key +- `craft_ui::runtime::keys::leftover_events`: `pub fn leftover_events(ctx: &Context) -> Vec`. The key, text and clipboard events (`Key`, `Text`, `Copy`, `Cut`, `Paste`, `PasteImage`) of the running pass that - `craft_ui::runtime::keys::textless_paste`: `pub fn textless_paste(ctx: &Context) -> bool`. Whether the running viewport's input hook read a paste with no text in this pass: the paste chord came while the - `craft_ui::runtime::keys::with_window_intents`: `pub fn with_window_intents(ctx: &Context, rtl: bool, take: impl FnOnce(&mut Intents) -> bool) -> bool`. Offers the window's intents to `take` while nothing in the window has framework focus or the focused stop is the diff --git a/docs/ui-docking.md b/docs/ui-docking.md index 7b710cb6b5..32830c9922 100644 --- a/docs/ui-docking.md +++ b/docs/ui-docking.md @@ -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. 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. +`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. Each floating window reads its events with `runtime::keys::leftover_events` once its content has run; a host that shows content in OS windows of its own reads them the same way. The host handles these `DockEvent`s: