mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
The palette fuzzy-matched every menu item against its label and menu path, separators included. A "---" row under File or Filter matched queries like "f" on its path and filled the results with blank rows. Skip separators the same way Help > Search already does. Co-authored-by: Fabio Lussi <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 file changed
+38
-14
@@ -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));
|
||||
|
||||
Reference in new issue
Block a user