From 48d19d281053524a4c794f69db88688d770ddbf4 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Tue, 24 Nov 2026 12:08:24 +0700 Subject: [PATCH] ui-contract: a command fired from a button runs as a command; only a menu's or the palette's fire stands for the key a component claims --- crates/ui-canvas/tests/it/canvas/tool_keys.rs | 17 ++++++++++++++++- .../ui-contract/src/runtime/commands/board.rs | 9 +++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/crates/ui-canvas/tests/it/canvas/tool_keys.rs b/crates/ui-canvas/tests/it/canvas/tool_keys.rs index aab42724d..41eedb0ef 100644 --- a/crates/ui-canvas/tests/it/canvas/tool_keys.rs +++ b/crates/ui-canvas/tests/it/canvas/tool_keys.rs @@ -20,7 +20,7 @@ use craft_ui::dialog::Dialog; use craft_ui::display::Text; use craft_ui::popover::Popover; use craft_ui::primitives::Draw; -use craft_ui::runtime::commands::{self, EditCommands, FireSource}; +use craft_ui::runtime::commands::{self, CommandSet as _, EditCommands, FireSource}; use craft_ui::runtime::frame::take_output; use craft_ui::testing::access::Declared; use craft_ui::testing::focus::{AccessActionKind, key_release}; @@ -169,6 +169,21 @@ fn a_fired_command_whose_key_the_tool_claims_reaches_it_with_nothing_focused() { assert!(take_output(canvas.fixture().ctx()).fired.is_empty(), "no app command ran"); } +/// A command a button fires runs as the button's command, though the tool claims its key: only a menu's or the +/// palette's stands for the key (a toolbar's tool button finishes a gesture the tool holds the keyboard for). +#[trial] +fn a_pointer_fired_command_whose_key_the_tool_claims_runs_as_a_command() { + let mut canvas = ToolFixture::new("Document", Outline::new(true)); + canvas.fixture().settle(); + assert_eq!(canvas.fixture().focused(), None, "nothing is focused"); + take_output(canvas.fixture().ctx()); + assert!(commands::fire(canvas.fixture().ctx(), EditCommands::Delete, FireSource::Pointer), "Delete runs"); + canvas.fixture().settle(); + assert_eq!(canvas.tool().seen, [], "the tool got no Delete"); + let fired = take_output(canvas.fixture().ctx()).fired; + assert!(fired.iter().any(|fired| fired.command.id == EditCommands::Delete.id()), "the app's Delete ran: {fired:?}"); +} + /// A tool that takes every key the host offers it, and the lone Alt tap while `taps`. #[derive(Default)] struct Taker { diff --git a/crates/ui-contract/src/runtime/commands/board.rs b/crates/ui-contract/src/runtime/commands/board.rs index 7af58b6f8..c06b9a868 100644 --- a/crates/ui-contract/src/runtime/commands/board.rs +++ b/crates/ui-contract/src/runtime/commands/board.rs @@ -1731,10 +1731,11 @@ fn queue(ctx: &Context, fired: Fired) { observed.push(fired.command.clone()); } }); - // A command whose key the focused component claims reaches it as that key, as the key's press does; an edit command - // goes to the focused component that lists it; otherwise it goes to the instance that declared it or, with none, - // stays an app command. - if fired.phase != FirePhase::Release { + // A command run from a menu or the palette whose key the focused component claims reaches it as that key, as the + // key's press does (a button's is the button's command, even while a canvas gesture claims every key); an edit + // command goes to the focused component that lists it; otherwise it goes to the instance that declared it or, with + // none, stays an app command. + if fired.phase != FirePhase::Release && matches!(fired.source, FireSource::Menu | FireSource::Palette) { let chords: Vec = bindings(ctx, &fired.command).into_iter().filter(|binding| !binding.trigger.is_double()).map(|binding| binding.trigger.chord).collect(); if keys::fire_claimed(ctx, &chords) {