From cb414bef729171d9e854d94dc21d31d9bf96b33a Mon Sep 17 00:00:00 2001 From: skylartaylor Date: Tue, 24 Nov 2026 15:42:43 +0100 Subject: [PATCH] AppCatalog::shown_only: chords the app dispatches itself An app whose own keymap dispatches some of its catalog commands' keys (a tool rail's tools) had to clear their default chords before handing them to the board, so the rail's buttons and tooltips showed no shortcut. shown_only(ids) keeps the chords in the board's keymap, so surfaces resolve and show them, while the key stage, runs_now and the declined/dimmed report pass over those commands as if unbound: the press fires nothing on the board and stays in egui's input for the app. Trial: a_shown_only_chord_shows_on_its_button_and_its_press_stays_in_the_input_unfired. --- .../ui-contract/src/runtime/commands/board.rs | 27 ++++++++++++---- .../ui-contract/src/runtime/commands/mod.rs | 26 +++++++++++++-- .../ui-shell/tests/it/commands/app_catalog.rs | 32 +++++++++++++++++++ docs/contract-api.md | 1 + 4 files changed, 78 insertions(+), 8 deletions(-) diff --git a/crates/ui-contract/src/runtime/commands/board.rs b/crates/ui-contract/src/runtime/commands/board.rs index c06b9a8686..bb65aa40bb 100644 --- a/crates/ui-contract/src/runtime/commands/board.rs +++ b/crates/ui-contract/src/runtime/commands/board.rs @@ -666,6 +666,12 @@ impl Board { self.instances.get(id).is_some_and(|i| self.live(ctx, i)) } + /// Whether the app dispatches the keys of `command` itself ([`AppCatalog::shown_only`]): the board shows its chord + /// but passes over its bindings when it matches a key. Only a command the linked sets do not define. + fn leaves_key(&self, command: &CommandRef) -> bool { + self.app.as_ref().is_some_and(|app| app.is_shown_only(command)) && self.registry.get(&command.id).is_none() + } + /// The definition of `command`: the linked sets' by id, else the frame's app catalog's. fn def(&self, command: &CommandRef) -> Option<&CommandDef> { self.registry.get(&command.id).or_else(|| self.app.as_ref()?.get(command)) @@ -1289,7 +1295,7 @@ pub(crate) fn runs_now(ctx: &Context, viewport: ViewportId, chord: &Chord) -> bo board.key_scopes(&chain) }); let Some(keys) = keys else { return false }; - let enabled = |command: &CommandRef| resolve_from(ctx, command, None).0.enabled; + let enabled = |command: &CommandRef| !with(ctx, |board| board.leaves_key(command)) && resolve_from(ctx, command, None).0.enabled; keys.matching(false, |active, outer| !matches!(keys.keymap.lookup_where(chord, false, active, outer, &enabled), craft_keymap::Lookup::None)) } @@ -2208,8 +2214,14 @@ pub(crate) fn begin(ctx: &Context) { }; // A binding whose command is disabled where the press lands (a window command with no floating window, a canvas's // view command it does not offer) is passed over: the press runs the next binding of its chord, in an outer scope, - // or of its next chord candidate, as if the disabled one were unbound. + // or of its next chord candidate, as if the disabled one were unbound. So is a binding of a command whose keys the + // app dispatches itself (`AppCatalog::shown_only`), whatever its state. + let leaves_key = |command: &CommandRef| with(ctx, |board| board.leaves_key(command)); + let board_runs = |hit: &Hit| !leaves_key(&hit.command); let enabled = |command: &CommandRef| { + if leaves_key(command) { + return false; + } let (_, instance) = with(ctx, |board| board.target_of(ctx, command, &board.here)); resolve_in(ctx, command, instance, Reach::Focus, None, Reads::default()).0.enabled }; @@ -2247,7 +2259,9 @@ pub(crate) fn begin(ctx: &Context) { // completes no double press and settles no wait, and the key a momentary or tool command holds stays // held by its press. The key of a wait or of a hold binding repeats nothing. let typing = typing_at(shortcut); - if wait.as_ref().is_some_and(|wait| Some(wait.key) == key) || keys.hold(&shortcuts.layout, &shortcut.input, typing).is_some() { + if wait.as_ref().is_some_and(|wait| Some(wait.key) == key) + || keys.hold(&shortcuts.layout, &shortcut.input, typing).is_some_and(|hold| board_runs(&hold)) + { continue; } let tap = keys.press(&shortcuts.layout, &shortcut.input, typing, Some((&mut matcher, time, true, &enabled))); @@ -2260,15 +2274,16 @@ pub(crate) fn begin(ctx: &Context) { let typing = typing_at(shortcut); let hold = key .filter(|_| !typing && !modal_at_start) - .and_then(|key| keys.hold(&shortcuts.layout, &shortcut.input, typing).map(|hold| (key, hold))); + .and_then(|key| keys.hold(&shortcuts.layout, &shortcut.input, typing).filter(board_runs).map(|hold| (key, hold))); let tap = keys.press(&shortcuts.layout, &shortcut.input, typing, Some((&mut matcher, time, false, &enabled))); match (hold, tap) { (Some((key, hold)), tap) => wait = Some(HoldWait { key, hold, since: time, delay, tap, viewport }), (None, Some(tap)) => fired.extend(press(tap, key, time, Some(shortcut.place))), // A chord that binds only disabled commands runs nothing; the app hears of the first one, to say - // why it did not run as a menu row's hint does, and may receive it as declined. + // why it did not run as a menu row's hint does, and may receive it as declined. A chord the app + // dispatches itself is the app's: it hears nothing of it here. (None, None) => { - if let Some(hit) = keys.press(&shortcuts.layout, &shortcut.input, typing, None) { + if let Some(hit) = keys.press(&shortcuts.layout, &shortcut.input, typing, None).filter(board_runs) { let (app, instance) = with(ctx, |board| board.target_of(ctx, &hit.command, &board.here)); let resolved = resolve_once(ctx, &hit.command, instance, Reach::Focus, None); decline(ctx, hit.clone(), app, &resolved, time); diff --git a/crates/ui-contract/src/runtime/commands/mod.rs b/crates/ui-contract/src/runtime/commands/mod.rs index 419f64f0eb..86fcf2666f 100644 --- a/crates/ui-contract/src/runtime/commands/mod.rs +++ b/crates/ui-contract/src/runtime/commands/mod.rs @@ -56,7 +56,7 @@ use craft_behavior::edit_history::Step; use craft_keymap::{Chord, Overrides}; use egui::{Context, Id, ViewportId}; use std::borrow::Cow; -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::sync::Arc; /// Commands an app defines at run time (plug-ins, its tool list), all in the app scope ([`Scope::app`]). They join @@ -71,6 +71,8 @@ pub struct AppCatalog { replaced: Arc<[(String, Vec)]>, /// Which chords fire while a text component has the keyboard (`typing_rule`); `None` keeps the keymap's default. typing: Option, + /// The ids of the catalog's commands whose keys the app dispatches itself (`shown_only`). + shown_only: Arc>, } impl AppCatalog { @@ -84,7 +86,7 @@ impl AppCatalog { } registry::index_id(&mut index, &def.command.key_text(), at)?; } - Ok(AppCatalog { defs: defs.into(), index: Arc::new(index), generation, replaced: Arc::from([]), typing: None }) + Ok(AppCatalog { defs: defs.into(), index: Arc::new(index), generation, replaced: Arc::from([]), typing: None, shown_only: Arc::default() }) } /// Gives the linked set's command `id` the default chords `bindings` in place of the set's own; an empty list drops @@ -111,6 +113,26 @@ impl AppCatalog { self } + /// Makes the chords of the catalog's commands `ids` shown only: the app dispatches their keys with a keymap of its + /// own. Menu rows, tooltips and buttons still show each command's chord (its bindings, under the overrides, as + /// [`ResolvedCommand::chord`] reads them), but the board passes over the command when it matches a key, as if the + /// command had no binding, so the press fires nothing on the board and stays in egui's input for the app. The + /// command still fires from a menu row, a button, the palette or automation. Later calls add to the earlier ones. + /// An id the linked sets define stays theirs and is ignored here. The board reads the ids every frame, so a change + /// needs no new [`generation`](Self::generation). + #[must_use] + pub fn shown_only>(mut self, ids: impl IntoIterator) -> Self { + let mut shown_only = (*self.shown_only).clone(); + shown_only.extend(ids.into_iter().map(Into::into)); + self.shown_only = Arc::new(shown_only); + self + } + + /// Whether the app dispatches the keys of `command` itself ([`shown_only`](Self::shown_only)). + pub(crate) fn is_shown_only(&self, command: &CommandRef) -> bool { + self.shown_only.contains(command.id.as_str()) + } + /// The default chords the app gives the linked set's command `id`, when it replaced them. pub(crate) fn replaced_defaults(&self, id: &str) -> Option<&[Binding]> { self.replaced.iter().find(|(at, _)| at == id).map(|(_, bindings)| bindings.as_slice()) diff --git a/crates/ui-shell/tests/it/commands/app_catalog.rs b/crates/ui-shell/tests/it/commands/app_catalog.rs index 8b4b271b4c..47055fb799 100644 --- a/crates/ui-shell/tests/it/commands/app_catalog.rs +++ b/crates/ui-shell/tests/it/commands/app_catalog.rs @@ -2,6 +2,7 @@ //! rules every app command follows, and a frame builds a keymap only for a catalog generation it has not built. // The catalog is given through `Frames::commands`, which keeps it for every later frame. +use craft_ui::buttons::{Button, ButtonChord}; use craft_ui::contract::{Bind, Props as _, Show as _, Tracked}; use craft_ui::runtime::commands::{ AppCatalog, Binding, CommandDef, CommandInput, CommandKind, CommandRef, CommandState, CommandStates, EditCommands, FirePhase, Scope, @@ -99,6 +100,37 @@ fn an_app_catalog_command_fires_by_its_chord_and_a_focused_text_field_keeps_a_ty assert_eq!(*name.borrow(), "q", "the focused field types the letter"); } +/// A catalog command whose chord is shown only (`AppCatalog::shown_only`), as a tool whose key the app's own keymap +/// dispatches: a button bound to it shows the chord, and a press of the chord fires nothing on the board and stays in +/// egui's input, where the app reads it. +#[trial] +fn a_shown_only_chord_shows_on_its_button_and_its_press_stays_in_the_input_unfired() { + let key_in_input = Cell::new(false); + let mut show = |ui: &mut Ui| { + AppShell::new(WindowKey::main()) + .body(|ui, _, _| { + let _ = Button::command(CommandRef::new(BRUSH)).chord(ButtonChord::Trailing).show(ui); + if ui.input(|input| input.key_pressed(Key::J)) { + key_in_input.set(true); + } + }) + .show(ui, &mut ()); + }; + let brush = def(BRUSH, "Brush", "J").with_kind(CommandKind::Tool { group: None }); + let mut frames = Frames::new(); + frames.commands(CommandInput::new().catalog(AppCatalog::new(vec![brush], 1).expect("the trial's catalog is valid").shown_only([BRUSH]))); + frames.idle(3, &mut show); + let painted: String = frames.texts().iter().map(|text| text.text.as_str()).collect(); + let (label, chord) = (painted.find("Brush"), painted.rfind('J')); + assert!(label.is_some() && label < chord, "the button shows the shown-only chord after its label: {painted:?}"); + + let _ = fired(&frames); + frames.frame(vec![key_press(Key::J, Modifiers::NONE)], &mut show); + assert!(key_in_input.get(), "the press stays in egui's input for the app's keymap"); + frames.frame(vec![key_release(Key::J, Modifiers::NONE)], &mut show); + assert!(fired(&frames).is_empty(), "the board fires nothing for a shown-only chord"); +} + /// A key's press and its release fire with the input times they arrived at, whichever frame runs them: a hold is /// read from the key's own times. Keys behind a pointer press wait for the next frame (one press per pass) and keep /// the time they arrived at. diff --git a/docs/contract-api.md b/docs/contract-api.md index 30983c458b..eb535c504b 100644 --- a/docs/contract-api.md +++ b/docs/contract-api.md @@ -3521,6 +3521,7 @@ The command board: the registry and typed sets, per-frame state, the queue, firi - `pub fn new(defs: Vec, generation: u64) -> Result`. A catalog of `defs`; `generation` changes when the app's rows do. Refuses two defs with one id (`Duplicate`) and a - `pub fn replace_defaults(mut self, id: impl Into, bindings: Vec) -> Self`. Gives the linked set's command `id` the default chords `bindings` in place of the set's own; an empty list drops - `pub fn typing_rule(mut self, rule: TypingRule) -> Self`. Sets which chords fire while a text component has the keyboard. The keymap's default, + - `pub fn shown_only>(mut self, ids: impl IntoIterator) -> Self`. Makes the chords of the catalog's commands `ids` shown only: the app dispatches their keys with a keymap of its - `pub fn generation(&self) -> u64`. The app's version of the catalog. The board rebuilds the keymap when it differs from the last frame's, and never - `craft_ui::runtime::commands::CommandGate`: `pub struct CommandGate`. The app's one gate over commands: a dialog is open, a workspace is not ready, a recorder has the keys or an - `pub fn new(props: P, allows: fn(&P, &CommandRef) -> bool) -> Self`. A gate that lets through the commands `allows` accepts under `props`.