use crate::theme::{ActiveTheme, ControlSize, Themeable};
use crate::traits::control_sized::ControlSized;
use gpui::{
App, FontWeight, IntoElement, ParentElement, RenderOnce, SharedString, Styled, Window, div,
};
pub fn kbd(key: impl Into<SharedString>) -> Kbd {
Kbd::new(key)
}
pub fn kbd_combo(keys: &[impl AsRef<str>]) -> Kbd {
let combined = keys
.iter()
.map(|k| k.as_ref())
.collect::<Vec<_>>()
.join("+");
Kbd::new(combined)
}
#[derive(IntoElement)]
pub struct Kbd {
key: SharedString,
size: ControlSize,
}
impl Kbd {
pub fn new(key: impl Into<SharedString>) -> Self {
Kbd {
key: key.into(),
size: ControlSize::default(),
}
}
}
impl RenderOnce for Kbd {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let metrics = theme.control(self.size);
div()
.h(metrics.height)
.min_w(metrics.height)
.px(metrics.padding_x)
.rounded(metrics.radius)
.text_size(metrics.text_size)
.font_weight(FontWeight::MEDIUM)
.font_family("Monaco, Consolas, monospace")
.line_height(metrics.line_height)
.bg(theme.surface())
.text_color(theme.fg_muted())
.border_1()
.border_color(theme.border())
.shadow_sm()
.whitespace_nowrap()
.flex()
.flex_none()
.items_center()
.justify_center()
.child(self.key)
}
}
impl ControlSized for Kbd {
fn control_size(mut self, size: ControlSize) -> Self {
self.size = size;
self
}
}