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 /// Sets the line height style of the label using a [`LineHeightStyle`].
89 ///
90 /// # Examples
91 ///
92 /// ```
93 /// use ui::prelude::*;
94 ///
95 /// let my_label = Label::new("Hello, World!").line_height_style(LineHeightStyle::UiLabel);
96 /// ```
97 fn line_height_style(mut self, line_height_style: LineHeightStyle) -> Self {
98 self.base = self.base.line_height_style(line_height_style);
99 self
100 }
101
102 /// Sets the color of the label using a [`Color`].
103 ///
104 /// # Examples
105 ///
106 /// ```
107 /// use ui::prelude::*;
108 ///
109 /// let my_label = Label::new("Hello, World!").color(Color::Accent);
110 /// ```
111 fn color(mut self, color: Color) -> Self {
112 self.base = self.base.color(color);
113 self
114 }
115
116 /// Sets the strikethrough property of the label.
117 ///
118 /// # Examples
119 ///
120 /// ```
121 /// use ui::prelude::*;
122 ///
123 /// let my_label = Label::new("Hello, World!").strikethrough(true);
124 /// ```
125 fn strikethrough(mut self, strikethrough: bool) -> Self {
126 self.base = self.base.strikethrough(strikethrough);
127 self
128 }
129
130 /// Sets the italic property of the label.
131 ///
132 /// # Examples
133 ///
134 /// ```
135 /// use ui::prelude::*;
136 ///
137 /// let my_label = Label::new("Hello, World!").italic(true);
138 /// ```
139 fn italic(mut self, italic: bool) -> Self {
140 self.base = self.base.italic(italic);
141 self
142 }
143}
144
145impl RenderOnce for Label {
146 fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
147 let target_label = if self.single_line {
148 SharedString::from(self.label.replace('\n', ""))
149 } else {
150 self.label
151 };
152 self.base.child(target_label)
153 }
154}