diff --git a/crates/ui-widgets/src/menu/bar.rs b/crates/ui-widgets/src/menu/bar.rs index 16868c75d..d6907e1b6 100644 --- a/crates/ui-widgets/src/menu/bar.rs +++ b/crates/ui-widgets/src/menu/bar.rs @@ -13,7 +13,7 @@ //! one the title takes the first letter no earlier title holds. The mnemonic is underlined while Alt is held. //! //! The Help menu can start with a search field over a list of `HelpEntry` commands: typing lists up to `search::MAX_RESULTS` -//! matches by label, English label or menu path, each as a command row with its path as detail. +//! matches by label, English label, menu path or command id, each as a command row with its path as detail. #[cfg(feature = "testing")] mod stories; @@ -21,6 +21,7 @@ mod stories; use super::search::{HelpEntry, search_order}; use super::words::MenuWords; use super::{Menu, MenuItem, MenuStep}; +use crate::buttons::Arrangement; use crate::buttons::face::{self, FaceLook, FaceSpec}; use crate::contract::{ Access, Bind, Common, Component, Cx, Focus as _, MeasureCx, MenuKey, Name, NoValue, Outcome, OverlayUi, Overlays as _, Pad, PaintUi, PartsCx, @@ -114,7 +115,7 @@ impl Component for TitleFace { face::measure( cx, &FaceSpec { - arrangement: crate::buttons::Arrangement::Inline, // P3(ribbon-w2): rb.R6 + arrangement: Arrangement::Inline, label: cx.resolved.label, role: None, lead: None, @@ -136,7 +137,7 @@ impl Component for TitleFace { fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { let spec = FaceSpec { - arrangement: crate::buttons::Arrangement::Inline, // P3(ribbon-w2): rb.R6 + arrangement: Arrangement::Inline, label: cx.resolved.label, role: None, lead: None, @@ -401,15 +402,14 @@ impl<'a> MenuBar<'a> { Self::open_only(cx, slots, declared, slots[to]); } - /// The help search's entries as `[label, English label, path]` texts. + /// The help search's entries as `[label, English label, path, command id]` texts. fn help_texts(entries: &[HelpEntry], env: &EnvView<'_>, separator: &str) -> Vec<[String; 4]> { entries .iter() .map(|entry| { let path: Vec = entry.path().iter().map(|label| env.resolve(label).as_str().to_owned()).collect(); let english = entry.english().map(|label| env.resolve(label).as_str().to_owned()); - // P3(dialogs-w9): dg.F26 — the fourth text is the command id. - [env.resolve(entry.label()).as_str().to_owned(), english.unwrap_or_default(), path.join(separator), String::new()] + [env.resolve(entry.label()).as_str().to_owned(), english.unwrap_or_default(), path.join(separator), entry.command().id.clone()] }) .collect() } @@ -571,7 +571,6 @@ impl Component for MenuBar<'_> { } } query.1 = query.1.saturating_add(1); - // P3(dialogs-w9): dg.F26 let refs: Vec<[&str; 4]> = rows .iter() .map(|[label, english, path, id]| [label.as_str(), english.as_str(), path.as_str(), id.as_str()]) diff --git a/crates/ui-widgets/src/menu/search.rs b/crates/ui-widgets/src/menu/search.rs index 44abfa45a..b35fa6700 100644 --- a/crates/ui-widgets/src/menu/search.rs +++ b/crates/ui-widgets/src/menu/search.rs @@ -1,5 +1,5 @@ -//! The command search of the Help menu: [`HelpEntry`], a command found by its label, its English label or its menu -//! path, and [`search_order`], which ranks the entries for a query. +//! The command search of the Help menu: [`HelpEntry`], a command found by its label, its English label, its menu +//! path or its command id, and [`search_order`], which ranks the entries for a query. use crate::runtime::commands::CommandRef; use crate::runtime::env::Label; @@ -65,23 +65,30 @@ impl HelpEntry { } /// The entries matching `query`, best first and in menu order within a rank, at most `MAX_RESULTS`. Each entry is -/// its `[label, english label, menu path, command id]` texts, matched by `folder` (case and accents folded). A label or -/// English label that matches ranks before an entry found only through its path; within each, the matcher's rank decides. +/// its `[label, english label, menu path, command id]` texts, matched by `folder` (case and accents folded). Every +/// word of the query must match one of an entry's texts. A word found in the label or English label ranks before one +/// found only in the path or command id; an entry ranks by its worst tier, then by the worst matcher rank among its +/// words, then by menu order. pub fn search_order(folder: &Folder, query: &str, texts: &[[&str; 4]]) -> Vec { - let query = query.trim(); - if query.is_empty() { + let matchers: Vec = query.split_whitespace().map(|word| Matcher::new(folder, word)).collect(); + if matchers.is_empty() { return Vec::new(); } - let matcher = Matcher::new(folder, query); let mut ranked: Vec<_> = texts .iter() .enumerate() - .filter_map(|(at, [label, english, path, _id])| { - let named = [label, english].into_iter().filter_map(|text| matcher.score(text)).map(|found| found.rank).min(); - match named { - Some(found) => Some((0_u8, found, at)), - None => matcher.score(path).map(|found| (1, found.rank, at)), + .filter_map(|(at, [label, english, path, id])| { + let mut worst = (0_u8, None); + for matcher in &matchers { + let named = [label, english].into_iter().filter_map(|text| matcher.score(text)).map(|found| found.rank).min(); + let (tier, rank) = match named { + Some(rank) => (0_u8, rank), + None => (1, [path, id].into_iter().filter_map(|text| matcher.score(text)).map(|found| found.rank).min()?), + }; + worst.0 = worst.0.max(tier); + worst.1 = worst.1.max(Some(rank)); } + Some((worst.0, worst.1, at)) }) .collect(); ranked.sort(); diff --git a/crates/ui-widgets/tests/it/menu/search.rs b/crates/ui-widgets/tests/it/menu/search.rs index a30998467..d4281bfc1 100644 --- a/crates/ui-widgets/tests/it/menu/search.rs +++ b/crates/ui-widgets/tests/it/menu/search.rs @@ -1 +1,32 @@ -//! Menu search trials (owed by dialogs-w9: this placeholder lets canvas-w8's `mod search;` line compile; dialogs-w9 writes the trials). +//! The help search's ranking: every word of a query, and the command id. + +use craft_i18n::Folder; +use craft_ui::menu::search::{MAX_RESULTS, search_order}; +use craft_ui::testing::trial; + +/// Runs `query` over `texts` in English. +fn order(query: &str, texts: &[[&str; 4]]) -> Vec { + search_order(&Folder::new("en"), query, texts) +} + +#[trial] +fn every_word_of_a_query_must_match_in_any_order_and_the_command_id_ranks_with_the_path() { + let table = [["Table", "Table", "Insert › Tables", "insert.table"]]; + assert_eq!(order("insert table", &table), vec![0]); + assert_eq!(order(" Table INSERT ", &table), vec![0], "any order, any case, extra spaces"); + assert!(order("insert chart", &table).is_empty(), "every word has to match"); + assert!(order("", &table).is_empty()); + assert!(order(" ", &table).is_empty()); + + let one = [["Other", "", "Format › Italic", ""], ["Italic", "", "Format", ""]]; + assert_eq!(order("italic", &one), vec![1, 0], "a label match ranks before a path-only match"); + + let ids = [["Slanted", "", "Format", "format.italic"], ["Format.italic tool", "", "Tools", "tools.x"]]; + assert_eq!(order("format.italic", &ids), vec![1, 0], "the id matches, ranking after a label match"); + assert_eq!(order("FORMAT.ITALIC", &ids), vec![1, 0]); + assert_eq!(order("slanted format.italic", &ids), vec![0], "a word in the label and a word in the id"); + + let labels: Vec = (0..MAX_RESULTS + 5).map(|n| format!("Item {n}")).collect(); + let many: Vec<[&str; 4]> = labels.iter().map(|label| [label.as_str(), "", "", ""]).collect(); + assert_eq!(order("item", &many).len(), MAX_RESULTS); +}