Skip to main content

guise/overlay/
tooltip.rs

1//! `Tooltip` — a small floating label, plus the [`tooltip`] helper for gpui's
2//! built-in `.tooltip(...)` attachment.
3
4use gpui::prelude::*;
5use gpui::{div, px, AnyView, App, IntoElement, SharedString, Window};
6
7use crate::theme::{theme, ColorName, Size};
8
9/// The floating tooltip bubble (a gpui view). Usually built for you by
10/// [`tooltip`]; construct directly only if you need a custom builder.
11pub struct Tooltip {
12    label: SharedString,
13}
14
15impl Tooltip {
16    pub fn new(label: impl Into<SharedString>) -> Self {
17        Tooltip {
18            label: label.into(),
19        }
20    }
21}
22
23impl Render for Tooltip {
24    fn render(&mut self, _window: &mut Window, cx: &mut gpui::Context<Self>) -> impl IntoElement {
25        let t = theme(cx);
26        let bg = if t.scheme.is_dark() {
27            t.color(ColorName::Dark, 4)
28        } else {
29            t.color(ColorName::Dark, 9)
30        };
31        div()
32            .px(px(10.0))
33            .py(px(6.0))
34            .rounded(px(t.radius(Size::Sm)))
35            .bg(bg.hsla())
36            .text_size(px(t.font_size(Size::Sm)))
37            .text_color(t.white.hsla())
38            .shadow_md()
39            .child(self.label.clone())
40    }
41}
42
43/// A builder for gpui's `.tooltip(...)`: attach a themed tooltip to any
44/// interactive element.
45///
46/// ```ignore
47/// div().id("hoverme").tooltip(guise::tooltip("Helpful hint"))
48/// ```
49pub fn tooltip(
50    label: impl Into<SharedString>,
51) -> impl Fn(&mut Window, &mut App) -> AnyView + 'static {
52    let label = label.into();
53    move |_window, cx| cx.new(|_cx| Tooltip::new(label.clone())).into()
54}