Show navigation history in the file finder modal

co-authored-by: Max <max@zed.dev>
This commit is contained in:
Kirill Bulatov 2023-05-17 17:35:06 +03:00
parent 711d2c6fe7
commit 201d513c50
8 changed files with 162 additions and 165 deletions

View file

@ -38,6 +38,7 @@ theme = { path = "../theme" }
util = { path = "../util" }
async-recursion = "1.0.0"
itertools = "0.10"
bincode = "1.2.1"
anyhow.workspace = true
futures.workspace = true

View file

@ -12,6 +12,7 @@ use gpui::{
platform::{CursorStyle, MouseButton},
AnyElement, AppContext, Border, Element, SizeConstraint, ViewContext, ViewHandle,
};
use std::sync::{atomic::AtomicUsize, Arc};
use theme::Theme;
pub use toggle_dock_button::ToggleDockButton;
@ -170,13 +171,21 @@ impl Dock {
pub fn new(
default_item_factory: DockDefaultItemFactory,
background_actions: BackgroundActions,
pane_history_timestamp: Arc<AtomicUsize>,
cx: &mut ViewContext<Workspace>,
) -> Self {
let position =
DockPosition::Hidden(settings::get::<WorkspaceSettings>(cx).default_dock_anchor);
let workspace = cx.weak_handle();
let pane =
cx.add_view(|cx| Pane::new(workspace, Some(position.anchor()), background_actions, cx));
let pane = cx.add_view(|cx| {
Pane::new(
workspace,
Some(position.anchor()),
background_actions,
pane_history_timestamp,
cx,
)
});
pane.update(cx, |pane, cx| {
pane.set_active(false, cx);
});

View file

@ -30,7 +30,17 @@ use gpui::{
};
use project::{Project, ProjectEntryId, ProjectPath};
use serde::Deserialize;
use std::{any::Any, cell::RefCell, cmp, mem, path::Path, rc::Rc};
use std::{
any::Any,
cell::RefCell,
cmp, mem,
path::Path,
rc::Rc,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
};
use theme::Theme;
use util::ResultExt;
@ -159,6 +169,8 @@ pub struct ItemNavHistory {
item: Rc<dyn WeakItemHandle>,
}
pub struct PaneNavHistory(Rc<RefCell<NavHistory>>);
struct NavHistory {
mode: NavigationMode,
backward_stack: VecDeque<NavigationEntry>,
@ -166,6 +178,7 @@ struct NavHistory {
closed_stack: VecDeque<NavigationEntry>,
paths_by_item: HashMap<usize, ProjectPath>,
pane: WeakViewHandle<Pane>,
next_timestamp: Arc<AtomicUsize>,
}
#[derive(Copy, Clone)]
@ -187,6 +200,7 @@ impl Default for NavigationMode {
pub struct NavigationEntry {
pub item: Rc<dyn WeakItemHandle>,
pub data: Option<Box<dyn Any>>,
pub timestamp: usize,
}
struct DraggedItem {
@ -226,6 +240,7 @@ impl Pane {
workspace: WeakViewHandle<Workspace>,
docked: Option<DockAnchor>,
background_actions: BackgroundActions,
next_timestamp: Arc<AtomicUsize>,
cx: &mut ViewContext<Self>,
) -> Self {
let pane_view_id = cx.view_id();
@ -249,6 +264,7 @@ impl Pane {
closed_stack: Default::default(),
paths_by_item: Default::default(),
pane: handle.clone(),
next_timestamp,
})),
toolbar: cx.add_view(|_| Toolbar::new(handle)),
tab_bar_context_menu: TabBarContextMenu {
@ -292,6 +308,10 @@ impl Pane {
}
}
pub fn nav_history(&self) -> PaneNavHistory {
PaneNavHistory(self.nav_history.clone())
}
pub fn go_back(
workspace: &mut Workspace,
pane: Option<WeakViewHandle<Pane>>,
@ -1942,6 +1962,7 @@ impl NavHistory {
self.backward_stack.push_back(NavigationEntry {
item,
data: data.map(|data| Box::new(data) as Box<dyn Any>),
timestamp: self.next_timestamp.fetch_add(1, Ordering::SeqCst),
});
self.forward_stack.clear();
}
@ -1952,6 +1973,7 @@ impl NavHistory {
self.forward_stack.push_back(NavigationEntry {
item,
data: data.map(|data| Box::new(data) as Box<dyn Any>),
timestamp: self.next_timestamp.fetch_add(1, Ordering::SeqCst),
});
}
NavigationMode::GoingForward => {
@ -1961,6 +1983,7 @@ impl NavHistory {
self.backward_stack.push_back(NavigationEntry {
item,
data: data.map(|data| Box::new(data) as Box<dyn Any>),
timestamp: self.next_timestamp.fetch_add(1, Ordering::SeqCst),
});
}
NavigationMode::ClosingItem => {
@ -1970,6 +1993,7 @@ impl NavHistory {
self.closed_stack.push_back(NavigationEntry {
item,
data: data.map(|data| Box::new(data) as Box<dyn Any>),
timestamp: self.next_timestamp.fetch_add(1, Ordering::SeqCst),
});
}
}
@ -1985,6 +2009,31 @@ impl NavHistory {
}
}
impl PaneNavHistory {
pub fn for_each_entry(
&self,
cx: &AppContext,
mut f: impl FnMut(&NavigationEntry, ProjectPath),
) {
let borrowed_history = self.0.borrow();
borrowed_history
.forward_stack
.iter()
.chain(borrowed_history.backward_stack.iter())
.chain(borrowed_history.closed_stack.iter())
.for_each(|entry| {
if let Some(path) = borrowed_history.paths_by_item.get(&entry.item.id()) {
f(entry, path.clone());
} else if let Some(item) = entry.item.upgrade(cx) {
let path = item.project_path(cx);
if let Some(path) = path {
f(entry, path);
}
}
})
}
}
pub struct PaneBackdrop<V: View> {
child_view: usize,
child: AnyElement<V>,

View file

@ -47,6 +47,7 @@ use gpui::{
WindowContext,
};
use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ProjectItem};
use itertools::Itertools;
use language::{LanguageRegistry, Rope};
use std::{
any::TypeId,
@ -55,7 +56,7 @@ use std::{
future::Future,
path::{Path, PathBuf},
str,
sync::Arc,
sync::{atomic::AtomicUsize, Arc},
time::Duration,
};
@ -481,6 +482,7 @@ pub struct Workspace {
_window_subscriptions: [Subscription; 3],
_apply_leader_updates: Task<Result<()>>,
_observe_current_user: Task<Result<()>>,
pane_history_timestamp: Arc<AtomicUsize>,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
@ -542,15 +544,24 @@ impl Workspace {
.detach();
let weak_handle = cx.weak_handle();
let pane_history_timestamp = Arc::new(AtomicUsize::new(0));
let center_pane = cx
.add_view(|cx| Pane::new(weak_handle.clone(), None, app_state.background_actions, cx));
let center_pane = cx.add_view(|cx| {
Pane::new(
weak_handle.clone(),
None,
app_state.background_actions,
pane_history_timestamp.clone(),
cx,
)
});
cx.subscribe(&center_pane, Self::handle_pane_event).detach();
cx.focus(&center_pane);
cx.emit(Event::PaneAdded(center_pane.clone()));
let dock = Dock::new(
app_state.dock_default_item_factory,
app_state.background_actions,
pane_history_timestamp.clone(),
cx,
);
let dock_pane = dock.pane().clone();
@ -665,6 +676,7 @@ impl Workspace {
_apply_leader_updates,
leader_updates_tx,
_window_subscriptions: subscriptions,
pane_history_timestamp,
};
this.project_remote_id_changed(project.read(cx).remote_id(), cx);
cx.defer(|this, cx| this.update_window_title(cx));
@ -825,6 +837,39 @@ impl Workspace {
&self.project
}
pub fn recent_navigation_history(
&self,
limit: Option<usize>,
cx: &AppContext,
) -> Vec<ProjectPath> {
let mut history: HashMap<ProjectPath, usize> = HashMap::default();
for pane in &self.panes {
let pane = pane.read(cx);
pane.nav_history()
.for_each_entry(cx, |entry, project_path| {
let timestamp = entry.timestamp;
match history.entry(project_path) {
hash_map::Entry::Occupied(mut entry) => {
if &timestamp > entry.get() {
entry.insert(timestamp);
}
}
hash_map::Entry::Vacant(entry) => {
entry.insert(timestamp);
}
}
});
}
history
.into_iter()
.sorted_by_key(|(_, timestamp)| *timestamp)
.map(|(project_path, _)| project_path)
.rev()
.take(limit.unwrap_or(usize::MAX))
.collect()
}
pub fn client(&self) -> &Client {
&self.app_state.client
}
@ -1386,6 +1431,7 @@ impl Workspace {
self.weak_handle(),
None,
self.app_state.background_actions,
self.pane_history_timestamp.clone(),
cx,
)
});