Merge pull request #2198 from zed-industries/more-item-defaults

Add more default impls to the item trait
This commit is contained in:
Mikayla Maki 2023-02-22 09:14:12 -08:00 committed by GitHub
commit d7305077bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 197 deletions

View file

@ -13,7 +13,6 @@ use gpui::{
elements::{ChildView, Flex, Label, ParentElement}, elements::{ChildView, Flex, Label, ParentElement},
serde_json, AnyViewHandle, AppContext, Element, ElementBox, Entity, ModelHandle, serde_json, AnyViewHandle, AppContext, Element, ElementBox, Entity, ModelHandle,
MutableAppContext, PromptLevel, RenderContext, Task, View, ViewContext, ViewHandle, MutableAppContext, PromptLevel, RenderContext, Task, View, ViewContext, ViewHandle,
WeakViewHandle,
}; };
use isahc::Request; use isahc::Request;
use language::Buffer; use language::Buffer;
@ -24,7 +23,6 @@ use serde::Serialize;
use workspace::{ use workspace::{
item::{Item, ItemHandle}, item::{Item, ItemHandle},
searchable::{SearchableItem, SearchableItemHandle}, searchable::{SearchableItem, SearchableItemHandle},
smallvec::SmallVec,
AppState, Workspace, AppState, Workspace,
}; };
@ -259,16 +257,10 @@ impl Item for FeedbackEditor {
self.editor.for_each_project_item(cx, f) self.editor.for_each_project_item(cx, f)
} }
fn to_item_events(_: &Self::Event) -> SmallVec<[workspace::item::ItemEvent; 2]> {
SmallVec::new()
}
fn is_singleton(&self, _: &AppContext) -> bool { fn is_singleton(&self, _: &AppContext) -> bool {
true true
} }
fn set_nav_history(&mut self, _: workspace::ItemNavHistory, _: &mut ViewContext<Self>) {}
fn can_save(&self, _: &AppContext) -> bool { fn can_save(&self, _: &AppContext) -> bool {
true true
} }
@ -295,7 +287,7 @@ impl Item for FeedbackEditor {
_: ModelHandle<Project>, _: ModelHandle<Project>,
_: &mut ViewContext<Self>, _: &mut ViewContext<Self>,
) -> Task<anyhow::Result<()>> { ) -> Task<anyhow::Result<()>> {
unreachable!("reload should not have been called") Task::Ready(Some(Ok(())))
} }
fn clone_on_split( fn clone_on_split(
@ -322,20 +314,6 @@ impl Item for FeedbackEditor {
)) ))
} }
fn serialized_item_kind() -> Option<&'static str> {
None
}
fn deserialize(
_: ModelHandle<Project>,
_: WeakViewHandle<Workspace>,
_: workspace::WorkspaceId,
_: workspace::ItemId,
_: &mut ViewContext<workspace::Pane>,
) -> Task<anyhow::Result<ViewHandle<Self>>> {
unreachable!()
}
fn as_searchable(&self, handle: &ViewHandle<Self>) -> Option<Box<dyn SearchableItemHandle>> { fn as_searchable(&self, handle: &ViewHandle<Self>) -> Option<Box<dyn SearchableItemHandle>> {
Some(Box::new(handle.clone())) Some(Box::new(handle.clone()))
} }

View file

