diff --git a/crates/ui-egui/src/menus/tests.rs b/crates/ui-egui/src/menus/tests.rs index 696d50d63f..d4703034ea 100644 --- a/crates/ui-egui/src/menus/tests.rs +++ b/crates/ui-egui/src/menus/tests.rs @@ -112,6 +112,41 @@ fn discover_rejects_invalid_search_and_selection_parameters() { assert!(app.run("help.discover.choose", json!({"index": 0})).is_err()); } +#[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()); + let found = app.run("help.discover.search", json!({"query": "file.newDialog"})).unwrap(); + let index = found["results"].as_array().unwrap().iter().position(|hit| hit["id"] == "file.newDialog").unwrap(); + assert_eq!(found["results"][index]["shortcut"], "Cmd+N"); + app.run("help.discover.choose", json!({"index": index})).unwrap(); + app.run("window.panel.close", json!({"panel": "discover"})).unwrap(); + app.run("shortcuts.set", json!({"id": "file.newDialog", "shortcut": "Cmd+Alt+Shift+F9"})).unwrap(); + + let reopened = app.run("help.discover", json!({})).unwrap(); + assert_eq!(reopened["query"], "file.newDialog"); + assert_eq!(reopened["selected"], index); + assert_eq!(reopened["results"][index]["id"], "file.newDialog"); + assert_eq!(reopened["results"][index]["shortcut"], "Cmd+Alt+Shift+F9"); + let picked = app.run("help.discover.choose", json!({"index": index})).unwrap(); + assert_eq!(picked["result"]["shortcut"], "Cmd+Alt+Shift+F9"); + assert_eq!(picked["location"]["shortcut"], "Cmd+Alt+Shift+F9"); + + app.run("shortcuts.set", json!({"id": "file.newDialog", "shortcut": "Cmd+Alt+Shift+F8"})).unwrap(); + let text = crate::tests_labels::painted_text(&mut app, crate::panels::discover::show); + assert!(text.contains(&pretty_shortcut("Cmd+Alt+Shift+F8")), "{text}"); + assert!(!text.contains(&pretty_shortcut("Cmd+Alt+Shift+F9")), "{text}"); + + let de = crate::i18n::Lang::from_code("de").unwrap(); + assert_ne!(crate::i18n::tr(de, "New…"), "New…"); + crate::i18n::set_current(de); + let back = app.run("help.discover.back", json!({})).unwrap(); + crate::i18n::set_current(crate::i18n::Lang::EN); + assert_eq!(back["query"], "file.newDialog"); + assert_eq!(back["results"][index]["label"], crate::i18n::tr(de, "New…")); + assert_eq!(back["results"][index]["path"][0], crate::i18n::tr(de, "File")); + crate::shortcut_editor::sync(&crate::state::UiState::default()); +} + #[test] fn help_bar_toggle_is_independent_and_saved_in_workspaces() { let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), crate::Services::default()); diff --git a/crates/ui-egui/src/panels/discover.rs b/crates/ui-egui/src/panels/discover.rs index 33df5c2db7..c5eaa78eea 100644 --- a/crates/ui-egui/src/panels/discover.rs +++ b/crates/ui-egui/src/panels/discover.rs @@ -16,6 +16,14 @@ pub struct Hit { pub label: String, pub path: Vec, pub shortcut: String, + /// The English label and menu path that `label` and `path` translate, and a command's menu + /// params, kept to bring a retained result up to date ([`refresh`]). + #[serde(skip)] + english_label: String, + #[serde(skip)] + english_path: Vec, + #[serde(skip)] + params: Value, } #[derive(Clone, Debug, Default)] @@ -24,6 +32,8 @@ 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)>, } fn matches(query: &str, text: &str) -> bool { @@ -31,6 +41,35 @@ 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()) +} + +/// 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. +fn refresh(app: &mut VectorcraftApp) { + let stamp = now(); + if app.ui.discover.current_for == Some(stamp) { + return; + } + let lang = stamp.1; + for hit in &mut app.ui.discover.results { + 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" => { + 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(); + } + _ => {} + } + } + app.ui.discover.current_for = Some(stamp); +} + fn search(app: &VectorcraftApp, query: &str) -> Vec { let query = query.trim().to_lowercase(); if query.is_empty() { @@ -47,6 +86,9 @@ fn search(app: &VectorcraftApp, query: &str) -> Vec { label: label.into(), path: vec![], shortcut: crate::shortcut_editor::tool_shortcut(tool.id).unwrap_or_default().into(), + english_label: tool.label.into(), + english_path: vec![], + params: Value::Null, }); } } @@ -55,7 +97,16 @@ fn search(app: &VectorcraftApp, query: &str) -> Vec { let label = crate::i18n::tr(lang, &entry.label); let path: Vec = entry.path.iter().map(|part| crate::i18n::tr(lang, part).into()).collect(); if matches(&query, &format!("{} {label} {} {} {id}", entry.label, entry.path.join(" "), path.join(" "))) { - hits.push(Hit { kind: "command", id, label: label.into(), path, shortcut: entry.shortcut }); + hits.push(Hit { + kind: "command", + id, + label: label.into(), + path, + shortcut: entry.shortcut, + english_label: entry.label, + english_path: entry.path, + params: entry.params, + }); } } for skill in super::learn::catalog::SKILLS { @@ -67,6 +118,9 @@ fn search(app: &VectorcraftApp, query: &str) -> Vec { label: tutorial.title.into(), path: vec![skill.title.into()], shortcut: String::new(), + english_label: tutorial.title.into(), + english_path: vec![skill.title.into()], + params: Value::Null, }); } } @@ -79,6 +133,7 @@ fn results(app: &VectorcraftApp) -> Value { } fn set_query(app: &mut VectorcraftApp, query: &str) { + app.ui.discover.current_for = Some(now()); app.ui.discover.results = search(app, query); app.ui.discover.query = query.into(); app.ui.discover.selected = None; @@ -89,6 +144,7 @@ pub(crate) fn command(app: &mut VectorcraftApp, id: &str, p: &Value) -> Option { let query = match p.get("query") { @@ -141,6 +197,7 @@ pub(crate) fn command(app: &mut VectorcraftApp, id: &str, p: &Value) -> Option