Implement `Selectable` for buttons (#3451)

Marshall Bowers created

This PR implements the `Selectable` trait for `ButtonLike`, `Button`,
and `IconButton`.

Release Notes:

- N/A

Change summary

crates/ui2/src/components/button/button.rs       | 28 ++++++++--------
crates/ui2/src/components/button/button_like.rs  | 22 +++++-------
crates/ui2/src/components/button/icon_button.rs  | 30 +++++++++---------
crates/ui2/src/components/stories/icon_button.rs |  6 +++
crates/ui2/src/selectable.rs                     |  8 ----
5 files changed, 44 insertions(+), 50 deletions(-)

Detailed changes

crates/ui2/src/components/button/button.rs 🔗

@@ -8,7 +8,6 @@ pub struct Button {
     base: ButtonLike,
     label: SharedString,
     label_color: Option<Color>,
-    selected: bool,
 }
 
 impl Button {
@@ -17,27 +16,18 @@ 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<Option<Color>>) -> Self {
         self.label_color = label_color.into();
         self
     }
 }
 
-impl Clickable for Button {
-    fn on_click(
-        mut self,
-        handler: impl Fn(&gpui::ClickEvent, &mut WindowContext) + 'static,
-    ) -> Self {
-        self.base = self.base.on_click(handler);
+impl Selectable for Button {
+    fn selected(mut self, selected: bool) -> Self {
+        self.base = self.base.selected(selected);
         self
     }
 }
@@ -49,6 +39,16 @@ impl Disableable for Button {
     }
 }
 
+impl Clickable for Button {
+    fn on_click(
+        mut self,
+        handler: impl Fn(&gpui::ClickEvent, &mut WindowContext) + 'static,
+    ) -> Self {
+        self.base = self.base.on_click(handler);
+        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

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<Box<dyn Fn(&mut WindowContext) -> AnyView>>,
     on_click: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
@@ -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<dyn Fn(&mut WindowContext) -> AnyView + 'static>,
-//     ) -> &mut Self {
-//         todo!()
-//     }
-// }
-
 impl ButtonCommon for ButtonLike {
     fn id(&self) -> &ElementId {
         &self.id

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

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()

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<dyn Fn(&mut WindowContext) -> AnyView + 'static>,
-    ) -> Self;
 }
 
 #[derive(Debug, Default, PartialEq, Eq, Hash, Clone, Copy)]