Rename ItemView to Item

This commit is contained in:
Antonio Scandurra 2022-03-17 11:32:46 +01:00
parent aced1e2315
commit 0efce8f70a
11 changed files with 122 additions and 135 deletions

View file

@ -25,7 +25,7 @@ use std::{
sync::Arc, sync::Arc,
}; };
use util::TryFutureExt; use util::TryFutureExt;
use workspace::{ItemNavHistory, ItemViewHandle as _, Settings, Workspace}; use workspace::{ItemHandle as _, ItemNavHistory, Settings, Workspace};
action!(Deploy); action!(Deploy);
@ -438,7 +438,7 @@ impl ProjectDiagnosticsEditor {
} }
} }
impl workspace::ItemView for ProjectDiagnosticsEditor { impl workspace::Item for ProjectDiagnosticsEditor {
fn tab_content(&self, style: &theme::Tab, cx: &AppContext) -> ElementBox { fn tab_content(&self, style: &theme::Tab, cx: &AppContext) -> ElementBox {
render_summary( render_summary(
&self.summary, &self.summary,

View file

@ -71,7 +71,7 @@ impl View for DiagnosticSummary {
impl StatusItemView for DiagnosticSummary { impl StatusItemView for DiagnosticSummary {
fn set_active_pane_item( fn set_active_pane_item(
&mut self, &mut self,
_: Option<&dyn workspace::ItemViewHandle>, _: Option<&dyn workspace::ItemHandle>,
_: &mut ViewContext<Self>, _: &mut ViewContext<Self>,
) { ) {
} }

View file

@ -6264,7 +6264,7 @@ mod tests {
#[gpui::test] #[gpui::test]
fn test_navigation_history(cx: &mut gpui::MutableAppContext) { fn test_navigation_history(cx: &mut gpui::MutableAppContext) {
populate_settings(cx); populate_settings(cx);
use workspace::ItemView; use workspace::Item;
let nav_history = Rc::new(RefCell::new(workspace::NavHistory::default())); let nav_history = Rc::new(RefCell::new(workspace::NavHistory::default()));
let buffer = MultiBuffer::build_simple(&sample_text(30, 5, 'a'), cx); let buffer = MultiBuffer::build_simple(&sample_text(30, 5, 'a'), cx);
@ -6283,7 +6283,7 @@ mod tests {
editor.select_display_ranges(&[DisplayPoint::new(13, 0)..DisplayPoint::new(13, 3)], cx); editor.select_display_ranges(&[DisplayPoint::new(13, 0)..DisplayPoint::new(13, 3)], cx);
let nav_entry = nav_history.borrow_mut().pop_backward().unwrap(); let nav_entry = nav_history.borrow_mut().pop_backward().unwrap();
editor.navigate(nav_entry.data.unwrap(), cx); editor.navigate(nav_entry.data.unwrap(), cx);
assert_eq!(nav_entry.item_view.id(), cx.view_id()); assert_eq!(nav_entry.item.id(), cx.view_id());
assert_eq!( assert_eq!(
editor.selected_display_ranges(cx), editor.selected_display_ranges(cx),
&[DisplayPoint::new(3, 0)..DisplayPoint::new(3, 0)] &[DisplayPoint::new(3, 0)..DisplayPoint::new(3, 0)]
@ -6309,7 +6309,7 @@ mod tests {
); );
let nav_entry = nav_history.borrow_mut().pop_backward().unwrap(); let nav_entry = nav_history.borrow_mut().pop_backward().unwrap();
editor.navigate(nav_entry.data.unwrap(), cx); editor.navigate(nav_entry.data.unwrap(), cx);
assert_eq!(nav_entry.item_view.id(), cx.view_id()); assert_eq!(nav_entry.item.id(), cx.view_id());
assert_eq!( assert_eq!(
editor.selected_display_ranges(cx), editor.selected_display_ranges(cx),
&[DisplayPoint::new(5, 0)..DisplayPoint::new(5, 0)] &[DisplayPoint::new(5, 0)..DisplayPoint::new(5, 0)]

View file

@ -10,7 +10,7 @@ use std::fmt::Write;
use std::path::PathBuf; use std::path::PathBuf;
use text::{Point, Selection}; use text::{Point, Selection};
use util::ResultExt; use util::ResultExt;
use workspace::{ItemNavHistory, ItemView, ItemViewHandle, Settings, StatusItemView}; use workspace::{Item, ItemHandle, ItemNavHistory, Settings, StatusItemView};
#[derive(Clone)] #[derive(Clone)]
pub struct BufferItemHandle(pub ModelHandle<Buffer>); pub struct BufferItemHandle(pub ModelHandle<Buffer>);
@ -24,7 +24,7 @@ pub struct MultiBufferItemHandle(pub ModelHandle<MultiBuffer>);
#[derive(Clone)] #[derive(Clone)]
struct WeakMultiBufferItemHandle(WeakModelHandle<MultiBuffer>); struct WeakMultiBufferItemHandle(WeakModelHandle<MultiBuffer>);
impl ItemView for Editor { impl Item for Editor {
fn navigate(&mut self, data: Box<dyn std::any::Any>, cx: &mut ViewContext<Self>) { fn navigate(&mut self, data: Box<dyn std::any::Any>, cx: &mut ViewContext<Self>) {
if let Some(data) = data.downcast_ref::<NavigationData>() { if let Some(data) = data.downcast_ref::<NavigationData>() {
let buffer = self.buffer.read(cx).read(cx); let buffer = self.buffer.read(cx).read(cx);
@ -206,7 +206,7 @@ impl View for CursorPosition {
impl StatusItemView for CursorPosition { impl StatusItemView for CursorPosition {
fn set_active_pane_item( fn set_active_pane_item(
&mut self, &mut self,
active_pane_item: Option<&dyn ItemViewHandle>, active_pane_item: Option<&dyn ItemHandle>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) { if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) {
@ -279,7 +279,7 @@ impl View for DiagnosticMessage {
impl StatusItemView for DiagnosticMessage { impl StatusItemView for DiagnosticMessage {
fn set_active_pane_item( fn set_active_pane_item(
&mut self, &mut self,
active_pane_item: Option<&dyn ItemViewHandle>, active_pane_item: Option<&dyn ItemHandle>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) { if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) {

View file

@ -8,7 +8,7 @@ use gpui::{
use language::OffsetRangeExt; use language::OffsetRangeExt;
use project::search::SearchQuery; use project::search::SearchQuery;
use std::ops::Range; use std::ops::Range;
use workspace::{ItemViewHandle, Pane, Settings, Toolbar, Workspace}; use workspace::{ItemHandle, Pane, Settings, Toolbar, Workspace};
action!(Deploy, bool); action!(Deploy, bool);
action!(Dismiss); action!(Dismiss);
@ -126,7 +126,7 @@ impl View for SearchBar {
impl Toolbar for SearchBar { impl Toolbar for SearchBar {
fn active_item_changed( fn active_item_changed(
&mut self, &mut self,
item: Option<Box<dyn ItemViewHandle>>, item: Option<Box<dyn ItemHandle>>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> bool { ) -> bool {
self.active_editor_subscription.take(); self.active_editor_subscription.take();

View file

@ -16,7 +16,7 @@ use std::{
path::PathBuf, path::PathBuf,
}; };
use util::ResultExt as _; use util::ResultExt as _;
use workspace::{ItemNavHistory, ItemView, Settings, Workspace}; use workspace::{Item, ItemNavHistory, Settings, Workspace};
action!(Deploy); action!(Deploy);
action!(Search); action!(Search);
@ -197,7 +197,7 @@ impl View for ProjectSearchView {
} }
} }
impl ItemView for ProjectSearchView { impl Item for ProjectSearchView {
fn act_as_type( fn act_as_type(
&self, &self,
type_id: TypeId, type_id: TypeId,

View file

@ -1,4 +1,4 @@
use crate::{ItemViewHandle, Settings, StatusItemView}; use crate::{ItemHandle, Settings, StatusItemView};
use futures::StreamExt; use futures::StreamExt;
use gpui::AppContext; use gpui::AppContext;
use gpui::{ use gpui::{
@ -187,5 +187,5 @@ impl View for LspStatus {
} }
impl StatusItemView for LspStatus { impl StatusItemView for LspStatus {
fn set_active_pane_item(&mut self, _: Option<&dyn ItemViewHandle>, _: &mut ViewContext<Self>) {} fn set_active_pane_item(&mut self, _: Option<&dyn ItemHandle>, _: &mut ViewContext<Self>) {}
} }

View file

@ -1,5 +1,5 @@
use super::{ItemViewHandle, SplitDirection}; use super::{ItemHandle, SplitDirection};
use crate::{ItemView, Settings, WeakItemViewHandle, Workspace}; use crate::{Item, Settings, WeakItemHandle, Workspace};
use collections::{HashMap, VecDeque}; use collections::{HashMap, VecDeque};
use gpui::{ use gpui::{
action, action,
@ -97,7 +97,7 @@ pub enum Event {
} }
pub struct Pane { pub struct Pane {
item_views: Vec<(Option<ProjectEntryId>, Box<dyn ItemViewHandle>)>, items: Vec<(Option<ProjectEntryId>, Box<dyn ItemHandle>)>,
active_item_index: usize, active_item_index: usize,
nav_history: Rc<RefCell<NavHistory>>, nav_history: Rc<RefCell<NavHistory>>,
toolbars: HashMap<TypeId, Box<dyn ToolbarHandle>>, toolbars: HashMap<TypeId, Box<dyn ToolbarHandle>>,
@ -108,7 +108,7 @@ pub struct Pane {
pub trait Toolbar: View { pub trait Toolbar: View {
fn active_item_changed( fn active_item_changed(
&mut self, &mut self,
item: Option<Box<dyn ItemViewHandle>>, item: Option<Box<dyn ItemHandle>>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> bool; ) -> bool;
fn on_dismiss(&mut self, cx: &mut ViewContext<Self>); fn on_dismiss(&mut self, cx: &mut ViewContext<Self>);
@ -117,7 +117,7 @@ pub trait Toolbar: View {
trait ToolbarHandle { trait ToolbarHandle {
fn active_item_changed( fn active_item_changed(
&self, &self,
item: Option<Box<dyn ItemViewHandle>>, item: Option<Box<dyn ItemHandle>>,
cx: &mut MutableAppContext, cx: &mut MutableAppContext,
) -> bool; ) -> bool;
fn on_dismiss(&self, cx: &mut MutableAppContext); fn on_dismiss(&self, cx: &mut MutableAppContext);
@ -126,7 +126,7 @@ trait ToolbarHandle {
pub struct ItemNavHistory { pub struct ItemNavHistory {
history: Rc<RefCell<NavHistory>>, history: Rc<RefCell<NavHistory>>,
item_view: Rc<dyn WeakItemViewHandle>, item: Rc<dyn WeakItemHandle>,
} }
#[derive(Default)] #[derive(Default)]
@ -152,14 +152,14 @@ impl Default for NavigationMode {
} }
pub struct NavigationEntry { pub struct NavigationEntry {
pub item_view: Rc<dyn WeakItemViewHandle>, pub item: Rc<dyn WeakItemHandle>,
pub data: Option<Box<dyn Any>>, pub data: Option<Box<dyn Any>>,
} }
impl Pane { impl Pane {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
item_views: Vec::new(), items: Vec::new(),
active_item_index: 0, active_item_index: 0,
nav_history: Default::default(), nav_history: Default::default(),
toolbars: Default::default(), toolbars: Default::default(),
@ -216,13 +216,13 @@ impl Pane {
// If the item is still present in this pane, then activate it. // If the item is still present in this pane, then activate it.
if let Some(index) = entry if let Some(index) = entry
.item_view .item
.upgrade(cx) .upgrade(cx)
.and_then(|v| pane.index_for_item_view(v.as_ref())) .and_then(|v| pane.index_for_item(v.as_ref()))
{ {
if let Some(item_view) = pane.active_item() { if let Some(item) = pane.active_item() {
pane.nav_history.borrow_mut().set_mode(mode); pane.nav_history.borrow_mut().set_mode(mode);
item_view.deactivated(cx); item.deactivated(cx);
pane.nav_history pane.nav_history
.borrow_mut() .borrow_mut()
.set_mode(NavigationMode::Normal); .set_mode(NavigationMode::Normal);
@ -242,7 +242,7 @@ impl Pane {
pane.nav_history pane.nav_history
.borrow_mut() .borrow_mut()
.paths_by_item .paths_by_item
.get(&entry.item_view.id()) .get(&entry.item.id())
.cloned() .cloned()
.map(|project_path| (project_path, entry)) .map(|project_path| (project_path, entry))
} }
@ -284,47 +284,47 @@ impl Pane {
&mut self, &mut self,
project_entry_id: ProjectEntryId, project_entry_id: ProjectEntryId,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
build_editor: impl FnOnce(&mut MutableAppContext) -> Box<dyn ItemViewHandle>, build_editor: impl FnOnce(&mut MutableAppContext) -> Box<dyn ItemHandle>,
) -> Box<dyn ItemViewHandle> { ) -> Box<dyn ItemHandle> {
for (ix, (existing_entry_id, item_view)) in self.item_views.iter().enumerate() { for (ix, (existing_entry_id, item)) in self.items.iter().enumerate() {
if *existing_entry_id == Some(project_entry_id) { if *existing_entry_id == Some(project_entry_id) {
let item_view = item_view.boxed_clone(); let item = item.boxed_clone();
self.activate_item(ix, cx); self.activate_item(ix, cx);
return item_view; return item;
} }
} }
let item_view = build_editor(cx); let item = build_editor(cx);
self.add_item(Some(project_entry_id), item_view.boxed_clone(), cx); self.add_item(Some(project_entry_id), item.boxed_clone(), cx);
item_view item
} }
pub(crate) fn add_item( pub(crate) fn add_item(
&mut self, &mut self,
project_entry_id: Option<ProjectEntryId>, project_entry_id: Option<ProjectEntryId>,
mut item: Box<dyn ItemViewHandle>, mut item: Box<dyn ItemHandle>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
item.set_nav_history(self.nav_history.clone(), cx); item.set_nav_history(self.nav_history.clone(), cx);
item.added_to_pane(cx); item.added_to_pane(cx);
let item_idx = cmp::min(self.active_item_index + 1, self.item_views.len()); let item_idx = cmp::min(self.active_item_index + 1, self.items.len());
self.item_views.insert(item_idx, (project_entry_id, item)); self.items.insert(item_idx, (project_entry_id, item));
self.activate_item(item_idx, cx); self.activate_item(item_idx, cx);
cx.notify(); cx.notify();
} }
pub fn item_views(&self) -> impl Iterator<Item = &Box<dyn ItemViewHandle>> { pub fn items(&self) -> impl Iterator<Item = &Box<dyn ItemHandle>> {
self.item_views.iter().map(|(_, view)| view) self.items.iter().map(|(_, view)| view)
} }
pub fn active_item(&self) -> Option<Box<dyn ItemViewHandle>> { pub fn active_item(&self) -> Option<Box<dyn ItemHandle>> {
self.item_views self.items
.get(self.active_item_index) .get(self.active_item_index)
.map(|(_, view)| view.clone()) .map(|(_, view)| view.clone())
} }
pub fn project_entry_id_for_item(&self, item: &dyn ItemViewHandle) -> Option<ProjectEntryId> { pub fn project_entry_id_for_item(&self, item: &dyn ItemHandle) -> Option<ProjectEntryId> {
self.item_views.iter().find_map(|(entry_id, existing)| { self.items.iter().find_map(|(entry_id, existing)| {
if existing.id() == item.id() { if existing.id() == item.id() {
*entry_id *entry_id
} else { } else {
@ -333,8 +333,8 @@ impl Pane {
}) })
} }
pub fn item_for_entry(&self, entry_id: ProjectEntryId) -> Option<Box<dyn ItemViewHandle>> { pub fn item_for_entry(&self, entry_id: ProjectEntryId) -> Option<Box<dyn ItemHandle>> {
self.item_views.iter().find_map(|(id, view)| { self.items.iter().find_map(|(id, view)| {
if *id == Some(entry_id) { if *id == Some(entry_id) {
Some(view.boxed_clone()) Some(view.boxed_clone())
} else { } else {
@ -343,25 +343,17 @@ impl Pane {
}) })
} }
pub fn index_for_item_view(&self, item_view: &dyn ItemViewHandle) -> Option<usize> { pub fn index_for_item(&self, item: &dyn ItemHandle) -> Option<usize> {
self.item_views self.items.iter().position(|(_, i)| i.id() == item.id())
.iter()
.position(|(_, i)| i.id() == item_view.id())
}
pub fn index_for_item(&self, item: &dyn ItemViewHandle) -> Option<usize> {
self.item_views
.iter()
.position(|(_, my_item)| my_item.id() == item.id())
} }
pub fn activate_item(&mut self, index: usize, cx: &mut ViewContext<Self>) { pub fn activate_item(&mut self, index: usize, cx: &mut ViewContext<Self>) {
if index < self.item_views.len() { if index < self.items.len() {
let prev_active_item_ix = mem::replace(&mut self.active_item_index, index); let prev_active_item_ix = mem::replace(&mut self.active_item_index, index);
if prev_active_item_ix != self.active_item_index if prev_active_item_ix != self.active_item_index
&& prev_active_item_ix < self.item_views.len() && prev_active_item_ix < self.items.len()
{ {
self.item_views[prev_active_item_ix].1.deactivated(cx); self.items[prev_active_item_ix].1.deactivated(cx);
} }
self.update_active_toolbar(cx); self.update_active_toolbar(cx);
self.focus_active_item(cx); self.focus_active_item(cx);
@ -374,15 +366,15 @@ impl Pane {
let mut index = self.active_item_index; let mut index = self.active_item_index;
if index > 0 { if index > 0 {
index -= 1; index -= 1;
} else if self.item_views.len() > 0 { } else if self.items.len() > 0 {
index = self.item_views.len() - 1; index = self.items.len() - 1;
} }
self.activate_item(index, cx); self.activate_item(index, cx);
} }
pub fn activate_next_item(&mut self, cx: &mut ViewContext<Self>) { pub fn activate_next_item(&mut self, cx: &mut ViewContext<Self>) {
let mut index = self.active_item_index; let mut index = self.active_item_index;
if index + 1 < self.item_views.len() { if index + 1 < self.items.len() {
index += 1; index += 1;
} else { } else {
index = 0; index = 0;
@ -391,14 +383,14 @@ impl Pane {
} }
pub fn close_active_item(&mut self, cx: &mut ViewContext<Self>) { pub fn close_active_item(&mut self, cx: &mut ViewContext<Self>) {
if !self.item_views.is_empty() { if !self.items.is_empty() {
self.close_item(self.item_views[self.active_item_index].1.id(), cx) self.close_item(self.items[self.active_item_index].1.id(), cx)
} }
} }
pub fn close_inactive_items(&mut self, cx: &mut ViewContext<Self>) { pub fn close_inactive_items(&mut self, cx: &mut ViewContext<Self>) {
if !self.item_views.is_empty() { if !self.items.is_empty() {
let active_item_id = self.item_views[self.active_item_index].1.id(); let active_item_id = self.items[self.active_item_index].1.id();
self.close_items(cx, |id| id != active_item_id); self.close_items(cx, |id| id != active_item_id);
} }
} }
@ -414,10 +406,10 @@ impl Pane {
) { ) {
let mut item_ix = 0; let mut item_ix = 0;
let mut new_active_item_index = self.active_item_index; let mut new_active_item_index = self.active_item_index;
self.item_views.retain(|(_, item_view)| { self.items.retain(|(_, item)| {
if should_close(item_view.id()) { if should_close(item.id()) {
if item_ix == self.active_item_index { if item_ix == self.active_item_index {
item_view.deactivated(cx); item.deactivated(cx);
} }
if item_ix < self.active_item_index { if item_ix < self.active_item_index {
@ -425,10 +417,10 @@ impl Pane {
} }
let mut nav_history = self.nav_history.borrow_mut(); let mut nav_history = self.nav_history.borrow_mut();
if let Some(path) = item_view.project_path(cx) { if let Some(path) = item.project_path(cx) {
nav_history.paths_by_item.insert(item_view.id(), path); nav_history.paths_by_item.insert(item.id(), path);
} else { } else {
nav_history.paths_by_item.remove(&item_view.id()); nav_history.paths_by_item.remove(&item.id());
} }
item_ix += 1; item_ix += 1;
@ -439,10 +431,10 @@ impl Pane {
} }
}); });
if self.item_views.is_empty() { if self.items.is_empty() {
cx.emit(Event::Remove); cx.emit(Event::Remove);
} else { } else {
self.active_item_index = cmp::min(new_active_item_index, self.item_views.len() - 1); self.active_item_index = cmp::min(new_active_item_index, self.items.len() - 1);
self.focus_active_item(cx); self.focus_active_item(cx);
self.activate(cx); self.activate(cx);
} }
@ -511,7 +503,7 @@ impl Pane {
} }
fn update_active_toolbar(&mut self, cx: &mut ViewContext<Self>) { fn update_active_toolbar(&mut self, cx: &mut ViewContext<Self>) {
let active_item = self.item_views.get(self.active_item_index); let active_item = self.items.get(self.active_item_index);
for (toolbar_type_id, toolbar) in &self.toolbars { for (toolbar_type_id, toolbar) in &self.toolbars {
let visible = toolbar.active_item_changed(active_item.map(|i| i.1.clone()), cx); let visible = toolbar.active_item_changed(active_item.map(|i| i.1.clone()), cx);
if Some(*toolbar_type_id) == self.active_toolbar_type { if Some(*toolbar_type_id) == self.active_toolbar_type {
@ -526,7 +518,7 @@ impl Pane {
enum Tabs {} enum Tabs {}
let tabs = MouseEventHandler::new::<Tabs, _, _>(0, cx, |mouse_state, cx| { let tabs = MouseEventHandler::new::<Tabs, _, _>(0, cx, |mouse_state, cx| {
let mut row = Flex::row(); let mut row = Flex::row();
for (ix, (_, item_view)) in self.item_views.iter().enumerate() { for (ix, (_, item)) in self.items.iter().enumerate() {
let is_active = ix == self.active_item_index; let is_active = ix == self.active_item_index;
row.add_child({ row.add_child({
@ -535,7 +527,7 @@ impl Pane {
} else { } else {
theme.workspace.tab.clone() theme.workspace.tab.clone()
}; };
let title = item_view.tab_content(&tab_style, cx); let title = item.tab_content(&tab_style, cx);
let mut style = if is_active { let mut style = if is_active {
theme.workspace.active_tab.clone() theme.workspace.active_tab.clone()
@ -552,9 +544,9 @@ impl Pane {
.with_child( .with_child(
Align::new({ Align::new({
let diameter = 7.0; let diameter = 7.0;
let icon_color = if item_view.has_conflict(cx) { let icon_color = if item.has_conflict(cx) {
Some(style.icon_conflict) Some(style.icon_conflict)
} else if item_view.is_dirty(cx) { } else if item.is_dirty(cx) {
Some(style.icon_dirty) Some(style.icon_dirty)
} else { } else {
None None
@ -598,7 +590,7 @@ impl Pane {
.with_child( .with_child(
Align::new( Align::new(
ConstrainedBox::new(if mouse_state.hovered { ConstrainedBox::new(if mouse_state.hovered {
let item_id = item_view.id(); let item_id = item.id();
enum TabCloseButton {} enum TabCloseButton {}
let icon = Svg::new("icons/x.svg"); let icon = Svg::new("icons/x.svg");
MouseEventHandler::new::<TabCloseButton, _, _>( MouseEventHandler::new::<TabCloseButton, _, _>(
@ -702,7 +694,7 @@ impl View for Pane {
impl<T: Toolbar> ToolbarHandle for ViewHandle<T> { impl<T: Toolbar> ToolbarHandle for ViewHandle<T> {
fn active_item_changed( fn active_item_changed(
&self, &self,
item: Option<Box<dyn ItemViewHandle>>, item: Option<Box<dyn ItemHandle>>,
cx: &mut MutableAppContext, cx: &mut MutableAppContext,
) -> bool { ) -> bool {
self.update(cx, |this, cx| this.active_item_changed(item, cx)) self.update(cx, |this, cx| this.active_item_changed(item, cx))
@ -718,10 +710,10 @@ impl<T: Toolbar> ToolbarHandle for ViewHandle<T> {
} }
impl ItemNavHistory { impl ItemNavHistory {
pub fn new<T: ItemView>(history: Rc<RefCell<NavHistory>>, item_view: &ViewHandle<T>) -> Self { pub fn new<T: Item>(history: Rc<RefCell<NavHistory>>, item: &ViewHandle<T>) -> Self {
Self { Self {
history, history,
item_view: Rc::new(item_view.downgrade()), item: Rc::new(item.downgrade()),
} }
} }
@ -730,7 +722,7 @@ impl ItemNavHistory {
} }
pub fn push<D: 'static + Any>(&self, data: Option<D>) { pub fn push<D: 'static + Any>(&self, data: Option<D>) {
self.history.borrow_mut().push(data, self.item_view.clone()); self.history.borrow_mut().push(data, self.item.clone());
} }
} }
@ -763,11 +755,7 @@ impl NavHistory {
self.mode = mode; self.mode = mode;
} }
pub fn push<D: 'static + Any>( pub fn push<D: 'static + Any>(&mut self, data: Option<D>, item: Rc<dyn WeakItemHandle>) {
&mut self,
data: Option<D>,
item_view: Rc<dyn WeakItemViewHandle>,
) {
match self.mode { match self.mode {
NavigationMode::Disabled => {} NavigationMode::Disabled => {}
NavigationMode::Normal => { NavigationMode::Normal => {
@ -775,7 +763,7 @@ impl NavHistory {
self.backward_stack.pop_front(); self.backward_stack.pop_front();
} }
self.backward_stack.push_back(NavigationEntry { self.backward_stack.push_back(NavigationEntry {
item_view, item,
data: data.map(|data| Box::new(data) as Box<dyn Any>), data: data.map(|data| Box::new(data) as Box<dyn Any>),
}); });
self.forward_stack.clear(); self.forward_stack.clear();
@ -785,7 +773,7 @@ impl NavHistory {
self.forward_stack.pop_front(); self.forward_stack.pop_front();
} }
self.forward_stack.push_back(NavigationEntry { self.forward_stack.push_back(NavigationEntry {
item_view, item,
data: data.map(|data| Box::new(data) as Box<dyn Any>), data: data.map(|data| Box::new(data) as Box<dyn Any>),
}); });
} }
@ -794,7 +782,7 @@ impl NavHistory {
self.backward_stack.pop_front(); self.backward_stack.pop_front();
} }
self.backward_stack.push_back(NavigationEntry { self.backward_stack.push_back(NavigationEntry {
item_view, item,
data: data.map(|data| Box::new(data) as Box<dyn Any>), data: data.map(|data| Box::new(data) as Box<dyn Any>),
}); });
} }

View file

@ -1,4 +1,4 @@
use crate::{ItemViewHandle, Pane, Settings}; use crate::{ItemHandle, Pane, Settings};
use gpui::{ use gpui::{
elements::*, AnyViewHandle, ElementBox, Entity, MutableAppContext, RenderContext, Subscription, elements::*, AnyViewHandle, ElementBox, Entity, MutableAppContext, RenderContext, Subscription,
View, ViewContext, ViewHandle, View, ViewContext, ViewHandle,
@ -7,7 +7,7 @@ use gpui::{
pub trait StatusItemView: View { pub trait StatusItemView: View {
fn set_active_pane_item( fn set_active_pane_item(
&mut self, &mut self,
active_pane_item: Option<&dyn crate::ItemViewHandle>, active_pane_item: Option<&dyn crate::ItemHandle>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
); );
} }
@ -16,7 +16,7 @@ trait StatusItemViewHandle {
fn to_any(&self) -> AnyViewHandle; fn to_any(&self) -> AnyViewHandle;
fn set_active_pane_item( fn set_active_pane_item(
&self, &self,
active_pane_item: Option<&dyn ItemViewHandle>, active_pane_item: Option<&dyn ItemHandle>,
cx: &mut MutableAppContext, cx: &mut MutableAppContext,
); );
} }
@ -114,7 +114,7 @@ impl<T: StatusItemView> StatusItemViewHandle for ViewHandle<T> {
fn set_active_pane_item( fn set_active_pane_item(
&self, &self,
active_pane_item: Option<&dyn ItemViewHandle>, active_pane_item: Option<&dyn ItemHandle>,
cx: &mut MutableAppContext, cx: &mut MutableAppContext,
) { ) {
self.update(cx, |this, cx| { self.update(cx, |this, cx| {

View file

@ -47,7 +47,7 @@ pub type BuildEditor = Arc<
ModelHandle<Project>, ModelHandle<Project>,
ModelHandle<Buffer>, ModelHandle<Buffer>,
&mut MutableAppContext, &mut MutableAppContext,
) -> Box<dyn ItemViewHandle>, ) -> Box<dyn ItemHandle>,
>; >;
action!(Open, Arc<AppState>); action!(Open, Arc<AppState>);
@ -106,7 +106,7 @@ pub fn init(cx: &mut MutableAppContext) {
pub fn register_editor_builder<F, V>(cx: &mut MutableAppContext, build_editor: F) pub fn register_editor_builder<F, V>(cx: &mut MutableAppContext, build_editor: F)
where where
V: ItemView, V: Item,
F: 'static + Fn(ModelHandle<Project>, ModelHandle<Buffer>, &mut ViewContext<V>) -> V, F: 'static + Fn(ModelHandle<Project>, ModelHandle<Buffer>, &mut ViewContext<V>) -> V,
{ {
cx.add_app_state::<BuildEditor>(Arc::new(move |window_id, project, model, cx| { cx.add_app_state::<BuildEditor>(Arc::new(move |window_id, project, model, cx| {
@ -141,7 +141,7 @@ pub struct JoinProjectParams {
pub app_state: Arc<AppState>, pub app_state: Arc<AppState>,
} }
pub trait ItemView: View { pub trait Item: View {
fn deactivated(&mut self, _: &mut ViewContext<Self>) {} fn deactivated(&mut self, _: &mut ViewContext<Self>) {}
fn navigate(&mut self, _: Box<dyn Any>, _: &mut ViewContext<Self>) {} fn navigate(&mut self, _: Box<dyn Any>, _: &mut ViewContext<Self>) {}
fn tab_content(&self, style: &theme::Tab, cx: &AppContext) -> ElementBox; fn tab_content(&self, style: &theme::Tab, cx: &AppContext) -> ElementBox;
@ -195,12 +195,12 @@ pub trait ItemView: View {
} }
} }
pub trait ItemViewHandle: 'static { pub trait ItemHandle: 'static {
fn tab_content(&self, style: &theme::Tab, cx: &AppContext) -> ElementBox; fn tab_content(&self, style: &theme::Tab, cx: &AppContext) -> ElementBox;
fn project_path(&self, cx: &AppContext) -> Option<ProjectPath>; fn project_path(&self, cx: &AppContext) -> Option<ProjectPath>;
fn boxed_clone(&self) -> Box<dyn ItemViewHandle>; fn boxed_clone(&self) -> Box<dyn ItemHandle>;
fn set_nav_history(&self, nav_history: Rc<RefCell<NavHistory>>, cx: &mut MutableAppContext); fn set_nav_history(&self, nav_history: Rc<RefCell<NavHistory>>, cx: &mut MutableAppContext);
fn clone_on_split(&self, cx: &mut MutableAppContext) -> Option<Box<dyn ItemViewHandle>>; fn clone_on_split(&self, cx: &mut MutableAppContext) -> Option<Box<dyn ItemHandle>>;
fn added_to_pane(&mut self, cx: &mut ViewContext<Pane>); fn added_to_pane(&mut self, cx: &mut ViewContext<Pane>);
fn deactivated(&self, cx: &mut MutableAppContext); fn deactivated(&self, cx: &mut MutableAppContext);
fn navigate(&self, data: Box<dyn Any>, cx: &mut MutableAppContext); fn navigate(&self, data: Box<dyn Any>, cx: &mut MutableAppContext);
@ -220,12 +220,12 @@ pub trait ItemViewHandle: 'static {
fn act_as_type(&self, type_id: TypeId, cx: &AppContext) -> Option<AnyViewHandle>; fn act_as_type(&self, type_id: TypeId, cx: &AppContext) -> Option<AnyViewHandle>;
} }
pub trait WeakItemViewHandle { pub trait WeakItemHandle {
fn id(&self) -> usize; fn id(&self) -> usize;
fn upgrade(&self, cx: &AppContext) -> Option<Box<dyn ItemViewHandle>>; fn upgrade(&self, cx: &AppContext) -> Option<Box<dyn ItemHandle>>;
} }
impl dyn ItemViewHandle { impl dyn ItemHandle {
pub fn downcast<T: View>(&self) -> Option<ViewHandle<T>> { pub fn downcast<T: View>(&self) -> Option<ViewHandle<T>> {
self.to_any().downcast() self.to_any().downcast()
} }
@ -236,7 +236,7 @@ impl dyn ItemViewHandle {
} }
} }
impl<T: ItemView> ItemViewHandle for ViewHandle<T> { impl<T: Item> ItemHandle for ViewHandle<T> {
fn tab_content(&self, style: &theme::Tab, cx: &AppContext) -> ElementBox { fn tab_content(&self, style: &theme::Tab, cx: &AppContext) -> ElementBox {
self.read(cx).tab_content(style, cx) self.read(cx).tab_content(style, cx)
} }
@ -245,7 +245,7 @@ impl<T: ItemView> ItemViewHandle for ViewHandle<T> {
self.read(cx).project_path(cx) self.read(cx).project_path(cx)
} }
fn boxed_clone(&self) -> Box<dyn ItemViewHandle> { fn boxed_clone(&self) -> Box<dyn ItemHandle> {
Box::new(self.clone()) Box::new(self.clone())
} }
@ -253,11 +253,11 @@ impl<T: ItemView> ItemViewHandle for ViewHandle<T> {
&self, &self,
// nav_history: Rc<RefCell<NavHistory>>, // nav_history: Rc<RefCell<NavHistory>>,
cx: &mut MutableAppContext, cx: &mut MutableAppContext,
) -> Option<Box<dyn ItemViewHandle>> { ) -> Option<Box<dyn ItemHandle>> {
self.update(cx, |item, cx| { self.update(cx, |item, cx| {
cx.add_option_view(|cx| item.clone_on_split(cx)) cx.add_option_view(|cx| item.clone_on_split(cx))
}) })
.map(|handle| Box::new(handle) as Box<dyn ItemViewHandle>) .map(|handle| Box::new(handle) as Box<dyn ItemHandle>)
} }
fn set_nav_history(&self, nav_history: Rc<RefCell<NavHistory>>, cx: &mut MutableAppContext) { fn set_nav_history(&self, nav_history: Rc<RefCell<NavHistory>>, cx: &mut MutableAppContext) {
@ -273,7 +273,7 @@ impl<T: ItemView> ItemViewHandle for ViewHandle<T> {
return; return;
} }
if T::should_activate_item_on_event(event) { if T::should_activate_item_on_event(event) {
if let Some(ix) = pane.index_for_item_view(&item) { if let Some(ix) = pane.index_for_item(&item) {
pane.activate_item(ix, cx); pane.activate_item(ix, cx);
pane.activate(cx); pane.activate(cx);
} }
@ -335,26 +335,25 @@ impl<T: ItemView> ItemViewHandle for ViewHandle<T> {
} }
} }
impl Into<AnyViewHandle> for Box<dyn ItemViewHandle> { impl Into<AnyViewHandle> for Box<dyn ItemHandle> {
fn into(self) -> AnyViewHandle { fn into(self) -> AnyViewHandle {
self.to_any() self.to_any()
} }
} }
impl Clone for Box<dyn ItemViewHandle> { impl Clone for Box<dyn ItemHandle> {
fn clone(&self) -> Box<dyn ItemViewHandle> { fn clone(&self) -> Box<dyn ItemHandle> {
self.boxed_clone() self.boxed_clone()
} }
} }
impl<T: ItemView> WeakItemViewHandle for WeakViewHandle<T> { impl<T: Item> WeakItemHandle for WeakViewHandle<T> {
fn id(&self) -> usize { fn id(&self) -> usize {
self.id() self.id()
} }
fn upgrade(&self, cx: &AppContext) -> Option<Box<dyn ItemViewHandle>> { fn upgrade(&self, cx: &AppContext) -> Option<Box<dyn ItemHandle>> {
self.upgrade(cx) self.upgrade(cx).map(|v| Box::new(v) as Box<dyn ItemHandle>)
.map(|v| Box::new(v) as Box<dyn ItemViewHandle>)
} }
} }
@ -556,7 +555,7 @@ impl Workspace {
&mut self, &mut self,
abs_paths: &[PathBuf], abs_paths: &[PathBuf],
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Task<Vec<Option<Result<Box<dyn ItemViewHandle>, Arc<anyhow::Error>>>>> { ) -> Task<Vec<Option<Result<Box<dyn ItemHandle>, Arc<anyhow::Error>>>>> {
let entries = abs_paths let entries = abs_paths
.iter() .iter()
.cloned() .cloned()
@ -652,28 +651,28 @@ impl Workspace {
&self, &self,
entry_id: ProjectEntryId, entry_id: ProjectEntryId,
cx: &AppContext, cx: &AppContext,
) -> Option<Box<dyn ItemViewHandle>> { ) -> Option<Box<dyn ItemHandle>> {
self.panes() self.panes()
.iter() .iter()
.find_map(|pane| pane.read(cx).item_for_entry(entry_id)) .find_map(|pane| pane.read(cx).item_for_entry(entry_id))
} }
pub fn item_of_type<T: ItemView>(&self, cx: &AppContext) -> Option<ViewHandle<T>> { pub fn item_of_type<T: Item>(&self, cx: &AppContext) -> Option<ViewHandle<T>> {
self.items_of_type(cx).max_by_key(|item| item.id()) self.items_of_type(cx).max_by_key(|item| item.id())
} }
pub fn items_of_type<'a, T: ItemView>( pub fn items_of_type<'a, T: Item>(
&'a self, &'a self,
cx: &'a AppContext, cx: &'a AppContext,
) -> impl 'a + Iterator<Item = ViewHandle<T>> { ) -> impl 'a + Iterator<Item = ViewHandle<T>> {
self.panes.iter().flat_map(|pane| { self.panes.iter().flat_map(|pane| {
pane.read(cx) pane.read(cx)
.item_views() .items()
.filter_map(|item| item.to_any().downcast()) .filter_map(|item| item.to_any().downcast())
}) })
} }
pub fn active_item(&self, cx: &AppContext) -> Option<Box<dyn ItemViewHandle>> { pub fn active_item(&self, cx: &AppContext) -> Option<Box<dyn ItemHandle>> {
self.active_pane().read(cx).active_item() self.active_pane().read(cx).active_item()
} }
@ -792,16 +791,16 @@ impl Workspace {
pane pane
} }
pub fn add_item(&mut self, item_view: Box<dyn ItemViewHandle>, cx: &mut ViewContext<Self>) { pub fn add_item(&mut self, item: Box<dyn ItemHandle>, cx: &mut ViewContext<Self>) {
self.active_pane() self.active_pane()
.update(cx, |pane, cx| pane.add_item(None, item_view, cx)) .update(cx, |pane, cx| pane.add_item(None, item, cx))
} }
pub fn open_path( pub fn open_path(
&mut self, &mut self,
path: ProjectPath, path: ProjectPath,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Task<Result<Box<dyn ItemViewHandle>, Arc<anyhow::Error>>> { ) -> Task<Result<Box<dyn ItemHandle>, Arc<anyhow::Error>>> {
let pane = self.active_pane().downgrade(); let pane = self.active_pane().downgrade();
let task = self.load_path(path, cx); let task = self.load_path(path, cx);
cx.spawn(|this, mut cx| async move { cx.spawn(|this, mut cx| async move {
@ -824,7 +823,7 @@ impl Workspace {
) -> Task< ) -> Task<
Result<( Result<(
ProjectEntryId, ProjectEntryId,
impl 'static + FnOnce(&mut MutableAppContext) -> Box<dyn ItemViewHandle>, impl 'static + FnOnce(&mut MutableAppContext) -> Box<dyn ItemHandle>,
)>, )>,
> { > {
let project = self.project().clone(); let project = self.project().clone();
@ -845,7 +844,7 @@ impl Workspace {
}) })
} }
pub fn activate_item(&mut self, item: &dyn ItemViewHandle, cx: &mut ViewContext<Self>) -> bool { pub fn activate_item(&mut self, item: &dyn ItemHandle, cx: &mut ViewContext<Self>) -> bool {
let result = self.panes.iter().find_map(|pane| { let result = self.panes.iter().find_map(|pane| {
if let Some(ix) = pane.read(cx).index_for_item(item) { if let Some(ix) = pane.read(cx).index_for_item(item) {
Some((pane.clone(), ix)) Some((pane.clone(), ix))

View file

@ -192,7 +192,7 @@ mod tests {
use theme::{Theme, ThemeRegistry, DEFAULT_THEME_NAME}; use theme::{Theme, ThemeRegistry, DEFAULT_THEME_NAME};
use util::test::temp_tree; use util::test::temp_tree;
use workspace::{ use workspace::{
open_paths, pane, ItemView, ItemViewHandle, OpenNew, Pane, SplitDirection, WorkspaceHandle, open_paths, pane, Item, ItemHandle, OpenNew, Pane, SplitDirection, WorkspaceHandle,
}; };
#[gpui::test] #[gpui::test]
@ -324,7 +324,7 @@ mod tests {
pane.active_item().unwrap().project_path(cx), pane.active_item().unwrap().project_path(cx),
Some(file1.clone()) Some(file1.clone())
); );
assert_eq!(pane.item_views().count(), 1); assert_eq!(pane.items().count(), 1);
}); });
// Open the second entry // Open the second entry
@ -338,7 +338,7 @@ mod tests {
pane.active_item().unwrap().project_path(cx), pane.active_item().unwrap().project_path(cx),
Some(file2.clone()) Some(file2.clone())
); );
assert_eq!(pane.item_views().count(), 2); assert_eq!(pane.items().count(), 2);
}); });
// Open the first entry again. The existing pane item is activated. // Open the first entry again. The existing pane item is activated.
@ -354,7 +354,7 @@ mod tests {
pane.active_item().unwrap().project_path(cx), pane.active_item().unwrap().project_path(cx),
Some(file1.clone()) Some(file1.clone())
); );
assert_eq!(pane.item_views().count(), 2); assert_eq!(pane.items().count(), 2);
}); });
// Split the pane with the first entry, then open the second entry again. // Split the pane with the first entry, then open the second entry again.
@ -393,7 +393,7 @@ mod tests {
Some(file3.clone()) Some(file3.clone())
); );
let pane_entries = pane let pane_entries = pane
.item_views() .items()
.map(|i| i.project_path(cx).unwrap()) .map(|i| i.project_path(cx).unwrap())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
assert_eq!(pane_entries, &[file1, file2, file3]); assert_eq!(pane_entries, &[file1, file2, file3]);