label.rs

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