From 788c89b024a3b45d038c2e901cbd80f7fa384bbd Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 22:13:26 +0000 Subject: [PATCH] Commands: a key command closes the open popups, as an app shortcut through close_popups does --- .../ui-contract/src/runtime/commands/board.rs | 7 ++- crates/ui-contract/src/runtime/overlay.rs | 7 +-- .../tests/it/pickers/select_list.rs | 44 ++++++++++++++++++- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/crates/ui-contract/src/runtime/commands/board.rs b/crates/ui-contract/src/runtime/commands/board.rs index 063bef5e5..b20c91459 100644 --- a/crates/ui-contract/src/runtime/commands/board.rs +++ b/crates/ui-contract/src/runtime/commands/board.rs @@ -920,9 +920,14 @@ pub(crate) fn begin(ctx: &Context) { } } with(ctx, |board| board.matcher = matcher); - if returning.is_some() && fired.iter().any(|fired| fired.phase == FirePhase::Press) { + let pressed = fired.iter().any(|fired| fired.phase == FirePhase::Press); + if returning.is_some() && pressed { focus::give_back_bar(ctx); } + // A command a key runs closes the open popups (menus, dropdowns, lists), as a menu pick does. + if pressed { + overlay::close_popups(ctx); + } for fired in fired { queue(ctx, fired); } diff --git a/crates/ui-contract/src/runtime/overlay.rs b/crates/ui-contract/src/runtime/overlay.rs index 4a73825e5..eab14afbd 100644 --- a/crates/ui-contract/src/runtime/overlay.rs +++ b/crates/ui-contract/src/runtime/overlay.rs @@ -286,9 +286,10 @@ pub fn request_close(ctx: &Context, id: Id, why: CloseReason) { push_close(ctx, id, why, false); } -/// Queues a close of every popup layer of the running viewport shown last frame: its menus, dropdowns and lists. An -/// app whose own dispatcher runs a shortcut from the keys the runtime leaves in egui's input calls it when the -/// shortcut runs, so the command closes the open popups as a menu pick does; modal layers, windows and toasts stay. +/// Queues a close of every popup layer of the running viewport shown last frame: its menus, dropdowns and lists. The +/// runtime's command dispatcher calls it when a key runs a command; an app whose own dispatcher runs a shortcut from the +/// keys the runtime leaves in egui's input calls it when the shortcut runs. Either way the command closes the open +/// popups as a menu pick does; modal layers, windows and toasts stay. pub fn close_popups(ctx: &Context) { if !crate::runtime::measure::effect(ctx, "overlay::close_popups") { return; diff --git a/crates/ui-widgets/tests/it/pickers/select_list.rs b/crates/ui-widgets/tests/it/pickers/select_list.rs index 9e3b28e68..d9bf009aa 100644 --- a/crates/ui-widgets/tests/it/pickers/select_list.rs +++ b/crates/ui-widgets/tests/it/pickers/select_list.rs @@ -1,15 +1,18 @@ //! The open list of a Select is one gesture: the rows the keys reach are the value until Escape puts it back or Tab //! chooses it, and typing on the closed field opens the list on the first match. The keys the list takes are its own; -//! an app shortcut it leaves runs and closes it. +//! a command or app shortcut it leaves runs and closes it. use craft_ui::buttons::Button; use craft_ui::contract::overlay::close_popups; -use craft_ui::contract::{Bind, Entry, Role, Show as _}; +use craft_ui::contract::{Bind, CommandSet, Entry, Role, Show as _, ShowContainer as _}; +use craft_ui::layout_components::Stack; use craft_ui::pickers::Select; +use craft_ui::runtime::commands::Commands as _; use craft_ui::testing::focus::key_release; use craft_ui::testing::input::{key_press, text}; use craft_ui::testing::{Fixture, Story, StoryData, StoryOutput, trial}; use egui::{Event, Key, Modifiers, Ui}; +use std::cell::Cell; /// A select over One to Four. fn select(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { @@ -168,3 +171,40 @@ fn an_app_shortcut_runs_with_the_list_open_and_closes_it_through_the_runtime() { assert!(!list_open(&mut fixture), "the app's shortcut closes the list"); assert_eq!(value(&mut fixture), 1, "and the value stays"); } + +/// The commands of the scope around the select. +#[derive(Clone, Copy, Debug, PartialEq, Eq, CommandSet)] +#[command(prefix = "selectList", scope = "trial.selectList")] +#[non_exhaustive] +pub enum SelectListCommand { + #[command(label = "Mark", default = "Cmd+1")] + Mark, +} + +/// The select in a stack whose command scope binds Cmd+1; the runs of the command are counted in `marks`. +fn select_in_scope(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let marks = Cell::new(0u32); + let out = Stack::new().command_scope(::SCOPE).show(ui, |ui, cx| { + while cx.take_command::().is_some() { + marks.set(marks.get() + 1); + } + select(ui, data); + }); + *data.value("marks", 0u32) += marks.get(); + out.into() +} + +#[trial] +fn a_key_command_runs_with_the_list_open_and_closes_it() { + let mut fixture = Fixture::story(&Story::new("select_in_scope", select_in_scope)); + fixture.settle(); + fixture.key(Key::Tab, Modifiers::NONE); + fixture.key(Key::ArrowDown, Modifiers::ALT); + fixture.settle(); + assert!(list_open(&mut fixture), "Alt+Down opens the list"); + fixture.key(Key::Num1, Modifiers::COMMAND); + fixture.settle(); + assert_eq!(*fixture.data().value("marks", 0u32), 1, "the command the key binds runs"); + assert!(!list_open(&mut fixture), "and closes the list"); + assert_eq!(value(&mut fixture), 1, "and the value stays"); +}