Use the new split pane's navigation history when cloning an item

This commit is contained in:
Antonio Scandurra 2022-02-25 17:22:30 +01:00
parent 88bfe5acb0
commit b506db7c93
6 changed files with 67 additions and 34 deletions

View file

@ -598,7 +598,11 @@ impl workspace::ItemView for ProjectDiagnosticsEditor {
matches!(event, Event::Saved | Event::Dirtied | Event::TitleChanged) matches!(event, Event::Saved | Event::Dirtied | Event::TitleChanged)
} }
fn clone_on_split(&self, cx: &mut ViewContext<Self>) -> Option<Self> fn clone_on_split(
&self,
nav_history: ItemNavHistory,
cx: &mut ViewContext<Self>,
) -> Option<Self>
where where
Self: Sized, Self: Sized,
{ {
@ -608,13 +612,8 @@ impl workspace::ItemView for ProjectDiagnosticsEditor {
self.settings.clone(), self.settings.clone(),
cx, cx,
); );
diagnostics.editor.update(cx, |editor, cx| { diagnostics.editor.update(cx, |editor, _| {
let nav_history = self editor.set_nav_history(Some(nav_history));
.editor
.read(cx)
.nav_history()
.map(|nav_history| ItemNavHistory::new(nav_history.history(), &cx.handle()));
editor.set_nav_history(nav_history);
}); });
Some(diagnostics) Some(diagnostics)
} }

View file

@ -834,7 +834,7 @@ impl Editor {
Self::new(EditorMode::Full, buffer, project, settings, None, cx) Self::new(EditorMode::Full, buffer, project, settings, None, cx)
} }
pub fn clone(&self, cx: &mut ViewContext<Self>) -> Self { pub fn clone(&self, nav_history: ItemNavHistory, cx: &mut ViewContext<Self>) -> Self {
let mut clone = Self::new( let mut clone = Self::new(
self.mode, self.mode,
self.buffer.clone(), self.buffer.clone(),
@ -845,10 +845,7 @@ impl Editor {
); );
clone.scroll_position = self.scroll_position; clone.scroll_position = self.scroll_position;
clone.scroll_top_anchor = self.scroll_top_anchor.clone(); clone.scroll_top_anchor = self.scroll_top_anchor.clone();
clone.nav_history = self clone.nav_history = Some(nav_history);
.nav_history
.as_ref()
.map(|nav_history| ItemNavHistory::new(nav_history.history(), &cx.handle()));
clone clone
} }

View file

@ -194,11 +194,15 @@ impl ItemView for Editor {
}) })
} }
fn clone_on_split(&self, cx: &mut ViewContext<Self>) -> Option<Self> fn clone_on_split(
&self,
nav_history: ItemNavHistory,
cx: &mut ViewContext<Self>,
) -> Option<Self>
where where
Self: Sized, Self: Sized,
{ {
Some(self.clone(cx)) Some(self.clone(nav_history, cx))
} }
fn deactivated(&mut self, cx: &mut ViewContext<Self>) { fn deactivated(&mut self, cx: &mut ViewContext<Self>) {

View file

@ -1,14 +1,18 @@
use crate::SearchOption;
use editor::{Anchor, Autoscroll, Editor, MultiBuffer}; use editor::{Anchor, Autoscroll, Editor, MultiBuffer};
use gpui::{ use gpui::{
action, elements::*, keymap::Binding, platform::CursorStyle, ElementBox, Entity, ModelContext, action, elements::*, keymap::Binding, platform::CursorStyle, AppContext, ElementBox, Entity,
ModelHandle, MutableAppContext, RenderContext, Task, View, ViewContext, ViewHandle, ModelContext, ModelHandle, MutableAppContext, RenderContext, Task, View, ViewContext,
ViewHandle,
}; };
use postage::watch; use postage::watch;
use project::{search::SearchQuery, Project}; use project::{search::SearchQuery, Project};
use std::{any::TypeId, ops::Range}; use std::{
use workspace::{Settings, Workspace}; any::{Any, TypeId},
ops::Range,
use crate::SearchOption; path::PathBuf,
};
use workspace::{Item, ItemNavHistory, ItemView, Settings, Workspace};
action!(Deploy); action!(Deploy);
action!(Search); action!(Search);
@ -98,13 +102,13 @@ impl ProjectFind {
} }
} }
impl workspace::Item for ProjectFind { impl Item for ProjectFind {
type View = ProjectFindView; type View = ProjectFindView;
fn build_view( fn build_view(
model: ModelHandle<Self>, model: ModelHandle<Self>,
workspace: &workspace::Workspace, workspace: &Workspace,
nav_history: workspace::ItemNavHistory, nav_history: ItemNavHistory,
cx: &mut gpui::ViewContext<Self::View>, cx: &mut gpui::ViewContext<Self::View>,
) -> Self::View { ) -> Self::View {
let settings = workspace.settings(); let settings = workspace.settings();
@ -186,7 +190,7 @@ impl View for ProjectFindView {
} }
} }
impl workspace::ItemView for ProjectFindView { impl ItemView for ProjectFindView {
fn act_as_type( fn act_as_type(
&self, &self,
type_id: TypeId, type_id: TypeId,
@ -202,6 +206,11 @@ impl workspace::ItemView for ProjectFindView {
} }
} }
fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
self.results_editor
.update(cx, |editor, cx| editor.deactivated(cx));
}
fn item_id(&self, _: &gpui::AppContext) -> usize { fn item_id(&self, _: &gpui::AppContext) -> usize {
self.model.id() self.model.id()
} }
@ -234,13 +243,17 @@ impl workspace::ItemView for ProjectFindView {
fn save_as( fn save_as(
&mut self, &mut self,
_: ModelHandle<Project>, _: ModelHandle<Project>,
_: std::path::PathBuf, _: PathBuf,
_: &mut ViewContext<Self>, _: &mut ViewContext<Self>,
) -> Task<anyhow::Result<()>> { ) -> Task<anyhow::Result<()>> {
unreachable!("save_as should not have been called") unreachable!("save_as should not have been called")
} }
fn clone_on_split(&self, cx: &mut ViewContext<Self>) -> Option<Self> fn clone_on_split(
&self,
nav_history: ItemNavHistory,
cx: &mut ViewContext<Self>,
) -> Option<Self>
where where
Self: Sized, Self: Sized,
{ {
@ -251,8 +264,8 @@ impl workspace::ItemView for ProjectFindView {
cx, cx,
) )
}); });
let results_editor = self.results_editor.update(cx, |editor, cx| { let results_editor = self.results_editor.update(cx, |results_editor, cx| {
cx.add_view(|cx| editor.clone_on_split(cx).unwrap()) cx.add_view(|cx| results_editor.clone(nav_history, cx))
}); });
cx.observe(&self.model, |this, _, cx| this.model_changed(true, cx)) cx.observe(&self.model, |this, _, cx| this.model_changed(true, cx))
.detach(); .detach();
@ -269,6 +282,11 @@ impl workspace::ItemView for ProjectFindView {
view.model_changed(false, cx); view.model_changed(false, cx);
Some(view) Some(view)
} }
fn navigate(&mut self, data: Box<dyn Any>, cx: &mut ViewContext<Self>) {
self.results_editor
.update(cx, |editor, cx| editor.navigate(data, cx));
}
} }
impl ProjectFindView { impl ProjectFindView {

View file

@ -149,6 +149,10 @@ impl Pane {
} }
} }
pub(crate) fn nav_history(&self) -> &Rc<RefCell<NavHistory>> {
&self.nav_history
}
pub fn activate(&self, cx: &mut ViewContext<Self>) { pub fn activate(&self, cx: &mut ViewContext<Self>) {
cx.emit(Event::Activate); cx.emit(Event::Activate);
} }

