use gpui::{AnyView, App, Context, IntoElement, SharedString, Window, div, prelude::*, px};
use theme::Theme;
use crate::popover;
pub struct Tooltip {
text: SharedString,
keystroke: Option<SharedString>,
}
impl Tooltip {
pub fn text(text: impl Into<SharedString>, _window: &mut Window, cx: &mut App) -> AnyView {
let text = text.into();
cx.new(|_| Self {
text,
keystroke: None,
})
.into()
}
pub fn with_keystroke(
text: impl Into<SharedString>,
keystroke: impl Into<SharedString>,
_window: &mut Window,
cx: &mut App,
) -> AnyView {
let (text, keystroke) = (text.into(), keystroke.into());
cx.new(|_| Self {
text,
keystroke: Some(keystroke),
})
.into()
}
}
impl Render for Tooltip {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = Theme::of(cx).clone();
popover::popover_card(&theme)
.px(px(8.0))
.py(px(5.0))
.flex()
.flex_row()
.items_center()
.gap(px(8.0))
.text_size(px(12.0))
.text_color(theme.text)
.child(self.text.clone())
.when_some(self.keystroke.clone(), |card, keystroke| {
card.child(
div()
.text_size(px(11.0))
.text_color(theme.text_faint)
.child(keystroke),
)
})
}
}