From 3e2c517dd1b0639a06548d4f5d547c09277511a9 Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Wed, 29 Nov 2023 13:01:26 -0500 Subject: [PATCH] Add `Disableable` trait (#3439) This PR adds a new `Disableable` trait to use for elements that are capable of being disabled. Release Notes: - N/A --- crates/ui2/src/clickable.rs | 2 ++ crates/ui2/src/components/button2.rs | 24 ++++++++---------------- crates/ui2/src/disableable.rs | 5 +++++ crates/ui2/src/fixed.rs | 4 ++++ crates/ui2/src/prelude.rs | 2 ++ crates/ui2/src/selectable.rs | 4 ++++ crates/ui2/src/ui2.rs | 2 ++ 7 files changed, 27 insertions(+), 16 deletions(-) create mode 100644 crates/ui2/src/disableable.rs diff --git a/crates/ui2/src/clickable.rs b/crates/ui2/src/clickable.rs index b25f6b0e70b032cff97849631cf48432e45b4f6f..44f40b4cd4ccb98244a369b70639e3a768f4ddfa 100644 --- a/crates/ui2/src/clickable.rs +++ b/crates/ui2/src/clickable.rs @@ -1,5 +1,7 @@ use gpui::{ClickEvent, WindowContext}; +/// A trait for elements that can be clicked. 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; } diff --git a/crates/ui2/src/components/button2.rs b/crates/ui2/src/components/button2.rs index e9192afea0b59a62c889373cd471dd915ed0fe3f..cc1b7d5a27df3feaa6bb889cc863d1e4cf522138 100644 --- a/crates/ui2/src/components/button2.rs +++ b/crates/ui2/src/components/button2.rs @@ -201,13 +201,11 @@ impl ButtonSize2 { // children: SmallVec<[AnyElement; 2]>, // } -pub trait ButtonCommon: Clickable { +pub trait ButtonCommon: Clickable + Disableable { fn id(&self) -> &ElementId; fn style(self, style: ButtonStyle2) -> Self; - fn disabled(self, disabled: bool) -> Self; fn size(self, size: ButtonSize2) -> Self; fn tooltip(self, tooltip: impl Fn(&mut WindowContext) -> AnyView + 'static) -> Self; - // fn width(&mut self, width: DefiniteLength) -> &mut Self; } // pub struct LabelButton { @@ -266,14 +264,6 @@ pub struct ButtonLike { } impl ButtonLike { - pub fn children( - &mut self, - children: impl IntoIterator>, - ) -> &mut Self { - self.children = children.into_iter().map(Into::into).collect(); - self - } - pub fn new(id: impl Into) -> Self { Self { id: id.into(), @@ -287,6 +277,13 @@ impl ButtonLike { } } +impl Disableable for ButtonLike { + fn disabled(mut self, disabled: bool) -> Self { + self.disabled = disabled; + self + } +} + impl Clickable for ButtonLike { fn on_click(mut self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self { self.on_click = Some(Box::new(handler)); @@ -317,11 +314,6 @@ impl ButtonCommon for ButtonLike { self } - fn disabled(mut self, disabled: bool) -> Self { - self.disabled = disabled; - self - } - fn size(mut self, size: ButtonSize2) -> Self { self.size = size; self diff --git a/crates/ui2/src/disableable.rs b/crates/ui2/src/disableable.rs new file mode 100644 index 0000000000000000000000000000000000000000..f9b4e5ba910be1e65b6f2b106e9a45b707f20744 --- /dev/null +++ b/crates/ui2/src/disableable.rs @@ -0,0 +1,5 @@ +/// A trait for elements that can be disabled. +pub trait Disableable { + /// Sets whether the element is disabled. + fn disabled(self, disabled: bool) -> Self; +} diff --git a/crates/ui2/src/fixed.rs b/crates/ui2/src/fixed.rs index 5d8a9365ec7ca1394e35c44ff62f4a2eb4da6d6b..a2c3ed3edc819ed9a4bc4d4a33ff3b9494c11075 100644 --- a/crates/ui2/src/fixed.rs +++ b/crates/ui2/src/fixed.rs @@ -1,6 +1,10 @@ use gpui::DefiniteLength; +/// A trait for elements that have a fixed with. pub trait FixedWidth { + /// Sets the width of the element. fn width(self, width: DefiniteLength) -> Self; + + /// Sets the element's width to the full width of its container. fn full_width(self) -> Self; } diff --git a/crates/ui2/src/prelude.rs b/crates/ui2/src/prelude.rs index 2d5d98ab2a8e148298679b7ab64a69fd55088ec4..de7e0857a788ad38886c3181b63010abbe345a44 100644 --- a/crates/ui2/src/prelude.rs +++ b/crates/ui2/src/prelude.rs @@ -1,9 +1,11 @@ +pub use gpui::prelude::*; pub use gpui::{ div, Element, ElementId, InteractiveElement, ParentElement, RenderOnce, SharedString, Styled, ViewContext, WindowContext, }; pub use crate::clickable::*; +pub use crate::disableable::*; pub use crate::fixed::*; pub use crate::selectable::*; pub use crate::StyledExt; diff --git a/crates/ui2/src/selectable.rs b/crates/ui2/src/selectable.rs index be4cdfa6e6c00abba2fd50a002a4a2964cb32970..e12d26708e5c81c971eb6e900812777f11413176 100644 --- a/crates/ui2/src/selectable.rs +++ b/crates/ui2/src/selectable.rs @@ -1,7 +1,11 @@ use gpui::{AnyView, WindowContext}; +/// A trait for elements that can be selected. pub trait Selectable { + /// Sets whether the element is selected. fn selected(self, selected: bool) -> Self; + + /// Sets the tooltip that should be shown when the element is selected. fn selected_tooltip( self, tooltip: Box AnyView + 'static>, diff --git a/crates/ui2/src/ui2.rs b/crates/ui2/src/ui2.rs index 2d9eadb5724c80d1c18f7c0199cf5e2850f5cbc5..6c5669741be05d2bdbf895d991c5e6a8f301e02f 100644 --- a/crates/ui2/src/ui2.rs +++ b/crates/ui2/src/ui2.rs @@ -14,6 +14,7 @@ mod clickable; mod components; +mod disableable; mod fixed; pub mod prelude; mod selectable; @@ -23,6 +24,7 @@ pub mod utils; pub use clickable::*; pub use components::*; +pub use disableable::*; pub use fixed::*; pub use prelude::*; pub use selectable::*;