Skip to main content

ccf_gpui_widgets/widgets/
tooltip.rs

1//! Tooltip widget
2//!
3//! A lightweight tooltip for displaying text messages.
4//! Uses GPUI's native `.tooltip()` method for positioning.
5//!
6//! # Example
7//!
8//! ```ignore
9//! use ccf_gpui_widgets::widgets::Tooltip;
10//!
11//! div()
12//!     .id("my_element")
13//!     .child("Hover me")
14//!     .tooltip(|_window, cx| {
15//!         cx.new(|_cx| Tooltip::new("This is a tooltip"))
16//!     })
17//! ```
18
19use gpui::prelude::*;
20use gpui::*;
21
22use crate::theme::{get_theme_or, Theme};
23
24/// A simple tooltip view for displaying text messages
25pub struct Tooltip {
26    text: SharedString,
27    custom_theme: Option<Theme>,
28}
29
30impl Tooltip {
31    /// Create a new tooltip with the given text
32    pub fn new(text: impl Into<SharedString>) -> Self {
33        Self {
34            text: text.into(),
35            custom_theme: None,
36        }
37    }
38
39    /// Set custom theme (builder pattern)
40    #[must_use]
41    pub fn theme(mut self, theme: Theme) -> Self {
42        self.custom_theme = Some(theme);
43        self
44    }
45}
46
47impl Render for Tooltip {
48    fn render(&mut self, _window: &mut Window, cx: &mut Context<'_, Self>) -> impl IntoElement {
49        let theme = get_theme_or(cx, self.custom_theme.as_ref());
50
51        div()
52            .px_2()
53            .py_1()
54            .bg(rgb(theme.tooltip_bg))
55            .border_1()
56            .border_color(rgb(theme.tooltip_border))
57            .rounded_md()
58            .shadow_md()
59            .text_sm()
60            .text_color(rgb(theme.tooltip_text))
61            .child(self.text.clone())
62    }
63}