Remove workspace::Item trait

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Keith Simmons <keith@zed.dev>
Co-Authored-By: Antonio Scandurra <antonio@zed.dev>
This commit is contained in:
Max Brunsfeld 2022-03-16 13:34:04 -07:00
parent e8efaed1b2
commit a88320dc5f
10 changed files with 281 additions and 517 deletions

View file

@ -7,7 +7,7 @@ use editor::{Anchor, Autoscroll, Editor, MultiBuffer, SelectAll};
use gpui::{
action, elements::*, keymap::Binding, platform::CursorStyle, AppContext, ElementBox, Entity,
ModelContext, ModelHandle, MutableAppContext, RenderContext, Task, View, ViewContext,
ViewHandle, WeakModelHandle,
ViewHandle, WeakModelHandle, WeakViewHandle,
};
use project::{search::SearchQuery, Project};
use std::{
@ -16,7 +16,7 @@ use std::{
path::PathBuf,
};
use util::ResultExt as _;
use workspace::{Item, ItemHandle, ItemNavHistory, ItemView, Settings, Workspace};
use workspace::{ItemNavHistory, ItemView, Settings, Workspace};
action!(Deploy);
action!(Search);
@ -26,7 +26,7 @@ action!(ToggleFocus);
const MAX_TAB_TITLE_LEN: usize = 24;
#[derive(Default)]
struct ActiveSearches(HashMap<WeakModelHandle<Project>, WeakModelHandle<ProjectSearch>>);
struct ActiveSearches(HashMap<WeakModelHandle<Project>, WeakViewHandle<ProjectSearchView>>);
pub fn init(cx: &mut MutableAppContext) {
cx.add_app_state(ActiveSearches::default());
@ -139,23 +139,6 @@ impl ProjectSearch {
}
}
impl Item for ProjectSearch {
type View = ProjectSearchView;
fn build_view(
model: ModelHandle<Self>,
_: &Workspace,
nav_history: ItemNavHistory,
cx: &mut gpui::ViewContext<Self::View>,
) -> Self::View {
ProjectSearchView::new(model, Some(nav_history), cx)
}
fn project_path(&self) -> Option<project::ProjectPath> {
None
}
}
enum ViewEvent {
UpdateTab,
}
@ -199,11 +182,11 @@ impl View for ProjectSearchView {
}
fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
let handle = cx.weak_handle();
cx.update_app_state(|state: &mut ActiveSearches, cx| {
state.0.insert(
self.model.read(cx).project.downgrade(),
self.model.downgrade(),
)
state
.0
.insert(self.model.read(cx).project.downgrade(), handle)
});
if self.model.read(cx).match_ranges.is_empty() {
@ -235,10 +218,6 @@ impl ItemView for ProjectSearchView {
.update(cx, |editor, cx| editor.deactivated(cx));
}
fn item(&self, _: &gpui::AppContext) -> Box<dyn ItemHandle> {
Box::new(self.model.clone())
}
fn tab_content(&self, tab_theme: &theme::Tab, cx: &gpui::AppContext) -> ElementBox {
let settings = cx.app_state::<Settings>();
let search_theme = &settings.theme.search;
@ -271,6 +250,10 @@ impl ItemView for ProjectSearchView {
None
}
fn project_entry(&self, _: &AppContext) -> Option<project::ProjectEntry> {
None
}
fn can_save(&self, _: &gpui::AppContext) -> bool {
true
}
@ -305,16 +288,18 @@ impl ItemView for ProjectSearchView {
unreachable!("save_as should not have been called")
}
fn clone_on_split(
&self,
nav_history: ItemNavHistory,
cx: &mut ViewContext<Self>,
) -> Option<Self>
fn clone_on_split(&self, cx: &mut ViewContext<Self>) -> Option<Self>
where
Self: Sized,
{
let model = self.model.update(cx, |model, cx| model.clone(cx));
Some(Self::new(model, Some(nav_history), cx))
Some(Self::new(model, cx))
}
fn set_nav_history(&mut self, nav_history: ItemNavHistory, cx: &mut ViewContext<Self>) {
self.results_editor.update(cx, |editor, _| {
editor.set_nav_history(Some(nav_history));
});
}
fn navigate(&mut self, data: Box<dyn Any>, cx: &mut ViewContext<Self>) {
@ -328,11 +313,7 @@ impl ItemView for ProjectSearchView {
}
impl ProjectSearchView {
fn new(
model: ModelHandle<ProjectSearch>,
nav_history: Option<ItemNavHistory>,
cx: &mut ViewContext<Self>,
) -> Self {
fn new(model: ModelHandle<ProjectSearch>, cx: &mut ViewContext<Self>) -> Self {
let project;
let excerpts;
let mut query_text = String::new();
@ -364,7 +345,6 @@ impl ProjectSearchView {
let results_editor = cx.add_view(|cx| {
let mut editor = Editor::for_buffer(excerpts, Some(project), cx);
editor.set_searchable(false);
editor.set_nav_history(nav_history);
editor
});
cx.observe(&results_editor, |_, _, cx| cx.emit(ViewEvent::UpdateTab))
@ -406,16 +386,19 @@ impl ProjectSearchView {
let existing = active_search
.and_then(|active_search| {
workspace
.items_of_type::<ProjectSearch>(cx)
.items_of_type::<ProjectSearchView>(cx)
.find(|search| search == active_search)
})
.or_else(|| workspace.item_of_type::<ProjectSearch>(cx));
.or_else(|| workspace.item_of_type::<ProjectSearchView>(cx));
if let Some(existing) = existing {
workspace.activate_item(&existing, cx);
} else {
let model = cx.add_model(|cx| ProjectSearch::new(workspace.project().clone(), cx));
workspace.open_item(model, cx);
workspace.open_item(
Box::new(cx.add_view(|cx| ProjectSearchView::new(model, cx))),
cx,
);
}
}
@ -450,7 +433,10 @@ impl ProjectSearchView {
model.search(new_query, cx);
model
});
workspace.open_item(model, cx);
workspace.open_item(
Box::new(cx.add_view(|cx| ProjectSearchView::new(model, cx))),
cx,
);
}
}
}
@ -732,7 +718,7 @@ mod tests {
let search = cx.add_model(|cx| ProjectSearch::new(project, cx));
let search_view = cx.add_view(Default::default(), |cx| {
ProjectSearchView::new(search.clone(), None, cx)
ProjectSearchView::new(search.clone(), cx)
});
search_view.update(cx, |search_view, cx| {