Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Use `Workspace::toggle_sidebar_item` when clicking on sidebar button
Antonio Scandurra64e0c16baa67
diff --git a/crates/workspace/src/sidebar.rs b/crates/workspace/src/sidebar.rsindex 2b114d83ecc..ed629745a87 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<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,diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rsindex e1007043dcf..cbac0911282 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);