diff --git a/crates/ui-egui/src/shortcut_editor.rs b/crates/ui-egui/src/shortcut_editor.rs index c02f72f02e..3417515bf1 100644 --- a/crates/ui-egui/src/shortcut_editor.rs +++ b/crates/ui-egui/src/shortcut_editor.rs @@ -226,7 +226,7 @@ fn fixed_entries() -> &'static (Vec, Vec) { if !own { by_the_menu_bar.insert(c.id); } - let group = if c.menu.is_empty() { id_group(c.id) } else { c.menu.join(" › ") }; + let group = command_group(c.id, c.menu); v.push(Entry { key: c.id.into(), label: c.label.into(), group, is_tool: false, shows_key: false }); } v.extend(crate::state::all_panels().map(|(id, label)| Entry { @@ -246,28 +246,30 @@ fn fixed_entries() -> &'static (Vec, Vec) { if !own { by_the_menu_bar.insert(id); } - tail.push(Entry { key: id.to_string(), label: label.to_string(), group: id_group(id), is_tool: false, shows_key: false }); + tail.push(Entry { key: id.to_string(), label: label.to_string(), group: command_group(id, &[]), is_tool: false, shows_key: false }); } // A command listed only because an item of the menu bar runs it can have the group and // label of another entry. When that entry is an engine command that no item of the menu bar - // runs, the command the item runs keeps the name and the other entry is shown in the group - // of its id: File › Export Selection… runs `file.exportSelection`, as the menu item does, - // and `document.exportSelection` shows as Document › Export Selection…. Otherwise the - // command is left out: Window › Learn stays the Learn panel's entry. Entries that still - // have the same name, plug-ins included, are given names of their own by - // [`distinct_names`]. + // runs, the command the item runs keeps the name and the other entry is shown in its own + // group ([`own_group`]): File › Export Selection… runs `file.exportSelection`, as the menu + // item does, and `document.exportSelection` shows as Document › Export Selection…. When + // that is the group the other entry has already, both are listed: Object › Expand… runs + // the Expand dialog, and `object.expand` shows its key. Otherwise the command is left out: + // Window › Learn stays the Learn panel's entry. Entries that still have the same name, + // plug-ins included, are given names of their own by [`distinct_names`]. let name = |e: &Entry| (e.group.clone(), e.label.clone()); - let new_names: HashSet<_> = v.iter().chain(&tail).filter(|e| by_the_menu_bar.contains(e.key.as_str())).map(name).collect(); - for e in v.iter_mut().chain(tail.iter_mut()) { - if new_names.contains(&name(e)) - && !by_the_menu_bar.contains(e.key.as_str()) + let movable = |e: &Entry| { + !by_the_menu_bar.contains(e.key.as_str()) && vectorcraft_engine::find_command(&e.key).is_some() && !menu_bar_items().contains_key(e.key.as_str()) - { - e.group = id_group(&e.key); + }; + let new_names: HashSet<_> = v.iter().chain(&tail).filter(|e| by_the_menu_bar.contains(e.key.as_str())).map(name).collect(); + for e in v.iter_mut().chain(tail.iter_mut()) { + if new_names.contains(&name(e)) && movable(e) { + e.group = own_group(&e.key); } } - let names: HashSet<_> = v.iter().chain(&tail).filter(|e| !by_the_menu_bar.contains(e.key.as_str())).map(name).collect(); + let names: HashSet<_> = v.iter().chain(&tail).filter(|e| !by_the_menu_bar.contains(e.key.as_str()) && !movable(e)).map(name).collect(); let keep = |e: &Entry| !by_the_menu_bar.contains(e.key.as_str()) || !names.contains(&name(e)); v.retain(keep); tail.retain(keep); @@ -402,6 +404,102 @@ fn name_of(e: &Entry, group: &str, label: &str) -> String { if e.shows_key { format!("{name} ({})", e.key) } else { name } } +/// The group of command `id`, whose menu path is `menu`: that path, or for a command no menu holds +/// the menu path of the item of the menu bar that runs it ([`menu_bar_path`]: Recolor Artwork… +/// goes under Edit › Edit Colors), or else [`own_group`]. +fn command_group(id: &str, menu: &[&str]) -> String { + if !menu.is_empty() { + return menu.join(" › "); + } + menu_bar_path(id).map_or_else(|| own_group(id), str::to_string) +} + +/// The group of a command that neither a menu nor an item of the menu bar holds: the menu path the +/// items of the menu bar that run the commands of its id's first two parts share (Remove Corners +/// goes under Object › Plug-ins › Path Precision Tools with Set Corners), else its [`GROUPS`] +/// entry, else [`id_group`]. +fn own_group(id: &str) -> String { + if let Some(path) = family_path(id) { + return path.to_string(); + } + GROUPS + .iter() + .find(|(key, _)| if key.ends_with('.') { id.starts_with(key) } else { id == *key }) + .map_or_else(|| id_group(id), |(_, g)| g.to_string()) +} + +/// The one menu path of the items of the menu bar that run the other commands whose id starts with +/// the first two parts of an id of three or more parts (`addon.pathtools.`). +fn family_path(id: &str) -> Option<&'static str> { + let mut parts = id.splitn(3, '.'); + let (Some(a), Some(b), Some(_)) = (parts.next(), parts.next(), parts.next()) else { return None }; + let prefix = format!("{a}.{b}."); + let mut paths = menu_bar_paths().keys().filter(|k| k.starts_with(&prefix) && k.as_str() != id).filter_map(|k| menu_bar_path(k)); + let first = paths.next()?; + paths.all(|p| p == first).then_some(first) +} + +/// The groups of the commands of Keyboard Shortcuts that no menu holds and whose id's first part +/// is no name: (id, or the start of ids when it ends with `.`; group). A panel's commands go under +/// the panel's Window menu item (Window › Layers), and a dialog's under the menu or panel that +/// opens it. +const GROUPS: &[(&str, &str)] = &[ + ("addon.", "Object › Plug-ins"), + ("app.", "Application"), + ("css.", "Window › CSS Properties"), + ("docInfo.", "Window › Document Info"), + ("imageTrace.", "Window › Image Trace"), + ("livePaint.", "Object › Live Paint"), + ("plugin.", "Object › Plug-ins"), + ("svgFilter.", "SVG Filters"), + ("svgInteractivity.", "Window › SVG Interactivity"), + ("threeD.", "Window › 3D and Materials"), + ("ui.action.", "Window › Actions"), + ("ui.actionSet.", "Window › Actions"), + ("ui.blendOptions", "Window › Properties"), + ("ui.brushObjectOptions", "Window › Brushes"), + ("ui.colorGuideOptions", "Window › Color Guide"), + ("ui.colorPicker", "Color"), + ("ui.colorRgbPercent", "Window › Color"), + ("ui.dialogReset", "Dialogs"), + ("ui.dxfOptionsDialog", "File"), + ("ui.epsOptionsDialog", "File"), + ("ui.fileInfoDialog", "File"), + ("ui.findAndReplace", "Window › Properties"), + ("ui.hpglOptionsDialog", "File"), + ("ui.hyphenation", "Window › Paragraph"), + ("ui.javascriptFilesDialog", "Window › SVG Interactivity"), + ("ui.justification", "Window › Paragraph"), + ("ui.layersPanelOptions", "Window › Layers"), + ("ui.loadAllStyles", "Window › Character Styles"), + ("ui.loadCharStyles", "Window › Character Styles"), + ("ui.loadParaStyles", "Window › Paragraph Styles"), + ("ui.newArtboard", "Window › Artboards"), + ("ui.newCharStyle", "Window › Character Styles"), + ("ui.newLayer", "Window › Layers"), + ("ui.newParaStyle", "Window › Paragraph Styles"), + ("ui.pdfSummary.", "File"), + ("ui.perspectiveUserPreset1.", "View › Perspective Grid › One Point Perspective"), + ("ui.perspectiveUserPreset2.", "View › Perspective Grid › Two Point Perspective"), + ("ui.perspectiveUserPreset3.", "View › Perspective Grid › Three Point Perspective"), + ("ui.rasterEffectsSettingsDialog", "Effect"), + ("ui.recolorRedo", "Edit › Edit Colors › Recolor Artwork"), + ("ui.recolorUndo", "Edit › Edit Colors › Recolor Artwork"), + ("ui.rename", "Window › Layers"), + ("ui.retype", "Window › Properties"), + ("ui.runInstalledScript", "File › Scripts"), + ("ui.sampleScreenColor", "Color"), + ("ui.savePdfDialog", "File"), + ("ui.shortcutImportDialog", "Shortcuts"), + ("ui.simplifyAuto", "Object › Path › Simplify"), + ("ui.simplifyMoreOptions", "Object › Path › Simplify"), + ("ui.smoothAuto", "Object › Path › Smooth"), + ("ui.spotColors", "Window › Swatches"), + ("ui.symbolOptions", "Window › Symbols"), + ("ui.tateChuYokoSettings", "Window › Character"), + ("ui.warichuSettings", "Window › Character"), +]; + /// The group of a command no menu holds: the first part of its id, capitalised (`edit.x` → "Edit"). fn id_group(id: &str) -> String { let mut c = id.split('.').next().unwrap_or_default().chars(); @@ -410,7 +508,7 @@ fn id_group(id: &str) -> String { /// Gives entries that have the same group and label names of their own, and leaves the others as /// they are. It runs on the whole snapshot, plug-ins included, after [`fixed_entries`] has shown an -/// engine command in the group of its id or left out a command listed only because an item of the +/// engine command in its own group ([`own_group`]) or left out a command listed only because an item of the /// menu bar runs it: /// 1. A command that an item of the menu bar runs, as a key press would run it, is shown under /// that item's menu path ([`menu_bar_path`]): the four Other Library… commands go under Window › @@ -2219,8 +2317,8 @@ mod tests { /// params, or with the one set of params all its items give) is listed in Edit › Keyboard /// Shortcuts, also when the command has no menu path of its own: Set Corners…, whose params doc /// names a field a call must give, the Perspective Grid toggles, Edit Custom Dictionary…, a - /// query, and Recolor Artwork…, a UI command. Such a command is listed in the group of its id, - /// next to the commands of its family that were listed before. A shortcut given to one survives + /// query, and Recolor Artwork…, a UI command. Such a command is listed under the menu path of + /// the item that runs it. A shortcut given to one survives /// a saved set's round trip. Find Next is left out: its menu item gives `find` empty, which the /// command rejects. When another entry has the same group and label, the command the menu item /// runs keeps the name if no menu item runs the other: File › Export Selection… is @@ -2244,7 +2342,9 @@ mod tests { assert_eq!(shown("file.exportSelection"), Some(("File".into(), "Export Selection…".into()))); assert_eq!(shown("document.exportSelection"), Some(("Document".into(), "Export Selection…".into()))); // No command listed only because an item of the menu bar runs it shares its group and label - // with another entry. + // with another entry, except an engine command that no item of the menu bar runs, whose own + // group is the same: that one shows its key. + let off_the_bar = |k: &str| vectorcraft_engine::find_command(k).is_some() && !menu_bar_items().contains_key(k); let own = |k: &str| { vectorcraft_engine::command_specs() .iter() @@ -2258,13 +2358,19 @@ mod tests { for e in v.iter().chain(tail) { names.entry((e.group.as_str(), e.label.as_str())).or_default().push(e.key.as_str()); } - let shared: Vec<_> = names.iter().filter(|(_, keys)| keys.len() > 1 && keys.iter().any(|k| !own(k))).collect(); + let shared: Vec<_> = names + .iter() + .filter(|(_, keys)| keys.len() > 1 && keys.iter().any(|k| !own(k)) && !keys.iter().filter(|k| own(k)).all(|k| off_the_bar(k))) + .collect(); assert!(shared.is_empty(), "{shared:?}"); + let keyed = |id: &str| entry(id).map(|e| (e.group, e.label, e.shows_key)); + assert_eq!(keyed("ui.expandDialog"), Some(("Object".into(), "Expand…".into(), false))); + assert_eq!(keyed("object.expand"), Some(("Object".into(), "Expand…".into(), true))); for (id, group) in [ - (crate::addons::pathtools::set_corners::COMMAND, "Addon"), - ("perspective.grid.snap", "Perspective"), - (crate::dialogs::custom_dictionary::COMMAND, "Text"), - ("ui.recolorDialog", "Ui"), + (crate::addons::pathtools::set_corners::COMMAND, "Object › Plug-ins › Path Precision Tools"), + ("perspective.grid.snap", "View › Perspective Grid"), + (crate::dialogs::custom_dictionary::COMMAND, "Edit"), + ("ui.recolorDialog", "Edit › Edit Colors"), ] { let e = entry(id).unwrap_or_else(|| panic!("{id} is listed")); assert_eq!(e.group, group, "{id}"); @@ -2575,4 +2681,46 @@ mod tests { assert!(needs_a_value("document.setUnits")); assert!(crate::palette::items().iter().all(|(_, id)| id != "document.setUnits")); } + + /// A command no menu holds is grouped under the menu path of the item of the menu bar that runs + /// it, or under the menu or panel it belongs to: no entry's group is a run-together id part + /// such as "Ui", "ImageTrace" or "ThreeD". + #[test] + fn commands_no_menu_holds_are_grouped_by_their_menu_or_panel() { + let menu_less = + |key: &str| vectorcraft_engine::find_command(key).map_or_else(|| menus::UI_COMMANDS.iter().any(|c| c.0 == key), |c| c.menu.is_empty()); + let mut names: HashSet = ["Tools", "Window"].into_iter().map(String::from).collect(); + names.extend(crate::state::all_panels().map(|(_, label)| label.to_string())); + names.extend(menus::menu_tree().into_iter().map(|(top, _)| top.to_string())); + names.extend(menu_bar_paths().values().flatten().flat_map(|(path, _)| path.split(" › ")).map(String::from)); + // A dialog's commands go under its command's label: Recolor Artwork, Character Style Options. + let labels = menus::UI_COMMANDS.iter().map(|c| c.1).chain(vectorcraft_engine::command_specs().iter().map(|c| c.label)); + names.extend(labels.map(|label| label.trim_end_matches('…').to_string())); + let mut wrong = vec![]; + for e in entries().iter().filter(|e| !e.is_tool && !e.key.contains(':') && menu_less(&e.key)) { + if let Some(path) = menu_bar_path(&e.key) + && e.group != path + { + wrong.push(format!("{}: {} is not under {path}", e.key, e.group)); + } + for part in e.group.split(" › ") { + let one_word = !part.contains(' ') && part.chars().skip(1).all(|c| !c.is_uppercase()); + if !names.contains(part) && (!one_word || ["Ui", "App", "Addon", "Css", "Plugin"].contains(&part)) { + wrong.push(format!("{}: {}", e.key, e.group)); + } + } + } + assert!(wrong.is_empty(), "{wrong:#?}"); + // Every row of GROUPS groups a listed command. + let unused: Vec<_> = GROUPS + .iter() + .filter(|(key, _)| !entries().iter().any(|e| if key.ends_with('.') { e.key.starts_with(key) } else { e.key == *key })) + .collect(); + assert!(unused.is_empty(), "{unused:?}"); + let group = |id: &str| entry(id).map(|e| e.group); + assert_eq!(group("ui.recolorDialog").as_deref(), Some("Edit › Edit Colors")); + assert_eq!(group("ui.rename").as_deref(), Some("Window › Layers")); + assert_eq!(group("ui.justification").as_deref(), Some("Window › Paragraph")); + assert_eq!(group("addon.pathtools.cornerRemove").as_deref(), Some("Object › Plug-ins › Path Precision Tools")); + } }