From 9d53287341c600f4d5ec066cf6960bfcf158d67a Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Wed, 29 Nov 2023 18:46:41 -0500 Subject: [PATCH] Implement `Selectable` for buttons (#3451) This PR implements the `Selectable` trait for `ButtonLike`, `Button`, and `IconButton`. Release Notes: - N/A --- crates/ui2/src/components/button/button.rs | 30 +++++++++---------- .../ui2/src/components/button/button_like.rs | 22 ++++++-------- .../ui2/src/components/button/icon_button.rs | 30 +++++++++---------- .../ui2/src/components/stories/icon_button.rs | 6 ++++ crates/ui2/src/selectable.rs | 8 ----- 5 files changed, 45 insertions(+), 51 deletions(-) diff --git a/crates/ui2/src/components/button/button.rs b/crates/ui2/src/components/button/button.rs index 9328c0f3dd..b4e666e9ad 100644 --- a/crates/ui2/src/components/button/button.rs +++ b/crates/ui2/src/components/button/button.rs @@ -8,7 +8,6 @@ pub struct Button { base: ButtonLike, label: SharedString, label_color: Option, - selected: bool, } impl Button { @@ -17,21 +16,29 @@ impl Button { base: ButtonLike::new(id), label: label.into(), label_color: None, - selected: false, } } - pub fn selected(mut self, selected: bool) -> Self { - self.selected = selected; - self - } - pub fn color(mut self, label_color: impl Into>) -> Self { self.label_color = label_color.into(); self } } +impl Selectable for Button { + fn selected(mut self, selected: bool) -> Self { + self.base = self.base.selected(selected); + self + } +} + +impl Disableable for Button { + fn disabled(mut self, disabled: bool) -> Self { + self.base = self.base.disabled(disabled); + self + } +} + impl Clickable for Button { fn on_click( mut self, @@ -42,13 +49,6 @@ impl Clickable for Button { } } -impl Disableable for Button { - fn disabled(mut self, disabled: bool) -> Self { - self.base = self.base.disabled(disabled); - self - } -} - impl ButtonCommon for Button { fn id(&self) -> &ElementId { self.base.id() @@ -76,7 +76,7 @@ impl RenderOnce for Button { fn render(self, _cx: &mut WindowContext) -> Self::Rendered { let label_color = if self.base.disabled { Color::Disabled - } else if self.selected { + } else if self.base.selected { Color::Selected } else { Color::Default diff --git a/crates/ui2/src/components/button/button_like.rs b/crates/ui2/src/components/button/button_like.rs index 4dd31e26c7..3c36feb59f 100644 --- a/crates/ui2/src/components/button/button_like.rs +++ b/crates/ui2/src/components/button/button_like.rs @@ -172,6 +172,7 @@ pub struct ButtonLike { id: ElementId, pub(super) style: ButtonStyle2, pub(super) disabled: bool, + pub(super) selected: bool, size: ButtonSize2, tooltip: Option AnyView>>, on_click: Option>, @@ -184,6 +185,7 @@ impl ButtonLike { id: id.into(), style: ButtonStyle2::default(), disabled: false, + selected: false, size: ButtonSize2::Default, tooltip: None, children: SmallVec::new(), @@ -199,6 +201,13 @@ impl Disableable for ButtonLike { } } +impl Selectable for ButtonLike { + fn selected(mut self, selected: bool) -> Self { + self.selected = selected; + 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)); @@ -206,19 +215,6 @@ impl Clickable for ButtonLike { } } -// impl Selectable for ButtonLike { -// fn selected(&mut self, selected: bool) -> &mut Self { -// todo!() -// } - -// fn selected_tooltip( -// &mut self, -// tooltip: Box AnyView + 'static>, -// ) -> &mut Self { -// todo!() -// } -// } - impl ButtonCommon for ButtonLike { fn id(&self) -> &ElementId { &self.id diff --git a/crates/ui2/src/components/button/icon_button.rs b/crates/ui2/src/components/button/icon_button.rs index 526e2d2070..7746c3f8be 100644 --- a/crates/ui2/src/components/button/icon_button.rs +++ b/crates/ui2/src/components/button/icon_button.rs @@ -9,7 +9,6 @@ pub struct IconButton { icon: Icon, icon_size: IconSize, icon_color: Color, - selected: bool, } impl IconButton { @@ -19,15 +18,9 @@ impl IconButton { icon, icon_size: IconSize::default(), icon_color: Color::Default, - selected: false, } } - pub fn selected(mut self, selected: bool) -> Self { - self.selected = selected; - self - } - pub fn icon_size(mut self, icon_size: IconSize) -> Self { self.icon_size = icon_size; self @@ -43,6 +36,20 @@ impl IconButton { } } +impl Disableable for IconButton { + fn disabled(mut self, disabled: bool) -> Self { + self.base = self.base.disabled(disabled); + self + } +} + +impl Selectable for IconButton { + fn selected(mut self, selected: bool) -> Self { + self.base = self.base.selected(selected); + self + } +} + impl Clickable for IconButton { fn on_click( mut self, @@ -53,13 +60,6 @@ impl Clickable for IconButton { } } -impl Disableable for IconButton { - fn disabled(mut self, disabled: bool) -> Self { - self.base = self.base.disabled(disabled); - self - } -} - impl ButtonCommon for IconButton { fn id(&self) -> &ElementId { self.base.id() @@ -87,7 +87,7 @@ impl RenderOnce for IconButton { fn render(self, _cx: &mut WindowContext) -> Self::Rendered { let icon_color = if self.base.disabled { Color::Disabled - } else if self.selected { + } else if self.base.selected { Color::Selected } else { self.icon_color diff --git a/crates/ui2/src/components/stories/icon_button.rs b/crates/ui2/src/components/stories/icon_button.rs index e0d62af8a9..04f8ab7c16 100644 --- a/crates/ui2/src/components/stories/icon_button.rs +++ b/crates/ui2/src/components/stories/icon_button.rs @@ -20,6 +20,12 @@ impl Render for IconButtonStory { .w_8() .child(IconButton::new("icon_a", Icon::Hash).selected(true)), ) + .child(Story::label("Disabled")) + .child( + div() + .w_8() + .child(IconButton::new("icon_a", Icon::Hash).disabled(true)), + ) .child(Story::label("With `on_click`")) .child( div() diff --git a/crates/ui2/src/selectable.rs b/crates/ui2/src/selectable.rs index e12d26708e..34c66ab1fa 100644 --- a/crates/ui2/src/selectable.rs +++ b/crates/ui2/src/selectable.rs @@ -1,15 +1,7 @@ -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>, - ) -> Self; } #[derive(Debug, Default, PartialEq, Eq, Hash, Clone, Copy)]