diff --git a/crates/ui-egui/src/i18n/tests.rs b/crates/ui-egui/src/i18n/tests.rs index b89e5183b0..25f4b73695 100644 --- a/crates/ui-egui/src/i18n/tests.rs +++ b/crates/ui-egui/src/i18n/tests.rs @@ -898,16 +898,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:#?}"); } diff --git a/crates/ui-egui/src/palette.rs b/crates/ui-egui/src/palette.rs index 86c50ae2ea..b6c9ae00bf 100644 --- a/crates/ui-egui/src/palette.rs +++ b/crates/ui-egui/src/palette.rs @@ -1014,6 +1014,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::(spots_wanted_id()), app.ui.palette_open)); @@ -1027,15 +1030,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::(field_id); d.remove::(field_id); - field + let rows = d.get_temp::>(rows_id).unwrap_or_default(); + d.remove::>(rows_id); + (field, rows) }); let now = ctx.memory(|m| m.focused()); if let Some(Some(before)) = ctx.data_mut(|d| d.remove_temp::>(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)); } @@ -1167,12 +1172,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(); } @@ -1181,6 +1188,7 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) { } } }); + ui.ctx().data_mut(|d| d.insert_temp(rows_id, rows)); r }) .inner; @@ -3163,6 +3171,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| { + 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 { diff --git a/crates/ui-egui/src/palette/search.rs b/crates/ui-egui/src/palette/search.rs index d87cf4c182..d7191891a5 100644 --- a/crates/ui-egui/src/palette/search.rs +++ b/crates/ui-egui/src/palette/search.rs @@ -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, 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) -> HashMap, u16> { let mut out: HashMap, 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) -> HashMap Option) -> HashMap Vec { 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) -> Vec { + let mut problems = vec![]; + let mut groups: std::collections::HashMap = 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()); + } }