Hide breadcrumbs when project search has no results

This commit is contained in:
Antonio Scandurra 2022-04-01 10:00:21 +02:00
parent 9f939bd007
commit 7f9ff47089
4 changed files with 61 additions and 14 deletions

View file

@ -8,16 +8,22 @@ use std::borrow::Cow;
use theme::SyntaxTheme; use theme::SyntaxTheme;
use workspace::{ItemHandle, Settings, ToolbarItemLocation, ToolbarItemView}; use workspace::{ItemHandle, Settings, ToolbarItemLocation, ToolbarItemView};
pub enum Event {
UpdateLocation,
}
pub struct Breadcrumbs { pub struct Breadcrumbs {
editor: Option<ViewHandle<Editor>>, editor: Option<ViewHandle<Editor>>,
editor_subscription: Option<Subscription>, project_search: Option<ViewHandle<ProjectSearchView>>,
subscriptions: Vec<Subscription>,
} }
impl Breadcrumbs { impl Breadcrumbs {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
editor: Default::default(), editor: Default::default(),
editor_subscription: Default::default(), subscriptions: Default::default(),
project_search: Default::default(),
} }
} }
@ -42,7 +48,7 @@ impl Breadcrumbs {
} }
impl Entity for Breadcrumbs { impl Entity for Breadcrumbs {
type Event = (); type Event = Event;
} }
impl View for Breadcrumbs { impl View for Breadcrumbs {
@ -90,19 +96,30 @@ impl ToolbarItemView for Breadcrumbs {
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> ToolbarItemLocation { ) -> ToolbarItemLocation {
cx.notify(); cx.notify();
self.editor_subscription = None; self.subscriptions.clear();
self.editor = None; self.editor = None;
self.project_search = None;
if let Some(item) = active_pane_item { if let Some(item) = active_pane_item {
if let Some(editor) = item.act_as::<Editor>(cx) { if let Some(editor) = item.act_as::<Editor>(cx) {
self.editor_subscription = self.subscriptions
Some(cx.subscribe(&editor, |_, _, event, cx| match event { .push(cx.subscribe(&editor, |_, _, event, cx| match event {
editor::Event::BufferEdited => cx.notify(), editor::Event::BufferEdited => cx.notify(),
editor::Event::SelectionsChanged { local } if *local => cx.notify(), editor::Event::SelectionsChanged { local } if *local => cx.notify(),
_ => {} _ => {}
})); }));
self.editor = Some(editor); self.editor = Some(editor);
if item.downcast::<ProjectSearchView>().is_some() { if let Some(project_search) = item.downcast::<ProjectSearchView>() {
ToolbarItemLocation::Secondary 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 { } else {
ToolbarItemLocation::PrimaryLeft ToolbarItemLocation::PrimaryLeft
} }
@ -113,4 +130,21 @@ impl ToolbarItemView for Breadcrumbs {
ToolbarItemLocation::Hidden 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
}
}
} }

View file

@ -2,8 +2,9 @@ use crate::{active_match_index, match_index_for_direction, Direction, SearchOpti
use collections::HashMap; use collections::HashMap;
use editor::{display_map::ToDisplayPoint, Anchor, Autoscroll, Bias, Editor}; use editor::{display_map::ToDisplayPoint, Anchor, Autoscroll, Bias, Editor};
use gpui::{ use gpui::{
action, elements::*, keymap::Binding, platform::CursorStyle, Entity, MutableAppContext, action, elements::*, keymap::Binding, platform::CursorStyle, AppContext, Entity,
RenderContext, Subscription, Task, View, ViewContext, ViewHandle, WeakViewHandle, MutableAppContext, RenderContext, Subscription, Task, View, ViewContext, ViewHandle,
WeakViewHandle,
}; };
use language::OffsetRangeExt; use language::OffsetRangeExt;
use project::search::SearchQuery; use project::search::SearchQuery;
@ -164,7 +165,12 @@ impl ToolbarItemView for BufferSearchBar {
ToolbarItemLocation::Hidden 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 { if self.active_editor.is_some() && !self.dismissed {
ToolbarItemLocation::Secondary ToolbarItemLocation::Secondary
} else { } else {

View file

@ -487,6 +487,10 @@ impl ProjectSearchView {
cx.notify(); cx.notify();
} }
} }
pub fn has_matches(&self) -> bool {
self.active_match_index.is_some()
}
} }
impl ProjectSearchBar { impl ProjectSearchBar {

View file

@ -1,7 +1,7 @@
use crate::{ItemHandle, Settings}; use crate::{ItemHandle, Settings};
use gpui::{ use gpui::{
elements::*, AnyViewHandle, ElementBox, Entity, MutableAppContext, RenderContext, View, elements::*, AnyViewHandle, AppContext, ElementBox, Entity, MutableAppContext, RenderContext,
ViewContext, ViewHandle, View, ViewContext, ViewHandle,
}; };
pub trait ToolbarItemView: View { pub trait ToolbarItemView: View {
@ -15,6 +15,7 @@ pub trait ToolbarItemView: View {
&self, &self,
_event: &Self::Event, _event: &Self::Event,
current_location: ToolbarItemLocation, current_location: ToolbarItemLocation,
_cx: &AppContext,
) -> ToolbarItemLocation { ) -> ToolbarItemLocation {
current_location current_location
} }
@ -121,7 +122,9 @@ impl Toolbar {
if let Some((_, current_location)) = if let Some((_, current_location)) =
this.items.iter_mut().find(|(i, _)| i.id() == item.id()) 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 { if new_location != *current_location {
*current_location = new_location; *current_location = new_location;
cx.notify(); cx.notify();