Add label docs

This commit is contained in:
Nate Butler 2024-01-09 13:49:27 -05:00
parent 324fd24709
commit 7dbe0519ec
2 changed files with 60 additions and 3 deletions

View file

@ -7,7 +7,7 @@ use crate::{
use super::button_icon::ButtonIcon; use super::button_icon::ButtonIcon;
/// [Button] can be use to create a button with a label and an optional icon. /// An element that creates a button with a label and an optional icon.
/// ///
/// Common buttons: /// Common buttons:
/// - Label, Icon + Label: [Button] (this component) /// - Label, Icon + Label: [Button] (this component)
@ -19,7 +19,8 @@ use super::button_icon::ButtonIcon;
/// ///
/// # Examples /// # Examples
/// ///
/// A button with a label only: /// **A button with a label**, is typically used in scenarios such as a form, where the button's label
/// indicates what action will be performed when the button is clicked.
/// ///
/// ``` /// ```
/// Button::new("button_id", "Click me!") /// Button::new("button_id", "Click me!")

View file

@ -2,6 +2,28 @@ use gpui::WindowContext;
use crate::{prelude::*, LabelCommon, LabelLike, LabelSize, LineHeightStyle}; use crate::{prelude::*, LabelCommon, LabelLike, LabelSize, LineHeightStyle};
/// A struct representing a label element in the UI.
///
/// The `Label` struct stores the label text and common properties for a label element.
/// It provides methods for modifying these properties.
///
/// # Examples
///
/// ```
/// Label::new("Hello, World!")
/// ```
///
/// **A colored label**, for example labeling a dangerous action:
///
/// ```
/// let my_label = Label::new("Delete").color(Color::Error);
/// ```
///
/// **A label with a strikethrough**, for example labeling something that has been deleted:
///
/// ```
/// let my_label = Label::new("Deleted").strikethrough(true);
/// ```
#[derive(IntoElement)] #[derive(IntoElement)]
pub struct Label { pub struct Label {
base: LabelLike, base: LabelLike,
@ -9,6 +31,13 @@ pub struct Label {
} }
impl Label { impl Label {
/// Create a new `Label` with the given text.
///
/// # Examples
///
/// ```
/// let my_label = Label::new("Hello, World!");
/// ```
pub fn new(label: impl Into<SharedString>) -> Self { pub fn new(label: impl Into<SharedString>) -> Self {
Self { Self {
base: LabelLike::new(), base: LabelLike::new(),
@ -18,26 +47,53 @@ impl Label {
} }
impl LabelCommon for Label { impl LabelCommon for Label {
/// Sets the size of the label using a [LabelSize].
///
/// # Examples
///
/// ```
/// let my_label = Label::new("Hello, World!").size(LabelSize::Large);
/// ```
fn size(mut self, size: LabelSize) -> Self { fn size(mut self, size: LabelSize) -> Self {
self.base = self.base.size(size); self.base = self.base.size(size);
self self
} }
/// Sets the line height style of the label using a [LineHeightStyle].
///
/// # Examples
///
/// ```
/// let my_label = Label::new("Hello, World!").line_height_style(LineHeightStyle::Normal);
/// ```
fn line_height_style(mut self, line_height_style: LineHeightStyle) -> Self { fn line_height_style(mut self, line_height_style: LineHeightStyle) -> Self {
self.base = self.base.line_height_style(line_height_style); self.base = self.base.line_height_style(line_height_style);
self self
} }
/// Sets the color of the label using a [Color].
///
/// # Examples
///
/// ```
/// let my_label = Label::new("Hello, World!").color(Color::Primary);
/// ```
fn color(mut self, color: Color) -> Self { fn color(mut self, color: Color) -> Self {
self.base = self.base.color(color); self.base = self.base.color(color);
self self
} }
/// Sets the strikethrough property of the label.
///
/// # Examples
///
/// ```
/// let my_label = Label::new("Hello, World!").strikethrough(true);
/// ```
fn strikethrough(mut self, strikethrough: bool) -> Self { fn strikethrough(mut self, strikethrough: bool) -> Self {
self.base = self.base.strikethrough(strikethrough); self.base = self.base.strikethrough(strikethrough);
self self
} }
}
impl RenderOnce for Label { impl RenderOnce for Label {
fn render(self, _cx: &mut WindowContext) -> impl IntoElement { fn render(self, _cx: &mut WindowContext) -> impl IntoElement {