diff --git a/crates/collab_ui/src/contact_finder.rs b/crates/collab_ui/src/contact_finder.rs index 83f094ebadf0765b03ade8d7d7e18e651e0b85d6..5eefa60b8f7ad4a78452d6fa2965b01a1def2266 100644 --- a/crates/collab_ui/src/contact_finder.rs +++ b/crates/collab_ui/src/contact_finder.rs @@ -33,7 +33,7 @@ impl View for ContactFinder { } fn render(&mut self, cx: &mut RenderContext) -> ElementBox { - ChildView::new(self.picker.clone(), cx).boxed() + ChildView::new(&self.picker, cx).boxed() } fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext) { diff --git a/crates/collab_ui/src/contact_list.rs b/crates/collab_ui/src/contact_list.rs index e92ecb7d88a526f82cb03528b4599c7194a059dd..bfce7db1bcd0c00e2828142fd31fe06d07eed09d 100644 --- a/crates/collab_ui/src/contact_list.rs +++ b/crates/collab_ui/src/contact_list.rs @@ -1339,7 +1339,7 @@ impl View for ContactList { .with_child( Flex::row() .with_child( - ChildView::new(self.filter_editor.clone(), cx) + ChildView::new(&self.filter_editor, cx) .contained() .with_style(theme.contact_list.user_query_editor.container) .flex(1., true) diff --git a/crates/command_palette/src/command_palette.rs b/crates/command_palette/src/command_palette.rs index 229e4a04e5e52b5fbf60a9820288b8b0a61a9cc0..3a1f2e6161825cebf884a8f9ec872e7fc89a47fd 100644 --- a/crates/command_palette/src/command_palette.rs +++ b/crates/command_palette/src/command_palette.rs @@ -85,7 +85,7 @@ impl CommandPalette { .unwrap_or_else(|| workspace.id()); cx.as_mut().defer(move |cx| { - let this = cx.add_view(workspace.clone(), |cx| Self::new(focused_view_id, cx)); + let this = cx.add_view(&workspace, |cx| Self::new(focused_view_id, cx)); workspace.update(cx, |workspace, cx| { workspace.toggle_modal(cx, |_, cx| { cx.subscribe(&this, Self::on_event).detach(); @@ -129,7 +129,7 @@ impl View for CommandPalette { } fn render(&mut self, cx: &mut RenderContext) -> gpui::ElementBox { - ChildView::new(self.picker.clone(), cx).boxed() + ChildView::new(&self.picker, cx).boxed() } fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext) { @@ -355,7 +355,7 @@ mod tests { }); workspace.update(cx, |workspace, cx| { - cx.focus(editor.clone()); + cx.focus(&editor); workspace.add_item(Box::new(editor.clone()), cx) }); diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index 223255544290ba93523a27ea5085f7871801e2a6..8abdeee2fb2f9e2dfcf40e21caeed9380c385b4e 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -602,16 +602,16 @@ impl Item for ProjectDiagnosticsEditor { )) } - fn act_as_type( - &self, + fn act_as_type<'a>( + &'a self, type_id: TypeId, - self_handle: &ViewHandle, - _: &AppContext, - ) -> Option { + self_handle: &'a ViewHandle, + _: &'a AppContext, + ) -> Option<&AnyViewHandle> { if type_id == TypeId::of::() { - Some(self_handle.into()) + Some(self_handle) } else if type_id == TypeId::of::() { - Some((&self.editor).into()) + Some(&self.editor) } else { None } diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index a5722e183e545e9eb2068a055433a6ffcd7eb27a..19c52a27643d68a1f869728b2847a9a171cb57c7 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -5753,7 +5753,7 @@ impl Editor { render: Arc::new({ let editor = rename_editor.clone(); move |cx: &mut BlockContext| { - ChildView::new(editor.clone(), cx) + ChildView::new(&editor, cx) .contained() .with_padding_left(cx.anchor_x) .boxed() diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index a053fd8acafe6dcae54a111a7a9be2af094a7cac..fa18c747f862785621422f5730e02561fa6eddbe 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -835,7 +835,7 @@ impl Item for Editor { .context("Project item at stored path was not a buffer")?; Ok(cx.update(|cx| { - cx.add_view(pane, |cx| { + cx.add_view(&pane, |cx| { let mut editor = Editor::for_buffer(buffer, Some(project), cx); editor.read_scroll_position_from_db(item_id, workspace_id, cx); editor diff --git a/crates/feedback/src/feedback_editor.rs b/crates/feedback/src/feedback_editor.rs index 9e759b4d1521549ec32e4a6d111bdedeb0d7f2f8..62ce0caa7dd635a509b5c9941e8bb3973c74c956 100644 --- a/crates/feedback/src/feedback_editor.rs +++ b/crates/feedback/src/feedback_editor.rs @@ -333,16 +333,16 @@ impl Item for FeedbackEditor { Some(Box::new(handle.clone())) } - fn act_as_type( - &self, + fn act_as_type<'a>( + &'a self, type_id: TypeId, - self_handle: &ViewHandle, - _: &AppContext, - ) -> Option { + self_handle: &'a ViewHandle, + _: &'a AppContext, + ) -> Option<&'a AnyViewHandle> { if type_id == TypeId::of::() { - Some(self_handle.into()) + Some(self_handle) } else if type_id == TypeId::of::() { - Some((&self.editor).into()) + Some(&self.editor) } else { None } diff --git a/crates/file_finder/src/file_finder.rs b/crates/file_finder/src/file_finder.rs index 51d4df45f58fe59e32b4cc1eefbd10f9d49ddeaf..bc7e23d1ae7a3ecdc7734273a29b2d89df28c101 100644 --- a/crates/file_finder/src/file_finder.rs +++ b/crates/file_finder/src/file_finder.rs @@ -51,7 +51,7 @@ impl View for FileFinder { } fn render(&mut self, cx: &mut RenderContext) -> ElementBox { - ChildView::new(self.picker.clone(), cx).boxed() + ChildView::new(&self.picker, cx).boxed() } fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext) { @@ -352,7 +352,8 @@ mod tests { let active_item = active_pane.read(cx).active_item().unwrap(); assert_eq!( active_item - .to_any() + .as_any() + .clone() .downcast::() .unwrap() .read(cx) diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index f48e45073d149a4e27d0478a1fcd4940e9abbf07..464bb071f3cb60518c0bcf9f78613801638a99e7 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -1812,16 +1812,11 @@ impl MutableAppContext { ) } - pub fn add_view( - &mut self, - parent_handle: impl Into, - build_view: F, - ) -> ViewHandle + pub fn add_view(&mut self, parent_handle: &AnyViewHandle, build_view: F) -> ViewHandle where T: View, F: FnOnce(&mut ViewContext) -> T, { - let parent_handle = parent_handle.into(); self.build_and_insert_view( parent_handle.window_id, ParentId::View(parent_handle.view_id), @@ -3794,11 +3789,7 @@ impl<'a, T: View> ViewContext<'a, T> { self.app.debug_elements(self.window_id).unwrap() } - pub fn focus(&mut self, handle: S) - where - S: Into, - { - let handle = handle.into(); + pub fn focus(&mut self, handle: &AnyViewHandle) { self.app.focus(handle.window_id, Some(handle.view_id)); } @@ -3890,8 +3881,7 @@ impl<'a, T: View> ViewContext<'a, T> { self.cx.parent(self.window_id, self.view_id) } - pub fn reparent(&mut self, view_handle: impl Into) { - let view_handle = view_handle.into(); + pub fn reparent(&mut self, view_handle: &AnyViewHandle) { if self.window_id != view_handle.window_id { panic!("Can't reparent view to a view from a different window"); } @@ -4677,12 +4667,16 @@ impl Clone for WeakModelHandle { impl Copy for WeakModelHandle {} pub struct ViewHandle { - window_id: usize, - view_id: usize, + any_handle: AnyViewHandle, view_type: PhantomData, - ref_counts: Arc>, - #[cfg(any(test, feature = "test-support"))] - handle_id: usize, +} + +impl Deref for ViewHandle { + type Target = AnyViewHandle; + + fn deref(&self) -> &Self::Target { + &self.any_handle + } } impl ViewHandle { @@ -4696,13 +4690,15 @@ impl ViewHandle { .handle_created(Some(type_name::()), view_id); Self { - window_id, - view_id, + any_handle: AnyViewHandle { + window_id, + view_id, + view_type: TypeId::of::(), + ref_counts: ref_counts.clone(), + #[cfg(any(test, feature = "test-support"))] + handle_id, + }, view_type: PhantomData, - ref_counts: ref_counts.clone(), - - #[cfg(any(test, feature = "test-support"))] - handle_id, } } @@ -4805,20 +4801,6 @@ impl Debug for ViewHandle { } } -impl Drop for ViewHandle { - fn drop(&mut self) { - self.ref_counts - .lock() - .dec_view(self.window_id, self.view_id); - #[cfg(any(test, feature = "test-support"))] - self.ref_counts - .lock() - .leak_detector - .lock() - .handle_dropped(self.view_id, self.handle_id); - } -} - impl Handle for ViewHandle { type Weak = WeakViewHandle; @@ -4897,19 +4879,10 @@ impl AnyViewHandle { pub fn downcast(self) -> Option> { if self.is::() { - let result = Some(ViewHandle { - window_id: self.window_id, - view_id: self.view_id, - ref_counts: self.ref_counts.clone(), + Some(ViewHandle { + any_handle: self, view_type: PhantomData, - #[cfg(any(test, feature = "test-support"))] - handle_id: self.handle_id, - }); - unsafe { - Arc::decrement_strong_count(Arc::as_ptr(&self.ref_counts)); - } - std::mem::forget(self); - result + }) } else { None } @@ -4951,33 +4924,9 @@ impl From<&AnyViewHandle> for AnyViewHandle { } } -impl From<&ViewHandle> for AnyViewHandle { - fn from(handle: &ViewHandle) -> Self { - Self::new( - handle.window_id, - handle.view_id, - TypeId::of::(), - handle.ref_counts.clone(), - ) - } -} - impl From> for AnyViewHandle { fn from(handle: ViewHandle) -> Self { - let any_handle = AnyViewHandle { - window_id: handle.window_id, - view_id: handle.view_id, - view_type: TypeId::of::(), - ref_counts: handle.ref_counts.clone(), - #[cfg(any(test, feature = "test-support"))] - handle_id: handle.handle_id, - }; - - unsafe { - Arc::decrement_strong_count(Arc::as_ptr(&handle.ref_counts)); - } - std::mem::forget(handle); - any_handle + handle.any_handle } } @@ -6142,7 +6091,7 @@ mod tests { } let (_, root_view) = cx.add_window(Default::default(), |_| TestView::default()); - let observing_view = cx.add_view(root_view, |_| TestView::default()); + let observing_view = cx.add_view(&root_view, |_| TestView::default()); let observing_model = cx.add_model(|_| Model); let observed_model = cx.add_model(|_| Model); diff --git a/crates/gpui/src/app/test_app_context.rs b/crates/gpui/src/app/test_app_context.rs index ad78346991e809751b89d06555ed1f72a97c94c2..21059da1812a99f851101ba9b3696adc9a3eb297 100644 --- a/crates/gpui/src/app/test_app_context.rs +++ b/crates/gpui/src/app/test_app_context.rs @@ -137,11 +137,7 @@ impl TestAppContext { (window_id, view) } - pub fn add_view( - &mut self, - parent_handle: impl Into, - build_view: F, - ) -> ViewHandle + pub fn add_view(&mut self, parent_handle: &AnyViewHandle, build_view: F) -> ViewHandle where T: View, F: FnOnce(&mut ViewContext) -> T, diff --git a/crates/gpui/src/presenter.rs b/crates/gpui/src/presenter.rs index ce20b02005d82e5bba42df27e1a9785f06b2fc61..71b79f9def39e0d678d0c32f518afc130c03171c 100644 --- a/crates/gpui/src/presenter.rs +++ b/crates/gpui/src/presenter.rs @@ -638,7 +638,7 @@ impl<'a> LayoutContext<'a> { (Some(layout_parent), Some(ParentId::View(app_parent))) => { if layout_parent != app_parent { panic!( - "View {} was laid out with parent {} when it was constructed with parent {}", + "View {} was laid out with parent {} when it was constructed with parent {}", print_error(view_id), print_error(*layout_parent), print_error(*app_parent)) @@ -1059,8 +1059,7 @@ pub struct ChildView { } impl ChildView { - pub fn new(view: impl Into, cx: &AppContext) -> Self { - let view = view.into(); + pub fn new(view: &AnyViewHandle, cx: &AppContext) -> Self { let view_name = cx.view_ui_name(view.window_id(), view.id()).unwrap(); Self { view: view.downgrade(), diff --git a/crates/language_selector/src/language_selector.rs b/crates/language_selector/src/language_selector.rs index 711e36f9c442ca1eaf297f7a14ed66a6cd0060bc..b395a2b735c065a0bcf00132640cc4c33e15933a 100644 --- a/crates/language_selector/src/language_selector.rs +++ b/crates/language_selector/src/language_selector.rs @@ -121,7 +121,7 @@ impl View for LanguageSelector { } fn render(&mut self, cx: &mut RenderContext) -> ElementBox { - ChildView::new(self.picker.clone(), cx).boxed() + ChildView::new(&self.picker, cx).boxed() } fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext) { diff --git a/crates/outline/src/outline.rs b/crates/outline/src/outline.rs index 52b168b70cbaa6f0d915733b47311e29cc2298df..52c709d91f42b3189845c00b0f6f0680010c9ad3 100644 --- a/crates/outline/src/outline.rs +++ b/crates/outline/src/outline.rs @@ -49,7 +49,7 @@ impl View for OutlineView { } fn render(&mut self, cx: &mut RenderContext) -> ElementBox { - ChildView::new(self.picker.clone(), cx).boxed() + ChildView::new(&self.picker, cx).boxed() } fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext) { diff --git a/crates/project_panel/src/project_panel.rs b/crates/project_panel/src/project_panel.rs index 66e47806951f45823dd127d568e3828435e8d361..b6e6799062d19b2c0da7076d110d13f8fbc153a2 100644 --- a/crates/project_panel/src/project_panel.rs +++ b/crates/project_panel/src/project_panel.rs @@ -1109,7 +1109,7 @@ impl ProjectPanel { .boxed(), ) .with_child(if show_editor && editor.is_some() { - ChildView::new(editor.unwrap().clone(), cx) + ChildView::new(editor.as_ref().unwrap(), cx) .contained() .with_margin_left(style.icon_spacing) .aligned() diff --git a/crates/project_symbols/src/project_symbols.rs b/crates/project_symbols/src/project_symbols.rs index 957292f0356b19d52d71ce3d9dbed75a47ad53d0..3039f83b4a44c61fb061d98b0ae1b3b2e43cc4c3 100644 --- a/crates/project_symbols/src/project_symbols.rs +++ b/crates/project_symbols/src/project_symbols.rs @@ -49,7 +49,7 @@ impl View for ProjectSymbolsView { } fn render(&mut self, cx: &mut RenderContext) -> ElementBox { - ChildView::new(self.picker.clone(), cx).boxed() + ChildView::new(&self.picker, cx).boxed() } fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext) { diff --git a/crates/recent_projects/src/recent_projects.rs b/crates/recent_projects/src/recent_projects.rs index e73d0b4fb51a5d4f94391fc15ed4f4ed23699ca7..c9a501a2a6a26d70b58e54e16a10b91eca996f4b 100644 --- a/crates/recent_projects/src/recent_projects.rs +++ b/crates/recent_projects/src/recent_projects.rs @@ -103,7 +103,7 @@ impl View for RecentProjectsView { } fn render(&mut self, cx: &mut RenderContext) -> ElementBox { - ChildView::new(self.picker.clone(), cx).boxed() + ChildView::new(&self.picker, cx).boxed() } fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext) { diff --git a/crates/search/src/buffer_search.rs b/crates/search/src/buffer_search.rs index f65b0ae0ad118b3ef057fdb6bb1a99a2c1d6f453..5fae189c6879b7955a4604f69732a2c448be372e 100644 --- a/crates/search/src/buffer_search.rs +++ b/crates/search/src/buffer_search.rs @@ -273,7 +273,7 @@ impl BufferSearchBar { } } if let Some(active_editor) = self.active_searchable_item.as_ref() { - cx.focus(active_editor); + cx.focus(active_editor.as_any()); } cx.emit(Event::UpdateLocation); cx.notify(); @@ -458,7 +458,7 @@ impl BufferSearchBar { fn focus_editor(&mut self, _: &FocusEditor, cx: &mut ViewContext) { if let Some(active_editor) = self.active_searchable_item.as_ref() { - cx.focus(active_editor); + cx.focus(active_editor.as_any()); } } diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs index 0a250cfe2b7953958089d1d85c094bf9d49630f8..6735d99c95f59da265182dd53feed0e4f50098a1 100644 --- a/crates/search/src/project_search.rs +++ b/crates/search/src/project_search.rs @@ -222,16 +222,16 @@ impl View for ProjectSearchView { } impl Item for ProjectSearchView { - fn act_as_type( - &self, + fn act_as_type<'a>( + &'a self, type_id: TypeId, - self_handle: &ViewHandle, - _: &gpui::AppContext, - ) -> Option { + self_handle: &'a ViewHandle, + _: &'a AppContext, + ) -> Option<&'a AnyViewHandle> { if type_id == TypeId::of::() { - Some(self_handle.into()) + Some(self_handle) } else if type_id == TypeId::of::() { - Some((&self.results_editor).into()) + Some(&self.results_editor) } else { None } @@ -246,7 +246,7 @@ impl Item for ProjectSearchView { &self, _detail: Option, tab_theme: &theme::Tab, - cx: &gpui::AppContext, + cx: &AppContext, ) -> ElementBox { Flex::row() .with_child( @@ -277,7 +277,7 @@ impl Item for ProjectSearchView { false } - fn can_save(&self, _: &gpui::AppContext) -> bool { + fn can_save(&self, _: &AppContext) -> bool { true } @@ -930,7 +930,7 @@ impl ToolbarItemView for ProjectSearchBar { self.active_project_search = None; if let Some(search) = active_pane_item.and_then(|i| i.downcast::()) { let query_editor = search.read(cx).query_editor.clone(); - cx.reparent(query_editor); + cx.reparent(&query_editor); self.subscription = Some(cx.observe(&search, |_, _, cx| cx.notify())); self.active_project_search = Some(search); ToolbarItemLocation::PrimaryLeft { diff --git a/crates/terminal_view/src/terminal_view.rs b/crates/terminal_view/src/terminal_view.rs index 7b1a2616528f26a4cff39bd4b0325acf3e2eef09..e325cddc45efc89e3de582e53408e661edcc3c0b 100644 --- a/crates/terminal_view/src/terminal_view.rs +++ b/crates/terminal_view/src/terminal_view.rs @@ -651,7 +651,7 @@ impl Item for TerminalView { project.create_terminal(cwd, window_id, cx) })?; - Ok(cx.add_view(pane, |cx| TerminalView::new(terminal, workspace_id, cx))) + Ok(cx.add_view(&pane, |cx| TerminalView::new(terminal, workspace_id, cx))) }) }) } diff --git a/crates/theme_selector/src/theme_selector.rs b/crates/theme_selector/src/theme_selector.rs index ae3278b7111c8e903293723759a1e8fd238a8697..7ccb9a1a022adf72f40a6dffaf3342330b111b6d 100644 --- a/crates/theme_selector/src/theme_selector.rs +++ b/crates/theme_selector/src/theme_selector.rs @@ -256,7 +256,7 @@ impl View for ThemeSelector { } fn render(&mut self, cx: &mut RenderContext) -> ElementBox { - ChildView::new(self.picker.clone(), cx).boxed() + ChildView::new(&self.picker, cx).boxed() } fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext) { diff --git a/crates/welcome/src/base_keymap_picker.rs b/crates/welcome/src/base_keymap_picker.rs index a37bcb18371c95f8ea8fdc1181ec9d0af3a6409e..dd7ca341b318c001c86f69da37dd6f00fb5ec4aa 100644 --- a/crates/welcome/src/base_keymap_picker.rs +++ b/crates/welcome/src/base_keymap_picker.rs @@ -75,7 +75,7 @@ impl View for BaseKeymapSelector { } fn render(&mut self, cx: &mut gpui::RenderContext<'_, Self>) -> gpui::ElementBox { - ChildView::new(self.picker.clone(), cx).boxed() + ChildView::new(&self.picker, cx).boxed() } fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext) { diff --git a/crates/welcome/src/welcome.rs b/crates/welcome/src/welcome.rs index fb55c79a51e09a37d02c1ffa1e26f0522f5e282c..fa057fdafdfac1e5f61c76e0636176d954f2ee21 100644 --- a/crates/welcome/src/welcome.rs +++ b/crates/welcome/src/welcome.rs @@ -32,7 +32,7 @@ pub fn show_welcome_experience(app_state: &Arc, cx: &mut MutableAppCon workspace.toggle_sidebar(SidebarSide::Left, cx); let welcome_page = cx.add_view(|cx| WelcomePage::new(cx)); workspace.add_item_to_center(Box::new(welcome_page.clone()), cx); - cx.focus(welcome_page); + cx.focus(&welcome_page); cx.notify(); }) .detach(); diff --git a/crates/workspace/src/dock.rs b/crates/workspace/src/dock.rs index f5ee8cad51988f6662fd2106a60c89f2798ede88..03629ffff01caf17eedce06ddfcf0903c78ba757 100644 --- a/crates/workspace/src/dock.rs +++ b/crates/workspace/src/dock.rs @@ -255,7 +255,7 @@ impl Dock { } } else { if focus { - cx.focus(pane); + cx.focus(&pane); } } } else if let Some(last_active_center_pane) = workspace @@ -264,7 +264,7 @@ impl Dock { .and_then(|pane| pane.upgrade(cx)) { if focus { - cx.focus(last_active_center_pane); + cx.focus(&last_active_center_pane); } } cx.emit(crate::Event::DockAnchorChanged); @@ -347,7 +347,7 @@ impl Dock { enum DockResizeHandle {} - let resizable = Container::new(ChildView::new(self.pane.clone(), cx).boxed()) + let resizable = Container::new(ChildView::new(&self.pane, cx).boxed()) .with_style(panel_style) .with_resize_handle::( resize_side as usize, diff --git a/crates/workspace/src/item.rs b/crates/workspace/src/item.rs index b55c9942f8b51707c3b550545971cc57e047b2ce..4a669c078be109710fb8359d063e62ab07a92b51 100644 --- a/crates/workspace/src/item.rs +++ b/crates/workspace/src/item.rs @@ -110,14 +110,14 @@ pub trait Item: View { fn is_edit_event(_: &Self::Event) -> bool { false } - fn act_as_type( - &self, + fn act_as_type<'a>( + &'a self, type_id: TypeId, - self_handle: &ViewHandle, - _: &AppContext, - ) -> Option { + self_handle: &'a ViewHandle, + _: &'a AppContext, + ) -> Option<&AnyViewHandle> { if TypeId::of::() == type_id { - Some(self_handle.into()) + Some(self_handle) } else { None } @@ -187,7 +187,7 @@ pub trait ItemHandle: 'static + fmt::Debug { fn navigate(&self, data: Box, cx: &mut MutableAppContext) -> bool; fn id(&self) -> usize; fn window_id(&self) -> usize; - fn to_any(&self) -> AnyViewHandle; + fn as_any(&self) -> &AnyViewHandle; fn is_dirty(&self, cx: &AppContext) -> bool; fn has_conflict(&self, cx: &AppContext) -> bool; fn can_save(&self, cx: &AppContext) -> bool; @@ -205,7 +205,7 @@ pub trait ItemHandle: 'static + fmt::Debug { project: ModelHandle, cx: &mut MutableAppContext, ) -> Task>; - fn act_as_type(&self, type_id: TypeId, cx: &AppContext) -> Option; + fn act_as_type<'a>(&'a self, type_id: TypeId, cx: &'a AppContext) -> Option<&'a AnyViewHandle>; fn to_followable_item_handle(&self, cx: &AppContext) -> Option>; fn on_release( &self, @@ -227,12 +227,12 @@ pub trait WeakItemHandle { impl dyn ItemHandle { pub fn downcast(&self) -> Option> { - self.to_any().downcast() + self.as_any().clone().downcast() } pub fn act_as(&self, cx: &AppContext) -> Option> { self.act_as_type(TypeId::of::(), cx) - .and_then(|t| t.downcast()) + .and_then(|t| t.clone().downcast()) } } @@ -513,8 +513,8 @@ impl ItemHandle for ViewHandle { self.window_id() } - fn to_any(&self) -> AnyViewHandle { - self.into() + fn as_any(&self) -> &AnyViewHandle { + self } fn is_dirty(&self, cx: &AppContext) -> bool { @@ -558,14 +558,14 @@ impl ItemHandle for ViewHandle { self.update(cx, |item, cx| item.git_diff_recalc(project, cx)) } - fn act_as_type(&self, type_id: TypeId, cx: &AppContext) -> Option { + fn act_as_type<'a>(&'a self, type_id: TypeId, cx: &'a AppContext) -> Option<&'a AnyViewHandle> { self.read(cx).act_as_type(type_id, self, cx) } fn to_followable_item_handle(&self, cx: &AppContext) -> Option> { if cx.has_global::() { let builders = cx.global::(); - let item = self.to_any(); + let item = self.as_any(); Some(builders.get(&item.view_type())?.1(item)) } else { None @@ -603,13 +603,13 @@ impl ItemHandle for ViewHandle { impl From> for AnyViewHandle { fn from(val: Box) -> Self { - val.to_any() + val.as_any().clone() } } impl From<&Box> for AnyViewHandle { fn from(val: &Box) -> Self { - val.to_any() + val.as_any().clone() } } diff --git a/crates/workspace/src/notifications.rs b/crates/workspace/src/notifications.rs index 1cb5d3f50d7539885e0b4db317238e3263c3169c..16faab87e803f4e9c3fa8699b4a2b5024b19631f 100644 --- a/crates/workspace/src/notifications.rs +++ b/crates/workspace/src/notifications.rs @@ -16,7 +16,7 @@ pub trait Notification: View { pub trait NotificationHandle { fn id(&self) -> usize; - fn to_any(&self) -> AnyViewHandle; + fn as_any(&self) -> &AnyViewHandle; } impl NotificationHandle for ViewHandle { @@ -24,14 +24,14 @@ impl NotificationHandle for ViewHandle { self.id() } - fn to_any(&self) -> AnyViewHandle { - self.into() + fn as_any(&self) -> &AnyViewHandle { + self } } impl From<&dyn NotificationHandle> for AnyViewHandle { fn from(val: &dyn NotificationHandle) -> Self { - val.to_any() + val.as_any().clone() } } diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rs index 66c0cc1a04abf52489e620bc00094aed0315b7df..c1b1131aac257198c8d5b8c216484dc4c5ccc1f9 100644 --- a/crates/workspace/src/pane.rs +++ b/crates/workspace/src/pane.rs @@ -413,7 +413,7 @@ impl Pane { mode: NavigationMode, cx: &mut ViewContext, ) -> Task<()> { - cx.focus(pane.clone()); + cx.focus(&pane); let to_load = pane.update(cx, |pane, cx| { loop { @@ -596,7 +596,7 @@ impl Pane { // If the item already exists, move it to the desired destination and activate it pane.update(cx, |pane, cx| { if existing_item_index != insertion_index { - cx.reparent(&item); + cx.reparent(item.as_any()); let existing_item_is_active = existing_item_index == pane.active_item_index; // If the caller didn't specify a destination and the added item is already @@ -626,7 +626,7 @@ impl Pane { }); } else { pane.update(cx, |pane, cx| { - cx.reparent(&item); + cx.reparent(item.as_any()); pane.items.insert(insertion_index, item); if insertion_index <= pane.active_item_index { pane.active_item_index += 1; @@ -649,7 +649,7 @@ impl Pane { pub fn items_of_type(&self) -> impl '_ + Iterator> { self.items .iter() - .filter_map(|item| item.to_any().downcast()) + .filter_map(|item| item.as_any().clone().downcast()) } pub fn active_item(&self) -> Option> { @@ -1048,7 +1048,7 @@ impl Pane { pub fn focus_active_item(&mut self, cx: &mut ViewContext) { if let Some(active_item) = self.active_item() { - cx.focus(active_item); + cx.focus(active_item.as_any()); } } @@ -1091,7 +1091,7 @@ impl Pane { cx, ); - cx.focus(to); + cx.focus(&to); } pub fn split(&mut self, direction: SplitDirection, cx: &mut ViewContext) { @@ -1556,7 +1556,7 @@ impl View for Pane { ChildView::new(&toolbar, cx).expanded().boxed() })) .with_child( - ChildView::new(active_item, cx) + ChildView::new(active_item.as_any(), cx) .flex(1., true) .boxed(), ) @@ -1615,14 +1615,14 @@ impl View for Pane { self.last_focused_view_by_item.get(&active_item.id()) { if let Some(last_focused_view) = weak_last_focused_view.upgrade(cx) { - cx.focus(last_focused_view); + cx.focus(&last_focused_view); return; } else { self.last_focused_view_by_item.remove(&active_item.id()); } } - cx.focus(active_item); + cx.focus(active_item.as_any()); } else if focused != self.tab_bar_context_menu.handle { self.last_focused_view_by_item .insert(active_item.id(), focused.downgrade()); @@ -1676,7 +1676,7 @@ fn render_tab_bar_button( .boxed(), ) .with_children( - context_menu.map(|menu| ChildView::new(menu, cx).aligned().bottom().right().boxed()), + context_menu.map(|menu| ChildView::new(&menu, cx).aligned().bottom().right().boxed()), ) .flex(1., false) .boxed() @@ -2278,7 +2278,8 @@ mod tests { .enumerate() .map(|(ix, item)| { let mut state = item - .to_any() + .as_any() + .clone() .downcast::() .unwrap() .read(cx) diff --git a/crates/workspace/src/searchable.rs b/crates/workspace/src/searchable.rs index 073e88bf6dd5777dee4915ae763f1826ae2720ba..b072d78a12f19b77776b9535f3a95580902493af 100644 --- a/crates/workspace/src/searchable.rs +++ b/crates/workspace/src/searchable.rs @@ -213,13 +213,13 @@ fn downcast_matches(matches: &Vec>) -> Vec> for AnyViewHandle { fn from(this: Box) -> Self { - this.to_any() + this.as_any().clone() } } impl From<&Box> for AnyViewHandle { fn from(this: &Box) -> Self { - this.to_any() + this.as_any().clone() } } diff --git a/crates/workspace/src/sidebar.rs b/crates/workspace/src/sidebar.rs index 37375b7e4a1aafe5095a33fffdf09bf10d372f33..337183a130033142adf2e29ec723e4b542d8bd56 100644 --- a/crates/workspace/src/sidebar.rs +++ b/crates/workspace/src/sidebar.rs @@ -23,7 +23,7 @@ pub trait SidebarItemHandle { fn id(&self) -> usize; fn should_show_badge(&self, cx: &AppContext) -> bool; fn is_focused(&self, cx: &AppContext) -> bool; - fn to_any(&self) -> AnyViewHandle; + fn as_any(&self) -> &AnyViewHandle; } impl SidebarItemHandle for ViewHandle @@ -42,14 +42,14 @@ where ViewHandle::is_focused(self, cx) || self.read(cx).contains_focused_view(cx) } - fn to_any(&self) -> AnyViewHandle { - self.into() + fn as_any(&self) -> &AnyViewHandle { + self } } impl From<&dyn SidebarItemHandle> for AnyViewHandle { fn from(val: &dyn SidebarItemHandle) -> Self { - val.to_any() + val.as_any().clone() } } @@ -192,7 +192,7 @@ impl View for Sidebar { if let Some(active_item) = self.active_item() { enum ResizeHandleTag {} let style = &cx.global::().theme.workspace.sidebar; - ChildView::new(active_item.to_any(), cx) + ChildView::new(active_item.as_any(), cx) .contained() .with_style(style.container) .with_resize_handle::( diff --git a/crates/workspace/src/status_bar.rs b/crates/workspace/src/status_bar.rs index 5261d22b6c8933ba9c382af85a6f025d092ed589..04801d934cbaf0768bb4f9fce880b51f8e28bcf4 100644 --- a/crates/workspace/src/status_bar.rs +++ b/crates/workspace/src/status_bar.rs @@ -14,7 +14,7 @@ pub trait StatusItemView: View { } trait StatusItemViewHandle { - fn to_any(&self) -> AnyViewHandle; + fn as_any(&self) -> &AnyViewHandle; fn set_active_pane_item( &self, active_pane_item: Option<&dyn ItemHandle>, @@ -42,14 +42,14 @@ impl View for StatusBar { let theme = &cx.global::().theme.workspace.status_bar; Flex::row() .with_children(self.left_items.iter().map(|i| { - ChildView::new(i.as_ref(), cx) + ChildView::new(i.as_any(), cx) .aligned() .contained() .with_margin_right(theme.item_spacing) .boxed() })) .with_children(self.right_items.iter().rev().map(|i| { - ChildView::new(i.as_ref(), cx) + ChildView::new(i.as_any(), cx) .aligned() .contained() .with_margin_left(theme.item_spacing) @@ -81,7 +81,7 @@ impl StatusBar { where T: 'static + StatusItemView, { - cx.reparent(&item); + cx.reparent(item.as_any()); self.left_items.push(Box::new(item)); cx.notify(); } @@ -90,7 +90,7 @@ impl StatusBar { where T: 'static + StatusItemView, { - cx.reparent(&item); + cx.reparent(item.as_any()); self.right_items.push(Box::new(item)); cx.notify(); } @@ -111,8 +111,8 @@ impl StatusBar { } impl StatusItemViewHandle for ViewHandle { - fn to_any(&self) -> AnyViewHandle { - self.into() + fn as_any(&self) -> &AnyViewHandle { + self } fn set_active_pane_item( @@ -128,6 +128,6 @@ impl StatusItemViewHandle for ViewHandle { impl From<&dyn StatusItemViewHandle> for AnyViewHandle { fn from(val: &dyn StatusItemViewHandle) -> Self { - val.to_any() + val.as_any().clone() } } diff --git a/crates/workspace/src/toolbar.rs b/crates/workspace/src/toolbar.rs index 55f1707766dddfc5070f3fc4dd72bba96383f43d..ac431c5590b811ad7165b4b41f300f33e0e21cb1 100644 --- a/crates/workspace/src/toolbar.rs +++ b/crates/workspace/src/toolbar.rs @@ -26,7 +26,7 @@ pub trait ToolbarItemView: View { trait ToolbarItemViewHandle { fn id(&self) -> usize; - fn to_any(&self) -> AnyViewHandle; + fn as_any(&self) -> &AnyViewHandle; fn set_active_pane_item( &self, active_pane_item: Option<&dyn ItemHandle>, @@ -71,7 +71,7 @@ impl View for Toolbar { match *position { ToolbarItemLocation::Hidden => {} ToolbarItemLocation::PrimaryLeft { flex } => { - let left_item = ChildView::new(item.as_ref(), cx) + let left_item = ChildView::new(item.as_any(), cx) .aligned() .contained() .with_margin_right(spacing); @@ -82,7 +82,7 @@ impl View for Toolbar { } } ToolbarItemLocation::PrimaryRight { flex } => { - let right_item = ChildView::new(item.as_ref(), cx) + let right_item = ChildView::new(item.as_any(), cx) .aligned() .contained() .with_margin_left(spacing) @@ -95,7 +95,7 @@ impl View for Toolbar { } ToolbarItemLocation::Secondary => { secondary_item = Some( - ChildView::new(item.as_ref(), cx) + ChildView::new(item.as_any(), cx) .constrained() .with_height(theme.height) .boxed(), @@ -272,7 +272,7 @@ impl Toolbar { pub fn item_of_type(&self) -> Option> { self.items .iter() - .find_map(|(item, _)| item.to_any().downcast()) + .find_map(|(item, _)| item.as_any().clone().downcast()) } pub fn hidden(&self) -> bool { @@ -285,8 +285,8 @@ impl ToolbarItemViewHandle for ViewHandle { self.id() } - fn to_any(&self) -> AnyViewHandle { - self.into() + fn as_any(&self) -> &AnyViewHandle { + self } fn set_active_pane_item( @@ -306,6 +306,6 @@ impl ToolbarItemViewHandle for ViewHandle { impl From<&dyn ToolbarItemViewHandle> for AnyViewHandle { fn from(val: &dyn ToolbarItemViewHandle) -> Self { - val.to_any() + val.as_any().clone() } } diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 3fffe57e3e8f494d61b85a0152944ce9d2c97f06..411e77f63f8b91b0b7eaf573a86650c98839bc21 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -464,7 +464,7 @@ type FollowableItemBuilders = HashMap< TypeId, ( FollowableItemBuilder, - fn(AnyViewHandle) -> Box, + fn(&AnyViewHandle) -> Box, ), >; pub fn register_followable_item(cx: &mut MutableAppContext) { @@ -478,7 +478,7 @@ pub fn register_followable_item(cx: &mut MutableAppContext) { .spawn(async move { Ok(Box::new(task.await?) as Box<_>) }) }) }, - |this| Box::new(this.downcast::().unwrap()), + |this| Box::new(this.clone().downcast::().unwrap()), ), ); }); @@ -1491,7 +1491,7 @@ impl Workspace { if active_item.is_focused(cx) { cx.focus_self(); } else { - cx.focus(active_item.to_any()); + cx.focus(active_item.as_any()); } } else { cx.focus_self(); @@ -1523,7 +1523,7 @@ impl Workspace { if active_item.is_focused(cx) { cx.focus_self(); } else { - cx.focus(active_item.to_any()); + cx.focus(active_item.as_any()); } } @@ -1546,7 +1546,7 @@ impl Workspace { }) .detach(); self.panes.push(pane.clone()); - cx.focus(pane.clone()); + cx.focus(&pane); cx.emit(Event::PaneAdded(pane.clone())); pane } @@ -1688,7 +1688,7 @@ impl Workspace { fn activate_pane_at_index(&mut self, action: &ActivatePane, cx: &mut ViewContext) { let panes = self.center.panes(); if let Some(pane) = panes.get(action.0).map(|p| (*p).clone()) { - cx.focus(pane); + cx.focus(&pane); } else { self.split_pane(self.active_pane.clone(), SplitDirection::Right, cx); } @@ -1699,7 +1699,7 @@ impl Workspace { if let Some(ix) = panes.iter().position(|pane| **pane == self.active_pane) { let next_ix = (ix + 1) % panes.len(); let next_pane = panes[next_ix].clone(); - cx.focus(next_pane); + cx.focus(&next_pane); } } @@ -1708,7 +1708,7 @@ impl Workspace { if let Some(ix) = panes.iter().position(|pane| **pane == self.active_pane) { let prev_ix = cmp::min(ix.wrapping_sub(1), panes.len() - 1); let prev_pane = panes[prev_ix].clone(); - cx.focus(prev_pane); + cx.focus(&prev_pane); } } @@ -1871,7 +1871,7 @@ impl Workspace { fn remove_pane(&mut self, pane: ViewHandle, cx: &mut ViewContext) { if self.center.remove(&pane).unwrap() { self.panes.retain(|p| p != &pane); - cx.focus(self.panes.last().unwrap().clone()); + cx.focus(self.panes.last().unwrap()); self.unfollow(&pane, cx); self.last_leaders_by_pane.remove(&pane.downgrade()); for removed_item in pane.read(cx).items() { @@ -2191,7 +2191,7 @@ impl Workspace { Some( Flex::column() .with_children(self.notifications.iter().map(|(_, _, notification)| { - ChildView::new(notification.as_ref(), cx) + ChildView::new(notification.as_any(), cx) .contained() .with_style(theme.notification) .boxed() @@ -2488,7 +2488,7 @@ impl Workspace { let active_item_was_focused = pane .read(cx) .active_item() - .map(|active_item| cx.is_child_focused(active_item.to_any())) + .map(|active_item| cx.is_child_focused(active_item.as_any())) .unwrap_or_default(); if let Some(index) = pane.update(cx, |pane, _| pane.index_for_item(item.as_ref())) { @@ -2715,14 +2715,14 @@ impl Workspace { cx.focus_self(); if let Some(active_pane) = active_pane { - cx.focus(active_pane); + cx.focus(&active_pane); } else { - cx.focus(workspace.panes.last().unwrap().clone()); + cx.focus(workspace.panes.last().unwrap()); } } else { let old_center_handle = old_center_pane.and_then(|weak| weak.upgrade(cx)); if let Some(old_center_handle) = old_center_handle { - cx.focus(old_center_handle) + cx.focus(&old_center_handle) } else { cx.focus_self() } @@ -3503,7 +3503,7 @@ mod tests { //Need to cause an effect flush in order to respect new focus workspace.update(cx, |workspace, cx| { workspace.add_item(Box::new(item_3_4.clone()), cx); - cx.focus(left_pane.clone()); + cx.focus(&left_pane); }); // When closing all of the items in the left pane, we should be prompted twice: diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index 01b493bf7dc59b05fa319ba070df690778b2d5d1..cf15dd95291e315466b8b314477c806dd529fdff 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -1020,7 +1020,8 @@ mod tests { .read(cx) .active_item() .unwrap() - .to_any() + .as_any() + .clone() .downcast::() .unwrap() .read(cx) @@ -1056,7 +1057,8 @@ mod tests { .read(cx) .active_item() .unwrap() - .to_any() + .as_any() + .clone() .downcast::() .unwrap() .read(cx) @@ -1092,7 +1094,8 @@ mod tests { .read(cx) .active_item() .unwrap() - .to_any() + .as_any() + .clone() .downcast::() .unwrap() .read(cx) @@ -1142,7 +1145,8 @@ mod tests { .read(cx) .active_item() .unwrap() - .to_any() + .as_any() + .clone() .downcast::() .unwrap() .read(cx)