1use gpui::{
2 Bounds, Div, Hsla, Path, PathBuilder, Pixels, Point, Size, Styled, Window, point, px, size,
3};
4use gpui_base::Placement;
5use gpui_material_foundation::{color, elevation_shadows};
6use gpui_material_theme::{
7 ColorRole, CornerRole, CornerShape, Direction, Elevation, MaterialTheme,
8};
9
10use crate::{button_style::finite_length, motion::OverlayMotion};
11
12#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
14pub enum TooltipPlacement {
15 #[default]
16 Above,
17 Below,
18 Left,
19 Right,
20 Start,
21 End,
22}
23
24impl TooltipPlacement {
25 pub(super) fn physical(self, direction: Direction) -> Placement {
26 match (self, direction) {
27 (Self::Above, _) => Placement::Top,
28 (Self::Below, _) => Placement::Bottom,
29 (Self::Left, _) | (Self::Start, Direction::Ltr) | (Self::End, Direction::Rtl) => {
30 Placement::Left
31 }
32 (Self::Right, _) | (Self::End, Direction::Ltr) | (Self::Start, Direction::Rtl) => {
33 Placement::Right
34 }
35 }
36 }
37}
38
39#[derive(Clone, Copy, Debug, Default, PartialEq)]
41pub struct TooltipColors {
42 pub container: Option<Hsla>,
43 pub content: Option<Hsla>,
44 pub title_content: Option<Hsla>,
45 pub action_content: Option<Hsla>,
46}
47
48type CaretPath = Box<dyn Fn(Bounds<Pixels>, TooltipPlacement) -> Option<Path<Pixels>>>;
49
50pub struct TooltipCaret {
52 size: Size<Pixels>,
53 path: Option<CaretPath>,
54}
55
56impl Default for TooltipCaret {
57 fn default() -> Self {
58 Self::new(size(px(16.), px(8.)))
59 }
60}
61
62impl TooltipCaret {
63 pub fn new(size: Size<Pixels>) -> Self {
65 Self {
66 size: size.map(finite_length),
67 path: None,
68 }
69 }
70
71 pub fn path(
74 mut self,
75 path: impl Fn(Bounds<Pixels>, TooltipPlacement) -> Option<Path<Pixels>> + 'static,
76 ) -> Self {
77 self.path = Some(Box::new(path));
78 self
79 }
80
81 pub(super) fn depth(&self, viewport: Size<Pixels>) -> Pixels {
82 self.size
83 .height
84 .min(viewport.width.min(viewport.height) * 0.125)
85 }
86
87 pub(super) fn paint(
88 &self,
89 surface: Bounds<Pixels>,
90 trigger: Bounds<Pixels>,
91 side: Option<Placement>,
92 style: SurfaceStyle,
93 window: &mut Window,
94 ) {
95 let Some(side) = side.or_else(|| infer_side(surface, trigger)) else {
96 return;
97 };
98 let [tl, tr, br, bl] = style
99 .shape
100 .radii(
101 (surface.size.width / style.motion.scale).into(),
102 (surface.size.height / style.motion.scale).into(),
103 style.direction,
104 )
105 .map(|radius| style.motion.px(radius));
106 let anchor = trigger.center();
107 let (lower, upper, center) = match side {
108 Placement::Top => (surface.left() + bl, surface.right() - br, anchor.x),
109 Placement::Bottom => (surface.left() + tl, surface.right() - tr, anchor.x),
110 Placement::Left => (surface.top() + tr, surface.bottom() - br, anchor.y),
111 Placement::Right => (surface.top() + tl, surface.bottom() - bl, anchor.y),
112 };
113 let span = (self.size.width * style.motion.scale).min(upper - lower);
114 let depth = self.depth(window.viewport_size()) * style.motion.scale;
115 if span <= px(0.) || depth <= px(0.) {
116 return;
117 }
118 let half = span * 0.5;
119 let center = center.max(lower + half).min(upper - half);
120 let (origin, dimensions, base_start, tip, base_end, side) = match side {
121 Placement::Top => (
122 point(center - half, surface.bottom()),
123 size(span, depth),
124 point(center - half, surface.bottom()),
125 point(center, surface.bottom() + depth),
126 point(center + half, surface.bottom()),
127 TooltipPlacement::Above,
128 ),
129 Placement::Bottom => (
130 point(center - half, surface.top() - depth),
131 size(span, depth),
132 point(center - half, surface.top()),
133 point(center, surface.top() - depth),
134 point(center + half, surface.top()),
135 TooltipPlacement::Below,
136 ),
137 Placement::Left => (
138 point(surface.right(), center - half),
139 size(depth, span),
140 point(surface.right(), center - half),
141 point(surface.right() + depth, center),
142 point(surface.right(), center + half),
143 TooltipPlacement::Left,
144 ),
145 Placement::Right => (
146 point(surface.left() - depth, center - half),
147 size(depth, span),
148 point(surface.left(), center - half),
149 point(surface.left() - depth, center),
150 point(surface.left(), center + half),
151 TooltipPlacement::Right,
152 ),
153 };
154 let path = self.path.as_ref().map_or_else(
155 || triangle(base_start, tip, base_end),
156 |path| path(Bounds::new(origin, dimensions), side),
157 );
158 if let Some(path) = path {
159 window.paint_path(path, style.container);
160 }
161 }
162}
163
164fn infer_side(surface: Bounds<Pixels>, trigger: Bounds<Pixels>) -> Option<Placement> {
165 if surface.bottom() <= trigger.top() {
166 Some(Placement::Top)
167 } else if surface.top() >= trigger.bottom() {
168 Some(Placement::Bottom)
169 } else if surface.right() <= trigger.left() {
170 Some(Placement::Left)
171 } else if surface.left() >= trigger.right() {
172 Some(Placement::Right)
173 } else {
174 None
175 }
176}
177
178fn triangle(a: Point<Pixels>, tip: Point<Pixels>, b: Point<Pixels>) -> Option<Path<Pixels>> {
179 let mut path = PathBuilder::fill();
180 path.move_to(a);
181 path.line_to(tip);
182 path.line_to(b);
183 path.close();
184 path.build().ok()
185}
186
187#[derive(Default)]
188pub(super) struct Appearance {
189 pub(super) colors: TooltipColors,
190 pub(super) shape: Option<CornerShape>,
191 pub(super) max_width: Option<Pixels>,
192 pub(super) tonal_elevation: Elevation,
193 pub(super) shadow_elevation: Option<Elevation>,
194 pub(super) caret: Option<TooltipCaret>,
195}
196
197#[derive(Clone, Copy)]
198pub(super) struct SurfaceStyle {
199 rich: bool,
200 shape: CornerShape,
201 direction: Direction,
202 motion: OverlayMotion,
203 container: Hsla,
204 pub(super) content: Hsla,
205 pub(super) title: Hsla,
206 pub(super) action: Hsla,
207 pub(super) action_override: Option<Hsla>,
208 width: Pixels,
209 height: Pixels,
210 shadow: Hsla,
211 elevation: Elevation,
212}
213
214impl Appearance {
215 pub(super) fn margin(&self, viewport: Size<Pixels>) -> Pixels {
216 let depth = self
217 .caret
218 .as_ref()
219 .map_or(px(0.), |caret| caret.depth(viewport));
220 (px(8.) + depth).min(viewport.width.min(viewport.height) * 0.25)
221 }
222
223 pub(super) fn resolve(
224 &self,
225 rich: bool,
226 viewport: Size<Pixels>,
227 motion: OverlayMotion,
228 theme: &MaterialTheme,
229 ) -> SurfaceStyle {
230 let tint = |role| color(theme.colors.color(role));
231 let default_container = if rich {
232 ColorRole::SurfaceContainer
233 } else {
234 ColorRole::InverseSurface
235 };
236 let default_content = if rich {
237 ColorRole::OnSurfaceVariant
238 } else {
239 ColorRole::InverseOnSurface
240 };
241 let corner = if rich {
242 CornerRole::Medium
243 } else {
244 CornerRole::ExtraSmall
245 };
246 let margin = self.margin(viewport);
247 let width = self
248 .max_width
249 .unwrap_or_else(|| viewport.width * 0.75)
250 .min(viewport.width - margin * 2.)
251 .min(self.max_width.unwrap_or(px(if rich { 320. } else { 200. })));
252 SurfaceStyle {
253 rich,
254 shape: self.shape.unwrap_or_else(|| corner.shape()),
255 direction: theme.layout.direction,
256 motion,
257 container: self.colors.container.unwrap_or_else(|| {
258 if self.tonal_elevation == Elevation::Level0 {
259 tint(default_container)
260 } else {
261 color(theme.colors.surface_at_elevation(self.tonal_elevation))
262 }
263 }),
264 content: self.colors.content.unwrap_or_else(|| tint(default_content)),
265 title: self
266 .colors
267 .title_content
268 .unwrap_or_else(|| tint(default_content)),
269 action: self
270 .colors
271 .action_content
272 .unwrap_or_else(|| tint(ColorRole::Primary)),
273 action_override: self.colors.action_content,
274 width,
275 height: viewport.height - margin * 2.,
276 shadow: tint(ColorRole::Shadow),
277 elevation: self.shadow_elevation.unwrap_or(if rich {
278 Elevation::Level2
279 } else {
280 Elevation::Level0
281 }),
282 }
283 }
284}
285
286impl SurfaceStyle {
287 pub(super) fn apply(self, div: Div) -> Div {
288 let scale = self.motion.scale;
289 let surface = div
290 .flex()
291 .flex_col()
292 .bg(self.container)
293 .text_color(self.content)
294 .min_w(self.width.min(px(40.)) * scale)
295 .min_h(self.height.min(px(24.)) * scale)
296 .max_w(self.width * scale)
297 .max_h(self.height * scale)
298 .overflow_hidden()
299 .shadow(elevation_shadows(self.elevation.dp(), scale, self.shadow));
300 let surface = if self.shape == CornerShape::Full {
301 surface.rounded_full()
302 } else {
303 let [tl, tr, br, bl] = self.shape.radii(f32::MAX, f32::MAX, self.direction);
304 surface
305 .rounded_tl(self.motion.px(tl))
306 .rounded_tr(self.motion.px(tr))
307 .rounded_br(self.motion.px(br))
308 .rounded_bl(self.motion.px(bl))
309 };
310 if self.rich {
311 surface.gap(self.motion.px(8.)).p(self.motion.px(16.))
312 } else {
313 surface
314 .px(self.motion.px(8.))
315 .py(self.motion.px(4.))
316 .justify_center()
317 }
318 }
319}