| 1 | use std::cell::{Cell, RefCell}; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use gpui_kit::component::tooltip::Tooltip; |
| 5 | use gpui_kit::*; |
| 6 | |
| 7 | use super::hooks::is_openable_link; |
| 8 | |
| 9 | const MAX_WIDTH: f32 = 480.; |
| 10 | |
| 11 | /// Shows the URL of the link under the mouse in a tooltip, as a browser shows a link's title. |
| 12 | /// Kept in the element's state so the show delay survives redraws. |
| 13 | #[derive(Clone, Default)] |
| 14 | struct LinkTooltipState { |
| 15 | active: Rc<RefCell<Option<ActiveTooltip>>>, |
| 16 | long_press: Rc<Cell<bool>>, |
| 17 | shown_url: Rc<RefCell<Option<String>>>, |
| 18 | pressed: Rc<Cell<bool>>, |
| 19 | } |
| 20 | |
| 21 | impl LinkTooltipState { |
| 22 | /// A shown tooltip belongs to one link: moving onto another link hides it, so it can show |
| 23 | /// again with the new URL. No tooltip while a button is held, as during a text selection. |
| 24 | fn hovers(&self, url: Option<String>) -> bool { |
| 25 | let Some(url) = url.filter(|_| !self.pressed.get()) else { |
| 26 | return false; |
| 27 | }; |
| 28 | let showing = matches!( |
| 29 | self.active.borrow().as_ref(), |
| 30 | Some(ActiveTooltip::Visible { .. } | ActiveTooltip::WaitingForHide { .. }) |
| 31 | ); |
| 32 | !showing || self.shown_url.borrow().as_deref() == Some(url.as_str()) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /// One frame of a text element's link tooltip: made in prepaint, wired up in paint. |
| 37 | pub struct LinkTooltip { |
| 38 | state: LinkTooltipState, |
| 39 | id: Option<TooltipId>, |
| 40 | } |
| 41 | |
| 42 | impl LinkTooltip { |
| 43 | pub(super) fn prepaint( |
| 44 | global_id: Option<&GlobalElementId>, |
| 45 | has_links: bool, |
| 46 | window: &mut Window, |
| 47 | ) -> Option<LinkTooltip> { |
| 48 | let state = |
| 49 | window.with_element_state(global_id?, |retained: Option<LinkTooltipState>, _| { |
| 50 | let state = retained.unwrap_or_default(); |
| 51 | (state.clone(), state) |
| 52 | }); |
| 53 | if !has_links { |
| 54 | state.long_press.set(false); |
| 55 | state.active.take(); |
| 56 | return None; |
| 57 | } |
| 58 | let id = set_tooltip_on_window(&state.active, window); |
| 59 | Some(LinkTooltip { state, id }) |
| 60 | } |
| 61 | |
| 62 | /// `url_at` gives the URL of the link at a window position. |
| 63 | pub(super) fn paint( |
| 64 | &self, |
| 65 | hitbox: &Hitbox, |
| 66 | url_at: impl Fn(Point<Pixels>) -> Option<String> + 'static, |
| 67 | window: &mut Window, |
| 68 | ) { |
| 69 | let url_at = Rc::new(move |position| url_at(position).filter(|url| is_openable_link(url))); |
| 70 | let build = Rc::new({ |
| 71 | let (url_at, shown_url) = (url_at.clone(), self.state.shown_url.clone()); |
| 72 | move |window: &mut Window, cx: &mut App| { |
| 73 | let url = url_at(window.mouse_position())?; |
| 74 | *shown_url.borrow_mut() = Some(url.clone()); |
| 75 | let tooltip = |
| 76 | Tooltip::element(move |_, _| div().max_w(px(MAX_WIDTH)).child(url.clone())); |
| 77 | Some((tooltip.build(window, cx), false)) |
| 78 | } |
| 79 | }); |
| 80 | let hovered = Rc::new({ |
| 81 | let (hitbox, url_at, state) = (hitbox.clone(), url_at.clone(), self.state.clone()); |
| 82 | move |window: &Window| { |
| 83 | hitbox.is_hovered(window) && state.hovers(url_at(window.mouse_position())) |
| 84 | } |
| 85 | }); |
| 86 | // Prepaint has no hover state yet, so it tests the bounds instead. |
| 87 | let hovered_in_prepaint = Rc::new({ |
| 88 | let (bounds, state) = (hitbox.bounds, self.state.clone()); |
| 89 | move |window: &Window| { |
| 90 | let position = window.mouse_position(); |
| 91 | bounds.contains(&position) && state.hovers(url_at(position)) |
| 92 | } |
| 93 | }); |
| 94 | let pressed = self.state.pressed.clone(); |
| 95 | let press_hitbox = hitbox.clone(); |
| 96 | window.on_mouse_event(move |event: &MouseDownEvent, phase, window, _| { |
| 97 | if phase.bubble() |
| 98 | && event.button == MouseButton::Left |
| 99 | && press_hitbox.is_hovered(window) |
| 100 | { |
| 101 | pressed.set(true); |
| 102 | } |
| 103 | }); |
| 104 | let pressed = self.state.pressed.clone(); |
| 105 | window.on_mouse_event(move |event: &MouseUpEvent, phase, _, _| { |
| 106 | if phase.capture() && event.button == MouseButton::Left { |
| 107 | pressed.set(false); |
| 108 | } |
| 109 | }); |
| 110 | register_tooltip_mouse_handlers( |
| 111 | &self.state.active, |
| 112 | self.id, |
| 113 | build, |
| 114 | hovered, |
| 115 | hovered_in_prepaint, |
| 116 | self.state.long_press.clone(), |
| 117 | None, |
| 118 | window, |
| 119 | ); |
| 120 | } |
| 121 | } |