pane: Add ability to use custom tooltip content (#22879)

Danilo Leal created

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

Change summary

crates/workspace/src/item.rs | 45 ++++++++++++++++++++++++++++---------
crates/workspace/src/pane.rs |  9 +++++--
2 files changed, 40 insertions(+), 14 deletions(-)

Detailed changes

crates/workspace/src/item.rs 🔗

@@ -178,6 +178,11 @@ impl TabContentParams {
     }
 }
 
+pub enum TabTooltipContent {
+    Text(SharedString),
+    Custom(Box<dyn Fn(&mut WindowContext) -> AnyView>),
+}
+
 pub trait Item: FocusableView + EventEmitter<Self::Event> {
     type Event;
 
@@ -206,6 +211,25 @@ pub trait Item: FocusableView + EventEmitter<Self::Event> {
         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<SharedString> {
+        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<TabTooltipContent> {
+        self.tab_tooltip_text(cx).map(TabTooltipContent::Text)
+    }
+
+    fn tab_description(&self, _: usize, _: &AppContext) -> Option<SharedString> {
+        None
+    }
+
     fn to_item_events(_event: &Self::Event, _f: impl FnMut(ItemEvent)) {}
 
     fn deactivated(&mut self, _: &mut ViewContext<Self>) {}
@@ -214,12 +238,6 @@ pub trait Item: FocusableView + EventEmitter<Self::Event> {
     fn navigate(&mut self, _: Box<dyn Any>, _: &mut ViewContext<Self>) -> bool {
         false
     }
-    fn tab_tooltip_text(&self, _: &AppContext) -> Option<SharedString> {
-        None
-    }
-    fn tab_description(&self, _: usize, _: &AppContext) -> Option<SharedString> {
-        None
-    }
 
     fn telemetry_event_text(&self) -> Option<&'static str> {
         None
@@ -398,10 +416,11 @@ pub trait ItemHandle: 'static + Send {
         handler: Box<dyn Fn(ItemEvent, &mut WindowContext)>,
     ) -> gpui::Subscription;
     fn focus_handle(&self, cx: &WindowContext) -> FocusHandle;
-    fn tab_tooltip_text(&self, cx: &AppContext) -> Option<SharedString>;
     fn tab_description(&self, detail: usize, cx: &AppContext) -> Option<SharedString>;
     fn tab_content(&self, params: TabContentParams, cx: &WindowContext) -> AnyElement;
     fn tab_icon(&self, cx: &WindowContext) -> Option<Icon>;
+    fn tab_tooltip_text(&self, cx: &AppContext) -> Option<SharedString>;
+    fn tab_tooltip_content(&self, cx: &AppContext) -> Option<TabTooltipContent>;
     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<ProjectPath>;
@@ -503,10 +522,6 @@ impl<T: Item> ItemHandle for View<T> {
         self.focus_handle(cx)
     }
 
-    fn tab_tooltip_text(&self, cx: &AppContext) -> Option<SharedString> {
-        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<T: Item> ItemHandle for View<T> {
         self.read(cx).tab_icon(cx)
     }
 
+    fn tab_tooltip_content(&self, cx: &AppContext) -> Option<TabTooltipContent> {
+        self.read(cx).tab_tooltip_content(cx)
+    }
+
+    fn tab_tooltip_text(&self, cx: &AppContext) -> Option<SharedString> {
+        self.read(cx).tab_tooltip_text(cx)
+    }
+
     fn dragged_tab_content(&self, params: TabContentParams, cx: &WindowContext) -> AnyElement {
         self.read(cx).tab_content(
             TabContentParams {

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>(indicator)
             .map(|this| {