From 2c6d0aee5fa02888912a2260e7cb3e568cd4f369 Mon Sep 17 00:00:00 2001 From: DarkSky Date: Sat, 5 Dec 2026 07:56:11 +0900 Subject: [PATCH] feat(desktop): add explicit quick search tag filter mode Remove implicit `#` prefix tag routing in default quick search. Queries such as `#123` and `#` now match titles, content, collections, tags, and commands literally. Add `QuickCommandId::FilterDocsByTag` ("Filter docs by tag") to enter a dedicated tag filter submode. Show a `Tag` pill badge and "Filter docs by tag..." placeholder in the search bar. Filter results to matching tags and tagged documents. Return to main search on empty `Backspace`, `Escape`, or pill click. --- .../frontend/desktop/src/doc_store/views.rs | 1 + .../frontend/desktop/src/quick_search/mod.rs | 105 ++++++++-- .../desktop/src/quick_search/modal.rs | 166 +++++++++++++-- packages/frontend/desktop/src/state/mod.rs | 21 +- .../desktop/src/workbench/interaction.rs | 4 + .../desktop/src/workbench/search_input.rs | 190 +++++++++++++++++- 6 files changed, 443 insertions(+), 44 deletions(-) diff --git a/packages/frontend/desktop/src/doc_store/views.rs b/packages/frontend/desktop/src/doc_store/views.rs index 3edb1eec9a..adde8a97ed 100644 --- a/packages/frontend/desktop/src/doc_store/views.rs +++ b/packages/frontend/desktop/src/doc_store/views.rs @@ -657,6 +657,7 @@ mod tests { } store.reindex_all(); let mut qs = QuickSearchState::default(); + qs.enter_tag_filter_mode(); qs.set_query("#Old name"); assert!(qs.items(&store).iter().any(|item| matches!(item, QuickSearchItem::Doc { summary, .. } if summary == "#Old name" diff --git a/packages/frontend/desktop/src/quick_search/mod.rs b/packages/frontend/desktop/src/quick_search/mod.rs index 0a1c5c0093..6e2c2eccba 100644 --- a/packages/frontend/desktop/src/quick_search/mod.rs +++ b/packages/frontend/desktop/src/quick_search/mod.rs @@ -25,6 +25,7 @@ pub enum QuickCommandId { /// Set the width of the page in the active pane. SetPageWidth(PageWidth), ColorMode(ThemePreference), + FilterDocsByTag, OpenTodayJournal, ToggleSidebar, } @@ -39,6 +40,7 @@ impl QuickCommandId { Self::ColorMode(ThemePreference::System) => "Change colour mode to System", Self::ColorMode(ThemePreference::Dark) => "Change colour mode to Dark", Self::ColorMode(ThemePreference::Light) => "Change colour mode to Light", + Self::FilterDocsByTag => "Filter docs by tag", Self::OpenTodayJournal => "Open Today's Journal", Self::ToggleSidebar => "Toggle Sidebar", } @@ -70,7 +72,11 @@ impl QuickCommandId { .filter(move |mode| *mode != color_mode) .map(Self::ColorMode), ) - .chain([Self::OpenTodayJournal, Self::ToggleSidebar]) + .chain([ + Self::FilterDocsByTag, + Self::OpenTodayJournal, + Self::ToggleSidebar, + ]) } } @@ -114,6 +120,7 @@ impl QuickSearchItem { pub struct QuickSearchState { pub open: bool, pub query: String, + pub tag_filter_mode: bool, pub selected_index: usize, pub scroll_y: f32, pub(crate) text_bounds: Option>, @@ -125,6 +132,7 @@ impl QuickSearchState { pub fn open(&mut self) { self.open = true; self.query.clear(); + self.tag_filter_mode = false; self.text_bounds = None; self.selected_index = 0; self.scroll_y = 0.0; @@ -133,11 +141,26 @@ impl QuickSearchState { pub fn close(&mut self) { self.open = false; self.query.clear(); + self.tag_filter_mode = false; self.text_bounds = None; self.selected_index = 0; self.scroll_y = 0.0; } + pub fn enter_tag_filter_mode(&mut self) { + self.tag_filter_mode = true; + self.query.clear(); + self.selected_index = 0; + self.scroll_y = 0.0; + } + + pub fn exit_tag_filter_mode(&mut self) { + self.tag_filter_mode = false; + self.query.clear(); + self.selected_index = 0; + self.scroll_y = 0.0; + } + pub fn set_query(&mut self, query: impl Into) { self.query = query.into(); self.selected_index = 0; @@ -167,7 +190,7 @@ impl QuickSearchState { PageWidth::Full } }); - if !self.query.trim().is_empty() { + if self.tag_filter_mode || !self.query.trim().is_empty() { return self.items_with_recent(store, &[], page_width); } @@ -207,7 +230,9 @@ impl QuickSearchState { page_width: Option, ) -> Vec { let query = self.query.trim(); - let tag_query = query.strip_prefix('#').map(str::trim); + let tag_query = self + .tag_filter_mode + .then(|| query.strip_prefix('#').unwrap_or(query).trim()); let (collections, tags) = group_matches(store, query, tag_query); let (text_hits, tag_hits) = if let Some(name) = tag_query { ( @@ -272,11 +297,11 @@ impl QuickSearchState { if tag_query.is_some() { out.extend(tags); out.extend(docs); - } else { - out.extend(docs); - out.extend(collections); - out.extend(tags); + return out; } + out.extend(docs); + out.extend(collections); + out.extend(tags); let q = query.to_ascii_lowercase(); for cmd in QuickCommandId::available(self.color_mode, page_width) { if !q.is_empty() && matches!(cmd, QuickCommandId::NewPage | QuickCommandId::NewEdgeless) @@ -309,7 +334,7 @@ fn group_matches( query: &str, tag_query: Option<&str>, ) -> (Vec, Vec) { - if query.is_empty() { + if tag_query.is_none() && query.is_empty() { return (Vec::new(), Vec::new()); } @@ -538,21 +563,44 @@ mod tests { ] ); - search.set_query("#ARCH"); - let results = search.items(&store); + let issue_id = store.create_doc("Issue #123", DocMode::Page, None, 1_764_320_000_000); + let issue_summary = store + .get_doc(&issue_id) + .expect("issue doc") + .meta + .summary + .clone(); + search.set_query("#123"); + let literal_results = search.items(&store); assert_eq!( - results.first(), - Some(&QuickSearchItem::Tag { - tag_name: "architecture".into(), - doc_count: 2, + literal_results.first(), + Some(&QuickSearchItem::Doc { + doc_id: issue_id, + title: "Issue #123".into(), + summary: issue_summary, + is_edgeless: false, + recent: false, }) ); - let mut groups: Vec<_> = results.iter().map(QuickSearchItem::group).collect(); - groups.dedup(); - assert_eq!(groups, ["Tags", "Docs", "Actions"]); + + search.set_query("#ARCH"); + let results = search.items(&store); + assert!( + !results + .iter() + .any(|item| matches!(item, QuickSearchItem::Tag { .. })) + ); search.set_query("#"); let results = search.items(&store); + assert!( + !results + .iter() + .any(|item| matches!(item, QuickSearchItem::Tag { .. })) + ); + + search.enter_tag_filter_mode(); + let results = search.items(&store); let tag_count = results .iter() .take_while(|item| item.group() == "Tags") @@ -562,6 +610,29 @@ mod tests { results.get(tag_count), Some(QuickSearchItem::Doc { .. }) )); + assert!(results.iter().all(|item| matches!( + item, + QuickSearchItem::Tag { .. } | QuickSearchItem::Doc { .. } + ))); + + for query in ["ARCH", "#ARCH"] { + search.set_query(query); + let results = search.items(&store); + assert_eq!( + results.first(), + Some(&QuickSearchItem::Tag { + tag_name: "architecture".into(), + doc_count: 2, + }) + ); + let mut groups: Vec<_> = results.iter().map(QuickSearchItem::group).collect(); + groups.dedup(); + assert_eq!(groups, ["Tags", "Docs"]); + } + + search.exit_tag_filter_mode(); + assert!(!search.tag_filter_mode); + assert!(search.query.is_empty()); } fn recent_titles(store: &DesktopDocStore, split: &SplitLayout) -> Vec { diff --git a/packages/frontend/desktop/src/quick_search/modal.rs b/packages/frontend/desktop/src/quick_search/modal.rs index a3d16c819b..eae0f1faf4 100644 --- a/packages/frontend/desktop/src/quick_search/modal.rs +++ b/packages/frontend/desktop/src/quick_search/modal.rs @@ -76,7 +76,11 @@ impl RenderOnce for QuickSearchModal { let vp_h = f32::from(viewport.height); let compact = vp_w < 640.0; let query = qs.query.trim(); - let needle = query.strip_prefix('#').unwrap_or(query).trim(); + let needle = if qs.tag_filter_mode { + query.strip_prefix('#').unwrap_or(query).trim() + } else { + query + }; let ends = row_ends(&items); let content_height = ends.last().copied().unwrap_or(RESULT_ROW); let height = list_height(&ends, content_height, vp_w, vp_h, self.height); @@ -291,8 +295,10 @@ impl RenderOnce for QuickSearchModal { } let input_shell = self.shell.clone(); + let pill_shell = self.shell.clone(); let focus = self.focus; let empty = qs.query.is_empty(); + let tag_filter_mode = qs.tag_filter_mode; let input = div() .id("desktop.qs.input") .snapshot("desktop.qs.input", Some("desktop.qs.modal")) @@ -305,11 +311,46 @@ impl RenderOnce for QuickSearchModal { }) .flex() .items_center() + .gap(px(if tag_filter_mode { 8.0 } else { 0.0 })) .px(px(16.0)) .h(px(INPUT_HEIGHT)) .flex_shrink(0.0) .border_b_1() .border_color(palette.border) + .children(tag_filter_mode.then(|| { + div() + .id("desktop.qs.tag_pill") + .snapshot("desktop.qs.tag_pill", Some("desktop.qs.input")) + .flex_shrink(0.0) + .flex() + .items_center() + .gap(px(4.0)) + .rounded(px(4.0)) + .px(px(8.0)) + .py(px(2.0)) + .bg(palette.bg_pill) + .text_size(px(12.0)) + .font_weight(FontWeight::MEDIUM) + .line_height(px(20.0)) + .text_color(palette.text_secondary) + .cursor_pointer() + .hover(move |s| s.bg(selected_bg)) + .on_mouse_down(MouseButton::Left, |_, _, app| { + app.stop_propagation(); + }) + .on_click(move |_, window, app| { + pill_shell.update(app, |shell, cx| shell.exit_tag_filter_mode(window, cx)); + app.stop_propagation(); + }) + .child( + svg() + .data(TAG_ICON_SVG) + .size(px(14.0)) + .flex_shrink(0.0) + .text_color(palette.text_secondary), + ) + .child("Tag") + })) .child( div() .relative() @@ -325,7 +366,11 @@ impl RenderOnce for QuickSearchModal { .text_ellipsis() .text_size(px(18.0)) .line_height(px(28.0)) - .child("Search docs, tags, or commands…") + .child(if tag_filter_mode { + "Filter docs by tag..." + } else { + "Search docs, tags, or commands…" + }) })) .child(search_input_handler( &self.input, @@ -446,6 +491,7 @@ fn updated_time(timestamp_ms: i64) -> String { fn command_icon(command: QuickCommandId) -> &'static [u8] { match command { QuickCommandId::NewPage | QuickCommandId::NewEdgeless => PLUS_ICON_SVG, + QuickCommandId::FilterDocsByTag => TAG_ICON_SVG, QuickCommandId::OpenTodayJournal => JOURNAL_ICON_SVG, QuickCommandId::ToggleSidebar => SIDEBAR_TOGGLE_SVG, QuickCommandId::SetPageWidth(_) => DOC_ICON_SVG, @@ -518,10 +564,12 @@ mod tests { QuickCommandId::SetPageWidth(PageWidth::Full), QuickCommandId::ColorMode(ThemePreference::System), QuickCommandId::ColorMode(ThemePreference::Dark), + QuickCommandId::FilterDocsByTag, QuickCommandId::OpenTodayJournal, QuickCommandId::ToggleSidebar, ] ); + assert_eq!(command_icon(QuickCommandId::FilterDocsByTag), TAG_ICON_SVG); state.execute_quick_command(QuickCommandId::SetPageWidth(PageWidth::Full)); assert!(state.effective_page_full_width("doc-arch")); state.open_quick_search(); @@ -561,7 +609,7 @@ mod tests { } #[test] - fn quick_search_restricts_tag_queries_and_appends_named_create_actions() { + fn quick_search_keeps_hash_literal_in_main_mode_and_filters_tags_in_submode() { let store = DesktopDocStore::seed_showcase("2026-12-02"); let mut qs = QuickSearchState::default(); for query in ["#architecture", "tags."] { @@ -571,11 +619,25 @@ mod tests { QuickSearchItem::Command(QuickCommandId::NewPage), QuickSearchItem::Command(QuickCommandId::NewEdgeless), ])); - if query.starts_with('#') { - assert!(items[..items.len() - 2].iter().any(|item| { - matches!(item, QuickSearchItem::Doc { summary, .. } if summary.contains(query)) - })); - } + assert!(!items.iter().any(|item| matches!( + item, + QuickSearchItem::Doc { summary, .. } if summary.contains("#architecture") + ))); + } + + qs.enter_tag_filter_mode(); + for query in ["architecture", "#architecture"] { + qs.set_query(query); + let items = qs.items(&store); + assert!( + !items + .iter() + .any(|item| matches!(item, QuickSearchItem::Command(_))) + ); + assert!(items.iter().any(|item| matches!( + item, + QuickSearchItem::Doc { summary, .. } if summary.contains("#architecture") + ))); } } @@ -837,12 +899,15 @@ mod tests { open_test_shell_with_state(scale_search_state(ThemeMode::Light)); app.update(|cx| { handle - .update(cx, |shell, window, cx| shell.open_search(window, cx)) + .update(cx, |shell, window, cx| { + shell.open_search(window, cx); + shell.enter_tag_filter_mode(window, cx); + }) .expect("desktop view"); }); window.request_frame(); let mut input = window.take_input_handler().expect("search input"); - input.replace_text_in_range(None, "#scale"); + input.replace_text_in_range(None, "scale"); window.set_input_handler(input); window.request_frame(); app.update(|cx| { @@ -880,7 +945,7 @@ mod tests { for (delta, expected_count, first_tag) in [ (0.0, 6, Some(0)), (-middle, 8, Some(512)), - (-100_000.0, 6, None), + (-100_000.0, 7, None), (100_000.0, 6, Some(0)), (-middle, 8, Some(512)), ] { @@ -914,7 +979,7 @@ mod tests { rows.iter() .all(|(id, _)| !id.starts_with("desktop.qs.tag.")) ); - assert!(rows.iter().any(|(id, _)| id.starts_with("desktop.qs.cmd."))); + assert!(rows.iter().all(|(id, _)| id.starts_with("desktop.qs.doc."))); } }) .expect("desktop view"); @@ -971,12 +1036,15 @@ mod tests { ] { app.update(|cx| { handle - .update(cx, |shell, window, cx| shell.open_search(window, cx)) + .update(cx, |shell, window, cx| { + shell.open_search(window, cx); + shell.enter_tag_filter_mode(window, cx); + }) .expect("desktop view"); }); window.request_frame(); let mut input = window.take_input_handler().expect("search input"); - input.replace_and_mark_text_in_range(None, "#scale", Some(6..6)); + input.replace_and_mark_text_in_range(None, "scale", Some(5..5)); window.set_input_handler(input); key(&window, "down"); key(&window, "enter"); @@ -984,8 +1052,9 @@ mod tests { handle .update(cx, |shell, _, _| { assert_eq!(shell.state.quick_search.selected_index, 0); - assert_eq!(shell.state.quick_search.query, "#scale"); + assert_eq!(shell.state.quick_search.query, "scale"); assert!(shell.state.quick_search.open); + assert!(shell.state.quick_search.tag_filter_mode); }) .expect("desktop view"); }); @@ -1172,4 +1241,71 @@ mod tests { .expect("save quick search evidence"); } } + + #[test] + fn quick_search_saves_tag_filter_evidence_when_requested() { + let Some(dir) = std::env::var_os("AFFINE_QS_TAG_EVIDENCE_DIR") else { + return; + }; + let dir = std::path::PathBuf::from(dir); + std::fs::create_dir_all(&dir).expect("create evidence dir"); + + for (theme, tag) in [(ThemeMode::Light, "light"), (ThemeMode::Dark, "dark")] { + let (app, handle, mut window) = open_test_shell(theme); + let set_query = |window: &mut HeadlessWindow, text: &str| { + let mut input = window.take_input_handler().expect("search input"); + input.replace_text_in_range(None, text); + window.set_input_handler(input); + window.request_frame(); + }; + let render = |name: &str| { + let image = app.update(|cx| { + handle + .update(cx, |_, window, _| window.render_to_image().expect("render")) + .expect("desktop view") + }); + image + .save(dir.join(format!("{name}-{tag}.png"))) + .expect("save evidence image"); + }; + + app.update(|cx| { + handle + .update(cx, |shell, window, cx| { + shell.state.store.create_doc( + "Issue #123", + DocMode::Page, + None, + 1_764_320_000_000, + ); + shell.open_search(window, cx); + }) + .expect("desktop view"); + }); + window.request_frame(); + set_query(&mut window, "#123"); + render("literal-hash"); + + app.update(|cx| { + handle + .update(cx, |shell, window, cx| shell.open_search(window, cx)) + .expect("desktop view"); + }); + window.request_frame(); + set_query(&mut window, "filter"); + render("cmd-tag-filter"); + + app.update(|cx| { + handle + .update(cx, |shell, window, cx| { + shell.enter_tag_filter_mode(window, cx); + }) + .expect("desktop view"); + }); + window.request_frame(); + render("submode-tag-filter"); + set_query(&mut window, "arch"); + render("submode-tag-filter-typed"); + } + } } diff --git a/packages/frontend/desktop/src/state/mod.rs b/packages/frontend/desktop/src/state/mod.rs index b5227a7add..6f15198012 100644 --- a/packages/frontend/desktop/src/state/mod.rs +++ b/packages/frontend/desktop/src/state/mod.rs @@ -708,12 +708,8 @@ impl DesktopAppState { pub fn execute_quick_command(&mut self, cmd: QuickCommandId) { match cmd { - QuickCommandId::NewPage => { - self.create_new_doc(DocMode::Page); - } - QuickCommandId::NewEdgeless => { - self.create_new_doc(DocMode::Edgeless); - } + QuickCommandId::NewPage => drop(self.create_new_doc(DocMode::Page)), + QuickCommandId::NewEdgeless => drop(self.create_new_doc(DocMode::Edgeless)), QuickCommandId::SetPageWidth(width) => { if let Some(doc_id) = self.split.active().active_route().doc_id() { let doc_id = doc_id.to_owned(); @@ -721,9 +717,8 @@ impl DesktopAppState { } } QuickCommandId::ColorMode(mode) => self.set_theme_preference(mode), - QuickCommandId::OpenTodayJournal => { - self.open_today_journal(); - } + QuickCommandId::FilterDocsByTag => self.quick_search.enter_tag_filter_mode(), + QuickCommandId::OpenTodayJournal => drop(self.open_today_journal()), QuickCommandId::ToggleSidebar => self.sidebar.toggle_open(), } } @@ -737,6 +732,13 @@ impl DesktopAppState { let Some(item) = items.get(self.quick_search.selected_index).cloned() else { return false; }; + if let QuickSearchItem::Command(cmd) = item { + if cmd != QuickCommandId::FilterDocsByTag { + self.quick_search.close(); + } + self.execute_quick_command(cmd); + return true; + } self.quick_search.close(); match item { QuickSearchItem::Doc { doc_id, .. } => { @@ -746,7 +748,6 @@ impl DesktopAppState { self.open_doc_in_active_pane(&doc_id); } } - QuickSearchItem::Command(cmd) => self.execute_quick_command(cmd), group => self.open_search_group(group, open_in_split), } true diff --git a/packages/frontend/desktop/src/workbench/interaction.rs b/packages/frontend/desktop/src/workbench/interaction.rs index f34184e1a9..da8b9b1b8e 100644 --- a/packages/frontend/desktop/src/workbench/interaction.rs +++ b/packages/frontend/desktop/src/workbench/interaction.rs @@ -195,6 +195,10 @@ impl DesktopShellView { self.save_preferences(before); false } + QuickCommandId::FilterDocsByTag => { + self.enter_tag_filter_mode(window, cx); + return; + } QuickCommandId::SetPageWidth(width) => { if let Some(doc_id) = self.state.split.active().active_route().doc_id() { let doc_id = doc_id.to_owned(); diff --git a/packages/frontend/desktop/src/workbench/search_input.rs b/packages/frontend/desktop/src/workbench/search_input.rs index cab0b61b20..f1b635c4bf 100644 --- a/packages/frontend/desktop/src/workbench/search_input.rs +++ b/packages/frontend/desktop/src/workbench/search_input.rs @@ -132,6 +132,27 @@ impl DesktopShellView { cx.notify(); } + pub(crate) fn enter_tag_filter_mode(&mut self, window: &mut Window, cx: &mut Context) { + self.state.quick_search.enter_tag_filter_mode(); + self.reset_search_input_field(window, cx); + } + + pub(crate) fn exit_tag_filter_mode(&mut self, window: &mut Window, cx: &mut Context) { + self.state.quick_search.exit_tag_filter_mode(); + self.reset_search_input_field(window, cx); + } + + fn reset_search_input_field(&mut self, window: &mut Window, cx: &mut Context) { + let changed = !self.search_input.display_text().is_empty() || self.search_input.composing(); + self.search_input = Utf16ImeBuffer::new(String::new(), false); + window.focus(&self.quick_search_focus, cx); + if changed { + window.notify_text_input_changed(&self.quick_search_focus); + } + window.notify_text_selection_changed(&self.quick_search_focus); + cx.notify(); + } + pub(super) fn handle_search_key( &mut self, event: &KeyDownEvent, @@ -145,6 +166,8 @@ impl DesktopShellView { if self.search_input.composing() { self.search_input.cancel_preedit(); self.search_input_changed(window, cx); + } else if self.state.quick_search.tag_filter_mode { + self.exit_tag_filter_mode(window, cx); } else { self.dismiss_modal(window, cx); } @@ -157,6 +180,15 @@ impl DesktopShellView { if self.search_input.composing() { return refocus; } + if key == "backspace" + && !command + && !modifiers.alt + && self.state.quick_search.tag_filter_mode + && self.state.quick_search.query.is_empty() + { + self.exit_tag_filter_mode(window, cx); + return true; + } match key { "up" | "down" => { @@ -851,32 +883,37 @@ mod tests { fn quick_search_group_rows_navigate_by_id_and_focus_the_destination_pane() { use crate::DesktopRoute; - for (query, row, key, route) in [ + for (tag_mode, query, row, key, route) in [ ( + true, "#diagrams", "desktop.qs.tag.architecture", Some("enter"), DesktopRoute::Tag("architecture".into()), ), ( + true, "#diagrams", "desktop.qs.tag.architecture", Some("shift-enter"), DesktopRoute::Tag("architecture".into()), ), ( + false, "Rust Native Core", "desktop.qs.collection.col-rust-core", Some("ctrl-enter"), DesktopRoute::Collection("col-rust-core".into()), ), ( + false, "Rust Native Core", "desktop.qs.collection.col-rust-core", Some("alt-enter"), DesktopRoute::Collection("col-rust-core".into()), ), ( + false, "Rust Native Core", "desktop.qs.collection.col-rust-core", None, @@ -887,6 +924,9 @@ mod tests { app.shell(|shell, window, cx| { assert!(shell.state.store.rename_tag("architecture", "Diagrams")); shell.open_search(window, cx); + if tag_mode { + shell.enter_tag_filter_mode(window, cx); + } }); app.frame(); app.input(|input| input.replace_text_in_range(None, query)); @@ -975,7 +1015,7 @@ mod tests { }) .collect::>() }); - assert_eq!(row_ids.len(), 11); + assert_eq!(row_ids.len(), 12); for (index, id) in row_ids .iter() .enumerate() @@ -1093,4 +1133,150 @@ mod tests { }); } } + + #[test] + fn quick_search_literal_hash_and_tag_filter_submode_round_trip() { + use crate::{DesktopRoute, quick_search::QuickCommandId}; + use affine_store::directory::DocMode; + + let app = TestShell::new(1280, 800); + let (issue_id, issue_summary) = app.shell(|shell, _, _| { + assert!(shell.state.store.rename_tag("architecture", "Diagrams")); + let id = + shell + .state + .store + .create_doc("Issue #123", DocMode::Page, None, 1_764_320_000_000); + let summary = shell + .state + .store + .get_doc(&id) + .expect("issue doc") + .meta + .summary + .clone(); + (id, summary) + }); + app.click("desktop.sidebar.quick_search"); + + // Typing `#123` in main mode searches literally without tag hijacking. + app.input(|input| input.replace_text_in_range(None, "#123")); + app.shell(|shell, window, _| { + let items = shell.state.quick_search.items(&shell.state.store); + assert!(!shell.state.quick_search.tag_filter_mode); + assert_eq!( + items.first(), + Some(&QuickSearchItem::Doc { + doc_id: issue_id.clone(), + title: "Issue #123".into(), + summary: issue_summary.clone(), + is_edgeless: false, + recent: false, + }) + ); + assert!( + !window + .frame_snapshot() + .elements + .contains_key("desktop.qs.tag_pill") + ); + }); + app.key("enter"); + app.shell(|shell, _, _| { + assert!(!shell.state.quick_search.open); + assert_eq!( + shell.state.split.active().active_route(), + &DesktopRoute::Doc(issue_id.clone()) + ); + }); + + // Activate `Filter docs by tag` via Enter and click, then exit via Backspace / Escape / pill. + for exit_method in ["backspace", "escape-empty", "escape-query", "pill"] { + app.click("desktop.sidebar.quick_search"); + app.input(|input| input.replace_text_in_range(None, "filter docs")); + let cmd_row = app.shell(|shell, _, _| { + let items = shell + .state + .quick_search + .items_with_split(&shell.state.store, &shell.state.split); + let index = items + .iter() + .position(|item| { + *item == QuickSearchItem::Command(QuickCommandId::FilterDocsByTag) + }) + .expect("FilterDocsByTag command"); + format!("desktop.qs.cmd.{index}") + }); + if exit_method == "pill" { + app.click(&cmd_row); + } else { + app.key("enter"); + } + app.shell(|shell, window, _| { + assert!(shell.state.quick_search.open); + assert!(shell.state.quick_search.tag_filter_mode); + assert!(shell.state.quick_search.query.is_empty()); + assert!(shell.search_input.display_text().is_empty()); + assert!(shell.quick_search_focus.is_focused(window)); + assert!( + window + .frame_snapshot() + .elements + .contains_key("desktop.qs.tag_pill") + ); + }); + + // Non-empty Backspace only edits the query and stays in tag filter mode. + app.input(|input| input.replace_text_in_range(None, "diag")); + app.key("backspace"); + app.shell(|shell, _, _| { + assert!(shell.state.quick_search.tag_filter_mode); + assert_eq!(shell.state.quick_search.query, "dia"); + }); + + match exit_method { + "backspace" => { + app.input(|input| input.replace_text_in_range(Some(0..3), "")); + app.key("backspace"); + } + "escape-empty" => { + app.input(|input| input.replace_text_in_range(Some(0..3), "")); + app.key("escape"); + } + "escape-query" => { + app.key("escape"); + } + "pill" => { + app.click("desktop.qs.tag_pill"); + } + _ => unreachable!(), + } + app.shell(|shell, window, _| { + assert!(shell.state.quick_search.open); + assert!(!shell.state.quick_search.tag_filter_mode); + assert!(shell.state.quick_search.query.is_empty()); + assert!(shell.search_input.display_text().is_empty()); + assert!(shell.quick_search_focus.is_focused(window)); + assert!( + !window + .frame_snapshot() + .elements + .contains_key("desktop.qs.tag_pill") + ); + }); + app.key("escape"); + app.shell(|shell, _, _| assert!(!shell.state.quick_search.open)); + } + + // Backdrop click closes the entire modal even while in tag filter mode. + app.click("desktop.sidebar.quick_search"); + app.shell(|shell, window, cx| shell.enter_tag_filter_mode(window, cx)); + app.frame(); + app.window.simulate_click(point(px(8.0), px(80.0))); + app.frame(); + app.shell(|shell, _, _| { + assert!(!shell.state.quick_search.open); + assert!(!shell.state.quick_search.tag_filter_mode); + }); + } }