fc_ui/capabilities/overlays/
tooltip.rs1use crate::capabilities::foundation::theme::use_theme;
4use gpui::{prelude::*, *};
5use std::time::Duration;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
8pub enum TooltipPlacement {
9 #[default]
10 Top,
11 Bottom,
12 Left,
13 Right,
14}
15
16#[derive(Default)]
17pub struct TooltipState {
18 is_visible: bool,
19}
20
21impl TooltipState {
22 pub fn new() -> Self {
23 Self::default()
24 }
25
26 pub fn is_visible(&self) -> bool {
27 self.is_visible
28 }
29}
30
31#[derive(IntoElement)]
32pub struct Tooltip {
33 content: SharedString,
34 placement: TooltipPlacement,
35 show_delay: Duration,
36 hide_delay: Duration,
37 child: Option<AnyElement>,
38 disabled: bool,
39 max_width: Option<Pixels>,
40 style: StyleRefinement,
41}
42
43impl Tooltip {
44 pub fn new(content: impl Into<SharedString>) -> Self {
45 Self {
46 content: content.into(),
47 placement: TooltipPlacement::default(),
48 show_delay: Duration::from_millis(500),
49 hide_delay: Duration::from_millis(0),
50 child: None,
51 disabled: false,
52 max_width: Some(px(300.0)),
53 style: StyleRefinement::default(),
54 }
55 }
56
57 pub fn placement(mut self, placement: TooltipPlacement) -> Self {
58 self.placement = placement;
59 self
60 }
61
62 pub fn show_delay(mut self, delay: Duration) -> Self {
65 self.show_delay = delay;
66 self
67 }
68
69 pub fn hide_delay(mut self, delay: Duration) -> Self {
72 self.hide_delay = delay;
73 self
74 }
75
76 pub fn child(mut self, child: impl IntoElement) -> Self {
77 self.child = Some(child.into_any_element());
78 self
79 }
80
81 pub fn disabled(mut self, disabled: bool) -> Self {
82 self.disabled = disabled;
83 self
84 }
85
86 pub fn max_width(mut self, width: Pixels) -> Self {
87 self.max_width = Some(width);
88 self
89 }
90}
91
92impl Styled for Tooltip {
93 fn style(&mut self) -> &mut StyleRefinement {
94 &mut self.style
95 }
96}
97
98impl RenderOnce for Tooltip {
99 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
100 let theme = use_theme(cx);
101 let placement = self.placement;
102 let user_style = self.style;
103 let max_width = self.max_width;
104 let content = self.content;
105
106 div()
109 .relative()
110 .group("")
111 .when_some(self.child, |this: Div, child| this.child(child))
112 .when(!self.disabled, |this: Div| {
113 let tooltip_box = div()
114 .occlude()
115 .px(px(8.0))
116 .py(px(4.0))
117 .bg(theme.tokens.popover)
118 .text_color(theme.tokens.popover_foreground)
119 .border_1()
120 .border_color(theme.tokens.border)
121 .rounded(theme.tokens.radius_sm)
122 .shadow_md()
123 .text_size(px(12.0))
124 .font_family(theme.tokens.font_family.clone())
125 .whitespace_nowrap()
126 .when_some(max_width, |div, width| div.max_w(width))
127 .opacity(0.0)
128 .invisible()
129 .group_hover("", |mut style| {
130 style.opacity = Some(1.0);
131 style.visibility = Some(gpui::Visibility::Visible);
132 style
133 })
134 .map(|this| {
135 let mut div = this;
136 div.style().refine(&user_style);
137 div
138 })
139 .child(content);
140
141 match placement {
146 TooltipPlacement::Top | TooltipPlacement::Bottom => this.child(
147 deferred(
148 anchored()
149 .snap_to_window_with_margin(px(8.0))
150 .anchor(match placement {
151 TooltipPlacement::Top => Corner::BottomLeft,
152 _ => Corner::TopLeft,
153 })
154 .child(tooltip_box),
155 )
156 .with_priority(1),
157 ),
158 TooltipPlacement::Left | TooltipPlacement::Right => this.child(
159 tooltip_box
160 .absolute()
161 .top_0()
162 .when(placement == TooltipPlacement::Left, |this: Div| {
163 this.right_full()
164 })
165 .when(placement == TooltipPlacement::Right, |this: Div| {
166 this.left_full()
167 }),
168 ),
169 }
170 })
171 }
172}
173
174pub fn tooltip<E: IntoElement>(child: E, content: impl Into<SharedString>) -> Tooltip {
175 Tooltip::new(content).child(child)
176}