Skip to main content

fc_ui/capabilities/primitives/
kbd.rs

1use crate::capabilities::foundation::theme::use_theme;
2use gpui::*;
3use smallvec::smallvec;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
6pub enum KBDSize {
7    Sm,
8    #[default]
9    Md,
10    Lg,
11}
12
13#[derive(IntoElement)]
14pub struct KBD {
15    label: SharedString,
16    size: KBDSize,
17    style: StyleRefinement,
18}
19
20impl KBD {
21    pub fn new(label: impl Into<SharedString>) -> Self {
22        Self {
23            label: label.into(),
24            size: KBDSize::default(),
25            style: StyleRefinement::default(),
26        }
27    }
28
29    pub fn size(mut self, size: KBDSize) -> Self {
30        self.size = size;
31        self
32    }
33}
34
35impl Styled for KBD {
36    fn style(&mut self) -> &mut StyleRefinement {
37        &mut self.style
38    }
39}
40
41impl RenderOnce for KBD {
42    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
43        let theme = use_theme(cx);
44        let user_style = self.style;
45
46        let (text_size, px_val, py_val, min_w) = match self.size {
47            KBDSize::Sm => (px(10.0), px(4.0), px(1.0), px(16.0)),
48            KBDSize::Md => (px(11.0), px(6.0), px(2.0), px(20.0)),
49            KBDSize::Lg => (px(12.0), px(8.0), px(3.0), px(24.0)),
50        };
51
52        let mut el = div()
53            .flex()
54            .items_center()
55            .justify_center()
56            .px(px_val)
57            .py(py_val)
58            .min_w(min_w)
59            .bg(theme.tokens.muted)
60            .border_1()
61            .border_color(theme.tokens.border)
62            .rounded(theme.tokens.radius_sm)
63            .text_size(text_size)
64            .font_family(theme.tokens.font_mono.clone())
65            .text_color(theme.tokens.muted_foreground)
66            .font_weight(FontWeight::MEDIUM)
67            .line_height(relative(1.0))
68            .shadow(smallvec![BoxShadow {
69                color: hsla(0.0, 0.0, 0.0, 0.15),
70                offset: point(px(0.0), px(1.0)),
71                blur_radius: px(0.0),
72                spread_radius: px(0.0),
73                inset: false,
74            }])
75            .child(self.label);
76        el.style().refine(&user_style);
77        el
78    }
79}