fc_ui/capabilities/primitives/
label.rs1use crate::capabilities::foundation::theme::use_theme;
4use gpui::{prelude::FluentBuilder as _, *};
5
6#[derive(IntoElement)]
16pub struct Label {
17 text: SharedString,
18 for_id: Option<ElementId>,
19 required: bool,
20 helper_text: Option<SharedString>,
21 disabled: bool,
22 style: StyleRefinement,
23}
24
25impl Label {
26 pub fn new(text: impl Into<SharedString>) -> Self {
28 Self {
29 text: text.into(),
30 for_id: None,
31 required: false,
32 helper_text: None,
33 disabled: false,
34 style: StyleRefinement::default(),
35 }
36 }
37
38 #[deprecated(
40 note = "has no effect: GPUI cannot resolve a FocusHandle from an ElementId, so the association is never rendered"
41 )]
42 pub fn for_id(mut self, id: impl Into<ElementId>) -> Self {
43 self.for_id = Some(id.into());
44 self
45 }
46
47 pub fn required(mut self, required: bool) -> Self {
49 self.required = required;
50 self
51 }
52
53 pub fn helper_text(mut self, text: impl Into<SharedString>) -> Self {
55 self.helper_text = Some(text.into());
56 self
57 }
58
59 pub fn disabled(mut self, disabled: bool) -> Self {
61 self.disabled = disabled;
62 self
63 }
64}
65
66impl Styled for Label {
67 fn style(&mut self) -> &mut StyleRefinement {
68 &mut self.style
69 }
70}
71
72impl RenderOnce for Label {
73 fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
74 let theme = use_theme(cx);
75 let user_style = self.style;
76
77 div()
78 .flex()
79 .flex_col()
80 .gap(px(4.0))
81 .child(
82 div()
83 .flex()
84 .items_center()
85 .gap(px(4.0))
86 .text_sm()
87 .font_family(theme.tokens.font_family.clone())
88 .font_weight(FontWeight::MEDIUM)
89 .text_color(if self.disabled {
90 theme.tokens.muted_foreground
91 } else {
92 theme.tokens.foreground
93 })
94 .line_height(relative(1.0))
95 .child(self.text)
96 .when(self.required, |this| {
97 this.child(div().text_color(theme.tokens.destructive).child("*"))
98 }),
99 )
100 .when_some(self.helper_text, |this, helper| {
101 this.child(
102 div()
103 .text_xs()
104 .font_family(theme.tokens.font_family.clone())
105 .text_color(theme.tokens.muted_foreground)
106 .line_height(relative(1.25))
107 .child(helper),
108 )
109 })
110 .map(|this| {
111 let mut div = this;
112 div.style().refine(&user_style);
113 div
114 })
115 }
116}
117
118impl From<String> for Label {
119 fn from(text: String) -> Self {
120 Self::new(text)
121 }
122}
123
124impl From<SharedString> for Label {
125 fn from(text: SharedString) -> Self {
126 Self::new(text)
127 }
128}