label.rs

  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    single_line: bool,
 40}
 41
 42impl Label {
 43    /// Creates a new [`Label`] with the given text.
 44    ///
 45    /// # Examples
 46    ///
 47    /// ```
 48    /// use ui::prelude::*;
 49    ///
 50    /// let my_label = Label::new("Hello, World!");
 51    /// ```
 52    pub fn new(label: impl Into<SharedString>) -> Self {
 53        Self {
 54            base: LabelLike::new(),
 55            label: label.into(),
 56            single_line: false,
 57        }
 58    }
 59}
 60
 61// Style methods.
 62impl Label {
 63    fn style(&mut self) -> &mut StyleRefinement {
 64        self.base.base.style()
 65    }
 66
 67    gpui::margin_style_methods!({
 68        visibility: pub
 69    });
 70}
 71
 72impl LabelCommon for Label {
 73    /// Sets the size of the label using a [`LabelSize`].
 74    ///
 75    /// # Examples
 76    ///
 77    /// ```
 78    /// use ui::prelude::*;
 79    ///
 80    /// let my_label = Label::new("Hello, World!").size(LabelSize::Small);
 81    /// ```
 82    fn size(mut self, size: LabelSize) -> Self {
 83        self.base = self.base.size(size);
 84        self
 85    }
 86
 87    fn weight(mut self, weight: gpui::FontWeight) -> Self {
 88        self.base = self.base.weight(weight);
 89        self
 90    }
 91
 92    /// Sets the line height style of the label using a [`LineHeightStyle`].
 93    ///
 94    /// # Examples
 95    ///
 96    /// ```
 97    /// use ui::prelude::*;
 98    ///
 99    /// let my_label = Label::new("Hello, World!").line_height_style(LineHeightStyle::UiLabel);
100    /// ```
101    fn line_height_style(mut self, line_height_style: LineHeightStyle) -> Self {
102        self.base = self.base.line_height_style(line_height_style);
103        self
104    }
105
106    /// Sets the color of the label using a [`Color`].
107    ///
108    /// # Examples
109    ///
110    /// ```
111    /// use ui::prelude::*;
112    ///
113    /// let my_label = Label::new("Hello, World!").color(Color::Accent);
114    /// ```
115    fn color(mut self, color: Color) -> Self {
116        self.base = self.base.color(color);
117        self
118    }
119
120    /// Sets the strikethrough property of the label.
121    ///
122    /// # Examples
123    ///
124    /// ```
125    /// use ui::prelude::*;
126    ///
127    /// let my_label = Label::new("Hello, World!").strikethrough(true);
128    /// ```
129    fn strikethrough(mut self, strikethrough: bool) -> Self {
130        self.base = self.base.strikethrough(strikethrough);
131        self
132    }
133
134    /// Sets the italic property of the label.
135    ///
136    /// # Examples
137    ///
138    /// ```
139    /// use ui::prelude::*;
140    ///
141    /// let my_label = Label::new("Hello, World!").italic(true);
142    /// ```
143    fn italic(mut self, italic: bool) -> Self {
144        self.base = self.base.italic(italic);
145        self
146    }
147
148    /// Sets the alpha property of the color of label.
149    ///
150    /// # Examples
151    ///
152    /// ```
153    /// use ui::prelude::*;
154    ///
155    /// let my_label = Label::new("Hello, World!").alpha(0.5);
156    /// ```
157    fn alpha(mut self, alpha: f32) -> Self {
158        self.base = self.base.alpha(alpha);
159        self
160    }
161
162    fn underline(mut self, underline: bool) -> Self {
163        self.base = self.base.underline(underline);
164        self
165    }
166
167    fn text_ellipsis(mut self) -> Self {
168        self.base = self.base.text_ellipsis();
169        self
170    }
171
172    fn single_line(mut self) -> Self {
173        self.single_line = true;
174        self.base = self.base.single_line();
175        self
176    }
177}
178
179impl RenderOnce for Label {
180    fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
181        let target_label = if self.single_line {
182            SharedString::from(self.label.replace('\n', ""))
183        } else {
184            self.label
185        };
186        self.base.child(target_label)
187    }
188}