From 5b40641fdee2cb0505be146be1264fd425a4f1a6 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 21 Apr 2023 15:52:05 +0200 Subject: [PATCH] Take a target view when marking an element as draggable --- crates/diagnostics/src/diagnostics.rs | 8 ++--- crates/drag_and_drop/src/drag_and_drop.rs | 16 ++++----- crates/editor/src/items.rs | 4 +-- crates/feedback/src/feedback_editor.rs | 9 +++-- crates/project_panel/src/project_panel.rs | 2 +- crates/search/src/project_search.rs | 4 +-- crates/terminal_view/src/terminal_view.rs | 4 +-- crates/theme_testbench/src/theme_testbench.rs | 7 +++- crates/welcome/src/welcome.rs | 6 ++-- crates/workspace/src/item.rs | 23 ++++++++++--- crates/workspace/src/pane.rs | 33 ++++++++++++++++--- crates/workspace/src/shared_screen.rs | 6 ++-- 12 files changed, 86 insertions(+), 36 deletions(-) diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index 99d2984f8e1c4e6f78ed84f0166e5d192c9cfe3d..1b5057d556909967019ba9054512476367d54118 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -530,12 +530,12 @@ impl ProjectDiagnosticsEditor { } impl Item for ProjectDiagnosticsEditor { - fn tab_content( + fn tab_content( &self, _detail: Option, style: &theme::Tab, cx: &AppContext, - ) -> Element { + ) -> Element { render_summary( &self.summary, &style.label.text, @@ -717,11 +717,11 @@ fn diagnostic_header_renderer(diagnostic: Diagnostic) -> RenderBlock { }) } -pub(crate) fn render_summary( +pub(crate) fn render_summary( summary: &DiagnosticSummary, text_style: &TextStyle, theme: &theme::ProjectDiagnostics, -) -> Element { +) -> Element { if summary.error_count == 0 && summary.warning_count == 0 { Label::new("No problems", text_style.clone()).boxed() } else { diff --git a/crates/drag_and_drop/src/drag_and_drop.rs b/crates/drag_and_drop/src/drag_and_drop.rs index dafb24a379d71c2b3f6e591a9d6f67b05140dde3..1a959b9a34e36140bb76ee189ce07835cba388ca 100644 --- a/crates/drag_and_drop/src/drag_and_drop.rs +++ b/crates/drag_and_drop/src/drag_and_drop.rs @@ -111,7 +111,7 @@ impl DragAndDrop { }) } - pub fn drag_started(event: MouseDown, cx: &mut ViewContext) { + pub fn drag_started(event: MouseDown, cx: &mut WindowContext) { cx.update_global(|this: &mut Self, _| { this.currently_dragged = Some(State::Down { region_offset: event.position - event.region.origin(), @@ -123,7 +123,7 @@ impl DragAndDrop { pub fn dragging( event: MouseDrag, payload: Rc, - cx: &mut ViewContext, + cx: &mut WindowContext, render: Rc) -> Element>, ) { let window_id = cx.window_id(); @@ -297,20 +297,20 @@ impl DragAndDrop { } pub trait Draggable { - fn as_draggable( + fn as_draggable( self, payload: P, - render: impl 'static + Fn(&P, &mut ViewContext) -> Element, + render: impl 'static + Fn(&P, &mut ViewContext) -> Element, ) -> Self where Self: Sized; } impl Draggable for MouseEventHandler { - fn as_draggable( + fn as_draggable( self, payload: P, - render: impl 'static + Fn(&P, &mut ViewContext) -> Element, + render: impl 'static + Fn(&P, &mut ViewContext) -> Element, ) -> Self where Self: Sized, @@ -319,12 +319,12 @@ impl Draggable for MouseEventHandler { let render = Rc::new(render); self.on_down(MouseButton::Left, move |e, _, cx| { cx.propagate_event(); - DragAndDrop::::drag_started(e, cx); + DragAndDrop::::drag_started(e, cx); }) .on_drag(MouseButton::Left, move |e, _, cx| { let payload = payload.clone(); let render = render.clone(); - DragAndDrop::::dragging(e, payload, cx, render) + DragAndDrop::::dragging(e, payload, cx, render) }) } } diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index dd7249b6db28632f305a946c1df08c8f8c852109..69c211a070bd626f8e660a2cca1ad7492439e806 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -558,12 +558,12 @@ impl Item for Editor { } } - fn tab_content( + fn tab_content( &self, detail: Option, style: &theme::Tab, cx: &AppContext, - ) -> Element { + ) -> Element { Flex::row() .with_child( Label::new(self.title(cx).to_string(), style.label.clone()) diff --git a/crates/feedback/src/feedback_editor.rs b/crates/feedback/src/feedback_editor.rs index 8c06b1ecbddca3993c86a68c00c027767d8f4863..3a0123eb489340c06f0a7361735f5e78aa1e6db9 100644 --- a/crates/feedback/src/feedback_editor.rs +++ b/crates/feedback/src/feedback_editor.rs @@ -26,7 +26,7 @@ use util::ResultExt; use workspace::{ item::{Item, ItemHandle}, searchable::{SearchableItem, SearchableItemHandle}, - AppState, Pane, Workspace, + AppState, Workspace, }; use crate::{submit_feedback_button::SubmitFeedbackButton, system_specs::SystemSpecs}; @@ -250,7 +250,12 @@ impl Item for FeedbackEditor { Some("Send Feedback".into()) } - fn tab_content(&self, _: Option, style: &theme::Tab, _: &AppContext) -> Element { + fn tab_content( + &self, + _: Option, + style: &theme::Tab, + _: &AppContext, + ) -> Element { Flex::row() .with_child( Svg::new("icons/feedback_16.svg") diff --git a/crates/project_panel/src/project_panel.rs b/crates/project_panel/src/project_panel.rs index 078bbee67a0a77590d008f9787d7764a061cb8ed..094e1ce0ba0f53377246ef60c9364affbe8a8abb 100644 --- a/crates/project_panel/src/project_panel.rs +++ b/crates/project_panel/src/project_panel.rs @@ -1251,7 +1251,7 @@ impl ProjectPanel { .as_draggable(entry_id, { let row_container_style = theme.dragged_entry.container; - move |_, cx: &mut ViewContext| { + move |_, cx: &mut ViewContext| { let theme = cx.global::().theme.clone(); Self::render_entry_visual_element( &details, diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs index 680191b1619d1e4aab22f6396602d3fdbb7311df..e0dac1b9adbe9e2593e7073a60566c6710ba4c1d 100644 --- a/crates/search/src/project_search.rs +++ b/crates/search/src/project_search.rs @@ -249,12 +249,12 @@ impl Item for ProjectSearchView { .update(cx, |editor, cx| editor.deactivated(cx)); } - fn tab_content( + fn tab_content( &self, _detail: Option, tab_theme: &theme::Tab, cx: &AppContext, - ) -> Element { + ) -> Element { Flex::row() .with_child( Svg::new("icons/magnifying_glass_12.svg") diff --git a/crates/terminal_view/src/terminal_view.rs b/crates/terminal_view/src/terminal_view.rs index 54e1d412aad9d58d46616493d6c389ca155b62f6..2c549a9957dc88ed5ef5b6ccea6e440908aa9058 100644 --- a/crates/terminal_view/src/terminal_view.rs +++ b/crates/terminal_view/src/terminal_view.rs @@ -546,12 +546,12 @@ impl Item for TerminalView { Some(self.terminal().read(cx).title().into()) } - fn tab_content( + fn tab_content( &self, _detail: Option, tab_theme: &theme::Tab, cx: &gpui::AppContext, - ) -> Element { + ) -> Element { let title = self.terminal().read(cx).title(); Flex::row() diff --git a/crates/theme_testbench/src/theme_testbench.rs b/crates/theme_testbench/src/theme_testbench.rs index 8b5b45792c0547d1777fc5a4590a7266355f76b7..fe6f1dcc67293ef8a09d39373f7108a80a3cee55 100644 --- a/crates/theme_testbench/src/theme_testbench.rs +++ b/crates/theme_testbench/src/theme_testbench.rs @@ -298,7 +298,12 @@ impl View for ThemeTestbench { } impl Item for ThemeTestbench { - fn tab_content(&self, _: Option, style: &theme::Tab, _: &AppContext) -> Element { + fn tab_content( + &self, + _: Option, + style: &theme::Tab, + _: &AppContext, + ) -> Element { Label::new("Theme Testbench", style.label.clone()) .aligned() .contained() diff --git a/crates/welcome/src/welcome.rs b/crates/welcome/src/welcome.rs index 3501dd52a0f378fb5dbea5b51fac2f2df9a3cbc9..2952ee6a36bbbd2e507b22e7f1c01904858e4bc7 100644 --- a/crates/welcome/src/welcome.rs +++ b/crates/welcome/src/welcome.rs @@ -10,7 +10,7 @@ use gpui::{ use settings::{settings_file::SettingsFile, Settings}; use workspace::{ - item::Item, open_new, sidebar::SidebarSide, AppState, Pane, PaneBackdrop, Welcome, Workspace, + item::Item, open_new, sidebar::SidebarSide, AppState, PaneBackdrop, Welcome, Workspace, WorkspaceId, }; @@ -202,12 +202,12 @@ impl Item for WelcomePage { Some("Welcome to Zed!".into()) } - fn tab_content( + fn tab_content( &self, _detail: Option, style: &theme::Tab, _cx: &gpui::AppContext, - ) -> Element { + ) -> Element { Flex::row() .with_child( Label::new("Welcome to Zed!", style.label.clone()) diff --git a/crates/workspace/src/item.rs b/crates/workspace/src/item.rs index 6b908d97ebb47a31a1ce289d5191ac6856a6b8c9..1d5df19d4282539c14e563b8c000815239839db7 100644 --- a/crates/workspace/src/item.rs +++ b/crates/workspace/src/item.rs @@ -54,12 +54,12 @@ pub trait Item: View { fn tab_description<'a>(&'a self, _: usize, _: &'a AppContext) -> Option> { None } - fn tab_content( + fn tab_content( &self, detail: Option, style: &theme::Tab, cx: &AppContext, - ) -> Element; + ) -> Element; fn for_each_project_item(&self, _: &AppContext, _: &mut dyn FnMut(usize, &dyn project::Item)) {} fn is_singleton(&self, _cx: &AppContext) -> bool { false @@ -181,6 +181,12 @@ pub trait ItemHandle: 'static + fmt::Debug { style: &theme::Tab, cx: &AppContext, ) -> Element; + fn dragged_tab_content( + &self, + detail: Option, + style: &theme::Tab, + cx: &AppContext, + ) -> Element; fn project_path(&self, cx: &AppContext) -> Option; fn project_entry_ids(&self, cx: &AppContext) -> SmallVec<[ProjectEntryId; 3]>; fn project_item_model_ids(&self, cx: &AppContext) -> SmallVec<[usize; 3]>; @@ -281,6 +287,15 @@ impl ItemHandle for ViewHandle { self.read(cx).tab_content(detail, style, cx) } + fn dragged_tab_content( + &self, + detail: Option, + style: &theme::Tab, + cx: &AppContext, + ) -> Element { + self.read(cx).tab_content(detail, style, cx) + } + fn project_path(&self, cx: &AppContext) -> Option { let this = self.read(cx); let mut result = None; @@ -927,12 +942,12 @@ pub(crate) mod test { }) } - fn tab_content( + fn tab_content( &self, detail: Option, _: &theme::Tab, _: &AppContext, - ) -> Element { + ) -> Element { self.tab_detail.set(detail); Empty::new().boxed() } diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rs index 4e003d65d8c38add399160c1d63ccde644ec800d..686569abe52583e2856a9e5e075203259f49cc1d 100644 --- a/crates/workspace/src/pane.rs +++ b/crates/workspace/src/pane.rs @@ -1401,7 +1401,7 @@ impl Pane { enum Tab {} let mouse_event_handler = MouseEventHandler::::new(ix, cx, |_, cx| { - Self::render_tab::( + Self::render_tab( &item, pane.clone(), ix == 0, @@ -1466,9 +1466,9 @@ impl Pane { let theme = cx.global::().theme.clone(); let detail = detail.clone(); - move |dragged_item: &DraggedItem, cx: &mut ViewContext| { + move |dragged_item: &DraggedItem, cx: &mut ViewContext| { let tab_style = &theme.workspace.tab_bar.dragged_tab; - Self::render_tab::( + Self::render_dragged_tab( &dragged_item.item, dragged_item.pane.clone(), false, @@ -1541,7 +1541,7 @@ impl Pane { tab_details } - fn render_tab( + fn render_tab( item: &Box, pane: WeakViewHandle, first: bool, @@ -1551,6 +1551,31 @@ impl Pane { cx: &mut ViewContext, ) -> Element { let title = item.tab_content(detail, &tab_style, cx); + Self::render_tab_with_title(title, item, pane, first, hovered, tab_style, cx) + } + + fn render_dragged_tab( + item: &Box, + pane: WeakViewHandle, + first: bool, + detail: Option, + hovered: bool, + tab_style: &theme::Tab, + cx: &mut ViewContext, + ) -> Element { + let title = item.dragged_tab_content(detail, &tab_style, cx); + Self::render_tab_with_title(title, item, pane, first, hovered, tab_style, cx) + } + + fn render_tab_with_title( + title: Element, + item: &Box, + pane: WeakViewHandle, + first: bool, + hovered: bool, + tab_style: &theme::Tab, + cx: &mut ViewContext, + ) -> Element { let mut container = tab_style.container.clone(); if first { container.border.left = false; diff --git a/crates/workspace/src/shared_screen.rs b/crates/workspace/src/shared_screen.rs index 51d96723908af10c9dff7bfda8bfa2959b48c355..f02fbc88f88e4b3106b7ffe663695e07a4c81eea 100644 --- a/crates/workspace/src/shared_screen.rs +++ b/crates/workspace/src/shared_screen.rs @@ -1,6 +1,6 @@ use crate::{ item::{Item, ItemEvent}, - ItemNavHistory, Pane, WorkspaceId, + ItemNavHistory, WorkspaceId, }; use anyhow::Result; use call::participant::{Frame, RemoteVideoTrack}; @@ -106,12 +106,12 @@ impl Item for SharedScreen { } } - fn tab_content( + fn tab_content( &self, _: Option, style: &theme::Tab, _: &AppContext, - ) -> gpui::Element { + ) -> gpui::Element { Flex::row() .with_child( Svg::new("icons/disable_screen_sharing_12.svg")