Document ui crate traits

This commit is contained in:
Nate Butler 2024-01-09 14:15:25 -05:00
parent 46065c2621
commit e020d7ca11
7 changed files with 21 additions and 5 deletions

View file

@ -1,6 +1,6 @@
use gpui::{ClickEvent, WindowContext}; use gpui::{ClickEvent, WindowContext};
/// A trait for elements that can be clicked. /// A trait for elements that can be clicked. Enables the use of the `on_click` method.
pub trait Clickable { pub trait Clickable {
/// Sets the click handler that will fire whenever the element is clicked. /// Sets the click handler that will fire whenever the element is clicked.
fn on_click(self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self; fn on_click(self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self;

View file

@ -4,10 +4,12 @@ use smallvec::SmallVec;
use crate::prelude::*; use crate::prelude::*;
/// A trait for buttons that can be Selected. Enables setting the [ButtonStyle] of a button when it is selected.
pub trait SelectableButton: Selectable { pub trait SelectableButton: Selectable {
fn selected_style(self, style: ButtonStyle) -> Self; fn selected_style(self, style: ButtonStyle) -> Self;
} }
/// A common set of traits all buttons must implement.
pub trait ButtonCommon: Clickable + Disableable { pub trait ButtonCommon: Clickable + Disableable {
/// A unique element ID to identify the button. /// A unique element ID to identify the button.
fn id(&self) -> &ElementId; fn id(&self) -> &ElementId;

View file

@ -19,6 +19,7 @@ pub enum LineHeightStyle {
UiLabel, UiLabel,
} }
/// A common set of traits all labels must implement.
pub trait LabelCommon { pub trait LabelCommon {
fn size(self, size: LabelSize) -> Self; fn size(self, size: LabelSize) -> Self;
fn line_height_style(self, line_height_style: LineHeightStyle) -> Self; fn line_height_style(self, line_height_style: LineHeightStyle) -> Self;

View file

@ -1,4 +1,4 @@
/// A trait for elements that can be disabled. /// A trait for elements that can be disabled. Generally used to implement disabling an element's interactivity and changing it's appearance to reflect that it is disabled.
pub trait Disableable { pub trait Disableable {
/// Sets whether the element is disabled. /// Sets whether the element is disabled.
fn disabled(self, disabled: bool) -> Self; fn disabled(self, disabled: bool) -> Self;

View file

@ -1,6 +1,6 @@
use gpui::DefiniteLength; use gpui::DefiniteLength;
/// A trait for elements that have a fixed with. /// A trait for elements that can have a fixed with. Enables the use of the `width` and `full_width` methods.
pub trait FixedWidth { pub trait FixedWidth {
/// Sets the width of the element. /// Sets the width of the element.
fn width(self, width: DefiniteLength) -> Self; fn width(self, width: DefiniteLength) -> Self;

View file

@ -1,18 +1,25 @@
/// A trait for elements that can be selected. /// A trait for elements that can be selected. Generally used to enable "toggle" or "active" behavior and styles on an element through the [Selection] status.
pub trait Selectable { pub trait Selectable {
/// Sets whether the element is selected. /// Sets whether the element is selected.
fn selected(self, selected: bool) -> Self; fn selected(self, selected: bool) -> Self;
} }
/// Represents the selection status of an element.
#[derive(Debug, Default, PartialEq, Eq, Hash, Clone, Copy)] #[derive(Debug, Default, PartialEq, Eq, Hash, Clone, Copy)]
pub enum Selection { pub enum Selection {
/// The element is not selected.
#[default] #[default]
Unselected, Unselected,
/// The selection state of the element is indeterminate.
Indeterminate, Indeterminate,
/// The element is selected.
Selected, Selected,
} }
impl Selection { impl Selection {
/// Returns the inverse of the current selection status.
///
/// Indeterminate states become selected if inverted.
pub fn inverse(&self) -> Self { pub fn inverse(&self) -> Self {
match self { match self {
Self::Unselected | Self::Indeterminate => Self::Selected, Self::Unselected | Self::Indeterminate => Self::Selected,

View file

@ -14,7 +14,7 @@ fn elevated<E: Styled>(this: E, cx: &mut WindowContext, index: ElevationIndex) -
.shadow(index.shadow()) .shadow(index.shadow())
} }
/// Extends [`Styled`](gpui::Styled) with Zed specific styling methods. /// Extends [gpui::Styled] with Zed specific styling methods.
pub trait StyledExt: Styled + Sized { pub trait StyledExt: Styled + Sized {
/// Horizontally stacks elements. /// Horizontally stacks elements.
/// ///
@ -119,26 +119,32 @@ pub trait StyledExt: Styled + Sized {
self.border_color(cx.theme().colors().border_variant) self.border_color(cx.theme().colors().border_variant)
} }
/// Sets the background color to red for debugging when building UI.
fn debug_bg_red(self) -> Self { fn debug_bg_red(self) -> Self {
self.bg(hsla(0. / 360., 1., 0.5, 1.)) self.bg(hsla(0. / 360., 1., 0.5, 1.))
} }
/// Sets the background color to green for debugging when building UI.
fn debug_bg_green(self) -> Self { fn debug_bg_green(self) -> Self {
self.bg(hsla(120. / 360., 1., 0.5, 1.)) self.bg(hsla(120. / 360., 1., 0.5, 1.))
} }
/// Sets the background color to blue for debugging when building UI.
fn debug_bg_blue(self) -> Self { fn debug_bg_blue(self) -> Self {
self.bg(hsla(240. / 360., 1., 0.5, 1.)) self.bg(hsla(240. / 360., 1., 0.5, 1.))
} }
/// Sets the background color to yellow for debugging when building UI.
fn debug_bg_yellow(self) -> Self { fn debug_bg_yellow(self) -> Self {
self.bg(hsla(60. / 360., 1., 0.5, 1.)) self.bg(hsla(60. / 360., 1., 0.5, 1.))
} }
/// Sets the background color to cyan for debugging when building UI.
fn debug_bg_cyan(self) -> Self { fn debug_bg_cyan(self) -> Self {
self.bg(hsla(160. / 360., 1., 0.5, 1.)) self.bg(hsla(160. / 360., 1., 0.5, 1.))
} }
/// Sets the background color to magenta for debugging when building UI.
fn debug_bg_magenta(self) -> Self { fn debug_bg_magenta(self) -> Self {
self.bg(hsla(300. / 360., 1., 0.5, 1.)) self.bg(hsla(300. / 360., 1., 0.5, 1.))
} }