From ce3a31d8bde0f86d964cc90f00c06c68d06e539e Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 14 Apr 2022 18:36:42 +0200 Subject: [PATCH] Persist project search focus state ...so that we can re-focus the previously-active editor when switching back to the project search tab. --- crates/search/src/project_search.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs index 2d1d6c780f..c49ca34052 100644 --- a/crates/search/src/project_search.rs +++ b/crates/search/src/project_search.rs @@ -55,6 +55,7 @@ pub struct ProjectSearchView { regex: bool, query_contains_error: bool, active_match_index: Option, + results_editor_was_focused: bool, } pub struct ProjectSearchBar { @@ -170,10 +171,10 @@ impl View for ProjectSearchView { .insert(self.model.read(cx).project.downgrade(), handle) }); - if self.model.read(cx).match_ranges.is_empty() { - cx.focus(&self.query_editor); - } else { + if self.results_editor_was_focused && !self.model.read(cx).match_ranges.is_empty() { self.focus_results_editor(cx); + } else { + cx.focus(&self.query_editor); } } } @@ -344,6 +345,10 @@ impl ProjectSearchView { cx.emit(ViewEvent::EditorEvent(event.clone())) }) .detach(); + cx.observe_focus(&query_editor, |this, _, _| { + this.results_editor_was_focused = false; + }) + .detach(); let results_editor = cx.add_view(|cx| { let mut editor = Editor::for_multibuffer(excerpts, Some(project), cx); @@ -352,6 +357,10 @@ impl ProjectSearchView { }); cx.observe(&results_editor, |_, _, cx| cx.emit(ViewEvent::UpdateTab)) .detach(); + cx.observe_focus(&results_editor, |this, _, _| { + this.results_editor_was_focused = true; + }) + .detach(); cx.subscribe(&results_editor, |this, _, event, cx| { if matches!(event, editor::Event::SelectionsChanged { .. }) { this.update_match_index(cx); @@ -370,6 +379,7 @@ impl ProjectSearchView { regex, query_contains_error: false, active_match_index: None, + results_editor_was_focused: false, }; this.model_changed(false, cx); this