From 1938fd85e8029830af0760a588a742ea0e1457b2 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 11 Aug 2023 14:42:56 +0300 Subject: [PATCH] Do not leak pane handles --- .../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(-) diff --git a/crates/quick_action_bar/src/quick_action_bar.rs b/crates/quick_action_bar/src/quick_action_bar.rs index 245983dc49b4cada10efb0714ed827cf6016ccb7..b506f8dc17c4ca29c6d7d3ab97cafa6101c7bb29 100644 --- a/crates/quick_action_bar/src/quick_action_bar.rs +++ b/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, + buffer_search_bar: ViewHandle, active_item: Option>, _inlays_enabled_subscription: Option, } impl QuickActionBar { - pub fn new(pane: ViewHandle) -> Self { + pub fn new(buffer_search_bar: ViewHandle) -> 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::(); - 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); - }); - } + }); }, )); } diff --git a/crates/search/src/buffer_search.rs b/crates/search/src/buffer_search.rs index fc3c78afe233c1516fbfed4ef98df381a62f34f8..d85d311b8f96f357680123cf4add0f79606f3fa1 100644 --- a/crates/search/src/buffer_search.rs +++ b/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) -> 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) -> 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) { + fn deploy_bar(pane: &mut Pane, action: &Deploy, cx: &mut ViewContext) { let mut propagate_action = true; if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { 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(); } diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index 8d851909b31612deec42dc4faa088a6c6a677bd1..de05c259c81d7c63ff856ab93d4162a34f992511 100644 --- a/crates/zed/src/zed.rs +++ b/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 =