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/// Label::new("Hello, World!")
 14/// ```
 15///
 16/// **A colored label**, for example labeling a dangerous action:
 17///
 18/// ```
 19/// let my_label = Label::new("Delete").color(Color::Error);
 20/// ```
 21///
 22/// **A label with a strikethrough**, for example labeling something that has been deleted:
 23///
 24/// ```
 25/// let my_label = Label::new("Deleted").strikethrough(true);
 26/// ```
 27#[derive(IntoElement)]
 28pub struct Label {
 29    base: LabelLike,
 30    label: SharedString,
 31}
 32
 33impl Label {
 34    /// Create a new `Label` with the given text.
 35    ///
 36    /// # Examples
 37    ///
 38    /// ```
 39    /// let my_label = Label::new("Hello, World!");
 40    /// ```
 41    pub fn new(label: impl Into<SharedString>) -> Self {
 42        Self {
 43            base: LabelLike::new(),
 44            label: label.into(),
 45        }
 46    }
 47}
 48
 49impl LabelCommon for Label {
 50    /// Sets the size of the label using a [LabelSize].
 51    ///
 52    /// # Examples
 53    ///
 54    /// ```
 55    /// let my_label = Label::new("Hello, World!").size(LabelSize::Large);
 56    /// ```
 57    fn size(mut self, size: LabelSize) -> Self {
 58        self.base = self.base.size(size);
 59        self
 60    }
 61
 62    /// Sets the line height style of the label using a [LineHeightStyle].
 63    ///
 64    /// # Examples
 65    ///
 66    /// ```
 67    /// let my_label = Label::new("Hello, World!").line_height_style(LineHeightStyle::Normal);
 68    /// ```
 69    fn line_height_style(mut self, line_height_style: LineHeightStyle) -> Self {
 70        self.base = self.base.line_height_style(line_height_style);
 71        self
 72    }
 73
 74    /// Sets the color of the label using a [Color].
 75    ///
 76    /// # Examples
 77    ///
 78    /// ```
 79    /// let my_label = Label::new("Hello, World!").color(Color::Primary);
 80    /// ```
 81    fn color(mut self, color: Color) -> Self {
 82        self.base = self.base.color(color);
 83        self
 84    }
 85
 86    /// Sets the strikethrough property of the label.
 87    ///
 88    /// # Examples
 89    ///
 90    /// ```
 91    /// let my_label = Label::new("Hello, World!").strikethrough(true);
 92    /// ```
 93    fn strikethrough(mut self, strikethrough: bool) -> Self {
 94        self.base = self.base.strikethrough(strikethrough);
 95        self
 96    }
 97
 98impl RenderOnce for Label {
 99    fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
100        self.base.child(self.label)
101    }
102}