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