diff --git a/crates/menu/src/shortcuts/keyboard.rs b/crates/menu/src/shortcuts/keyboard.rs index 9c2f74804..9166e2533 100644 --- a/crates/menu/src/shortcuts/keyboard.rs +++ b/crates/menu/src/shortcuts/keyboard.rs @@ -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 }); diff --git a/crates/menu/src/shortcuts/mod.rs b/crates/menu/src/shortcuts/mod.rs index b7fc0b93e..d8dd9b7f0 100644 --- a/crates/menu/src/shortcuts/mod.rs +++ b/crates/menu/src/shortcuts/mod.rs @@ -312,6 +312,10 @@ pub struct Options { pub area_tabs: bool, /// The app's own Show entries. pub areas: Vec, + /// 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(), diff --git a/crates/menu/src/shortcuts/rows.rs b/crates/menu/src/shortcuts/rows.rs index 309ccf16a..485d8fc7f 100644 --- a/crates/menu/src/shortcuts/rows.rs +++ b/crates/menu/src/shortcuts/rows.rs @@ -210,8 +210,30 @@ fn listed<'k>(keymap: &'k Keymap, options: &Options) -> impl Iterator Vec { + 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 { 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, 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), _ => {} diff --git a/crates/menu/src/shortcuts/view/detail.rs b/crates/menu/src/shortcuts/view/detail.rs index d38514868..d1394ee81 100644 --- a/crates/menu/src/shortcuts/view/detail.rs +++ b/crates/menu/src/shortcuts/view/detail.rs @@ -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); } } diff --git a/crates/menu/tests/it/shortcuts/rows.rs b/crates/menu/tests/it/shortcuts/rows.rs index df232b1c3..5ff4299e3 100644 --- a/crates/menu/tests/it/shortcuts/rows.rs +++ b/crates/menu/tests/it/shortcuts/rows.rs @@ -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"); +}