diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index 7d003be560..b7f833c9ac 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -63,7 +63,7 @@ mod ui_commands; pub use context::context_items; #[cfg(test)] use entries::flatten; -pub use entries::{InsertableItem, MenuEntry, context_entries, insertable, insertable_items, menu_entries, plays_in_app, shown_label}; +pub use entries::{InsertableItem, MenuEntry, context_entries, insertable, insertable_items, is_row_of, menu_entries, plays_in_app, shown_label}; pub(crate) use invoke::graph_dialog; pub use invoke::{click_target, invoke}; pub(crate) use labels::lists_plugin; diff --git a/crates/ui-egui/src/menus/entries.rs b/crates/ui-egui/src/menus/entries.rs index 6524edfd21..d90b4b9872 100644 --- a/crates/ui-egui/src/menus/entries.rs +++ b/crates/ui-egui/src/menus/entries.rs @@ -29,15 +29,49 @@ pub struct MenuEntry { pub shown_path: Vec, } -/// Flattened menu for `ui.menu.list`. +/// Flattened menu for `ui.menu.list`. A row that shows a panel of the Window menu, there or in another menu (Type › +/// Glyphs), lists the params a click on it runs: `window.panel` with `toggle: true`, which puts a showing panel away +/// ([`is_row_of`]). pub fn menu_entries(app: &VectorcraftApp) -> Vec { let mut out = vec![]; for (title, items) in menu_tree_named(app.session.prefs.font_names_in_english, &Listed::of(app)) { flatten(app, vec![title.to_string()], vec![crate::i18n::t(title).to_string()], &items, &mut out); } + let window_panels: Vec = out + .iter() + .filter(|e| e.path.first().is_some_and(|t| t == "Window") && e.command.as_deref() == Some(PANEL)) + .map(|e| e.params.clone()) + .collect(); + for e in &mut out { + if e.command.as_deref() == Some(PANEL) + && window_panels.contains(&e.params) + && let Some(params) = e.params.as_object_mut() + { + params.entry("toggle").or_insert(Value::Bool(true)); + } + } out } +/// The command of the rows that show a panel. +const PANEL: &str = "window.panel"; + +/// Whether `e` is the row of the menu item that runs command `id` with `params`: the same command and params, or a +/// panel row that [`menu_entries`] lists with `toggle: true` added. +pub fn is_row_of(e: &MenuEntry, id: &str, params: &Value) -> bool { + if e.command.as_deref() != Some(id) { + return false; + } + if e.params == *params { + return true; + } + let mut listed = e.params.clone(); + id == PANEL + && params.get("toggle").is_none() + && listed.as_object_mut().and_then(|o| o.remove("toggle")) == Some(Value::Bool(true)) + && listed == *params +} + /// Flattened canvas context menu for `ui.contextMenu.list` (paths are its submenus). pub fn context_entries(app: &VectorcraftApp) -> Vec { let mut out = vec![]; diff --git a/crates/ui-egui/src/menus/tree.rs b/crates/ui-egui/src/menus/tree.rs index 13b33dde44..fa5e15d238 100644 --- a/crates/ui-egui/src/menus/tree.rs +++ b/crates/ui-egui/src/menus/tree.rs @@ -45,7 +45,9 @@ impl Listed { } pub fn menu_tree_named(english_names: bool, listed: &Listed) -> Vec<(&'static str, Vec)> { - let panel = |label: &'static str, id: &'static str| cp(label, "window.panel", json!({ "panel": id, "toggle": true })); + // A Window row names the command the panel's shortcut runs, so it shows that shortcut; a click toggles the panel + // (`menu_entries` lists the row with `toggle: true`). + let panel = |label: &'static str, id: &'static str| cp(label, "window.panel", json!({ "panel": id })); vec![ ( // The application menu, shown on macOS only: the layout merges it with the items the diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rs index eaa0065ba4..7ee393527f 100644 --- a/crates/ui-egui/src/shortcuts.rs +++ b/crates/ui-egui/src/shortcuts.rs @@ -276,7 +276,8 @@ fn menu_target(c: &CommandRef) -> Option<(String, serde_json::Value)> { } /// What a click on the menu bar's or the canvas menu's row `c` runs ([`crate::menus::click_target`]): a "…" row with -/// params opens its dialog on them (Clean Up…). `None` for a command that no menu fired. +/// params opens its dialog on them (Clean Up…), and a row of a Window menu panel runs the params it is listed with +/// (`toggle: true`, [`crate::menus::is_row_of`]). `None` for a command that no menu fired. fn clicked_row(app: &VectorcraftApp, source: FireSource, c: &CommandRef) -> Option<(String, serde_json::Value)> { if source != FireSource::Menu || c.params.as_object().is_none_or(serde_json::Map::is_empty) { return None; @@ -284,8 +285,8 @@ fn clicked_row(app: &VectorcraftApp, source: FireSource, c: &CommandRef) -> Opti let row = crate::menus::menu_entries(app) .into_iter() .chain(crate::menus::context_entries(app)) - .find(|e| e.command.as_deref() == Some(c.id.as_str()) && e.params == c.params)?; - Some(crate::menus::click_target(&row.label, &c.id, &c.params)) + .find(|e| crate::menus::is_row_of(e, &c.id, &c.params))?; + Some(crate::menus::click_target(&row.label, &c.id, &row.params)) } /// Params used when a shortcut invokes its command. The named panel function keys retain their diff --git a/crates/ui-egui/src/shortcuts/tests.rs b/crates/ui-egui/src/shortcuts/tests.rs index 0f302c1551..49d09ddf13 100644 --- a/crates/ui-egui/src/shortcuts/tests.rs +++ b/crates/ui-egui/src/shortcuts/tests.rs @@ -246,6 +246,65 @@ fn named_panel_default_chords_toggle_their_panels() { } } +/// Each Window menu row that shows a panel shows the chord of that panel's shortcut, as production's did, and the +/// rows of the panels with a default key show one. +#[test] +fn window_menu_panel_rows_show_their_panels_shortcuts() { + let mut app = new_app(); + let ctx = ready(&mut app); + let rows: Vec = + app.menus.bar.items().into_iter().filter(|it| it.command.id == "window.panel").map(|it| it.command.clone()).collect(); + assert!(!rows.is_empty(), "the Window menu lists panels"); + let mut shown = 0; + for row in &rows { + let panel = row.params["panel"].as_str().expect("a panel row names its panel"); + let entry = crate::keymap::command_of(&format!("panel:{panel}")); + let chord = craft::ui::runtime::commands::chord(&ctx, row); + assert_eq!(chord, craft::ui::runtime::commands::chord(&ctx, &entry), "{panel}: the row shows its shortcut's chord"); + shown += usize::from(chord.is_some()); + } + let with_keys = crate::shortcut_editor::PANEL_SHORTCUTS.iter().filter(|(panel, _)| rows.iter().any(|r| r.params["panel"] == *panel)).count(); + assert!(with_keys > 0 && shown >= with_keys, "{shown} rows show a chord, {with_keys} panels have a default key"); +} + +/// A click on a Window menu row toggles its panel: it puts a showing panel away and shows it again. +#[test] +fn a_window_menu_click_toggles_its_panel() { + use craft::ui::runtime::commands::{FirePhase, FireSource, Fired}; + let mut app = new_app(); + let ctx = ready(&mut app); + let row = app + .menus + .bar + .items() + .into_iter() + .find(|it| it.command.id == "window.panel" && it.command.params["panel"] == "layers") + .map(|it| it.command.clone()); + let row = row.expect("a Window › Layers row"); + let before = crate::panel_docking::showing(&app.ui, "layers"); + let click = |app: &mut VectorcraftApp| { + crate::shortcuts::run_fired(app, &ctx, &Fired::new(row.clone(), FireSource::Menu, FirePhase::Press, 0.0)); + key_frame(app, &ctx, vec![]); + crate::panel_docking::showing(&app.ui, "layers") + }; + let once = click(&mut app); + assert_ne!(once, before, "the first click toggles Layers"); + assert_eq!(click(&mut app), before, "the second click toggles it back"); +} + +/// `ui.menu.list` lists a Window menu panel row with the toggle a click on it runs, and a plug-in's row that shows a +/// panel the Window menu does not list (Object › Plug-ins › Packaging Tools › Fold Lines) without one. +#[test] +fn listed_window_rows_toggle_and_plugin_panel_rows_do_not() { + let app = new_app(); + let entries = crate::menus::menu_entries(&app); + let row = + |panel: &str| entries.iter().find(|e| e.command.as_deref() == Some("window.panel") && e.params["panel"] == panel).map(|e| e.params.clone()); + assert_eq!(row("layers").map(|p| p["toggle"].clone()), Some(json!(true)), "Window › Layers toggles"); + let fold = row(crate::addons::packaging::ID).expect("a Fold Lines row"); + assert!(fold.get("toggle").is_none(), "Fold Lines shows its panel: {fold}"); +} + /// A shortcut on a panel without a function key of its own runs `window.panel` with the params it has: no toggle is /// added, as production checked. #[test]