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