| 1 | use std::{ |
| 2 | rc::{Rc, Weak}, |
| 3 | time::Duration, |
| 4 | }; |
| 5 | |
| 6 | use gpui::{ |
| 7 | AnyElement, AnyView, App, Bounds, Context, Div, ElementId, InteractiveElement, IntoElement, |
| 8 | ParentElement, Pixels, Render, RenderOnce, Role, Stateful, StatefulInteractiveElement, Styled, |
| 9 | Task, Window, deferred, div, prelude::FluentBuilder as _, px, |
| 10 | }; |
| 11 | |
| 12 | use crate::{Placement, Positioner}; |
| 13 | |
| 14 | const TOOLTIP_PRIORITY: usize = 200; |
| 15 | const WINDOW_MARGIN: Pixels = px(4.); |
| 16 | const GRACE_PERIOD: Duration = Duration::from_millis(300); |
| 17 | const SHOW_DELAY: Duration = Duration::from_millis(500); |
| 18 | |
| 19 | type TooltipBuilder = Rc<dyn Fn(&mut Window, &mut App) -> AnyView>; |
| 20 | type TooltipRenderer = Rc<dyn Fn(AnyView, TooltipTransition, &mut Window, &mut App) -> AnyElement>; |
| 21 | |
| 22 | /// An unstyled tooltip popup. |
| 23 | /// |
| 24 | /// This corresponds to Base UI's `Tooltip.Popup`: it owns the accessible |
| 25 | /// tooltip role and accepts application-owned content and presentation. |
| 26 | #[derive(IntoElement)] |
| 27 | pub struct Tooltip { |
| 28 | base: Stateful<Div>, |
| 29 | } |
| 30 | |
| 31 | impl Tooltip { |
| 32 | pub fn new(id: impl Into<ElementId>) -> Self { |
| 33 | Self { |
| 34 | base: div().id(id).role(Role::Tooltip), |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl Styled for Tooltip { |
| 40 | fn style(&mut self) -> &mut gpui::StyleRefinement { |
| 41 | self.base.style() |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | impl ParentElement for Tooltip { |
| 46 | fn extend(&mut self, children: impl IntoIterator<Item = AnyElement>) { |
| 47 | self.base.extend(children); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | impl RenderOnce for Tooltip { |
| 52 | fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement { |
| 53 | self.base |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// Content requested by a tooltip trigger. |
| 58 | #[derive(Clone)] |
| 59 | pub struct TooltipRequest { |
| 60 | build: TooltipBuilder, |
| 61 | trigger_bounds: Bounds<Pixels>, |
| 62 | preferred_placement: Option<Placement>, |
| 63 | trigger_alive: Option<Weak<()>>, |
| 64 | } |
| 65 | |
| 66 | impl TooltipRequest { |
| 67 | pub fn new( |
| 68 | trigger_bounds: Bounds<Pixels>, |
| 69 | build: impl Fn(&mut Window, &mut App) -> AnyView + 'static, |
| 70 | ) -> Self { |
| 71 | Self { |
| 72 | build: Rc::new(build), |
| 73 | trigger_bounds, |
| 74 | preferred_placement: None, |
| 75 | trigger_alive: None, |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | pub fn placement(mut self, placement: Placement) -> Self { |
| 80 | self.preferred_placement = Some(placement); |
| 81 | self |
| 82 | } |
| 83 | |
| 84 | /// The tooltip closes once `trigger_alive` can no longer be upgraded, |
| 85 | /// i.e. when the trigger stopped being rendered. |
| 86 | pub fn while_alive(mut self, trigger_alive: Weak<()>) -> Self { |
| 87 | self.trigger_alive = Some(trigger_alive); |
| 88 | self |
| 89 | } |
| 90 | |
| 91 | fn trigger_gone(&self) -> bool { |
| 92 | self.trigger_alive |
| 93 | .as_ref() |
| 94 | .is_some_and(|alive| alive.upgrade().is_none()) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /// Presentation transition requested by the Base tooltip lifecycle. |
| 99 | #[derive(Clone, Copy, Debug)] |
| 100 | pub enum TooltipTransition { |
| 101 | Enter { |
| 102 | epoch: usize, |
| 103 | }, |
| 104 | Switch { |
| 105 | epoch: usize, |
| 106 | previous: Bounds<Pixels>, |
| 107 | current: Bounds<Pixels>, |
| 108 | }, |
| 109 | } |
| 110 | |
| 111 | /// Per-window tooltip provider and overlay. |
| 112 | /// |
| 113 | /// Show requests are ignored on iOS and Android, where touch input must not |
| 114 | /// open hover tooltips. This does not control GPUI's native `.tooltip()` API. |
| 115 | pub struct TooltipOverlay { |
| 116 | enabled: bool, |
| 117 | content: Option<TooltipRequest>, |
| 118 | previous_bounds: Option<Bounds<Pixels>>, |
| 119 | epoch: usize, |
| 120 | had_recent_tooltip: bool, |
| 121 | animation_epoch: usize, |
| 122 | is_switching: bool, |
| 123 | show_task: Option<Task<()>>, |
| 124 | hide_task: Option<Task<()>>, |
| 125 | trigger_check_scheduled: bool, |
| 126 | renderer: TooltipRenderer, |
| 127 | } |
| 128 | |
| 129 | impl TooltipOverlay { |
| 130 | pub fn new() -> Self { |
| 131 | Self { |
| 132 | enabled: !crate::is_mobile(), |
| 133 | content: None, |
| 134 | previous_bounds: None, |
| 135 | epoch: 0, |
| 136 | had_recent_tooltip: false, |
| 137 | animation_epoch: 0, |
| 138 | is_switching: false, |
| 139 | show_task: None, |
| 140 | hide_task: None, |
| 141 | trigger_check_scheduled: false, |
| 142 | renderer: Rc::new(|view, _, _, _| div().child(view).into_any_element()), |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | pub fn render_with( |
| 147 | mut self, |
| 148 | renderer: impl Fn(AnyView, TooltipTransition, &mut Window, &mut App) -> AnyElement + 'static, |
| 149 | ) -> Self { |
| 150 | self.renderer = Rc::new(renderer); |
| 151 | self |
| 152 | } |
| 153 | |
| 154 | fn next_epoch(&mut self) -> usize { |
| 155 | self.epoch += 1; |
| 156 | self.epoch |
| 157 | } |
| 158 | |
| 159 | pub fn request_show( |
| 160 | &mut self, |
| 161 | content: TooltipRequest, |
| 162 | window: &mut Window, |
| 163 | cx: &mut Context<Self>, |
| 164 | ) { |
| 165 | // Gate both delayed display and the immediate grace-period switch. |
| 166 | // Keep this in Base so every managed component shares the policy. |
| 167 | if !self.enabled { |
| 168 | return; |
| 169 | } |
| 170 | self.hide_task = None; |
| 171 | let was_visible = self.content.is_some(); |
| 172 | if was_visible || self.had_recent_tooltip { |
| 173 | self.previous_bounds = self.content.as_ref().map(|content| content.trigger_bounds); |
| 174 | self.content = Some(content); |
| 175 | self.show_task = None; |
| 176 | self.is_switching = was_visible; |
| 177 | self.animation_epoch += 1; |
| 178 | cx.notify(); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | let epoch = self.next_epoch(); |
| 183 | self.show_task = Some(cx.spawn_in(window, async move |this, cx| { |
| 184 | cx.background_executor().timer(SHOW_DELAY).await; |
| 185 | let _ = this.update_in(cx, |this, _, cx| { |
| 186 | if this.epoch == epoch { |
| 187 | this.content = Some(content); |
| 188 | this.previous_bounds = None; |
| 189 | this.is_switching = false; |
| 190 | this.animation_epoch += 1; |
| 191 | cx.notify(); |
| 192 | } |
| 193 | }); |
| 194 | })); |
| 195 | } |
| 196 | |
| 197 | pub fn request_hide(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 198 | self.show_task = None; |
| 199 | if self.content.is_none() { |
| 200 | return; |
| 201 | } |
| 202 | let epoch = self.next_epoch(); |
| 203 | self.had_recent_tooltip = true; |
| 204 | self.hide_task = Some(cx.spawn_in(window, async move |this, cx| { |
| 205 | cx.background_executor().timer(GRACE_PERIOD).await; |
| 206 | let _ = this.update_in(cx, |this, _, cx| { |
| 207 | if this.epoch == epoch { |
| 208 | this.content = None; |
| 209 | this.previous_bounds = None; |
| 210 | this.had_recent_tooltip = false; |
| 211 | cx.notify(); |
| 212 | } |
| 213 | }); |
| 214 | })); |
| 215 | } |
| 216 | |
| 217 | pub fn hide(&mut self, cx: &mut Context<Self>) { |
| 218 | let changed = self.content.is_some() |
| 219 | || self.previous_bounds.is_some() |
| 220 | || self.had_recent_tooltip |
| 221 | || self.show_task.is_some() |
| 222 | || self.hide_task.is_some(); |
| 223 | self.content = None; |
| 224 | self.previous_bounds = None; |
| 225 | self.had_recent_tooltip = false; |
| 226 | self.is_switching = false; |
| 227 | self.show_task = None; |
| 228 | self.hide_task = None; |
| 229 | if changed { |
| 230 | cx.notify(); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | impl Default for TooltipOverlay { |
| 236 | fn default() -> Self { |
| 237 | Self::new() |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | impl TooltipOverlay { |
| 242 | fn schedule_trigger_check(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 243 | if self.trigger_check_scheduled { |
| 244 | return; |
| 245 | } |
| 246 | self.trigger_check_scheduled = true; |
| 247 | let overlay = cx.weak_entity(); |
| 248 | // Element state of a trigger that was not rendered is dropped only after the frame. |
| 249 | window.on_next_frame(move |_, cx| { |
| 250 | let _ = overlay.update(cx, |overlay, cx| { |
| 251 | overlay.trigger_check_scheduled = false; |
| 252 | if overlay |
| 253 | .content |
| 254 | .as_ref() |
| 255 | .is_some_and(TooltipRequest::trigger_gone) |
| 256 | { |
| 257 | overlay.hide(cx); |
| 258 | } |
| 259 | }); |
| 260 | }); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | impl Render for TooltipOverlay { |
| 265 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 266 | if self |
| 267 | .content |
| 268 | .as_ref() |
| 269 | .is_some_and(TooltipRequest::trigger_gone) |
| 270 | { |
| 271 | self.content = None; |
| 272 | self.previous_bounds = None; |
| 273 | self.is_switching = false; |
| 274 | } |
| 275 | if self |
| 276 | .content |
| 277 | .as_ref() |
| 278 | .is_some_and(|content| content.trigger_alive.is_some()) |
| 279 | { |
| 280 | self.schedule_trigger_check(window, cx); |
| 281 | } |
| 282 | let Some(content) = self.content.as_ref() else { |
| 283 | return div().into_any_element(); |
| 284 | }; |
| 285 | let view = (content.build)(window, cx); |
| 286 | let transition = match (self.is_switching, self.previous_bounds) { |
| 287 | (true, Some(previous)) => TooltipTransition::Switch { |
| 288 | epoch: self.animation_epoch, |
| 289 | previous, |
| 290 | current: content.trigger_bounds, |
| 291 | }, |
| 292 | _ => TooltipTransition::Enter { |
| 293 | epoch: self.animation_epoch, |
| 294 | }, |
| 295 | }; |
| 296 | let rendered = (self.renderer)(view, transition, window, cx); |
| 297 | deferred( |
| 298 | TooltipPositioner::new(content.trigger_bounds) |
| 299 | .when_some(content.preferred_placement, |this, placement| { |
| 300 | this.placement(placement) |
| 301 | }) |
| 302 | .child(rendered), |
| 303 | ) |
| 304 | .with_priority(TOOLTIP_PRIORITY) |
| 305 | .into_any_element() |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /// An unstyled tooltip positioner with viewport-aware flipping and clamping. |
| 310 | /// |
| 311 | /// This is a tooltip-named view of [`crate::Positioner`]'s side placement. It |
| 312 | /// adds no element of its own; the shared positioner is what gets rendered. |
| 313 | pub struct TooltipPositioner(Positioner); |
| 314 | |
| 315 | impl TooltipPositioner { |
| 316 | pub fn new(trigger_bounds: Bounds<Pixels>) -> Self { |
| 317 | Self(Positioner::side(trigger_bounds).margin(WINDOW_MARGIN)) |
| 318 | } |
| 319 | |
| 320 | pub fn placement(mut self, placement: Placement) -> Self { |
| 321 | self.0 = self.0.placement(placement); |
| 322 | self |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | impl ParentElement for TooltipPositioner { |
| 327 | fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { |
| 328 | self.0.extend(elements); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | impl IntoElement for TooltipPositioner { |
| 333 | type Element = Positioner; |
| 334 | |
| 335 | fn into_element(self) -> Self::Element { |
| 336 | self.0 |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | #[cfg(test)] |
| 341 | mod tests { |
| 342 | use super::*; |
| 343 | use gpui::{AppContext as _, point, size}; |
| 344 | |
| 345 | fn bounds(x: f32, y: f32, width: f32, height: f32) -> Bounds<Pixels> { |
| 346 | Bounds::new(point(px(x), px(y)), size(px(width), px(height))) |
| 347 | } |
| 348 | |
| 349 | #[gpui::test] |
| 350 | fn provider_owns_grace_switch_and_dismiss(cx: &mut gpui::TestAppContext) { |
| 351 | let state = cx.update(|cx| cx.new(|_| TooltipOverlay::new())); |
| 352 | let cx = cx.add_empty_window(); |
| 353 | cx.update(|window, cx| { |
| 354 | state.update(cx, |tooltip, cx| { |
| 355 | tooltip.had_recent_tooltip = true; |
| 356 | tooltip.request_show( |
| 357 | TooltipRequest::new(bounds(0., 0., 20., 20.), |_, _| { |
| 358 | panic!("content is not rendered by this lifecycle test") |
| 359 | }), |
| 360 | window, |
| 361 | cx, |
| 362 | ); |
| 363 | }); |
| 364 | }); |
| 365 | cx.update(|_, cx| assert!(state.read(cx).content.is_some())); |
| 366 | |
| 367 | cx.update(|_, cx| { |
| 368 | state.update(cx, |tooltip, cx| tooltip.hide(cx)); |
| 369 | }); |
| 370 | cx.update(|_, cx| assert!(state.read(cx).content.is_none())); |
| 371 | } |
| 372 | |
| 373 | #[test] |
| 374 | fn tooltip_priority_exceeds_popup_layer() { |
| 375 | assert!(TOOLTIP_PRIORITY > crate::POPUP_PRIORITY); |
| 376 | } |
| 377 | |
| 378 | #[gpui::test] |
| 379 | fn disabled_provider_ignores_delayed_and_immediate_requests(cx: &mut gpui::TestAppContext) { |
| 380 | let state = cx.update(|cx| { |
| 381 | cx.new(|_| TooltipOverlay { |
| 382 | enabled: false, |
| 383 | ..TooltipOverlay::new() |
| 384 | }) |
| 385 | }); |
| 386 | let cx = cx.add_empty_window(); |
| 387 | for had_recent_tooltip in [false, true] { |
| 388 | cx.update(|window, cx| { |
| 389 | state.update(cx, |tooltip, cx| { |
| 390 | tooltip.had_recent_tooltip = had_recent_tooltip; |
| 391 | tooltip.request_show( |
| 392 | TooltipRequest::new(bounds(0., 0., 20., 20.), |_, _| { |
| 393 | panic!("disabled tooltips must not build content") |
| 394 | }), |
| 395 | window, |
| 396 | cx, |
| 397 | ); |
| 398 | assert!(tooltip.content.is_none()); |
| 399 | assert!(tooltip.show_task.is_none()); |
| 400 | assert!(tooltip.hide_task.is_none()); |
| 401 | assert_eq!(tooltip.animation_epoch, 0); |
| 402 | }); |
| 403 | }); |
| 404 | } |
| 405 | } |
| 406 | } |