Merge pull request #3432 from dotCooCoo/fix/shortcuts-own-areas

This commit is contained in:
BFlat
2026-11-25 12:32:04 +07:00
committed by GitHub
7 changed files with 113 additions and 15 deletions
+1 -1
View File
@@ -661,7 +661,7 @@ pub(crate) fn show(ui: &mut Ui, _cx: &mut BodyCx<'_>, env: &mut Env<'_>, state:
state.keyboard.key = Some(key);
}
if let Some(c) = go {
state.go_to(&c);
state.go_to(env, &c);
}
if assign && let (Some(command), Some(key)) = (state.selected.clone(), state.keyboard.key) {
let trigger = Trigger::single(Chord { mods, key });
+5
View File
@@ -312,6 +312,10 @@ pub struct Options {
pub area_tabs: bool,
/// The app's own Show entries.
pub areas: Vec<Area>,
/// false: Show lists All Commands, each menu, Tools, each panel and Other before the app's own
/// [`areas`](Self::areas); true: it lists only the app's own entries (and Fixed Keys), and the
/// dialog opens on the first of them.
pub own_areas: bool,
/// `None`: every catalog entry is listed. Otherwise the table, its Show entries and search
/// list only the entries it returns true for; the others keep their bindings and still count
/// in conflicts.
@@ -348,6 +352,7 @@ impl Default for Options {
sort_by_label: false,
area_tabs: false,
areas: Vec::new(),
own_areas: false,
visible: None,
keyboard: false,
fixed: Vec::new(),
+23 -7
View File
@@ -210,8 +210,30 @@ fn listed<'k>(keymap: &'k Keymap, options: &Options) -> impl Iterator<Item = &'k
keymap.catalog().iter().filter(move |b| visible.is_none_or(|f| f(b)))
}
/// The Show entries, in catalog order, leaving out areas with no rows.
/// The Show entries, in catalog order, leaving out areas with no rows; with [`Options::own_areas`], only the app's own
/// entries and Fixed Keys.
pub(crate) fn areas(keymap: &Keymap, options: &Options, text: &Say) -> Vec<AreaEntry> {
let mut out = if options.own_areas { Vec::new() } else { built_in_areas(keymap, options, text) };
let catalog = listed(keymap, options);
for a in options.areas.iter().filter(|a| catalog.clone().any(a.rows)) {
out.push(AreaEntry { id: AreaId::App(a.id), label: text.segment(a.label).into_owned() });
}
if !options.fixed.is_empty() {
out.push(AreaEntry { id: AreaId::Fixed, label: text.words().fixed_keys.as_str().to_owned() });
}
out
}
/// The area that shows `b`'s row: All Commands when Show lists it, else the first listed area that holds the row.
pub(crate) fn area_of(areas: &[AreaEntry], b: &CommandDef, options: &Options) -> AreaId {
if areas.iter().any(|area| area.id == AreaId::All) {
return AreaId::All;
}
areas.iter().find(|area| in_area(&area.id, b, options)).map_or(AreaId::All, |area| area.id.clone())
}
/// All Commands, each menu, Tools, each panel and Other, leaving out areas with no rows.
fn built_in_areas(keymap: &Keymap, options: &Options, text: &Say) -> Vec<AreaEntry> {
let catalog = listed(keymap, options);
let mut out = vec![AreaEntry { id: AreaId::All, label: text.words().all_commands.as_str().to_owned() }];
let mut menus: Vec<&str> = Vec::new();
@@ -239,12 +261,6 @@ pub(crate) fn areas(keymap: &Keymap, options: &Options, text: &Say) -> Vec<AreaE
if catalog.clone().any(|b| top_menu(b).is_none() && !is_tool(b)) {
out.push(AreaEntry { id: AreaId::Other, label: text.words().other.as_str().to_owned() });
}
for a in options.areas.iter().filter(|a| catalog.clone().any(a.rows)) {
out.push(AreaEntry { id: AreaId::App(a.id), label: text.segment(a.label).into_owned() });
}
if !options.fixed.is_empty() {
out.push(AreaEntry { id: AreaId::Fixed, label: text.words().fixed_keys.as_str().to_owned() });
}
out
}
+14 -4
View File
@@ -399,6 +399,15 @@ impl DialogState {
self.opened = Some(Opened { profiles: env.sets.as_ref().map(|(p, _)| (**p).clone()), overrides: env.keymap.overrides().clone(), holds });
self.session = Some(EditSession::begin(env.keymap));
self.areas = rows::areas(env.keymap, env.options, env.text);
// Without All Commands in Show, the dialog opens on the area that lists the selected command, else the first.
if !self.areas.iter().any(|area| area.id == self.area) {
let listed = |id: &AreaId| self.areas.iter().any(|area| area.id == *id);
let selected = self.selected.as_ref().and_then(|command| env.keymap.bindable(command));
let home = selected.map(|b| rows::area_of(&self.areas, b, env.options)).filter(listed);
if let Some(area) = home.or_else(|| self.areas.first().map(|area| area.id.clone())) {
self.area = area;
}
}
self.sync_set(env);
}
@@ -457,11 +466,12 @@ impl DialogState {
}
}
/// Selects a command and shows its row: area All, no search, no filter.
pub(crate) fn go_to(&mut self, command: &CommandRef) {
/// Selects a command and shows its row: area All (or, when Show does not list All, the first listed area that holds
/// the row), no search, no filter.
pub(crate) fn go_to(&mut self, env: &Env, command: &CommandRef) {
self.select_row(command);
self.keys_pick = None;
self.area = AreaId::All;
self.area = env.keymap.bindable(command).map_or(AreaId::All, |b| rows::area_of(&self.areas, b, env.options));
self.query.clear();
self.filter = Filter::All;
self.collapsed.clear();
@@ -714,7 +724,7 @@ impl DialogState {
let first = c.holders.first().map(|h| h.command.clone());
self.apply_and_say(env, &c.command, c.old, c.binding, Policy::Steal);
if let Some(holder) = first {
self.go_to(&holder);
self.go_to(env, &holder);
}
}
}
+1 -1
View File
@@ -92,7 +92,7 @@ fn message(ui: &mut Ui, env: &mut Env<'_>, state: &mut DialogState) {
}
let out = notice.show(ui);
match (out.action, &goto) {
(Some(0), Some(command)) => state.go_to(command),
(Some(0), Some(command)) => state.go_to(env, command),
(Some(at), Some(_)) if undo && at == 1 => state.undo(env),
(Some(0), None) if undo => state.undo(env),
_ => {}
+1 -1
View File
@@ -257,6 +257,6 @@ fn holders(ui: &mut Ui, env: &Env<'_>, state: &mut DialogState) {
}
}
if let Some(command) = go {
state.go_to(&command);
state.go_to(env, &command);
}
}
+68 -1
View File
@@ -4,9 +4,10 @@ use std::sync::Arc;
use craft_host::{AppInfo, Host};
use craft_keymap::{Binding, CommandDef, CommandRef, Keymap, Platform, Policy, Scope, Scopes};
use craft_menu::shortcuts::{HoldSetting, Outcome};
use craft_menu::shortcuts::{Area, HoldSetting, Outcome};
use craft_ui::contract::Role;
use craft_ui::testing::trial;
use egui::{Key, Modifiers};
use super::fixture::{self, Rig};
@@ -110,3 +111,69 @@ fn the_summary_without_a_ui_is_the_one_summarize_writes() {
let (title, _) = text.split_once('\n').expect("a title line");
assert_eq!(craft_menu::shortcuts::summary::text_for(&km, &host, title), text);
}
fn is_tool(def: &CommandDef) -> bool {
matches!(def.kind, craft_keymap::CommandKind::Tool { .. })
}
fn area(rig: &Rig) -> String {
let shared = rig.shared.borrow();
shared.state.snapshot(&shared.km).expect("the dialog has shown").area
}
/// With `own_areas`, Show lists only the app's own entries, the dialog opens on the first of them, and its table
/// lists that entry's rows. Without it, the built-in entries come first and the dialog opens on All Commands.
#[trial]
fn own_areas_show_only_the_apps_entries_and_open_on_the_first() {
let tune = |own: bool| {
move |options: &mut craft_menu::shortcuts::Options| {
options.area_tabs = true;
options.areas = vec![Area::new("tools", "Tools", is_tool), Area::new("menu", "Menu Commands", |def| !is_tool(def))];
options.own_areas = own;
}
};
let rig = Rig::tuned(Platform::Mac, tune(true));
assert_eq!(rig.names_with(Role::TAB, ""), ["Tools", "Menu Commands"], "only the app's entries are tabs");
assert_eq!(area(&rig), "tools", "the dialog opens on the first of them");
let rows = rig.names_with(Role::ROW, "");
assert!(rows.iter().all(|row| row.contains("Marquee")) && !rows.is_empty(), "the table lists the Tools rows only: {rows:?}");
let rig = Rig::tuned(Platform::Mac, tune(false));
let tabs = rig.names_with(Role::TAB, "");
assert_eq!(tabs.first().map(String::as_str), Some("All Commands"), "the built-in entries come first: {tabs:?}");
assert!(tabs.iter().any(|t| t == "Menu Commands"), "the app's entries follow them: {tabs:?}");
assert_eq!(area(&rig), "all");
}
/// A dialog opened with a command selected, with Show listing only the app's entries, opens on the entry that lists it.
#[trial]
fn own_areas_open_on_the_selected_commands_entry() {
let mut rig = Rig::tuned(Platform::Mac, |options| {
options.areas = vec![Area::new("tools", "Tools", is_tool), Area::new("menu", "Menu Commands", |def| !is_tool(def))];
options.own_areas = true;
});
{
let mut shared = rig.shared.borrow_mut();
let mut state = craft_menu::shortcuts::DialogState::new();
state.select(&CommandRef::from("file.export"));
shared.state = state;
}
rig.pass();
assert_eq!(area(&rig), "menu", "Export… is listed under Menu Commands");
assert!(rig.names_with(Role::ROW, "").iter().any(|row| row.starts_with("Export…")), "its row shows");
}
/// Accept and Go to Conflict, with Show listing only the app's entries, shows the holder under the entry that lists
/// it.
#[trial]
fn going_to_a_conflict_picks_an_area_that_lists_the_holder() {
let mut rig = Rig::tuned(Platform::Mac, |options| {
options.areas = vec![Area::new("tools", "Tools", is_tool), Area::new("menu", "Menu Commands", |def| !is_tool(def))];
options.own_areas = true;
});
rig.click("Marquee Tool: M");
rig.press(Modifiers::COMMAND, Key::E);
rig.click("Accept and Go to Conflict");
assert_eq!(area(&rig), "menu", "Export… is listed under Menu Commands");
assert!(rig.has("Export…"), "the holder's row shows");
}