From c63ca09eed5905c33375bd1960e2db69d47e2269 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 30 Nov 2023 12:10:25 -0800 Subject: [PATCH] Reintroduce pane navigation history in zed2 --- crates/editor2/src/items.rs | 65 +++++++++++++++++------------------ crates/workspace2/src/pane.rs | 46 +++++++++++++++++++------ 2 files changed, 68 insertions(+), 43 deletions(-) diff --git a/crates/editor2/src/items.rs b/crates/editor2/src/items.rs index eca3b99d7807b455ba48ed99ef0287a9ee084abf..43b3294471fc9932df259d9f3271485c5b0d7a89 100644 --- a/crates/editor2/src/items.rs +++ b/crates/editor2/src/items.rs @@ -529,39 +529,38 @@ fn deserialize_anchor(buffer: &MultiBufferSnapshot, anchor: proto::EditorAnchor) impl Item for Editor { fn navigate(&mut self, data: Box, cx: &mut ViewContext) -> bool { - todo!(); - // if let Ok(data) = data.downcast::() { - // let newest_selection = self.selections.newest::(cx); - // let buffer = self.buffer.read(cx).read(cx); - // let offset = if buffer.can_resolve(&data.cursor_anchor) { - // data.cursor_anchor.to_point(&buffer) - // } else { - // buffer.clip_point(data.cursor_position, Bias::Left) - // }; - - // let mut scroll_anchor = data.scroll_anchor; - // if !buffer.can_resolve(&scroll_anchor.anchor) { - // scroll_anchor.anchor = buffer.anchor_before( - // buffer.clip_point(Point::new(data.scroll_top_row, 0), Bias::Left), - // ); - // } - - // drop(buffer); - - // if newest_selection.head() == offset { - // false - // } else { - // let nav_history = self.nav_history.take(); - // self.set_scroll_anchor(scroll_anchor, cx); - // self.change_selections(Some(Autoscroll::fit()), cx, |s| { - // s.select_ranges([offset..offset]) - // }); - // self.nav_history = nav_history; - // true - // } - // } else { - // false - // } + if let Ok(data) = data.downcast::() { + let newest_selection = self.selections.newest::(cx); + let buffer = self.buffer.read(cx).read(cx); + let offset = if buffer.can_resolve(&data.cursor_anchor) { + data.cursor_anchor.to_point(&buffer) + } else { + buffer.clip_point(data.cursor_position, Bias::Left) + }; + + let mut scroll_anchor = data.scroll_anchor; + if !buffer.can_resolve(&scroll_anchor.anchor) { + scroll_anchor.anchor = buffer.anchor_before( + buffer.clip_point(Point::new(data.scroll_top_row, 0), Bias::Left), + ); + } + + drop(buffer); + + if newest_selection.head() == offset { + false + } else { + let nav_history = self.nav_history.take(); + self.set_scroll_anchor(scroll_anchor, cx); + self.change_selections(Some(Autoscroll::fit()), cx, |s| { + s.select_ranges([offset..offset]) + }); + self.nav_history = nav_history; + true + } + } else { + false + } } fn tab_tooltip_text(&self, cx: &AppContext) -> Option { diff --git a/crates/workspace2/src/pane.rs b/crates/workspace2/src/pane.rs index 1bf83f63958ab5c0d7eebb098e4f274bf4d42612..aa91cfc91b9637a5dabcdd84fbba4a429918675f 100644 --- a/crates/workspace2/src/pane.rs +++ b/crates/workspace2/src/pane.rs @@ -457,6 +457,28 @@ impl Pane { !self.nav_history.0.lock().forward_stack.is_empty() } + fn navigate_backward(&mut self, cx: &mut ViewContext) { + if let Some(workspace) = self.workspace.upgrade() { + let pane = cx.view().downgrade(); + cx.window_context().defer(move |cx| { + workspace.update(cx, |workspace, cx| { + workspace.go_back(pane, cx).detach_and_log_err(cx) + }) + }) + } + } + + fn navigate_forward(&mut self, cx: &mut ViewContext) { + if let Some(workspace) = self.workspace.upgrade() { + let pane = cx.view().downgrade(); + cx.window_context().defer(move |cx| { + workspace.update(cx, |workspace, cx| { + workspace.go_forward(pane, cx).detach_and_log_err(cx) + }) + }) + } + } + fn history_updated(&mut self, cx: &mut ViewContext) { self.toolbar.update(cx, |_, cx| cx.notify()); } @@ -1483,12 +1505,20 @@ impl Pane { .child( div().border().border_color(gpui::red()).child( IconButton::new("navigate_backward", Icon::ArrowLeft) + .on_click({ + let view = cx.view().clone(); + move |_, cx| view.update(cx, Self::navigate_backward) + }) .disabled(!self.can_navigate_backward()), ), ) .child( div().border().border_color(gpui::red()).child( IconButton::new("navigate_forward", Icon::ArrowRight) + .on_click({ + let view = cx.view().clone(); + move |_, cx| view.update(cx, Self::navigate_backward) + }) .disabled(!self.can_navigate_forward()), ), ), @@ -1935,18 +1965,14 @@ impl Render for Pane { v_stack() .key_context("Pane") .track_focus(&self.focus_handle) - .on_action(cx.listener(|pane: &mut Pane, _: &SplitLeft, cx| { - pane.split(SplitDirection::Left, cx) - })) + .on_action(cx.listener(|pane, _: &SplitLeft, cx| pane.split(SplitDirection::Left, cx))) + .on_action(cx.listener(|pane, _: &SplitUp, cx| pane.split(SplitDirection::Up, cx))) .on_action( - cx.listener(|pane: &mut Pane, _: &SplitUp, cx| pane.split(SplitDirection::Up, cx)), + cx.listener(|pane, _: &SplitRight, cx| pane.split(SplitDirection::Right, cx)), ) - .on_action(cx.listener(|pane: &mut Pane, _: &SplitRight, cx| { - pane.split(SplitDirection::Right, cx) - })) - .on_action(cx.listener(|pane: &mut Pane, _: &SplitDown, cx| { - pane.split(SplitDirection::Down, cx) - })) + .on_action(cx.listener(|pane, _: &SplitDown, cx| pane.split(SplitDirection::Down, cx))) + .on_action(cx.listener(|pane, _: &GoBack, cx| pane.navigate_backward(cx))) + .on_action(cx.listener(|pane, _: &GoForward, cx| pane.navigate_forward(cx))) // cx.add_action(Pane::toggle_zoom); // cx.add_action(|pane: &mut Pane, action: &ActivateItem, cx| { // pane.activate_item(action.0, true, true, cx);