Document ui crate traits
This commit is contained in:
parent
46065c2621
commit
e020d7ca11
7 changed files with 21 additions and 5 deletions
|
@ -1,6 +1,6 @@
|
|||
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 {
|
||||
/// Sets the click handler that will fire whenever the element is clicked.
|
||||
fn on_click(self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self;
|
||||
|
|
|
@ -4,10 +4,12 @@ use smallvec::SmallVec;
|
|||
|
||||
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 {
|
||||
fn selected_style(self, style: ButtonStyle) -> Self;
|
||||
}
|
||||
|
||||
/// A common set of traits all buttons must implement.
|
||||
pub trait ButtonCommon: Clickable + Disableable {
|
||||
/// A unique element ID to identify the button.
|
||||
fn id(&self) -> &ElementId;
|
||||
|
|
|
@ -19,6 +19,7 @@ pub enum LineHeightStyle {
|
|||
UiLabel,
|
||||
}
|
||||
|
||||
/// A common set of traits all labels must implement.
|
||||
pub trait LabelCommon {
|
||||
fn size(self, size: LabelSize) -> Self;
|
||||
fn line_height_style(self, line_height_style: LineHeightStyle) -> Self;
|
||||
|
|
|
@ -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 {
|
||||
/// Sets whether the element is disabled.
|
||||
fn disabled(self, disabled: bool) -> Self;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
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 {
|
||||
/// Sets the width of the element.
|
||||
fn width(self, width: DefiniteLength) -> Self;
|
||||
|
|
|
@ -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 {
|
||||
/// Sets whether the element is selected.
|
||||
fn selected(self, selected: bool) -> Self;
|
||||
}
|
||||
|
||||
/// Represents the selection status of an element.
|
||||
#[derive(Debug, Default, PartialEq, Eq, Hash, Clone, Copy)]
|
||||
pub enum Selection {
|
||||
/// The element is not selected.
|
||||
#[default]
|
||||
Unselected,
|
||||
/// The selection state of the element is indeterminate.
|
||||
Indeterminate,
|
||||
/// The element is selected.
|
||||
Selected,
|
||||
}
|
||||
|
||||
impl Selection {
|
||||
/// Returns the inverse of the current selection status.
|
||||
///
|
||||
/// Indeterminate states become selected if inverted.
|
||||
pub fn inverse(&self) -> Self {
|
||||
match self {
|
||||
Self::Unselected | Self::Indeterminate => Self::Selected,
|
||||
|
|
|
@ -14,7 +14,7 @@ fn elevated<E: Styled>(this: E, cx: &mut WindowContext, index: ElevationIndex) -
|
|||
.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 {
|
||||
/// Horizontally stacks elements.
|
||||
///
|
||||
|
@ -119,26 +119,32 @@ pub trait StyledExt: Styled + Sized {
|
|||
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 {
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
self.bg(hsla(300. / 360., 1., 0.5, 1.))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue