From b778279d6f81bc22fd07a37a32330b2c72f547ba Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 07:27:00 -0800 Subject: [PATCH] Help: kept command results follow their menu items A kept command result now takes its menu item's current label and menu path when the menu state changes, so Show Grid reads Hide Grid once the grid shows, and a command that no menu lists any more drops out. The results are brought up to date only when the menus' state hash changes, with the menu entries looked up by command and params. Help commands reject parameters that are not an object. --- crates/ui-egui/src/menus/tests.rs | 22 +++++++++++++ crates/ui-egui/src/native_menu.rs | 2 +- crates/ui-egui/src/panels/discover.rs | 47 +++++++++++++++++++++------ docs/mcp.md | 8 +++-- 4 files changed, 66 insertions(+), 13 deletions(-) diff --git a/crates/ui-egui/src/menus/tests.rs b/crates/ui-egui/src/menus/tests.rs index 21ba570e5d..205817891c 100644 --- a/crates/ui-egui/src/menus/tests.rs +++ b/crates/ui-egui/src/menus/tests.rs @@ -166,11 +166,33 @@ fn discover_rejects_invalid_search_and_selection_parameters() { for index in [json!(-1), json!(1.5), json!(1000000), json!("0"), Value::Null] { assert!(app.run("help.discover.choose", json!({"index": index})).is_err()); } + for params in [json!(5), json!("rectangle"), json!([1]), json!(true)] { + for id in ["help.discover", "help.discover.search", "help.discover.choose", "help.discover.back"] { + assert!(app.run(id, params.clone()).is_err(), "{id} {params}"); + } + assert_eq!(app.ui.discover.query, before); + } app.run("help.discover.search", json!({"query": " "})).unwrap(); assert!(app.ui.discover.results.is_empty()); assert!(app.run("help.discover.choose", json!({"index": 0})).is_err()); } +/// A kept command result follows its menu item: Show Grid reads Hide Grid once the grid shows. +#[test] +fn discover_command_results_follow_their_menu_label() { + crate::i18n::set_current(crate::i18n::Lang::EN); + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({})).unwrap(); + let found = app.run("help.discover.search", json!({"query": "view.grid"})).unwrap(); + let index = found["results"].as_array().unwrap().iter().position(|hit| hit["id"] == "view.grid" && hit["path"][0] == "View").unwrap(); + assert_eq!(found["results"][index]["label"], "Show Grid"); + app.run("help.discover.choose", json!({"index": index})).unwrap(); + app.run("view.grid", json!({})).unwrap(); + let back = app.run("help.discover", json!({})).unwrap(); + assert_eq!(back["selected"], index); + assert_eq!(back["results"][index]["label"], "Hide Grid"); +} + #[test] fn discover_results_show_the_current_shortcut_and_language_after_a_change() { let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), crate::Services::default()); diff --git a/crates/ui-egui/src/native_menu.rs b/crates/ui-egui/src/native_menu.rs index 510131be0e..40ebc46ba5 100644 --- a/crates/ui-egui/src/native_menu.rs +++ b/crates/ui-egui/src/native_menu.rs @@ -610,7 +610,7 @@ pub fn sync(app: &mut VectorcraftApp, ctx: &egui::Context) { /// interaction is open (for example a move, scale or shape drag, a dialog's or the font menu's /// preview, or a typing session), the revision, which each of its previews changes, is left out: /// the hash changes as the interaction begins and as it ends. -fn state_hash(app: &VectorcraftApp, input: Option) -> u64 { +pub(crate) fn state_hash(app: &VectorcraftApp, input: Option) -> u64 { let mut h = DefaultHasher::new(); input.hash(&mut h); app.run_count.hash(&mut h); diff --git a/crates/ui-egui/src/panels/discover.rs b/crates/ui-egui/src/panels/discover.rs index 9a08d2d0c7..2b38e16591 100644 --- a/crates/ui-egui/src/panels/discover.rs +++ b/crates/ui-egui/src/panels/discover.rs @@ -32,8 +32,9 @@ pub struct View { pub results: Vec, pub selected: Option, pub focus: bool, - /// The shortcut generation and language the results were last brought up to date for. - current_for: Option<(u64, crate::i18n::Lang)>, + /// The menu state ([`crate::native_menu::state_hash`]: commands run, shortcuts, the language, + /// recent files and more) the results were last brought up to date for. + current_for: Option, } fn matches(query: &str, text: &str) -> bool { @@ -41,32 +42,55 @@ fn matches(query: &str, text: &str) -> bool { query.split_whitespace().all(|word| text.contains(word)) } -fn now() -> (u64, crate::i18n::Lang) { - (crate::shortcut_editor::GENERATION.load(std::sync::atomic::Ordering::Relaxed), crate::i18n::current()) +fn now(app: &VectorcraftApp) -> u64 { + crate::native_menu::state_hash(app, None) } -/// Updates the retained results' labels, menu paths and shortcuts after a shortcut or the UI -/// language changes, keeping the query, the results' order and the selected result. +/// Updates the retained results' labels, menu paths and shortcuts after the menu state changes (a +/// command, a shortcut or the UI language), keeping the query, the results' order and the selected +/// result. A command result takes its menu item's current label and place (Show Grid becomes Hide +/// Grid), and leaves the results when no menu lists it any more, such as a recent file that was +/// removed. fn refresh(app: &mut VectorcraftApp) { - let stamp = now(); + let stamp = now(app); if app.ui.discover.current_for == Some(stamp) { return; } - let lang = stamp.1; - for hit in &mut app.ui.discover.results { + let lang = crate::i18n::current(); + let entries = if app.ui.discover.results.iter().any(|hit| hit.kind == "command") { menus::menu_entries(app) } else { Vec::new() }; + let mut by_command: std::collections::HashMap<(&str, String), Vec<&menus::MenuEntry>> = std::collections::HashMap::new(); + for entry in &entries { + if let Some(id) = entry.command.as_deref() { + by_command.entry((id, entry.params.to_string())).or_default().push(entry); + } + } + let selected = app.ui.discover.selected.take(); + let mut kept = Vec::with_capacity(app.ui.discover.results.len()); + for (index, mut hit) in std::mem::take(&mut app.ui.discover.results).into_iter().enumerate() { match hit.kind { "tool" => { hit.label = crate::i18n::tr(lang, &hit.english_label).into(); hit.shortcut = crate::shortcut_editor::tool_shortcut(&hit.id).unwrap_or_default().into(); } "command" => { + let same = by_command.get(&(hit.id.as_str(), hit.params.to_string())).map_or(&[][..], Vec::as_slice); + let Some(entry) = same.iter().find(|e| e.path == hit.english_path).or_else(|| same.first()) else { + continue; + }; + hit.english_label = entry.label.clone(); + hit.english_path = entry.path.clone(); hit.label = crate::i18n::tr(lang, &hit.english_label).into(); hit.path = hit.english_path.iter().map(|part| crate::i18n::tr(lang, part).into()).collect(); hit.shortcut = menus::item_shortcut(&hit.id, &hit.params).or_else(|| menus::shortcut_of(&hit.id)).unwrap_or_default().into(); } _ => {} } + if selected == Some(index) { + app.ui.discover.selected = Some(kept.len()); + } + kept.push(hit); } + app.ui.discover.results = kept; app.ui.discover.current_for = Some(stamp); } @@ -133,7 +157,7 @@ fn results(app: &VectorcraftApp) -> Value { } fn set_query(app: &mut VectorcraftApp, query: &str) { - app.ui.discover.current_for = Some(now()); + app.ui.discover.current_for = Some(now(app)); app.ui.discover.results = search(app, query); app.ui.discover.query = query.into(); app.ui.discover.selected = None; @@ -141,6 +165,9 @@ fn set_query(app: &mut VectorcraftApp, query: &str) { pub(crate) fn command(app: &mut VectorcraftApp, id: &str, p: &Value) -> Option> { let spec = menus::UI_COMMANDS.iter().find(|spec| spec.0 == id && id.starts_with("help.discover"))?; + if !(p.is_object() || p.is_null()) { + return Some(Err("`params` must be an object".into())); + } if let Err(e) = vectorcraft_engine::cmd::params::check_keys(id, spec.3, p) { return Some(Err(e.to_string())); } diff --git a/docs/mcp.md b/docs/mcp.md index 4b440d4f8b..fcc78d8a40 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3269,13 +3269,17 @@ the results. VectorCraft limits a query to 1024 characters and rejects a longer words without regard to case, using translated tool and command labels as well as their English labels, ids, and menu paths. Tutorial text remains in English. After a shortcut or the interface language changes, the kept results show the current shortcuts, labels, and menu paths, with the -query and the selected result unchanged. +query and the selected result unchanged. A command result also shows its menu item's current +label (Show Grid becomes Hide Grid), and a command that no menu lists any more, such as a removed +recent file, drops out of the results. Parameters other than an object are an error. `help.discover.choose {index}` shows that result. A tool result marks its toolbar location without choosing the tool. A command result shows its menu path and current shortcut without running it. A tutorial result shows its numbered steps; a tool link in a step marks that tool's location. The reply is `{result, location}`, with the chosen result and the tool-location reply, tutorial -id, or command path. An invalid index returns an error and preserves the selected result. +id, or command path. An invalid index returns an error and preserves the selected result. When a +tool cannot be marked because Tab or Presentation Mode hides the toolbars, the panel shows the +tool-location reply's `reason` in the status bar. Home clears the search. Back and `help.discover.back {}` return to its results with the query preserved. Escape in the search field closes the panel. Escape on the canvas still cancels an