Remove Editor::find_or_create
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
6f9c37851c
commit
bff414cfbc
6 changed files with 63 additions and 28 deletions
|
@ -195,6 +195,16 @@ pub trait Item: View {
|
|||
}
|
||||
}
|
||||
|
||||
pub trait ProjectItem: Item {
|
||||
type Item: project::Item;
|
||||
|
||||
fn for_project_item(
|
||||
project: ModelHandle<Project>,
|
||||
item: ModelHandle<Self::Item>,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Self;
|
||||
}
|
||||
|
||||
pub trait ItemHandle: 'static {
|
||||
fn tab_content(&self, style: &theme::Tab, cx: &AppContext) -> ElementBox;
|
||||
fn project_path(&self, cx: &AppContext) -> Option<ProjectPath>;
|
||||
|
@ -833,6 +843,31 @@ impl Workspace {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn open_project_item<T>(
|
||||
&mut self,
|
||||
project_item: ModelHandle<T::Item>,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> ViewHandle<T>
|
||||
where
|
||||
T: ProjectItem,
|
||||
{
|
||||
use project::Item as _;
|
||||
|
||||
if let Some(item) = project_item
|
||||
.read(cx)
|
||||
.entry_id(cx)
|
||||
.and_then(|entry_id| self.active_pane().read(cx).item_for_entry(entry_id))
|
||||
.and_then(|item| item.downcast())
|
||||
{
|
||||
self.activate_item(&item, cx);
|
||||
return item;
|
||||
}
|
||||
|
||||
let item = cx.add_view(|cx| T::for_project_item(self.project().clone(), project_item, cx));
|
||||
self.add_item(Box::new(item.clone()), cx);
|
||||
item
|
||||
}
|
||||
|
||||
pub fn activate_item(&mut self, item: &dyn ItemHandle, cx: &mut ViewContext<Self>) -> bool {
|
||||
let result = self.panes.iter().find_map(|pane| {
|
||||
if let Some(ix) = pane.read(cx).index_for_item(item) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue