From 64e0c16baa67f4211e84bc3aa84cb1d09b68b322 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 4 May 2023 17:06:37 +0200 Subject: [PATCH] Use `Workspace::toggle_sidebar_item` when clicking on sidebar button Previously, we were mistakenly using `Sidebar::toggle_item`, which only performs part of the toggle operation. --- crates/workspace/src/sidebar.rs | 28 +++++++++++++++++++++------- crates/workspace/src/workspace.rs | 5 +++-- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/crates/workspace/src/sidebar.rs b/crates/workspace/src/sidebar.rs index 2b114d83eccf6e7a68ae4d8db0ec96e41330d798..ed629745a87efa447e999718352eca3dc1db616c 100644 --- a/crates/workspace/src/sidebar.rs +++ b/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, + workspace: WeakViewHandle, } #[derive(Clone, Debug, Deserialize, PartialEq)] @@ -210,9 +211,13 @@ impl View for Sidebar { } impl SidebarButtons { - pub fn new(sidebar: ViewHandle, cx: &mut ViewContext) -> Self { + pub fn new( + sidebar: ViewHandle, + workspace: WeakViewHandle, + cx: &mut ViewContext, + ) -> 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::( ix, diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index e1007043dcfc91d0f66204782b2b8cfa158ea76e..cbac09112828839fbe04ea0b11a05c498c559132 100644 --- a/crates/workspace/src/workspace.rs +++ b/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(¢er_pane.clone(), cx); status_bar.add_left_item(left_sidebar_buttons, cx);