use crate::theme::{ActiveTheme, Themeable};
use gpui::{
App, Div, ElementId, FontWeight, IntoElement, ParentElement, RenderOnce, SharedString, Styled,
Window, div, prelude::FluentBuilder, rems,
};
pub fn label(text: impl Into<SharedString>) -> Label {
Label::new(text)
}
#[derive(IntoElement)]
pub struct Label {
text: SharedString,
for_id: Option<ElementId>,
required: bool,
disabled: bool,
}
impl Label {
pub fn new(text: impl Into<SharedString>) -> Self {
Label {
text: text.into(),
for_id: None,
required: false,
disabled: false,
}
}
pub fn for_id(mut self, id: impl Into<ElementId>) -> Self {
self.for_id = Some(id.into());
self
}
pub fn required(mut self, required: bool) -> Self {
self.required = required;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
impl RenderOnce for Label {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let text_color = if self.disabled {
theme.fg_disabled()
} else {
theme.fg()
};
div()
.flex()
.items_center()
.gap(rems(0.25))
.text_sm()
.font_weight(FontWeight::MEDIUM)
.text_color(text_color)
.child(self.text)
.when(self.required, |this: Div| {
this.child(div().text_color(theme.danger()).child("*"))
})
}
}