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