From 7f9ff47089b7e352912126951e7af684f67fb513 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 1 Apr 2022 10:00:21 +0200 Subject: [PATCH] Hide breadcrumbs when project search has no results --- crates/breadcrumbs/src/breadcrumbs.rs | 50 ++++++++++++++++++++++----- crates/search/src/buffer_search.rs | 12 +++++-- crates/search/src/project_search.rs | 4 +++ crates/workspace/src/toolbar.rs | 9 +++-- 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/crates/breadcrumbs/src/breadcrumbs.rs b/crates/breadcrumbs/src/breadcrumbs.rs index 94b8a4459e..85723d9399 100644 --- a/crates/breadcrumbs/src/breadcrumbs.rs +++ b/crates/breadcrumbs/src/breadcrumbs.rs @@ -8,16 +8,22 @@ use std::borrow::Cow; use theme::SyntaxTheme; use workspace::{ItemHandle, Settings, ToolbarItemLocation, ToolbarItemView}; +pub enum Event { + UpdateLocation, +} + pub struct Breadcrumbs { editor: Option>, - editor_subscription: Option, + project_search: Option>, + subscriptions: Vec, } impl Breadcrumbs { pub fn new() -> Self { Self { editor: Default::default(), - editor_subscription: Default::default(), + subscriptions: Default::default(), + project_search: Default::default(), } } @@ -42,7 +48,7 @@ impl Breadcrumbs { } impl Entity for Breadcrumbs { - type Event = (); + type Event = Event; } impl View for Breadcrumbs { @@ -90,19 +96,30 @@ impl ToolbarItemView for Breadcrumbs { cx: &mut ViewContext, ) -> ToolbarItemLocation { cx.notify(); - self.editor_subscription = None; + self.subscriptions.clear(); self.editor = None; + self.project_search = None; if let Some(item) = active_pane_item { if let Some(editor) = item.act_as::(cx) { - self.editor_subscription = - Some(cx.subscribe(&editor, |_, _, event, cx| match event { + self.subscriptions + .push(cx.subscribe(&editor, |_, _, event, cx| match event { editor::Event::BufferEdited => cx.notify(), editor::Event::SelectionsChanged { local } if *local => cx.notify(), _ => {} })); self.editor = Some(editor); - if item.downcast::().is_some() { - ToolbarItemLocation::Secondary + if let Some(project_search) = item.downcast::() { + self.subscriptions + .push(cx.subscribe(&project_search, |_, _, _, cx| { + cx.emit(Event::UpdateLocation); + })); + self.project_search = Some(project_search.clone()); + + if project_search.read(cx).has_matches() { + ToolbarItemLocation::Secondary + } else { + ToolbarItemLocation::Hidden + } } else { ToolbarItemLocation::PrimaryLeft } @@ -113,4 +130,21 @@ impl ToolbarItemView for Breadcrumbs { ToolbarItemLocation::Hidden } } + + fn location_for_event( + &self, + _: &Event, + current_location: ToolbarItemLocation, + cx: &AppContext, + ) -> ToolbarItemLocation { + if let Some(project_search) = self.project_search.as_ref() { + if project_search.read(cx).has_matches() { + ToolbarItemLocation::Secondary + } else { + ToolbarItemLocation::Hidden + } + } else { + current_location + } + } } diff --git a/crates/search/src/buffer_search.rs b/crates/search/src/buffer_search.rs index 87f145d4fc..b5f8eedf80 100644 --- a/crates/search/src/buffer_search.rs +++ b/crates/search/src/buffer_search.rs @@ -2,8 +2,9 @@ use crate::{active_match_index, match_index_for_direction, Direction, SearchOpti use collections::HashMap; use editor::{display_map::ToDisplayPoint, Anchor, Autoscroll, Bias, Editor}; use gpui::{ - action, elements::*, keymap::Binding, platform::CursorStyle, Entity, MutableAppContext, - RenderContext, Subscription, Task, View, ViewContext, ViewHandle, WeakViewHandle, + action, elements::*, keymap::Binding, platform::CursorStyle, AppContext, Entity, + MutableAppContext, RenderContext, Subscription, Task, View, ViewContext, ViewHandle, + WeakViewHandle, }; use language::OffsetRangeExt; use project::search::SearchQuery; @@ -164,7 +165,12 @@ impl ToolbarItemView for BufferSearchBar { ToolbarItemLocation::Hidden } - fn location_for_event(&self, _: &Self::Event, _: ToolbarItemLocation) -> ToolbarItemLocation { + fn location_for_event( + &self, + _: &Self::Event, + _: ToolbarItemLocation, + _: &AppContext, + ) -> ToolbarItemLocation { if self.active_editor.is_some() && !self.dismissed { ToolbarItemLocation::Secondary } else { diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs index 7fb7036576..0226e2c844 100644 --- a/crates/search/src/project_search.rs +++ b/crates/search/src/project_search.rs @@ -487,6 +487,10 @@ impl ProjectSearchView { cx.notify(); } } + + pub fn has_matches(&self) -> bool { + self.active_match_index.is_some() + } } impl ProjectSearchBar { diff --git a/crates/workspace/src/toolbar.rs b/crates/workspace/src/toolbar.rs index 06a4f6440b..8bcca1bde3 100644 --- a/crates/workspace/src/toolbar.rs +++ b/crates/workspace/src/toolbar.rs @@ -1,7 +1,7 @@ use crate::{ItemHandle, Settings}; use gpui::{ - elements::*, AnyViewHandle, ElementBox, Entity, MutableAppContext, RenderContext, View, - ViewContext, ViewHandle, + elements::*, AnyViewHandle, AppContext, ElementBox, Entity, MutableAppContext, RenderContext, + View, ViewContext, ViewHandle, }; pub trait ToolbarItemView: View { @@ -15,6 +15,7 @@ pub trait ToolbarItemView: View { &self, _event: &Self::Event, current_location: ToolbarItemLocation, + _cx: &AppContext, ) -> ToolbarItemLocation { current_location } @@ -121,7 +122,9 @@ impl Toolbar { if let Some((_, current_location)) = this.items.iter_mut().find(|(i, _)| i.id() == item.id()) { - let new_location = item.read(cx).location_for_event(event, *current_location); + let new_location = item + .read(cx) + .location_for_event(event, *current_location, cx); if new_location != *current_location { *current_location = new_location; cx.notify();