use super::form;
use gpui_kit::{
App, CursorStyle, FocusHandle, InteractiveElement, IntoElement, RenderOnce, SharedString,
StatefulInteractiveElement, Styled, Window, prelude::FluentBuilder as _,
};
#[derive(IntoElement)]
pub struct Label {
text: SharedString,
disabled: bool,
no_margin: bool,
target: Option<FocusHandle>,
}
impl Label {
pub fn new(text: impl Into<SharedString>) -> Self {
Self {
text: text.into(),
disabled: false,
no_margin: false,
target: None,
}
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn no_margin(mut self, no_margin: bool) -> Self {
self.no_margin = no_margin;
self
}
pub fn target(mut self, focus: &FocusHandle) -> Self {
self.target = Some(focus.clone());
self
}
}
impl RenderOnce for Label {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let label = form::label(self.text.clone(), self.disabled)
.when(self.no_margin, |this| this.mb_0())
.when(super::direction::is_rtl(), |this| this.text_right());
match self.target.filter(|_| !self.disabled) {
Some(target) => label
.id(gpui_kit::ElementId::Name(self.text))
.cursor(CursorStyle::Arrow)
.on_click(move |_, window, cx| target.focus(window, cx))
.into_any_element(),
None => label.into_any_element(),
}
}
}