mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 02:03:02 +00:00
Merge pull request #8083 from dotCooCoo/feat/palette-synonyms
Command Palette: a search finds a command by a word with the same meaning
This commit is contained in:
@@ -920,16 +920,15 @@ fn anti_aliasing_method_labels_are_in_complete_catalogs() {
|
||||
}
|
||||
|
||||
/// Each row in the `search` context, which adds a language's words to a group of words with the same
|
||||
/// meaning for the command palette, names a group and adds single words of no other group. No
|
||||
/// catalog has to have these rows.
|
||||
/// meaning for the command palette, names a group and adds single words of no other group, and no
|
||||
/// two rows of a catalog add one word to different groups. No catalog has to have these rows.
|
||||
#[test]
|
||||
fn search_rows_name_a_group_and_add_single_words() {
|
||||
let mut bad = Vec::new();
|
||||
for l in LANGUAGES.iter().filter(|l| !l.source.is_empty()) {
|
||||
let (entries, _) = parse_entries(l.source);
|
||||
for (_, src, tr) in entries.iter().filter(|(ctx, _, _)| ctx == crate::palette::words::CONTEXT) {
|
||||
bad.extend(crate::palette::words::row_problems(src, tr).into_iter().map(|p| format!("{}: {p}", l.code)));
|
||||
}
|
||||
let rows = entries.iter().filter(|(ctx, _, _)| ctx == crate::palette::words::CONTEXT).map(|(_, src, tr)| (src.as_str(), tr.as_str()));
|
||||
bad.extend(crate::palette::words::catalog_problems(rows).into_iter().map(|p| format!("{}: {p}", l.code)));
|
||||
}
|
||||
assert!(bad.is_empty(), "{bad:#?}");
|
||||
}
|
||||
|
||||
@@ -1016,6 +1016,9 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
// another widget, such as a field in a window above the backdrop.
|
||||
let focus_id = egui::Id::unique("palette-focus-before");
|
||||
let field_id = egui::Id::unique("palette-field");
|
||||
// The Recent Commands rows drawn in the last frame, so a pick with the keyboard, which leaves the
|
||||
// focus on its row, gives the focus back too.
|
||||
let rows_id = egui::Id::unique("palette-recent-rows");
|
||||
// The menu bar and the toolbars note where they draw their controls only while the palette is
|
||||
// open; the pass that opens it asks for another, which they draw noting them.
|
||||
let wanted = ctx.data_mut(|d| std::mem::replace(d.get_temp_mut_or_default::<bool>(spots_wanted_id()), app.ui.palette_open));
|
||||
@@ -1029,15 +1032,17 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
});
|
||||
}
|
||||
if !app.ui.palette_open {
|
||||
let field = ctx.data_mut(|d| {
|
||||
let (field, rows) = ctx.data_mut(|d| {
|
||||
let field = d.get_temp::<egui::Id>(field_id);
|
||||
d.remove::<egui::Id>(field_id);
|
||||
field
|
||||
let rows = d.get_temp::<Vec<egui::Id>>(rows_id).unwrap_or_default();
|
||||
d.remove::<Vec<egui::Id>>(rows_id);
|
||||
(field, rows)
|
||||
});
|
||||
let now = ctx.memory(|m| m.focused());
|
||||
if let Some(Some(before)) = ctx.data_mut(|d| d.remove_temp::<Option<egui::Id>>(focus_id))
|
||||
&& !crate::panel_docking::modal_window_up(app, ctx)
|
||||
&& (now.is_none() || now == field)
|
||||
&& (now.is_none() || now == field || now.is_some_and(|n| rows.contains(&n)))
|
||||
{
|
||||
ctx.memory_mut(|m| m.request_focus(before));
|
||||
}
|
||||
@@ -1169,12 +1174,14 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
if !list_open && !history_focused {
|
||||
r.request_focus();
|
||||
}
|
||||
let mut rows = Vec::new();
|
||||
egui::Popup::menu(&button).id(recent_list_id()).show(|ui| {
|
||||
if recent.is_empty() {
|
||||
ui.add_enabled(false, egui::Button::new(tl!("No recent commands")));
|
||||
}
|
||||
for (i, e) in recent.iter().enumerate() {
|
||||
let row = ui.add(egui::Button::new(e.shown.as_str()).shortcut_text(menus::shown_shortcut(&e.shortcut)));
|
||||
rows.push(row.id);
|
||||
if i == rsel {
|
||||
row.request_focus();
|
||||
}
|
||||
@@ -1183,6 +1190,7 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
}
|
||||
}
|
||||
});
|
||||
ui.ctx().data_mut(|d| d.insert_temp(rows_id, rows));
|
||||
r
|
||||
})
|
||||
.inner;
|
||||
@@ -3165,6 +3173,44 @@ mod tests {
|
||||
assert_eq!(ctx.memory(|m| m.focused()), Some(above), "the clicked field keeps the focus");
|
||||
}
|
||||
|
||||
/// A pick with the keyboard from Recent Commands that opens no dialog gives the focus back to
|
||||
/// the widget that had it when the palette opened, though the picked row had the keyboard.
|
||||
#[test]
|
||||
fn a_pick_from_recent_commands_gives_the_focus_back() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), crate::Services::default());
|
||||
app.ui.recent_commands = vec!["paint.swap".into()];
|
||||
let mut text = String::new();
|
||||
let field = egui::Id::unique("other field");
|
||||
let mut frame = |app: &mut VectorcraftApp, focus: bool, events: Vec<egui::Event>| {
|
||||
ctx.run_ui(egui::RawInput { events, ..Default::default() }, |ui| {
|
||||
let r = ui.add(egui::TextEdit::singleline(&mut text).id(field));
|
||||
if focus {
|
||||
r.request_focus();
|
||||
}
|
||||
show(app, ui.ctx());
|
||||
})
|
||||
.textures_delta
|
||||
.clear();
|
||||
};
|
||||
frame(&mut app, true, vec![]);
|
||||
frame(&mut app, false, vec![]);
|
||||
app.ui.palette_open = true;
|
||||
frame(&mut app, false, vec![]);
|
||||
egui::Popup::open_id(&ctx, recent_list_id());
|
||||
frame(&mut app, false, vec![]);
|
||||
frame(&mut app, false, vec![]);
|
||||
assert!(recent_list_open(&ctx), "the list is open");
|
||||
assert_ne!(ctx.memory(|m| m.focused()), Some(field), "the row has the keyboard");
|
||||
let enter = egui::Event::Key { key: egui::Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE };
|
||||
frame(&mut app, false, vec![enter]);
|
||||
assert!(!app.ui.palette_open, "the pick closes the palette");
|
||||
assert!(app.ui.dialog.is_none(), "{}", app.ui.status);
|
||||
frame(&mut app, false, vec![]);
|
||||
frame(&mut app, false, vec![]);
|
||||
assert_eq!(ctx.memory(|m| m.focused()), Some(field));
|
||||
}
|
||||
|
||||
/// The whole window, with a document whose rectangle is selected, drawn a frame at a time in
|
||||
/// English with screen readers on.
|
||||
struct Window {
|
||||
|
||||
@@ -84,8 +84,9 @@ pub(crate) struct Index {
|
||||
initials_text: String,
|
||||
/// Each result's offset and length in `initials_text`.
|
||||
initials: Vec<(u32, u16)>,
|
||||
/// Each word of a group of words with the same meaning ([`super::words::GROUPS`], and the
|
||||
/// words the UI language's catalog adds), with the group's place in the table.
|
||||
/// Each word of a group of words with the same meaning ([`super::words::GROUPS`], the words the
|
||||
/// UI language's catalog adds, and the English words' stem forms), with the group's place in the
|
||||
/// table.
|
||||
synonyms: HashMap<Box<str>, u16>,
|
||||
}
|
||||
|
||||
@@ -96,8 +97,10 @@ pub(crate) fn is_one_word(w: &str) -> bool {
|
||||
}
|
||||
|
||||
/// The words of each group of [`super::words::GROUPS`], with the words `search_row` gives for the
|
||||
/// group's catalog row ([`super::words::row_key`]) in the UI language. A word the catalog gives
|
||||
/// that is not a single word, or that is already a word of a group, is left out.
|
||||
/// group's catalog row ([`super::words::row_key`]) in the UI language, and the English stem forms
|
||||
/// ([`stems`]) of the group's English words (`setting` of settings). A word the catalog gives that
|
||||
/// is not a single word, or that is already a word of a group, is left out, and so is a stem form
|
||||
/// that is already a word of a group.
|
||||
fn synonym_groups(search_row: impl Fn(&str) -> Option<String>) -> HashMap<Box<str>, u16> {
|
||||
let mut out: HashMap<Box<str>, u16> = HashMap::new();
|
||||
let groups = super::words::GROUPS.iter().enumerate().filter_map(|(g, words)| u16::try_from(g).ok().map(|g| (g, words)));
|
||||
@@ -106,7 +109,7 @@ fn synonym_groups(search_row: impl Fn(&str) -> Option<String>) -> HashMap<Box<st
|
||||
out.insert(w.into(), g);
|
||||
}
|
||||
}
|
||||
for (g, words) in groups {
|
||||
for (g, words) in groups.clone() {
|
||||
let Some(text) = search_row(&super::words::row_key(words)) else { continue };
|
||||
for (w, single) in super::words::row_words(&text) {
|
||||
if single {
|
||||
@@ -114,6 +117,11 @@ fn synonym_groups(search_row: impl Fn(&str) -> Option<String>) -> HashMap<Box<st
|
||||
}
|
||||
}
|
||||
}
|
||||
for (g, words) in groups {
|
||||
for form in words.iter().flat_map(|w| stems(w)) {
|
||||
out.entry(form.into()).or_insert(g);
|
||||
}
|
||||
}
|
||||
out.shrink_to_fit();
|
||||
out
|
||||
}
|
||||
@@ -1197,8 +1205,9 @@ mod tests {
|
||||
|
||||
/// A query word finds a result whose label or menu path holds another word of its group of words
|
||||
/// with the same meaning: below a result whose label holds the word itself, above one whose menu
|
||||
/// path holds the word, and with a menu path word of the group last. A stem form of the query word
|
||||
/// and of a result's word meet the group too; ids, word starts and words of no group do not.
|
||||
/// path holds the word, and with a menu path word of the group last. A stem form of the query word,
|
||||
/// of a result's word and of a group's word meet the group too; ids, word starts and words of no
|
||||
/// group do not.
|
||||
#[test]
|
||||
fn a_word_with_the_same_meaning_finds_the_result() {
|
||||
let c = synthetic(vec![
|
||||
@@ -1214,6 +1223,10 @@ mod tests {
|
||||
let c = synthetic(vec![entry("Zz Unite", "a.one"), entry("Zz Merge", "b.two")]);
|
||||
assert_eq!(ranked(&c, "merge"), ["b.two"], "merge is in no group");
|
||||
assert_eq!(ranked(&c, "combine"), ["a.one"]);
|
||||
// The stem form of a plural word of a group is in the group too: "setting" meets settings.
|
||||
let c = synthetic(vec![entry("Zz Preferences", "a.one"), entry("Zz Other", "b.two")]);
|
||||
assert_eq!(ranked(&c, "setting zz"), ["a.one"], "a stem form of a group's word");
|
||||
assert_eq!(ranked(&c, "settings zz"), ["a.one"]);
|
||||
// Among results that match equally well, the whole query read with the word of the same
|
||||
// meaning ("colour" as "color") puts the label Color first, but never above a label that
|
||||
// holds the query word itself.
|
||||
@@ -1360,6 +1373,7 @@ mod tests {
|
||||
assert!(place(&c, "unite", "object.slice.combine").is_some(), "{:?}", ranked(&c, "unite"));
|
||||
assert!(place(&c, "delete", "view.guides.clear").is_some(), "{:?}", ranked(&c, "delete"));
|
||||
assert!(place(&c, "settings", "edit.preferences").is_some(), "{:?}", ranked(&c, "settings"));
|
||||
assert!(place(&c, "setting", "edit.preferences").is_some(), "{:?}", ranked(&c, "setting"));
|
||||
let merge = ranked(&c, "merge");
|
||||
let unite = merge.iter().position(|id| id == "object.pathfinder.unite");
|
||||
let named = |id: &String| c.list.iter().any(|e| &e.id == id && split_name(&e.shown).0 == "Merge");
|
||||
|
||||
@@ -91,6 +91,27 @@ pub(crate) fn row_problems(source: &str, text: &str) -> Vec<String> {
|
||||
problems
|
||||
}
|
||||
|
||||
/// What is wrong with one catalog's `search` rows, each a group's `source` and its translation
|
||||
/// `text`: each row's [`row_problems`], and a word that rows add to two different groups.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn catalog_problems<'a>(rows: impl IntoIterator<Item = (&'a str, &'a str)>) -> Vec<String> {
|
||||
let mut problems = vec![];
|
||||
let mut groups: std::collections::HashMap<String, &str> = std::collections::HashMap::new();
|
||||
for (source, text) in rows {
|
||||
problems.extend(row_problems(source, text));
|
||||
for (w, _) in row_words(text).filter(|(_, single)| *single) {
|
||||
match groups.get(w.as_str()) {
|
||||
Some(&first) if first != source => problems.push(format!("{w:?} is a word of {first:?} and {source:?}")),
|
||||
Some(_) => {}
|
||||
None => {
|
||||
groups.insert(w, source);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
problems
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -122,4 +143,16 @@ mod tests {
|
||||
assert_eq!(row_problems("remove, delete, erase, clear", "ganz entfernen").len(), 1, "two words");
|
||||
assert_eq!(row_problems("remove, delete, erase, clear", "Text").len(), 1, "a word of another group once lowercased");
|
||||
}
|
||||
|
||||
/// A catalog's rows fail when they add one word to two groups, and pass when a word is repeated
|
||||
/// for one group or the catalog has no rows.
|
||||
#[test]
|
||||
fn a_word_in_two_groups_of_a_catalog_is_checked() {
|
||||
let (remove, artboard) = ("remove, delete, erase, clear", "artboard, canvas");
|
||||
let problems = catalog_problems([(remove, "retrait"), (artboard, "plan, Retrait")]);
|
||||
assert_eq!(problems.len(), 1, "{problems:?}");
|
||||
assert!(problems.iter().all(|p| p.contains("retrait") && p.contains(remove) && p.contains(artboard)), "{problems:?}");
|
||||
assert!(catalog_problems([(remove, "retrait, retrait"), (remove, "Retrait"), (artboard, "plan")]).is_empty());
|
||||
assert!(catalog_problems([]).is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user