Commands: a key command closes the open popups, as an app shortcut through close_popups does

This commit is contained in:
aiongg
2026-11-12 22:54:16 +00:00
parent 4e7ab74238
commit 788c89b024
3 changed files with 52 additions and 6 deletions
@@ -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);
}
+4 -3
View File
@@ -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;
@@ -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(<SelectListCommand as CommandSet>::SCOPE).show(ui, |ui, cx| {
while cx.take_command::<SelectListCommand>().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");
}