From 130ea79c95611be311db2299d1acf78e01e5987e Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Thu, 18 Jan 2024 15:04:37 +0100 Subject: [PATCH] Fix focus and re-focus of project-wide search This fixes the issue of `Cmd-shift-f` not refocusing the search bar when a project-wide search already exists. It also fixes the handlers for "search in new" and "new search". Co-authored-by: Kirill --- crates/search/src/project_search.rs | 50 ++++++++++++++--------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs index efb7af3879..c1ebcfe0a6 100644 --- a/crates/search/src/project_search.rs +++ b/crates/search/src/project_search.rs @@ -94,31 +94,29 @@ pub fn init(cx: &mut AppContext) { search_bar.select_next_match(action, cx) }, ); - register_workspace_action( - workspace, - move |search_bar, action: &SelectPrevMatch, cx| { - search_bar.select_prev_match(action, cx) - }, - ); - register_workspace_action_for_dismissed_search( - workspace, - move |workspace, action: &NewSearch, cx| { - ProjectSearchView::new_search(workspace, action, cx) - }, - ); - register_workspace_action_for_dismissed_search( - workspace, - move |workspace, action: &DeploySearch, cx| { - ProjectSearchView::deploy_search(workspace, action, cx) - }, - ); - register_workspace_action_for_dismissed_search( - workspace, - move |workspace, action: &SearchInNew, cx| { - ProjectSearchView::search_in_new(workspace, action, cx) - }, - ); + // Only handle search_in_new if there is a search present + register_workspace_action_for_present_search(workspace, |workspace, action, cx| { + ProjectSearchView::search_in_new(workspace, action, cx) + }); + + // Both on present and dismissed search, we need to unconditionally handle those actions to focus from the editor. + workspace.register_action(move |workspace, action: &DeploySearch, cx| { + if workspace.has_active_modal(cx) { + cx.propagate(); + return; + } + ProjectSearchView::deploy_search(workspace, action, cx); + cx.notify(); + }); + workspace.register_action(move |workspace, action: &NewSearch, cx| { + if workspace.has_active_modal(cx) { + cx.propagate(); + return; + } + ProjectSearchView::new_search(workspace, action, cx); + cx.notify(); + }); }) .detach(); } @@ -2057,7 +2055,7 @@ fn register_workspace_action( }); } -fn register_workspace_action_for_dismissed_search( +fn register_workspace_action_for_present_search( workspace: &mut Workspace, callback: fn(&mut Workspace, &A, &mut ViewContext), ) { @@ -2073,7 +2071,7 @@ fn register_workspace_action_for_dismissed_search( .toolbar() .read(cx) .item_of_type::() - .map(|search_bar| search_bar.read(cx).active_project_search.is_none()) + .map(|search_bar| search_bar.read(cx).active_project_search.is_some()) .unwrap_or(false); if should_notify { callback(workspace, action, cx);