mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 19:11:26 +00:00
Help: a result shows the shortcut as it is now
Help kept each result's shortcut, label and menu path from the moment of the search. After a shortcut changed, reopening Help or choosing a kept result still showed and returned the old binding, and a change of the interface language left the old labels in place. The kept results now take the current shortcuts, labels and menu paths when Help reopens, when a result is chosen or shown, and when the panel draws after a change. The query, the order of the results and the selected result stay as they were.
This commit is contained in:
@@ -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());
|
||||
|
||||
@@ -16,6 +16,14 @@ pub struct Hit {
|
||||
pub label: String,
|
||||
pub path: Vec<String>,
|
||||
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<String>,
|
||||
#[serde(skip)]
|
||||
params: Value,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
@@ -24,6 +32,8 @@ pub struct View {
|
||||
pub results: Vec<Hit>,
|
||||
pub selected: Option<usize>,
|
||||
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<Hit> {
|
||||
let query = query.trim().to_lowercase();
|
||||
if query.is_empty() {
|
||||
@@ -47,6 +86,9 @@ fn search(app: &VectorcraftApp, query: &str) -> Vec<Hit> {
|
||||
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<Hit> {
|
||||
let label = crate::i18n::tr(lang, &entry.label);
|
||||
let path: Vec<String> = 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<Hit> {
|
||||
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<R
|
||||
if let Err(e) = vectorcraft_engine::cmd::params::check_keys(id, spec.3, p) {
|
||||
return Some(Err(e.to_string()));
|
||||
}
|
||||
refresh(app);
|
||||
Some(match id {
|
||||
"help.discover" | "help.discover.search" => {
|
||||
let query = match p.get("query") {
|
||||
@@ -141,6 +197,7 @@ pub(crate) fn command(app: &mut VectorcraftApp, id: &str, p: &Value) -> Option<R
|
||||
}
|
||||
|
||||
pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
refresh(app);
|
||||
let mut query = app.ui.discover.query.clone();
|
||||
let field = ui.add(egui::TextEdit::singleline(&mut query).char_limit(MAX_QUERY).hint_text(tl!("Search")).desired_width(f32::INFINITY));
|
||||
if std::mem::take(&mut app.ui.discover.focus) {
|
||||
|
||||
+3
-1
@@ -3267,7 +3267,9 @@ the results. VectorCraft limits a query to 1024 characters and rejects a longer
|
||||
`{query, results: [{kind, id, label, path, shortcut}], selected}`. `kind` is `tool`, `command`, or
|
||||
`tutorial`; `selected` is a zero-based result index or `null`. Search matches all the query's
|
||||
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.
|
||||
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.
|
||||
|
||||
`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.
|
||||
|
||||
Reference in New Issue
Block a user