Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pane: Add ability to use custom tooltip content #22879

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions crates/workspace/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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>) {}
Expand All @@ -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
Expand Down Expand Up @@ -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>;
Expand Down Expand Up @@ -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()
}
Expand All @@ -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 {
Expand Down
9 changes: 6 additions & 3 deletions crates/workspace/src/pane.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
item::{
ActivateOnClose, ClosePosition, Item, ItemHandle, ItemSettings, PreviewTabsSettings,
ShowDiagnostics, TabContentParams, WeakItemHandle,
ShowDiagnostics, TabContentParams, TabTooltipContent, WeakItemHandle,
},
move_item,
notifications::NotifyResultExt,
Expand Down Expand Up @@ -2146,8 +2146,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| {
Expand Down
Loading