@ -21,7 +21,7 @@ use gpui::{
use project::{LocalWorktree, Project}; use project::{LocalWorktree, Project};
use serde::Deserialize; use serde::Deserialize;
use settings::{Settings, TerminalBlink, WorkingDirectory}; use settings::{Settings, TerminalBlink, WorkingDirectory};
use smallvec::SmallVec; use smallvec::{smallvec, SmallVec};
use smol::Timer; use smol::Timer;
use terminal::{ use terminal::{
alacritty_terminal::{ alacritty_terminal::{
@ -616,43 +616,6 @@ impl Item for TerminalView {
None None
} }
fn for_each_project_item(&self, _: &AppContext, _: &mut dyn FnMut(usize, &dyn project::Item)) {}
fn is_singleton(&self, _cx: &gpui::AppContext) -> bool {
false
}
fn set_nav_history(&mut self, _: workspace::ItemNavHistory, _: &mut ViewContext<Self>) {}
fn can_save(&self, _cx: &gpui::AppContext) -> bool {
false
}
fn save(
&mut self,
_project: gpui::ModelHandle<Project>,
_cx: &mut ViewContext<Self>,
) -> gpui::Task<gpui::anyhow::Result<()>> {
unreachable!("save should not have been called");
}
fn save_as(
&mut self,
_project: gpui::ModelHandle<Project>,
_abs_path: std::path::PathBuf,
_cx: &mut ViewContext<Self>,
) -> gpui::Task<gpui::anyhow::Result<()>> {
unreachable!("save_as should not have been called");
}
fn reload(
&mut self,
_project: gpui::ModelHandle<Project>,
_cx: &mut ViewContext<Self>,
) -> gpui::Task<gpui::anyhow::Result<()>> {
gpui::Task::ready(Ok(()))
}
fn is_dirty(&self, _cx: &gpui::AppContext) -> bool { fn is_dirty(&self, _cx: &gpui::AppContext) -> bool {
self.has_bell() self.has_bell()
} }
@ -667,10 +630,10 @@ impl Item for TerminalView {
fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> { fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
match event { match event {
Event::BreadcrumbsChanged => smallvec::smallvec![ItemEvent::UpdateBreadcrumbs], Event::BreadcrumbsChanged => smallvec![ItemEvent::UpdateBreadcrumbs],
Event::TitleChanged | Event::Wakeup => smallvec::smallvec![ItemEvent::UpdateTab], Event::TitleChanged | Event::Wakeup => smallvec![ItemEvent::UpdateTab],
Event::CloseTerminal => smallvec::smallvec![ItemEvent::CloseItem], Event::CloseTerminal => smallvec![ItemEvent::CloseItem],
_ => smallvec::smallvec![], _ => smallvec![],
} }
} }

View file

@ -11,12 +11,8 @@ use gpui::{
}; };
use project::Project; use project::Project;
use settings::Settings; use settings::Settings;
use smallvec::SmallVec;
use theme::{ColorScheme, Layer, Style, StyleSet}; use theme::{ColorScheme, Layer, Style, StyleSet};
use workspace::{ use workspace::{item::Item, register_deserializable_item, Pane, Workspace};
item::{Item, ItemEvent},
register_deserializable_item, Pane, Workspace,
};
actions!(theme, [DeployThemeTestbench]); actions!(theme, [DeployThemeTestbench]);
@ -314,47 +310,6 @@ impl Item for ThemeTestbench {
.boxed() .boxed()
} }
fn for_each_project_item(&self, _: &AppContext, _: &mut dyn FnMut(usize, &dyn project::Item)) {}
fn is_singleton(&self, _: &AppContext) -> bool {
false
}
fn set_nav_history(&mut self, _: workspace::ItemNavHistory, _: &mut ViewContext<Self>) {}
fn can_save(&self, _: &AppContext) -> bool {
false
}
fn save(
&mut self,
_: gpui::ModelHandle<Project>,
_: &mut ViewContext<Self>,
) -> gpui::Task<gpui::anyhow::Result<()>> {
unreachable!("save should not have been called");
}
fn save_as(
&mut self,
_: gpui::ModelHandle<Project>,
_: std::path::PathBuf,
_: &mut ViewContext<Self>,
) -> gpui::Task<gpui::anyhow::Result<()>> {
unreachable!("save_as should not have been called");
}
fn reload(
&mut self,
_: gpui::ModelHandle<Project>,
_: &mut ViewContext<Self>,
) -> gpui::Task<gpui::anyhow::Result<()>> {
gpui::Task::ready(Ok(()))
}
fn to_item_events(_: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
SmallVec::new()
}
fn serialized_item_kind() -> Option<&'static str> { fn serialized_item_kind() -> Option<&'static str> {
Some("ThemeTestBench") Some("ThemeTestBench")
} }

View file

@ -49,9 +49,11 @@ pub trait Item: View {
} }
fn tab_content(&self, detail: Option<usize>, style: &theme::Tab, cx: &AppContext) fn tab_content(&self, detail: Option<usize>, style: &theme::Tab, cx: &AppContext)
-> ElementBox; -> ElementBox;
fn for_each_project_item(&self, _: &AppContext, _: &mut dyn FnMut(usize, &dyn project::Item)); fn for_each_project_item(&self, _: &AppContext, _: &mut dyn FnMut(usize, &dyn project::Item)) {}
fn is_singleton(&self, cx: &AppContext) -> bool; fn is_singleton(&self, _cx: &AppContext) -> bool {
fn set_nav_history(&mut self, _: ItemNavHistory, _: &mut ViewContext<Self>); false
}
fn set_nav_history(&mut self, _: ItemNavHistory, _: &mut ViewContext<Self>) {}
fn clone_on_split(&self, _workspace_id: WorkspaceId, _: &mut ViewContext<Self>) -> Option<Self> fn clone_on_split(&self, _workspace_id: WorkspaceId, _: &mut ViewContext<Self>) -> Option<Self>
where where
Self: Sized, Self: Sized,
@ -64,23 +66,31 @@ pub trait Item: View {
fn has_conflict(&self, _: &AppContext) -> bool { fn has_conflict(&self, _: &AppContext) -> bool {
false false
} }
fn can_save(&self, cx: &AppContext) -> bool; fn can_save(&self, _cx: &AppContext) -> bool {
false
}
fn save( fn save(
&mut self, &mut self,
project: ModelHandle<Project>, _project: ModelHandle<Project>,
cx: &mut ViewContext<Self>, _cx: &mut ViewContext<Self>,
) -> Task<Result<()>>; ) -> Task<Result<()>> {
unimplemented!("save() must be implemented if can_save() returns true")
}
fn save_as( fn save_as(
&mut self, &mut self,
project: ModelHandle<Project>, _project: ModelHandle<Project>,
abs_path: PathBuf, _abs_path: PathBuf,
cx: &mut ViewContext<Self>, _cx: &mut ViewContext<Self>,
) -> Task<Result<()>>; ) -> Task<Result<()>> {
unimplemented!("save_as() must be implemented if can_save() returns true")
}
fn reload( fn reload(
&mut self, &mut self,
project: ModelHandle<Project>, _project: ModelHandle<Project>,
cx: &mut ViewContext<Self>, _cx: &mut ViewContext<Self>,
) -> Task<Result<()>>; ) -> Task<Result<()>> {
unimplemented!("reload() must be implemented if can_save() returns true")
}
fn git_diff_recalc( fn git_diff_recalc(
&mut self, &mut self,
_project: ModelHandle<Project>, _project: ModelHandle<Project>,
@ -88,7 +98,9 @@ pub trait Item: View {
) -> Task<Result<()>> { ) -> Task<Result<()>> {
Task::ready(Ok(())) Task::ready(Ok(()))
} }
fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]>; fn to_item_events(_event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
SmallVec::new()
}
fn should_close_item_on_event(_: &Self::Event) -> bool { fn should_close_item_on_event(_: &Self::Event) -> bool {
false false
} }
@ -124,15 +136,21 @@ pub trait Item: View {
fn added_to_workspace(&mut self, _workspace: &mut Workspace, _cx: &mut ViewContext<Self>) {} fn added_to_workspace(&mut self, _workspace: &mut Workspace, _cx: &mut ViewContext<Self>) {}
fn serialized_item_kind() -> Option<&'static str>; fn serialized_item_kind() -> Option<&'static str> {
None
}
fn deserialize( fn deserialize(
project: ModelHandle<Project>, _project: ModelHandle<Project>,
workspace: WeakViewHandle<Workspace>, _workspace: WeakViewHandle<Workspace>,
workspace_id: WorkspaceId, _workspace_id: WorkspaceId,
item_id: ItemId, _item_id: ItemId,
cx: &mut ViewContext<Pane>, _cx: &mut ViewContext<Pane>,
) -> Task<Result<ViewHandle<Self>>>; ) -> Task<Result<ViewHandle<Self>>> {
unimplemented!(
"deserialize() must be implemented if serialized_item_kind() returns Some(_)"
)
}
} }
pub trait ItemHandle: 'static + fmt::Debug { pub trait ItemHandle: 'static + fmt::Debug {

View file

@ -1,23 +1,18 @@
use crate::{ use crate::{
item::ItemEvent, persistence::model::ItemId, Item, ItemNavHistory, Pane, Workspace, WorkspaceId, item::{Item, ItemEvent},
ItemNavHistory, WorkspaceId,
}; };
use anyhow::{anyhow, Result};
use call::participant::{Frame, RemoteVideoTrack}; use call::participant::{Frame, RemoteVideoTrack};
use client::{proto::PeerId, User}; use client::{proto::PeerId, User};
use futures::StreamExt; use futures::StreamExt;
use gpui::{ use gpui::{
elements::*, elements::*,
geometry::{rect::RectF, vector::vec2f}, geometry::{rect::RectF, vector::vec2f},
AppContext, Entity, ModelHandle, MouseButton, RenderContext, Task, View, ViewContext, AppContext, Entity, MouseButton, RenderContext, Task, View, ViewContext,
ViewHandle, WeakViewHandle,
}; };
use project::Project;
use settings::Settings; use settings::Settings;
use smallvec::SmallVec; use smallvec::SmallVec;
use std::{ use std::sync::{Arc, Weak};
path::PathBuf,
sync::{Arc, Weak},
};
pub enum Event { pub enum Event {
Close, Close,
@ -130,12 +125,6 @@ impl Item for SharedScreen {
.boxed() .boxed()
} }
fn for_each_project_item(&self, _: &AppContext, _: &mut dyn FnMut(usize, &dyn project::Item)) {}
fn is_singleton(&self, _: &AppContext) -> bool {
false
}
fn set_nav_history(&mut self, history: ItemNavHistory, _: &mut ViewContext<Self>) { fn set_nav_history(&mut self, history: ItemNavHistory, _: &mut ViewContext<Self>) {
self.nav_history = Some(history); self.nav_history = Some(history);
} }
@ -149,52 +138,9 @@ impl Item for SharedScreen {
Some(Self::new(&track, self.peer_id, self.user.clone(), cx)) Some(Self::new(&track, self.peer_id, self.user.clone(), cx))
} }
fn can_save(&self, _: &AppContext) -> bool {
false
}
fn save(
&mut self,
_: ModelHandle<project::Project>,
_: &mut ViewContext<Self>,
) -> Task<Result<()>> {
Task::ready(Err(anyhow!("Item::save called on SharedScreen")))
}
fn save_as(
&mut self,
_: ModelHandle<project::Project>,
_: PathBuf,
_: &mut ViewContext<Self>,
) -> Task<Result<()>> {
Task::ready(Err(anyhow!("Item::save_as called on SharedScreen")))
}
fn reload(
&mut self,
_: ModelHandle<project::Project>,
_: &mut ViewContext<Self>,
) -> Task<Result<()>> {
Task::ready(Err(anyhow!("Item::reload called on SharedScreen")))
}
fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> { fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
match event { match event {
Event::Close => smallvec::smallvec!(ItemEvent::CloseItem), Event::Close => smallvec::smallvec!(ItemEvent::CloseItem),
} }
} }
fn serialized_item_kind() -> Option<&'static str> {
None
}
fn deserialize(
_project: ModelHandle<Project>,
_workspace: WeakViewHandle<Workspace>,
_workspace_id: WorkspaceId,
_item_id: ItemId,
_cx: &mut ViewContext<Pane>,
) -> Task<Result<ViewHandle<Self>>> {
unreachable!("Shared screen can not be deserialized")
}
} }

View file

@ -1589,13 +1589,17 @@ impl Workspace {
} }
let item = pane.read(cx).active_item()?; let item = pane.read(cx).active_item()?;
let new_pane = self.add_pane(cx); let maybe_pane_handle =
if let Some(clone) = item.clone_on_split(self.database_id(), cx.as_mut()) { if let Some(clone) = item.clone_on_split(self.database_id(), cx.as_mut()) {
Pane::add_item(self, &new_pane, clone, true, true, None, cx); let new_pane = self.add_pane(cx);
} Pane::add_item(self, &new_pane, clone, true, true, None, cx);
self.center.split(&pane, &new_pane, direction).unwrap(); self.center.split(&pane, &new_pane, direction).unwrap();
Some(new_pane)
} else {
None
};
cx.notify(); cx.notify();
Some(new_pane) maybe_pane_handle
} }
pub fn split_pane_with_item(&mut self, action: &SplitWithItem, cx: &mut ViewContext<Self>) { pub fn split_pane_with_item(&mut self, action: &SplitWithItem, cx: &mut ViewContext<Self>) {