Do not leak pane handles

Kirill Bulatov created

Change summary

crates/quick_action_bar/src/quick_action_bar.rs | 36 +++++-------------
crates/search/src/buffer_search.rs              | 25 ++++++++----
crates/zed/src/zed.rs                           | 10 ++--
3 files changed, 32 insertions(+), 39 deletions(-)

Detailed changes

crates/quick_action_bar/src/quick_action_bar.rs 🔗

@@ -6,18 +6,18 @@ use gpui::{
 };
 
 use search::{buffer_search, BufferSearchBar};
-use workspace::{item::ItemHandle, Pane, ToolbarItemLocation, ToolbarItemView};
+use workspace::{item::ItemHandle, ToolbarItemLocation, ToolbarItemView};
 
 pub struct QuickActionBar {
-    pane: ViewHandle<Pane>,
+    buffer_search_bar: ViewHandle<BufferSearchBar>,
     active_item: Option<Box<dyn ItemHandle>>,
     _inlays_enabled_subscription: Option<Subscription>,
 }
 
 impl QuickActionBar {
-    pub fn new(pane: ViewHandle<Pane>) -> Self {
+    pub fn new(buffer_search_bar: ViewHandle<BufferSearchBar>) -> Self {
         Self {
-            pane,
+            buffer_search_bar,
             active_item: None,
             _inlays_enabled_subscription: None,
         }
@@ -59,17 +59,7 @@ impl View for QuickActionBar {
         ));
 
         if editor.read(cx).buffer().read(cx).is_singleton() {
-            let buffer_search_bar = self
-                .pane
-                .read(cx)
-                .toolbar()
-                .read(cx)
-                .item_of_type::<BufferSearchBar>();
-            let search_bar_shown = buffer_search_bar
-                .as_ref()
-                .map(|bar| !bar.read(cx).is_dismissed())
-                .unwrap_or(false);
-
+            let search_bar_shown = !self.buffer_search_bar.read(cx).is_dismissed();
             let search_action = buffer_search::Deploy { focus: true };
 
             bar = bar.with_child(render_quick_action_bar_button(
@@ -82,17 +72,13 @@ impl View for QuickActionBar {
                 ),
                 cx,
                 move |this, cx| {
-                    if search_bar_shown {
-                        if let Some(buffer_search_bar) = buffer_search_bar.as_ref() {
-                            buffer_search_bar.update(cx, |buffer_search_bar, cx| {
-                                buffer_search_bar.dismiss(&buffer_search::Dismiss, cx);
-                            });
+                    this.buffer_search_bar.update(cx, |buffer_search_bar, cx| {
+                        if search_bar_shown {
+                            buffer_search_bar.dismiss(&buffer_search::Dismiss, cx);
+                        } else {
+                            buffer_search_bar.deploy(&search_action, cx);
                         }
-                    } else {
-                        this.pane.update(cx, |pane, cx| {
-                            BufferSearchBar::deploy(pane, &search_action, cx);
-                        });
-                    }
+                    });
                 },
             ));
         }

crates/search/src/buffer_search.rs 🔗

@@ -36,7 +36,7 @@ pub enum Event {
 }
 
 pub fn init(cx: &mut AppContext) {
-    cx.add_action(BufferSearchBar::deploy);
+    cx.add_action(BufferSearchBar::deploy_bar);
     cx.add_action(BufferSearchBar::dismiss);
     cx.add_action(BufferSearchBar::focus_editor);
     cx.add_action(BufferSearchBar::select_next_match);
@@ -327,6 +327,19 @@ impl BufferSearchBar {
         cx.notify();
     }
 
+    pub fn deploy(&mut self, deploy: &Deploy, cx: &mut ViewContext<Self>) -> bool {
+        if self.show(cx) {
+            self.search_suggested(cx);
+            if deploy.focus {
+                self.select_query(cx);
+                cx.focus_self();
+            }
+            return true;
+        }
+
+        false
+    }
+
     pub fn show(&mut self, cx: &mut ViewContext<Self>) -> bool {
         if self.active_searchable_item.is_none() {
             return false;
@@ -553,21 +566,15 @@ impl BufferSearchBar {
         .into_any()
     }
 
-    pub fn deploy(pane: &mut Pane, action: &Deploy, cx: &mut ViewContext<Pane>) {
+    fn deploy_bar(pane: &mut Pane, action: &Deploy, cx: &mut ViewContext<Pane>) {
         let mut propagate_action = true;
         if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
             search_bar.update(cx, |search_bar, cx| {
-                if search_bar.show(cx) {
-                    search_bar.search_suggested(cx);
-                    if action.focus {
-                        search_bar.select_query(cx);
-                        cx.focus_self();
-                    }
+                if search_bar.deploy(action, cx) {
                     propagate_action = false;
                 }
             });
         }
-
         if propagate_action {
             cx.propagate_action();
         }

crates/zed/src/zed.rs 🔗

@@ -257,16 +257,16 @@ pub fn initialize_workspace(
             let workspace_handle = cx.handle();
             cx.subscribe(&workspace_handle, {
                 move |workspace, _, event, cx| {
-                    if let workspace::Event::PaneAdded(pane_handle) = event {
-                        pane_handle.update(cx, |pane, cx| {
+                    if let workspace::Event::PaneAdded(pane) = event {
+                        pane.update(cx, |pane, cx| {
                             pane.toolbar().update(cx, |toolbar, cx| {
                                 let breadcrumbs = cx.add_view(|_| Breadcrumbs::new(workspace));
                                 toolbar.add_item(breadcrumbs, cx);
+                                let buffer_search_bar = cx.add_view(BufferSearchBar::new);
+                                toolbar.add_item(buffer_search_bar.clone(), cx);
                                 let quick_action_bar =
-                                    cx.add_view(|_| QuickActionBar::new(pane_handle.clone()));
+                                    cx.add_view(|_| QuickActionBar::new(buffer_search_bar));
                                 toolbar.add_item(quick_action_bar, cx);
-                                let buffer_search_bar = cx.add_view(BufferSearchBar::new);
-                                toolbar.add_item(buffer_search_bar, cx);
                                 let project_search_bar = cx.add_view(|_| ProjectSearchBar::new());
                                 toolbar.add_item(project_search_bar, cx);
                                 let submit_feedback_button =