diff --git a/crates/gpui2/src/app.rs b/crates/gpui2/src/app.rs index 9ec76a86d4e7bd0726cc1a38f98a942728c7e43d..974395e8971f92a3524a5a8177f792a567b88904 100644 --- a/crates/gpui2/src/app.rs +++ b/crates/gpui2/src/app.rs @@ -157,6 +157,7 @@ pub struct AppContext { flushing_effects: bool, pending_updates: usize, pub(crate) active_drag: Option, + pub(crate) active_tooltip: Option, pub(crate) next_frame_callbacks: HashMap>, pub(crate) frame_consumers: HashMap>, pub(crate) background_executor: BackgroundExecutor, @@ -215,6 +216,7 @@ impl AppContext { flushing_effects: false, pending_updates: 0, active_drag: None, + active_tooltip: None, next_frame_callbacks: HashMap::default(), frame_consumers: HashMap::default(), background_executor: executor, diff --git a/crates/gpui2/src/interactive.rs b/crates/gpui2/src/interactive.rs index 7966b68e121c7882e9f32bb219ac07cefeed0f22..def7315a7cca6fe8cd3c7b9376438218cb226852 100644 --- a/crates/gpui2/src/interactive.rs +++ b/crates/gpui2/src/interactive.rs @@ -358,7 +358,7 @@ pub trait StatefulInteractive: StatelessInteractive { self.stateful_interaction().tooltip_builder.is_none(), "calling tooltip more than once on the same element is not supported" ); - self.stateful_interaction().tooltip_builder = Some(Box::new(move |view_state, cx| { + self.stateful_interaction().tooltip_builder = Some(Arc::new(move |view_state, cx| { build_tooltip(view_state, cx).into() })); @@ -602,6 +602,10 @@ pub trait ElementInteraction: 'static { if let Some(hover_listener) = stateful.hover_listener.take() { let was_hovered = element_state.hover_state.clone(); let has_mouse_down = element_state.pending_mouse_down.lock().is_some(); + + let active_tooltip = element_state.active_tooltip.clone(); + let tooltip_builder = stateful.tooltip_builder.clone(); + cx.on_mouse_event(move |view_state, event: &MouseMoveEvent, phase, cx| { if phase != DispatchPhase::Bubble { return; @@ -612,11 +616,26 @@ pub trait ElementInteraction: 'static { if is_hovered != was_hovered.clone() { *was_hovered = is_hovered; drop(was_hovered); + if let Some(tooltip_builder) = &tooltip_builder { + let mut active_tooltip = active_tooltip.lock(); + if is_hovered && active_tooltip.is_none() { + *active_tooltip = Some(tooltip_builder(view_state, cx)); + } else if !is_hovered { + active_tooltip.take(); + } + } + hover_listener(view_state, is_hovered, cx); } }); } + if let Some(active_tooltip) = element_state.active_tooltip.lock().as_ref() { + if *element_state.hover_state.lock() { + cx.active_tooltip = Some(active_tooltip.clone()); + } + } + let active_state = element_state.active_state.clone(); if active_state.lock().is_none() { let active_group_bounds = stateful @@ -804,6 +823,7 @@ pub struct InteractiveElementState { hover_state: Arc>, pending_mouse_down: Arc>>, scroll_offset: Option>>>, + active_tooltip: Arc>>, } impl InteractiveElementState { @@ -1155,7 +1175,7 @@ pub(crate) type DragListener = pub(crate) type HoverListener = Box) + 'static>; -pub(crate) type TooltipBuilder = Box) -> AnyView + 'static>; +pub(crate) type TooltipBuilder = Arc) -> AnyView + 'static>; pub type KeyListener = Box< dyn Fn( diff --git a/crates/gpui2/src/window.rs b/crates/gpui2/src/window.rs index e998766bbb8126ac62b99ac2611caec72a941dc6..46ac30592bb8e6e60c134c702acb675e7fea4a39 100644 --- a/crates/gpui2/src/window.rs +++ b/crates/gpui2/src/window.rs @@ -987,6 +987,14 @@ impl<'a> WindowContext<'a> { cx.active_drag = Some(active_drag); }); }); + } else if let Some(active_tooltip) = self.app.active_tooltip.take() { + self.stack(1, |cx| { + cx.with_element_offset(Some(cx.mouse_position()), |cx| { + let available_space = + size(AvailableSpace::MinContent, AvailableSpace::MinContent); + active_tooltip.draw(available_space, cx); + }); + }); } self.window.root_view = Some(root_view); diff --git a/crates/ui2/src/components.rs b/crates/ui2/src/components.rs index 7a9cac01ca7c54999210b3220986062906196e1b..f10f8cee8d74cba0bdc0bfc6e307f147f8e424bc 100644 --- a/crates/ui2/src/components.rs +++ b/crates/ui2/src/components.rs @@ -31,6 +31,7 @@ mod theme_selector; mod title_bar; mod toast; mod toolbar; +mod tooltip; mod traffic_lights; mod workspace; @@ -67,5 +68,6 @@ pub use theme_selector::*; pub use title_bar::*; pub use toast::*; pub use toolbar::*; +pub use tooltip::*; pub use traffic_lights::*; pub use workspace::*; diff --git a/crates/ui2/src/components/tooltip.rs b/crates/ui2/src/components/tooltip.rs new file mode 100644 index 0000000000000000000000000000000000000000..eb53b506ebd8467eb9f6d31dab2abe36dd62496a --- /dev/null +++ b/crates/ui2/src/components/tooltip.rs @@ -0,0 +1,36 @@ +use gpui2::{ + div, px, Div, ParentElement, Render, SharedString, Styled, View, ViewContext, VisualContext, +}; +use theme2::ActiveTheme; + +#[derive(Clone, Debug)] +pub struct TextTooltip { + title: SharedString, +} + +impl TextTooltip { + pub fn new(str: SharedString) -> Self { + Self { title: str } + } + + pub fn build_view(str: SharedString, cx: &mut C) -> C::Result> { + cx.build_view(|cx| TextTooltip::new(str)) + } +} + +impl Render for TextTooltip { + type Element = Div; + + fn render(&mut self, cx: &mut ViewContext) -> Self::Element { + let theme = cx.theme(); + div() + .bg(theme.colors().background) + .rounded(px(8.)) + .border() + .border_color(theme.colors().border) + .text_color(theme.colors().text) + .pl_2() + .pr_2() + .child(self.title.clone()) + } +} diff --git a/crates/workspace2/src/pane.rs b/crates/workspace2/src/pane.rs index acda04c5a47cbf147ef1fac3d54c900be7eb5614..a1ec16848840355748666fb7cd0ad99ee789d4f9 100644 --- a/crates/workspace2/src/pane.rs +++ b/crates/workspace2/src/pane.rs @@ -9,9 +9,8 @@ use crate::{ use anyhow::Result; use collections::{HashMap, HashSet, VecDeque}; use gpui::{ - AppContext, AsyncWindowContext, Component, CursorStyle, Div, EntityId, EventEmitter, - FocusHandle, Model, PromptLevel, Render, Task, View, ViewContext, VisualContext, WeakView, - WindowContext, + AnyView, AppContext, AsyncWindowContext, Component, Div, EntityId, EventEmitter, FocusHandle, + Model, PromptLevel, Render, Task, View, ViewContext, VisualContext, WeakView, WindowContext, }; use parking_lot::Mutex; use project2::{Project, ProjectEntryId, ProjectPath}; @@ -27,7 +26,7 @@ use std::{ }, }; use ui::v_stack; -use ui::{prelude::*, Icon, IconButton, IconColor, IconElement}; +use ui::{prelude::*, Icon, IconButton, IconColor, IconElement, TextTooltip}; use util::truncate_and_remove_front; #[derive(PartialEq, Clone, Copy, Deserialize, Debug)] @@ -1402,7 +1401,10 @@ impl Pane { .on_hover(|_, hovered, _| { dbg!(hovered); }) - // .tooltip(|pane, cx| cx.create_view( tooltip.child("Hovering the tab")) + .when_some(item.tab_tooltip_text(cx), |div, text| { + div.tooltip(move |_, cx| TextTooltip::build_view(text.clone(), cx)) + }) + // .tooltip(|pane, cx| cx.build_view(|cx| div().child(title))) // .on_drag(move |pane, cx| pane.render_tab(ix, item.boxed_clone(), detail, cx)) // .drag_over::(|d| d.bg(cx.theme().colors().element_drop_target)) // .on_drop(|_view, state: View, cx| {