label.rs

  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}
 38
 39impl Label {
 40    /// Creates a new [`Label`] with the given text.
 41    ///
 42    /// # Examples
 43    ///
 44    /// ```
 45    /// use ui::prelude::*;
 46    ///
 47    /// let my_label = Label::new("Hello, World!");
 48    /// ```
 49    pub fn new(label: impl Into<SharedString>) -> Self {
 50        Self {
 51            base: LabelLike::new(),
 52            label: label.into(),
 53        }
 54    }
 55}
 56
 57impl LabelCommon for Label {
 58    /// Sets the size of the label using a [`LabelSize`].
 59    ///
 60    /// # Examples
 61    ///
 62    /// ```
 63    /// use ui::prelude::*;
 64    ///
 65    /// let my_label = Label::new("Hello, World!").size(LabelSize::Small);
 66    /// ```
 67    fn size(mut self, size: LabelSize) -> Self {
 68        self.base = self.base.size(size);
 69        self
 70    }
 71
 72    /// Sets the line height style of the label using a [`LineHeightStyle`].
 73    ///
 74    /// # Examples
 75    ///
 76    /// ```
 77    /// use ui::prelude::*;
 78    ///
 79    /// let my_label = Label::new("Hello, World!").line_height_style(LineHeightStyle::UiLabel);
 80    /// ```
 81    fn line_height_style(mut self, line_height_style: LineHeightStyle) -> Self {
 82        self.base = self.base.line_height_style(line_height_style);
 83        self
 84    }
 85
 86    /// Sets the color of the label using a [`Color`].
 87    ///
 88    /// # Examples
 89    ///
 90    /// ```
 91    /// use ui::prelude::*;
 92    ///
 93    /// let my_label = Label::new("Hello, World!").color(Color::Accent);
 94    /// ```
 95    fn color(mut self, color: Color) -> Self {
 96        self.base = self.base.color(color);
 97        self
 98    }
 99
100    /// Sets the strikethrough property of the label.
101    ///
102    /// # Examples
103    ///
104    /// ```
105    /// use ui::prelude::*;
106    ///
107    /// let my_label = Label::new("Hello, World!").strikethrough(true);
108    /// ```
109    fn strikethrough(mut self, strikethrough: bool) -> Self {
110        self.base = self.base.strikethrough(strikethrough);
111        self
112    }
113}
114
115impl RenderOnce for Label {
116    fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
117        self.base.child(self.label)
118    }
119}