From 5c239be7572fa8dbe38a83160b26b22d5d383174 Mon Sep 17 00:00:00 2001 From: Danilo Leal <67129314+danilo-leal@users.noreply.github.com> Date: Thu, 9 Jan 2025 12:34:30 -0300 Subject: [PATCH] pane: Add ability to use custom tooltip content (#22879) This PR is an alternate version of https://github.com/zed-industries/zed/pull/22850, but now using a similar approach to the existing `tab_content` and `tab_content_text`, where `tab_tooltip_content` refers to the existing `tab_tooltip_text` if there's no custom tooltip content/trait defined, meaning it will simplify render the text/string content in this case. This is all motivated by https://github.com/zed-industries/zed/pull/21955, as we want to pull off the ability to add custom content to a terminal tab tooltip. Release Notes: - N/A --- crates/workspace/src/item.rs | 45 +++++++++++++++++++++++++++--------- crates/workspace/src/pane.rs | 9 +++++--- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/crates/workspace/src/item.rs b/crates/workspace/src/item.rs index 5796d64f87780687905124d4714fca48fb8b706b..eaf321b3f7a960c995a586c3071695215c9179b3 100644 --- a/crates/workspace/src/item.rs +++ b/crates/workspace/src/item.rs @@ -178,6 +178,11 @@ impl TabContentParams { } } +pub enum TabTooltipContent { + Text(SharedString), + Custom(Box AnyView>), +} + pub trait Item: FocusableView + EventEmitter { type Event; @@ -206,6 +211,25 @@ pub trait Item: FocusableView + EventEmitter { None } + /// Returns the tab tooltip text. + /// + /// Use this if you don't need to customize the tab tooltip content. + fn tab_tooltip_text(&self, _: &AppContext) -> Option { + None + } + + /// Returns the tab tooltip content. + /// + /// By default this returns a Tooltip text from + /// `tab_tooltip_text`. + fn tab_tooltip_content(&self, cx: &AppContext) -> Option { + self.tab_tooltip_text(cx).map(TabTooltipContent::Text) + } + + fn tab_description(&self, _: usize, _: &AppContext) -> Option { + None + } + fn to_item_events(_event: &Self::Event, _f: impl FnMut(ItemEvent)) {} fn deactivated(&mut self, _: &mut ViewContext) {} @@ -214,12 +238,6 @@ pub trait Item: FocusableView + EventEmitter { fn navigate(&mut self, _: Box, _: &mut ViewContext) -> bool { false } - fn tab_tooltip_text(&self, _: &AppContext) -> Option { - None - } - fn tab_description(&self, _: usize, _: &AppContext) -> Option { - None - } fn telemetry_event_text(&self) -> Option<&'static str> { None @@ -398,10 +416,11 @@ pub trait ItemHandle: 'static + Send { handler: Box, ) -> gpui::Subscription; fn focus_handle(&self, cx: &WindowContext) -> FocusHandle; - fn tab_tooltip_text(&self, cx: &AppContext) -> Option; fn tab_description(&self, detail: usize, cx: &AppContext) -> Option; fn tab_content(&self, params: TabContentParams, cx: &WindowContext) -> AnyElement; fn tab_icon(&self, cx: &WindowContext) -> Option; + fn tab_tooltip_text(&self, cx: &AppContext) -> Option; + fn tab_tooltip_content(&self, cx: &AppContext) -> Option; fn telemetry_event_text(&self, cx: &WindowContext) -> Option<&'static str>; fn dragged_tab_content(&self, params: TabContentParams, cx: &WindowContext) -> AnyElement; fn project_path(&self, cx: &AppContext) -> Option; @@ -503,10 +522,6 @@ impl ItemHandle for View { self.focus_handle(cx) } - fn tab_tooltip_text(&self, cx: &AppContext) -> Option { - self.read(cx).tab_tooltip_text(cx) - } - fn telemetry_event_text(&self, cx: &WindowContext) -> Option<&'static str> { self.read(cx).telemetry_event_text() } @@ -523,6 +538,14 @@ impl ItemHandle for View { self.read(cx).tab_icon(cx) } + fn tab_tooltip_content(&self, cx: &AppContext) -> Option { + self.read(cx).tab_tooltip_content(cx) + } + + fn tab_tooltip_text(&self, cx: &AppContext) -> Option { + self.read(cx).tab_tooltip_text(cx) + } + fn dragged_tab_content(&self, params: TabContentParams, cx: &WindowContext) -> AnyElement { self.read(cx).tab_content( TabContentParams { diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rs index d1b8acb22d349617941ba1547553452b10ed897a..05ab9a8f90e2bdb4a0e1c3efa972cc02d85a560e 100644 --- a/crates/workspace/src/pane.rs +++ b/crates/workspace/src/pane.rs @@ -1,7 +1,7 @@ use crate::{ item::{ ActivateOnClose, ClosePosition, Item, ItemHandle, ItemSettings, PreviewTabsSettings, - ShowDiagnostics, TabContentParams, WeakItemHandle, + ShowDiagnostics, TabContentParams, TabTooltipContent, WeakItemHandle, }, move_item, notifications::NotifyResultExt, @@ -2149,8 +2149,11 @@ impl Pane { this.drag_split_direction = None; this.handle_external_paths_drop(paths, cx) })) - .when_some(item.tab_tooltip_text(cx), |tab, text| { - tab.tooltip(move |cx| Tooltip::text(text.clone(), cx)) + .when_some(item.tab_tooltip_content(cx), |tab, content| match content { + TabTooltipContent::Text(text) => { + tab.tooltip(move |cx| Tooltip::text(text.clone(), cx)) + } + TabTooltipContent::Custom(element_fn) => tab.tooltip(move |cx| element_fn(cx)), }) .start_slot::(indicator) .map(|this| {