tooltip.rs

 1use gpui::{Div, Render};
 2use settings2::Settings;
 3use theme2::{ActiveTheme, ThemeSettings};
 4
 5use crate::prelude::*;
 6use crate::{h_stack, v_stack, KeyBinding, Label, LabelSize, StyledExt, TextColor};
 7
 8pub struct TextTooltip {
 9    title: SharedString,
10    meta: Option<SharedString>,
11    key_binding: Option<KeyBinding>,
12}
13
14impl TextTooltip {
15    pub fn new(title: impl Into<SharedString>) -> Self {
16        Self {
17            title: title.into(),
18            meta: None,
19            key_binding: None,
20        }
21    }
22
23    pub fn meta(mut self, meta: impl Into<SharedString>) -> Self {
24        self.meta = Some(meta.into());
25        self
26    }
27
28    pub fn key_binding(mut self, key_binding: impl Into<Option<KeyBinding>>) -> Self {
29        self.key_binding = key_binding.into();
30        self
31    }
32}
33
34impl Render for TextTooltip {
35    type Element = Div<Self>;
36
37    fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
38        let ui_font = ThemeSettings::get_global(cx).ui_font.family.clone();
39        v_stack()
40            .elevation_2(cx)
41            .font(ui_font)
42            .text_ui_sm()
43            .text_color(cx.theme().colors().text)
44            .py_1()
45            .px_2()
46            .child(
47                h_stack()
48                    .child(self.title.clone())
49                    .when_some(self.key_binding.clone(), |this, key_binding| {
50                        this.justify_between().child(key_binding)
51                    }),
52            )
53            .when_some(self.meta.clone(), |this, meta| {
54                this.child(
55                    Label::new(meta)
56                        .size(LabelSize::Small)
57                        .color(TextColor::Muted),
58                )
59            })
60    }
61}