Use `Workspace::toggle_sidebar_item` when clicking on sidebar button

Antonio Scandurra created

Previously, we were mistakenly using `Sidebar::toggle_item`, which only
performs part of the toggle operation.

Change summary

crates/workspace/src/sidebar.rs   | 28 +++++++++++++++++++++-------
crates/workspace/src/workspace.rs |  5 +++--
2 files changed, 24 insertions(+), 9 deletions(-)

Detailed changes

crates/workspace/src/sidebar.rs 🔗

@@ -1,7 +1,7 @@
-use crate::StatusItemView;
+use crate::{StatusItemView, Workspace};
 use gpui::{
     elements::*, impl_actions, platform::CursorStyle, platform::MouseButton, AnyViewHandle,
-    AppContext, Entity, Subscription, View, ViewContext, ViewHandle, WindowContext,
+    AppContext, Entity, Subscription, View, ViewContext, ViewHandle, WeakViewHandle, WindowContext,
 };
 use serde::Deserialize;
 use settings::Settings;
@@ -84,6 +84,7 @@ struct Item {
 
 pub struct SidebarButtons {
     sidebar: ViewHandle<Sidebar>,
+    workspace: WeakViewHandle<Workspace>,
 }
 
 #[derive(Clone, Debug, Deserialize, PartialEq)]
@@ -210,9 +211,13 @@ impl View for Sidebar {
 }
 
 impl SidebarButtons {
-    pub fn new(sidebar: ViewHandle<Sidebar>, cx: &mut ViewContext<Self>) -> Self {
+    pub fn new(
+        sidebar: ViewHandle<Sidebar>,
+        workspace: WeakViewHandle<Workspace>,
+        cx: &mut ViewContext<Self>,
+    ) -> Self {
         cx.observe(&sidebar, |_, _, cx| cx.notify()).detach();
-        Self { sidebar }
+        Self { sidebar, workspace }
     }
 }
 
@@ -279,9 +284,18 @@ impl View for SidebarButtons {
                             .with_style(style.container)
                     })
                     .with_cursor_style(CursorStyle::PointingHand)
-                    .on_click(MouseButton::Left, move |_, this, cx| {
-                        this.sidebar
-                            .update(cx, |sidebar, cx| sidebar.toggle_item(ix, cx));
+                    .on_click(MouseButton::Left, {
+                        let action = action.clone();
+                        move |_, this, cx| {
+                            if let Some(workspace) = this.workspace.upgrade(cx) {
+                                let action = action.clone();
+                                cx.window_context().defer(move |cx| {
+                                    workspace.update(cx, |workspace, cx| {
+                                        workspace.toggle_sidebar_item(&action, cx)
+                                    });
+                                });
+                            }
+                        }
                     })
                     .with_tooltip::<Self>(
                         ix,

crates/workspace/src/workspace.rs 🔗

@@ -583,10 +583,11 @@ impl Workspace {
 
         let left_sidebar = cx.add_view(|_| Sidebar::new(SidebarSide::Left));
         let right_sidebar = cx.add_view(|_| Sidebar::new(SidebarSide::Right));
-        let left_sidebar_buttons = cx.add_view(|cx| SidebarButtons::new(left_sidebar.clone(), cx));
+        let left_sidebar_buttons =
+            cx.add_view(|cx| SidebarButtons::new(left_sidebar.clone(), weak_handle.clone(), cx));
         let toggle_dock = cx.add_view(|cx| ToggleDockButton::new(handle, cx));
         let right_sidebar_buttons =
-            cx.add_view(|cx| SidebarButtons::new(right_sidebar.clone(), cx));
+            cx.add_view(|cx| SidebarButtons::new(right_sidebar.clone(), weak_handle.clone(), cx));
         let status_bar = cx.add_view(|cx| {
             let mut status_bar = StatusBar::new(&center_pane.clone(), cx);
             status_bar.add_left_item(left_sidebar_buttons, cx);