1#![allow(missing_docs)]
2
3use gpui::{StyleRefinement, WindowContext};
4
5use crate::{prelude::*, LabelCommon, LabelLike, LabelSize, LineHeightStyle};
6
7/// A struct representing a label element in the UI.
8///
9/// The `Label` struct stores the label text and common properties for a label element.
10/// It provides methods for modifying these properties.
11///
12/// # Examples
13///
14/// ```
15/// use ui::prelude::*;
16///
17/// Label::new("Hello, World!");
18/// ```
19///
20/// **A colored label**, for example labeling a dangerous action:
21///
22/// ```
23/// use ui::prelude::*;
24///
25/// let my_label = Label::new("Delete").color(Color::Error);
26/// ```
27///
28/// **A label with a strikethrough**, for example labeling something that has been deleted:
29///
30/// ```
31/// use ui::prelude::*;
32///
33/// let my_label = Label::new("Deleted").strikethrough(true);
34/// ```
35#[derive(IntoElement)]
36pub struct Label {
37 base: LabelLike,
38 label: SharedString,
39}
40
41impl Label {
42 /// Creates a new [`Label`] with the given text.
43 ///
44 /// # Examples
45 ///
46 /// ```
47 /// use ui::prelude::*;
48 ///
49 /// let my_label = Label::new("Hello, World!");
50 /// ```
51 pub fn new(label: impl Into<SharedString>) -> Self {
52 Self {
53 base: LabelLike::new(),
54 label: label.into(),
55 }
56 }
57}
58
59// Style methods.
60impl Label {
61 fn style(&mut self) -> &mut StyleRefinement {
62 self.base.base.style()
63 }
64
65 gpui::margin_style_methods!({
66 visibility: pub
67 });
68}
69
70impl LabelCommon for Label {
71 /// Sets the size of the label using a [`LabelSize`].
72 ///
73 /// # Examples
74 ///
75 /// ```
76 /// use ui::prelude::*;
77 ///
78 /// let my_label = Label::new("Hello, World!").size(LabelSize::Small);
79 /// ```
80 fn size(mut self, size: LabelSize) -> Self {
81 self.base = self.base.size(size);
82 self
83 }
84
85 fn weight(mut self, weight: gpui::FontWeight) -> Self {
86 self.base = self.base.weight(weight);
87 self
88 }
89
90 /// Sets the line height style of the label using a [`LineHeightStyle`].
91 ///
92 /// # Examples
93 ///
94 /// ```
95 /// use ui::prelude::*;
96 ///
97 /// let my_label = Label::new("Hello, World!").line_height_style(LineHeightStyle::UiLabel);
98 /// ```
99 fn line_height_style(mut self, line_height_style: LineHeightStyle) -> Self {
100 self.base = self.base.line_height_style(line_height_style);
101 self
102 }
103
104 /// Sets the color of the label using a [`Color`].
105 ///
106 /// # Examples
107 ///
108 /// ```
109 /// use ui::prelude::*;
110 ///
111 /// let my_label = Label::new("Hello, World!").color(Color::Accent);
112 /// ```
113 fn color(mut self, color: Color) -> Self {
114 self.base = self.base.color(color);
115 self
116 }
117
118 /// Sets the strikethrough property of the label.
119 ///
120 /// # Examples
121 ///
122 /// ```
123 /// use ui::prelude::*;
124 ///
125 /// let my_label = Label::new("Hello, World!").strikethrough(true);
126 /// ```
127 fn strikethrough(mut self, strikethrough: bool) -> Self {
128 self.base = self.base.strikethrough(strikethrough);
129 self
130 }
131
132 /// Sets the italic property of the label.
133 ///
134 /// # Examples
135 ///
136 /// ```
137 /// use ui::prelude::*;
138 ///
139 /// let my_label = Label::new("Hello, World!").italic(true);
140 /// ```
141 fn italic(mut self, italic: bool) -> Self {
142 self.base = self.base.italic(italic);
143 self
144 }
145
146 /// Sets the alpha property of the color of label.
147 ///
148 /// # Examples
149 ///
150 /// ```
151 /// use ui::prelude::*;
152 ///
153 /// let my_label = Label::new("Hello, World!").alpha(0.5);
154 /// ```
155 fn alpha(mut self, alpha: f32) -> Self {
156 self.base = self.base.alpha(alpha);
157 self
158 }
159
160 fn underline(mut self, underline: bool) -> Self {
161 self.base = self.base.underline(underline);
162 self
163 }
164
165 fn text_ellipsis(mut self) -> Self {
166 self.base = self.base.text_ellipsis();
167 self
168 }
169
170 fn single_line(mut self) -> Self {
171 self.label = SharedString::from(self.label.replace('\n', ""));
172 self.base = self.base.single_line();
173 self
174 }
175}
176
177impl RenderOnce for Label {
178 fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
179 self.base.child(self.label)
180 }
181}