From 3db67a48b5ace2eef88d530fd838464588570486 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 25 Apr 2023 09:28:50 -0700 Subject: [PATCH] Preserve previous focus when re-focusing project search --- crates/search/src/project_search.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs index 18a3171209..986a4b4bfb 100644 --- a/crates/search/src/project_search.rs +++ b/crates/search/src/project_search.rs @@ -85,6 +85,7 @@ pub struct ProjectSearchView { query_contains_error: bool, active_match_index: Option, search_id: usize, + query_editor_was_focused: bool, } pub struct ProjectSearchBar { @@ -218,7 +219,11 @@ impl View for ProjectSearchView { }); if cx.is_self_focused() { - self.focus_query_editor(cx); + if self.query_editor_was_focused { + cx.focus(&self.query_editor); + } else { + cx.focus(&self.results_editor); + } } } } @@ -448,6 +453,7 @@ impl ProjectSearchView { regex, query_contains_error: false, active_match_index: None, + query_editor_was_focused: false, }; this.model_changed(cx); this @@ -549,10 +555,11 @@ impl ProjectSearchView { } } - fn focus_query_editor(&self, cx: &mut ViewContext) { + fn focus_query_editor(&mut self, cx: &mut ViewContext) { self.query_editor.update(cx, |query_editor, cx| { query_editor.select_all(&SelectAll, cx); }); + self.query_editor_was_focused = true; cx.focus(&self.query_editor); } @@ -561,11 +568,12 @@ impl ProjectSearchView { .update(cx, |query_editor, cx| query_editor.set_text(query, cx)); } - fn focus_results_editor(&self, cx: &mut ViewContext) { + fn focus_results_editor(&mut self, cx: &mut ViewContext) { self.query_editor.update(cx, |query_editor, cx| { let cursor = query_editor.selections.newest_anchor().head(); query_editor.change_selections(None, cx, |s| s.select_ranges([cursor.clone()..cursor])); }); + self.query_editor_was_focused = false; cx.focus(&self.results_editor); }