View file

@ -156,7 +156,7 @@ pub trait ItemView: View {
fn item_id(&self, cx: &AppContext) -> usize; fn item_id(&self, cx: &AppContext) -> usize;
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 clone_on_split(&self, _: &mut ViewContext<Self>) -> Option<Self> fn clone_on_split(&self, _: ItemNavHistory, _: &mut ViewContext<Self>) -> Option<Self>
where where
Self: Sized, Self: Sized,
{ {
@ -229,7 +229,11 @@ pub trait ItemViewHandle: '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 ItemViewHandle>;
fn clone_on_split(&self, cx: &mut MutableAppContext) -> Option<Box<dyn ItemViewHandle>>; fn clone_on_split(
&self,
nav_history: Rc<RefCell<NavHistory>>,
cx: &mut MutableAppContext,
) -> Option<Box<dyn ItemViewHandle>>;
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);
@ -373,9 +377,15 @@ impl<T: ItemView> ItemViewHandle for ViewHandle<T> {
Box::new(self.clone()) Box::new(self.clone())
} }
fn clone_on_split(&self, cx: &mut MutableAppContext) -> Option<Box<dyn ItemViewHandle>> { fn clone_on_split(
&self,
nav_history: Rc<RefCell<NavHistory>>,
cx: &mut MutableAppContext,
) -> Option<Box<dyn ItemViewHandle>> {
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(ItemNavHistory::new(nav_history, &cx.handle()), cx)
})
}) })
.map(|handle| Box::new(handle) as Box<dyn ItemViewHandle>) .map(|handle| Box::new(handle) as Box<dyn ItemViewHandle>)
} }
@ -1047,7 +1057,8 @@ impl Workspace {
let new_pane = self.add_pane(cx); let new_pane = self.add_pane(cx);
self.activate_pane(new_pane.clone(), cx); self.activate_pane(new_pane.clone(), cx);
if let Some(item) = pane.read(cx).active_item() { if let Some(item) = pane.read(cx).active_item() {
if let Some(clone) = item.clone_on_split(cx.as_mut()) { let nav_history = new_pane.read(cx).nav_history().clone();
if let Some(clone) = item.clone_on_split(nav_history, cx.as_mut()) {
new_pane.update(cx, |new_pane, cx| new_pane.add_item_view(clone, cx)); new_pane.update(cx, |new_pane, cx| new_pane.add_item_view(clone, cx));
} }
} }