1use gpui::{AnyView, App, Context, IntoElement, SharedString, Window, div, prelude::*, px};
17
18use theme::{TextStyle, Theme, Typeset};
19
20use crate::{popover, surface::Surfaced as _};
21
22pub struct Tooltip {
23 text: SharedString,
24 keystroke: Option<SharedString>,
26}
27
28impl Tooltip {
29 pub fn text(text: impl Into<SharedString>, _window: &mut Window, cx: &mut App) -> AnyView {
31 let text = text.into();
32 cx.new(|_| Self {
33 text,
34 keystroke: None,
35 })
36 .into()
37 }
38
39 pub fn with_keystroke(
42 text: impl Into<SharedString>,
43 keystroke: impl Into<SharedString>,
44 _window: &mut Window,
45 cx: &mut App,
46 ) -> AnyView {
47 let (text, keystroke) = (text.into(), keystroke.into());
48 cx.new(|_| Self {
49 text,
50 keystroke: Some(keystroke),
51 })
52 .into()
53 }
54}
55
56impl Render for Tooltip {
57 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
58 let theme = Theme::of(cx).clone();
59 popover::popover_card(&theme)
62 .px(px(8.0))
63 .py(px(5.0))
64 .flex()
65 .flex_row()
66 .items_center()
67 .gap(px(Theme::SPACE))
68 .text_style(TextStyle::Callout)
69 .text_color(theme.text)
70 .child(self.text.clone())
71 .when_some(self.keystroke.clone(), |card, keystroke| {
72 card.child(
73 div()
74 .text_style(TextStyle::Subheadline)
75 .text_color(theme.text_faint)
76 .child(keystroke),
77 )
78 })
79 .surface(&theme, theme.popover_surface)
80 }
81}