Skip to main content

guise/
kbd.rs

1//! `Kbd` — a keyboard-key cap.
2
3use gpui::prelude::*;
4use gpui::{div, px, App, FontWeight, IntoElement, SharedString, Window};
5
6use crate::theme::{theme, ColorName, Size};
7
8/// A keyboard key. The Mantine `Kbd`.
9#[derive(IntoElement)]
10pub struct Kbd {
11    key: SharedString,
12}
13
14impl Kbd {
15    pub fn new(key: impl Into<SharedString>) -> Self {
16        Kbd { key: key.into() }
17    }
18}
19
20impl RenderOnce for Kbd {
21    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
22        let t = theme(cx);
23        let bg = t
24            .color(ColorName::Gray, if t.scheme.is_dark() { 7 } else { 0 })
25            .hsla();
26        let border = t
27            .color(ColorName::Gray, if t.scheme.is_dark() { 6 } else { 3 })
28            .hsla();
29
30        div()
31            .px(px(7.0))
32            .py(px(2.0))
33            .rounded(px(t.radius(Size::Sm)))
34            .border_1()
35            .border_color(border)
36            .bg(bg)
37            .text_color(t.text().hsla())
38            .text_size(px(t.font_size(Size::Xs)))
39            .font_weight(FontWeight::SEMIBOLD)
40            .child(self.key)
41    }
42}