Skip past entries in the navigation history that can't be re-opened

This commit is contained in:
Antonio Scandurra 2022-01-18 09:58:40 +01:00
parent 11b7270f68
commit d480738cc5

View file

@ -7,12 +7,12 @@ use gpui::{
geometry::{rect::RectF, vector::vec2f}, geometry::{rect::RectF, vector::vec2f},
keymap::Binding, keymap::Binding,
platform::CursorStyle, platform::CursorStyle,
Entity, MutableAppContext, Quad, RenderContext, View, ViewContext, Entity, MutableAppContext, Quad, RenderContext, Task, View, ViewContext, ViewHandle,
}; };
use postage::watch; use postage::watch;
use project::ProjectPath; use project::ProjectPath;
use std::{any::Any, cell::RefCell, cmp, mem, rc::Rc}; use std::{any::Any, cell::RefCell, cmp, mem, rc::Rc};
use util::TryFutureExt; use util::ResultExt;
action!(Split, SplitDirection); action!(Split, SplitDirection);
action!(ActivateItem, usize); action!(ActivateItem, usize);
@ -42,8 +42,12 @@ pub fn init(cx: &mut MutableAppContext) {
cx.add_action(|pane: &mut Pane, action: &Split, cx| { cx.add_action(|pane: &mut Pane, action: &Split, cx| {
pane.split(action.0, cx); pane.split(action.0, cx);
}); });
cx.add_action(Pane::go_back); cx.add_action(|workspace: &mut Workspace, _: &GoBack, cx| {
cx.add_action(Pane::go_forward); Pane::go_back(workspace, cx).detach();
});
cx.add_action(|workspace: &mut Workspace, _: &GoForward, cx| {
Pane::go_forward(workspace, cx).detach();
});
cx.add_bindings(vec![ cx.add_bindings(vec![
Binding::new("shift-cmd-{", ActivatePrevItem, Some("Pane")), Binding::new("shift-cmd-{", ActivatePrevItem, Some("Pane")),
@ -78,20 +82,20 @@ pub struct Navigation(RefCell<NavigationHistory>);
#[derive(Default)] #[derive(Default)]
struct NavigationHistory { struct NavigationHistory {
mode: NavigationHistoryMode, mode: NavigationMode,
backward_stack: Vec<NavigationEntry>, backward_stack: Vec<NavigationEntry>,
forward_stack: Vec<NavigationEntry>, forward_stack: Vec<NavigationEntry>,
paths_by_item: HashMap<usize, ProjectPath>, paths_by_item: HashMap<usize, ProjectPath>,
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
enum NavigationHistoryMode { enum NavigationMode {
Normal, Normal,
GoingBack, GoingBack,
GoingForward, GoingForward,
} }
impl Default for NavigationHistoryMode { impl Default for NavigationMode {
fn default() -> Self { fn default() -> Self {
Self::Normal Self::Normal
} }
@ -116,20 +120,31 @@ impl Pane {
cx.emit(Event::Activate); cx.emit(Event::Activate);
} }
pub fn go_back(workspace: &mut Workspace, _: &GoBack, cx: &mut ViewContext<Workspace>) { pub fn go_back(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> Task<()> {
Self::navigate_history(workspace, NavigationHistoryMode::GoingBack, cx); Self::navigate_history(
workspace,
workspace.active_pane().clone(),
NavigationMode::GoingBack,
cx,
)
} }
pub fn go_forward(workspace: &mut Workspace, _: &GoForward, cx: &mut ViewContext<Workspace>) { pub fn go_forward(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> Task<()> {
Self::navigate_history(workspace, NavigationHistoryMode::GoingForward, cx); Self::navigate_history(
workspace,
workspace.active_pane().clone(),
NavigationMode::GoingForward,
cx,
)
} }
fn navigate_history( fn navigate_history(
workspace: &mut Workspace, workspace: &mut Workspace,
mode: NavigationHistoryMode, pane: ViewHandle<Pane>,
mode: NavigationMode,
cx: &mut ViewContext<Workspace>, cx: &mut ViewContext<Workspace>,
) -> Option<()> { ) -> Task<()> {
let (project_path, entry) = workspace.active_pane().update(cx, |pane, cx| { let to_load = pane.update(cx, |pane, cx| {
// Retrieve the weak item handle from the history. // Retrieve the weak item handle from the history.
let entry = pane.navigation.pop(mode)?; let entry = pane.navigation.pop(mode)?;
@ -142,7 +157,7 @@ impl Pane {
if let Some(item_view) = pane.active_item() { if let Some(item_view) = pane.active_item() {
pane.navigation.set_mode(mode); pane.navigation.set_mode(mode);
item_view.deactivated(cx); item_view.deactivated(cx);
pane.navigation.set_mode(NavigationHistoryMode::Normal); pane.navigation.set_mode(NavigationMode::Normal);
} }
pane.active_item_index = index; pane.active_item_index = index;
@ -164,34 +179,37 @@ impl Pane {
.cloned() .cloned()
.map(|project_path| (project_path, entry)) .map(|project_path| (project_path, entry))
} }
})?; });
// If the item was no longer present, then load it again from its previous path. if let Some((project_path, entry)) = to_load {
let task = workspace.load_path(project_path, cx); // If the item was no longer present, then load it again from its previous path.
cx.spawn(|workspace, mut cx| { let pane = pane.downgrade();
async move { let task = workspace.load_path(project_path, cx);
let item = task.await?; cx.spawn(|workspace, mut cx| async move {
workspace.update(&mut cx, |workspace, cx| { let item = task.await;
let pane = workspace.active_pane().clone(); if let Some(pane) = cx.read(|cx| pane.upgrade(cx)) {
pane.update(cx, |pane, cx| { if let Some(item) = item.log_err() {
pane.navigation.set_mode(mode); workspace.update(&mut cx, |workspace, cx| {
let item_view = pane.open_item(item, workspace, cx); pane.update(cx, |p, _| p.navigation.set_mode(mode));
pane.navigation.set_mode(NavigationHistoryMode::Normal); let item_view = workspace.open_item_in_pane(item, &pane, cx);
pane.update(cx, |p, _| p.navigation.set_mode(NavigationMode::Normal));
if let Some(data) = entry.data { if let Some(data) = entry.data {
item_view.navigate(data, cx); item_view.navigate(data, cx);
} }
});
cx.notify(); } else {
}); workspace
}); .update(&mut cx, |workspace, cx| {
Ok(()) Self::navigate_history(workspace, pane, mode, cx)
} })
.log_err() .await;
}) }
.detach(); }
})
None } else {
Task::ready(())
}
} }
pub fn open_item<T>( pub fn open_item<T>(
@ -516,35 +534,35 @@ impl View for Pane {
} }
impl Navigation { impl Navigation {
fn pop(&self, mode: NavigationHistoryMode) -> Option<NavigationEntry> { fn pop(&self, mode: NavigationMode) -> Option<NavigationEntry> {
match mode { match mode {
NavigationHistoryMode::Normal => None, NavigationMode::Normal => None,
NavigationHistoryMode::GoingBack => self.0.borrow_mut().backward_stack.pop(), NavigationMode::GoingBack => self.0.borrow_mut().backward_stack.pop(),
NavigationHistoryMode::GoingForward => self.0.borrow_mut().forward_stack.pop(), NavigationMode::GoingForward => self.0.borrow_mut().forward_stack.pop(),
} }
} }
fn set_mode(&self, mode: NavigationHistoryMode) { fn set_mode(&self, mode: NavigationMode) {
self.0.borrow_mut().mode = mode; self.0.borrow_mut().mode = mode;
} }
pub fn push<D: 'static + Any, T: ItemView>(&self, data: Option<D>, cx: &mut ViewContext<T>) { pub fn push<D: 'static + Any, T: ItemView>(&self, data: Option<D>, cx: &mut ViewContext<T>) {
let mut state = self.0.borrow_mut(); let mut state = self.0.borrow_mut();
match state.mode { match state.mode {
NavigationHistoryMode::Normal => { NavigationMode::Normal => {
state.backward_stack.push(NavigationEntry { state.backward_stack.push(NavigationEntry {
item_view: Box::new(cx.weak_handle()), item_view: Box::new(cx.weak_handle()),
data: data.map(|data| Box::new(data) as Box<dyn Any>), data: data.map(|data| Box::new(data) as Box<dyn Any>),
}); });
state.forward_stack.clear(); state.forward_stack.clear();
} }
NavigationHistoryMode::GoingBack => { NavigationMode::GoingBack => {
state.forward_stack.push(NavigationEntry { state.forward_stack.push(NavigationEntry {
item_view: Box::new(cx.weak_handle()), item_view: Box::new(cx.weak_handle()),
data: data.map(|data| Box::new(data) as Box<dyn Any>), data: data.map(|data| Box::new(data) as Box<dyn Any>),
}); });
} }
NavigationHistoryMode::GoingForward => { NavigationMode::GoingForward => {
state.backward_stack.push(NavigationEntry { state.backward_stack.push(NavigationEntry {
item_view: Box::new(cx.weak_handle()), item_view: Box::new(cx.weak_handle()),
data: data.map(|data| Box::new(data) as Box<dyn Any>), data: data.map(|data| Box::new(data) as Box<dyn Any>),