PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Keep menu separators out of the command palette (#2479) (#2632)
Fabio Lussi62aa421d7563
diff --git a/crates/ui-egui/src/palette.rs b/crates/ui-egui/src/palette.rsindex fa149827b..547e25757 100644--- a/crates/ui-egui/src/palette.rs+++ b/crates/ui-egui/src/palette.rs@@ -87,6 +87,29 @@ fn recent_hits(recents: &[String], menus: &[crate::menus::MenuItem], lang: crate.collect()}+/// The menu commands matching `q`, as palette rows. Separators are not commands: they would+/// match on their menu path and list as blank "---" rows (#2479).+fn menu_hits(menus: Vec<crate::menus::MenuItem>, q: &str, lang: crate::i18n::Lang) -> Vec<(i32, String, String, Option<String>, bool)> {+ menus+ .into_iter()+ .filter(|m| m.label != "---")+ .filter_map(|m| {+ let path_en = m.path.join(" › ");+ let path = m.path.iter().map(|p| crate::i18n::tr(lang, p)).collect::<Vec<_>>().join(" › ");+ let label = crate::i18n::tr_id(lang, &m.id, &m.label);+ // Match what is shown and the English name (commands are documented in English).+ let s = fuzzy_score(q, &format!("{label} {path} {} {path_en}", m.label))?;+ Some((+ s,+ m.id,+ label.trim_end_matches('…').to_string(),+ Some(path + &m.shortcut.map(|s| format!(" {}", crate::shortcuts::pretty(&s))).unwrap_or_default()),+ m.enabled,+ ))+ })+ .collect()+}+pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {if !app.ui.palette_open {return;@@ -144,20 +167,7 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {// fresh session) it falls back to the full fuzzy list, as before.let mut hits: Vec<(i32, String, String, Option<String>, bool)> = if empty { recent_hits(&recents, &menus, lang) } else { Vec::new() };if !empty || hits.is_empty() {- hits.extend(menus.into_iter().filter_map(|m| {- let path_en = m.path.join(" › ");- let path = m.path.iter().map(|p| crate::i18n::tr(lang, p)).collect::<Vec<_>>().join(" › ");- let label = crate::i18n::tr_id(lang, &m.id, &m.label);- // Match what is shown and the English name (commands are documented in English).- let s = fuzzy_score(&q, &format!("{label} {path} {} {path_en}", m.label))?;- Some((- s,- m.id,- label.trim_end_matches('…').to_string(),- Some(path + &m.shortcut.map(|s| format!(" {}", crate::shortcuts::pretty(&s))).unwrap_or_default()),- m.enabled,- ))- }));+ hits.extend(menu_hits(menus, &q, lang));for tool in crate::state::Tool::ALL {if let Some(s) = fuzzy_score(&q, &format!("{} {}", tl!(tool.label()), tool.label())) {hits.push((s + 2, format!("tool:{tool:?}"), tl!(tool.label()).into(), Some(format!("Tool {}", tool.key())), true));@@ -315,6 +325,20 @@ mod tests {assert_eq!(super::recent_ids(&ctx).len(), 1, "re-running it moves it to the front, no duplicate");}+ /// #2479: menu separators matched on their menu path ("f" → File, Filter) and filled the+ /// palette with blank "---" rows.+ #[test]+ fn separators_are_not_palette_hits() {+ let app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ let menus = crate::menus::menu_items(&app);+ assert!(menus.iter().any(|m| m.label == "---"), "the menus have separators to filter");+ for q in ["f", "file", "filter", "", "-"] {+ let hits = super::menu_hits(menus.clone(), q, crate::i18n::Lang::EN);+ assert!(!hits.is_empty(), "{q:?} still matches commands");+ assert!(hits.iter().all(|h| h.1 != "---" && h.2 != "---"), "{q:?} lists a separator");+ }+ }+#[test]fn fuzzy_ranks_prefix_and_contiguous_higher() {assert!(fuzzy_score("hue", "Hue/Saturation…").unwrap() > fuzzy_score("hue", "Channel Mixer Hue").unwrap_or(-99));