Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
pane: Add ability to use custom tooltip content (#22879)
Danilo Leal5c239be7572f
diff --git a/crates/workspace/src/item.rs b/crates/workspace/src/item.rsindex 5796d64f877..eaf321b3f7a 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<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 {diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rsindex d1b8acb22d3..05ab9a8f90e 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>(indicator).map(|this| {