Restore a bunch of random workspace stuff

This commit is contained in:
Mikayla 2023-11-16 23:05:28 -08:00
parent e67c44a562
commit 9a3cd073c7
No known key found for this signature in database
11 changed files with 719 additions and 779 deletions

View file

@ -8,8 +8,8 @@ use anyhow::Result;
use collections::{HashMap, HashSet, VecDeque};
use gpui::{
actions, prelude::*, Action, AppContext, AsyncWindowContext, Component, Div, EntityId,
EventEmitter, FocusHandle, Focusable, FocusableView, Model, PromptLevel, Render, Task, View,
ViewContext, VisualContext, WeakView, WindowContext,
EventEmitter, FocusHandle, Focusable, FocusableView, Model, Pixels, Point, PromptLevel, Render,
Task, View, ViewContext, VisualContext, WeakView, WindowContext,
};
use parking_lot::Mutex;
use project2::{Project, ProjectEntryId, ProjectPath};
@ -102,29 +102,6 @@ actions!(
const MAX_NAVIGATION_HISTORY_LEN: usize = 1024;
pub fn init(cx: &mut AppContext) {
// todo!()
// cx.add_action(Pane::toggle_zoom);
// cx.add_action(|pane: &mut Pane, action: &ActivateItem, cx| {
// pane.activate_item(action.0, true, true, cx);
// });
// cx.add_action(|pane: &mut Pane, _: &ActivateLastItem, cx| {
// pane.activate_item(pane.items.len() - 1, true, true, cx);
// });
// cx.add_action(|pane: &mut Pane, _: &ActivatePrevItem, cx| {
// pane.activate_prev_item(true, cx);
// });
// cx.add_action(|pane: &mut Pane, _: &ActivateNextItem, cx| {
// pane.activate_next_item(true, cx);
// });
// cx.add_async_action(Pane::close_active_item);
// cx.add_async_action(Pane::close_inactive_items);
// cx.add_async_action(Pane::close_clean_items);
// cx.add_async_action(Pane::close_items_to_the_left);
// cx.add_async_action(Pane::close_items_to_the_right);
// cx.add_async_action(Pane::close_all_items);
}
pub enum Event {
AddItem { item: Box<dyn ItemHandle> },
ActivateItem { local: bool },
@ -140,7 +117,10 @@ pub enum Event {
impl fmt::Debug for Event {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Event::AddItem { item } => f.debug_struct("AddItem").field("item", &item.id()).finish(),
Event::AddItem { item } => f
.debug_struct("AddItem")
.field("item", &item.item_id())
.finish(),
Event::ActivateItem { local } => f
.debug_struct("ActivateItem")
.field("local", local)
@ -524,7 +504,7 @@ impl Pane {
.0
.lock()
.paths_by_item
.insert(item.id(), (project_path, abs_path));
.insert(item.item_id(), (project_path, abs_path));
}
}
}
@ -548,7 +528,7 @@ impl Pane {
};
let existing_item_index = self.items.iter().position(|existing_item| {
if existing_item.id() == item.id() {
if existing_item.item_id() == item.item_id() {
true
} else if existing_item.is_singleton(cx) {
existing_item
@ -613,21 +593,21 @@ impl Pane {
self.items.iter()
}
// pub fn items_of_type<T: View>(&self) -> impl '_ + Iterator<Item = ViewHandle<T>> {
// self.items
// .iter()
// .filter_map(|item| item.as_any().clone().downcast())
// }
pub fn items_of_type<T: Render>(&self) -> impl '_ + Iterator<Item = View<T>> {
self.items
.iter()
.filter_map(|item| item.to_any().downcast().ok())
}
pub fn active_item(&self) -> Option<Box<dyn ItemHandle>> {
self.items.get(self.active_item_index).cloned()
}
// pub fn pixel_position_of_cursor(&self, cx: &AppContext) -> Option<Vector2F> {
// self.items
// .get(self.active_item_index)?
// .pixel_position_of_cursor(cx)
// }
pub fn pixel_position_of_cursor(&self, cx: &AppContext) -> Option<Point<Pixels>> {
self.items
.get(self.active_item_index)?
.pixel_position_of_cursor(cx)
}
pub fn item_for_entry(
&self,
@ -644,24 +624,26 @@ impl Pane {
}
pub fn index_for_item(&self, item: &dyn ItemHandle) -> Option<usize> {
self.items.iter().position(|i| i.id() == item.id())
self.items
.iter()
.position(|i| i.item_id() == item.item_id())
}
// pub fn toggle_zoom(&mut self, _: &ToggleZoom, cx: &mut ViewContext<Self>) {
// // Potentially warn the user of the new keybinding
// let workspace_handle = self.workspace().clone();
// cx.spawn(|_, mut cx| async move { notify_of_new_dock(&workspace_handle, &mut cx) })
// .detach();
// pub fn toggle_zoom(&mut self, _: &ToggleZoom, cx: &mut ViewContext<Self>) {
// // Potentially warn the user of the new keybinding
// let workspace_handle = self.workspace().clone();
// cx.spawn(|_, mut cx| async move { notify_of_new_dock(&workspace_handle, &mut cx) })
// .detach();
// if self.zoomed {
// cx.emit(Event::ZoomOut);
// } else if !self.items.is_empty() {
// if !self.has_focus {
// cx.focus_self();
// }
// cx.emit(Event::ZoomIn);
// if self.zoomed {
// cx.emit(Event::ZoomOut);
// } else if !self.items.is_empty() {
// if !self.has_focus {
// cx.focus_self();
// }
// cx.emit(Event::ZoomIn);
// }
// }
pub fn activate_item(
&mut self,
@ -689,9 +671,9 @@ impl Pane {
if let Some(newly_active_item) = self.items.get(index) {
self.activation_history
.retain(|&previously_active_item_id| {
previously_active_item_id != newly_active_item.id()
previously_active_item_id != newly_active_item.item_id()
});
self.activation_history.push(newly_active_item.id());
self.activation_history.push(newly_active_item.item_id());
}
self.update_toolbar(cx);
@ -705,25 +687,25 @@ impl Pane {
}
}
// pub fn activate_prev_item(&mut self, activate_pane: bool, cx: &mut ViewContext<Self>) {
// let mut index = self.active_item_index;
// if index > 0 {
// index -= 1;
// } else if !self.items.is_empty() {
// index = self.items.len() - 1;
// }
// self.activate_item(index, activate_pane, activate_pane, cx);
// }
pub fn activate_prev_item(&mut self, activate_pane: bool, cx: &mut ViewContext<Self>) {
let mut index = self.active_item_index;
if index > 0 {
index -= 1;
} else if !self.items.is_empty() {
index = self.items.len() - 1;
}
self.activate_item(index, activate_pane, activate_pane, cx);
}
// pub fn activate_next_item(&mut self, activate_pane: bool, cx: &mut ViewContext<Self>) {
// let mut index = self.active_item_index;
// if index + 1 < self.items.len() {
// index += 1;
// } else {
// index = 0;
// }
// self.activate_item(index, activate_pane, activate_pane, cx);
// }
pub fn activate_next_item(&mut self, activate_pane: bool, cx: &mut ViewContext<Self>) {
let mut index = self.active_item_index;
if index + 1 < self.items.len() {
index += 1;
} else {
index = 0;
}
self.activate_item(index, activate_pane, activate_pane, cx);
}
pub fn close_active_item(
&mut self,
@ -733,7 +715,7 @@ impl Pane {
if self.items.is_empty() {
return None;
}
let active_item_id = self.items[self.active_item_index].id();
let active_item_id = self.items[self.active_item_index].item_id();
Some(self.close_item_by_id(
active_item_id,
action.save_intent.unwrap_or(SaveIntent::Close),
@ -750,106 +732,106 @@ impl Pane {
self.close_items(cx, save_intent, move |view_id| view_id == item_id_to_close)
}
// pub fn close_inactive_items(
// &mut self,
// _: &CloseInactiveItems,
// cx: &mut ViewContext<Self>,
// ) -> Option<Task<Result<()>>> {
// if self.items.is_empty() {
// return None;
// }
pub fn close_inactive_items(
&mut self,
_: &CloseInactiveItems,
cx: &mut ViewContext<Self>,
) -> Option<Task<Result<()>>> {
if self.items.is_empty() {
return None;
}
// let active_item_id = self.items[self.active_item_index].id();
// Some(self.close_items(cx, SaveIntent::Close, move |item_id| {
// item_id != active_item_id
// }))
// }
let active_item_id = self.items[self.active_item_index].item_id();
Some(self.close_items(cx, SaveIntent::Close, move |item_id| {
item_id != active_item_id
}))
}
// pub fn close_clean_items(
// &mut self,
// _: &CloseCleanItems,
// cx: &mut ViewContext<Self>,
// ) -> Option<Task<Result<()>>> {
// let item_ids: Vec<_> = self
// .items()
// .filter(|item| !item.is_dirty(cx))
// .map(|item| item.id())
// .collect();
// Some(self.close_items(cx, SaveIntent::Close, move |item_id| {
// item_ids.contains(&item_id)
// }))
// }
pub fn close_clean_items(
&mut self,
_: &CloseCleanItems,
cx: &mut ViewContext<Self>,
) -> Option<Task<Result<()>>> {
let item_ids: Vec<_> = self
.items()
.filter(|item| !item.is_dirty(cx))
.map(|item| item.item_id())
.collect();
Some(self.close_items(cx, SaveIntent::Close, move |item_id| {
item_ids.contains(&item_id)
}))
}
// pub fn close_items_to_the_left(
// &mut self,
// _: &CloseItemsToTheLeft,
// cx: &mut ViewContext<Self>,
// ) -> Option<Task<Result<()>>> {
// if self.items.is_empty() {
// return None;
// }
// let active_item_id = self.items[self.active_item_index].id();
// Some(self.close_items_to_the_left_by_id(active_item_id, cx))
// }
pub fn close_items_to_the_left(
&mut self,
_: &CloseItemsToTheLeft,
cx: &mut ViewContext<Self>,
) -> Option<Task<Result<()>>> {
if self.items.is_empty() {
return None;
}
let active_item_id = self.items[self.active_item_index].item_id();
Some(self.close_items_to_the_left_by_id(active_item_id, cx))
}
// pub fn close_items_to_the_left_by_id(
// &mut self,
// item_id: usize,
// cx: &mut ViewContext<Self>,
// ) -> Task<Result<()>> {
// let item_ids: Vec<_> = self
// .items()
// .take_while(|item| item.id() != item_id)
// .map(|item| item.id())
// .collect();
// self.close_items(cx, SaveIntent::Close, move |item_id| {
// item_ids.contains(&item_id)
// })
// }
pub fn close_items_to_the_left_by_id(
&mut self,
item_id: EntityId,
cx: &mut ViewContext<Self>,
) -> Task<Result<()>> {
let item_ids: Vec<_> = self
.items()
.take_while(|item| item.item_id() != item_id)
.map(|item| item.item_id())
.collect();
self.close_items(cx, SaveIntent::Close, move |item_id| {
item_ids.contains(&item_id)
})
}
// pub fn close_items_to_the_right(
// &mut self,
// _: &CloseItemsToTheRight,
// cx: &mut ViewContext<Self>,
// ) -> Option<Task<Result<()>>> {
// if self.items.is_empty() {
// return None;
// }
// let active_item_id = self.items[self.active_item_index].id();
// Some(self.close_items_to_the_right_by_id(active_item_id, cx))
// }
pub fn close_items_to_the_right(
&mut self,
_: &CloseItemsToTheRight,
cx: &mut ViewContext<Self>,
) -> Option<Task<Result<()>>> {
if self.items.is_empty() {
return None;
}
let active_item_id = self.items[self.active_item_index].item_id();
Some(self.close_items_to_the_right_by_id(active_item_id, cx))
}
// pub fn close_items_to_the_right_by_id(
// &mut self,
// item_id: usize,
// cx: &mut ViewContext<Self>,
// ) -> Task<Result<()>> {
// let item_ids: Vec<_> = self
// .items()
// .rev()
// .take_while(|item| item.id() != item_id)
// .map(|item| item.id())
// .collect();
// self.close_items(cx, SaveIntent::Close, move |item_id| {
// item_ids.contains(&item_id)
// })
// }
pub fn close_items_to_the_right_by_id(
&mut self,
item_id: EntityId,
cx: &mut ViewContext<Self>,
) -> Task<Result<()>> {
let item_ids: Vec<_> = self
.items()
.rev()
.take_while(|item| item.item_id() != item_id)
.map(|item| item.item_id())
.collect();
self.close_items(cx, SaveIntent::Close, move |item_id| {
item_ids.contains(&item_id)
})
}
// pub fn close_all_items(
// &mut self,
// action: &CloseAllItems,
// cx: &mut ViewContext<Self>,
// ) -> Option<Task<Result<()>>> {
// if self.items.is_empty() {
// return None;
// }
pub fn close_all_items(
&mut self,
action: &CloseAllItems,
cx: &mut ViewContext<Self>,
) -> Option<Task<Result<()>>> {
if self.items.is_empty() {
return None;
}
// Some(
// self.close_items(cx, action.save_intent.unwrap_or(SaveIntent::Close), |_| {
// true
// }),
// )
// }
Some(
self.close_items(cx, action.save_intent.unwrap_or(SaveIntent::Close), |_| {
true
}),
)
}
pub(super) fn file_names_for_prompt(
items: &mut dyn Iterator<Item = &Box<dyn ItemHandle>>,
@ -896,7 +878,7 @@ impl Pane {
let mut items_to_close = Vec::new();
let mut dirty_items = Vec::new();
for item in &self.items {
if should_close(item.id()) {
if should_close(item.item_id()) {
items_to_close.push(item.boxed_clone());
if item.is_dirty(cx) {
dirty_items.push(item.boxed_clone());
@ -949,7 +931,7 @@ impl Pane {
for item in workspace.items(cx) {
if !items_to_close
.iter()
.any(|item_to_close| item_to_close.id() == item.id())
.any(|item_to_close| item_to_close.item_id() == item.item_id())
{
let other_project_item_ids = item.project_item_model_ids(cx);
project_item_ids.retain(|id| !other_project_item_ids.contains(id));
@ -977,7 +959,11 @@ impl Pane {
// Remove the item from the pane.
pane.update(&mut cx, |pane, cx| {
if let Some(item_ix) = pane.items.iter().position(|i| i.id() == item.id()) {
if let Some(item_ix) = pane
.items
.iter()
.position(|i| i.item_id() == item.item_id())
{
pane.remove_item(item_ix, false, cx);
}
})?;
@ -995,7 +981,7 @@ impl Pane {
cx: &mut ViewContext<Self>,
) {
self.activation_history
.retain(|&history_entry| history_entry != self.items[item_index].id());
.retain(|&history_entry| history_entry != self.items[item_index].item_id());
if item_index == self.active_item_index {
let index_to_activate = self
@ -1003,7 +989,7 @@ impl Pane {
.pop()
.and_then(|last_activated_item| {
self.items.iter().enumerate().find_map(|(index, item)| {
(item.id() == last_activated_item).then_some(index)
(item.item_id() == last_activated_item).then_some(index)
})
})
// We didn't have a valid activation history entry, so fallback
@ -1020,7 +1006,9 @@ impl Pane {
let item = self.items.remove(item_index);
cx.emit(Event::RemoveItem { item_id: item.id() });
cx.emit(Event::RemoveItem {
item_id: item.item_id(),
});
if self.items.is_empty() {
item.deactivated(cx);
self.update_toolbar(cx);
@ -1041,16 +1029,20 @@ impl Pane {
.0
.lock()
.paths_by_item
.get(&item.id())
.get(&item.item_id())
.and_then(|(_, abs_path)| abs_path.clone());
self.nav_history
.0
.lock()
.paths_by_item
.insert(item.id(), (path, abs_path));
.insert(item.item_id(), (path, abs_path));
} else {
self.nav_history.0.lock().paths_by_item.remove(&item.id());
self.nav_history
.0
.lock()
.paths_by_item
.remove(&item.item_id());
}
if self.items.is_empty() && self.zoomed {
@ -1323,7 +1315,7 @@ impl Pane {
) -> Option<()> {
let (item_index_to_delete, item_id) = self.items().enumerate().find_map(|(i, item)| {
if item.is_singleton(cx) && item.project_entry_ids(cx).as_slice() == [entry_id] {
Some((i, item.id()))
Some((i, item.item_id()))
} else {
None
}
@ -1354,10 +1346,10 @@ impl Pane {
) -> impl Component<Self> {
let label = item.tab_content(Some(detail), cx);
let close_icon = || {
let id = item.id();
let id = item.item_id();
div()
.id(item.id())
.id(item.item_id())
.invisible()
.group_hover("", |style| style.visible())
.child(IconButton::new("close_tab", Icon::Close).on_click(
@ -1387,7 +1379,7 @@ impl Pane {
div()
.group("")
.id(item.id())
.id(item.item_id())
.cursor_pointer()
.when_some(item.tab_tooltip_text(cx), |div, text| {
div.tooltip(move |_, cx| cx.build_view(|cx| Tooltip::new(text.clone())).into())
@ -1914,6 +1906,25 @@ impl Render for Pane {
.on_action(|pane: &mut Pane, _: &SplitUp, cx| pane.split(SplitDirection::Up, cx))
.on_action(|pane: &mut Pane, _: &SplitRight, cx| pane.split(SplitDirection::Right, cx))
.on_action(|pane: &mut Pane, _: &SplitDown, cx| pane.split(SplitDirection::Down, cx))
// cx.add_action(Pane::toggle_zoom);
// cx.add_action(|pane: &mut Pane, action: &ActivateItem, cx| {
// pane.activate_item(action.0, true, true, cx);
// });
// cx.add_action(|pane: &mut Pane, _: &ActivateLastItem, cx| {
// pane.activate_item(pane.items.len() - 1, true, true, cx);
// });
// cx.add_action(|pane: &mut Pane, _: &ActivatePrevItem, cx| {
// pane.activate_prev_item(true, cx);
// });
// cx.add_action(|pane: &mut Pane, _: &ActivateNextItem, cx| {
// pane.activate_next_item(true, cx);
// });
// cx.add_async_action(Pane::close_active_item);
// cx.add_async_action(Pane::close_inactive_items);
// cx.add_async_action(Pane::close_clean_items);
// cx.add_async_action(Pane::close_items_to_the_left);
// cx.add_async_action(Pane::close_items_to_the_right);
// cx.add_async_action(Pane::close_all_items);
.size_full()
.on_action(|pane: &mut Self, action: &CloseActiveItem, cx| {
pane.close_active_item(action, cx)