From f09d9ef723d3f04051a1d06a00a3abf732440ece Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Fri, 1 Dec 2023 13:09:35 -0500 Subject: [PATCH 1/2] Clean up some `ButtonLike` doc comments --- crates/ui2/src/components/button/button_like.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/crates/ui2/src/components/button/button_like.rs b/crates/ui2/src/components/button/button_like.rs index 678e308f90d0b0c4df9a80ab774812f25afe32e1..4161cf6c7fd01ee8d9502b1a06de9c211946e7cc 100644 --- a/crates/ui2/src/components/button/button_like.rs +++ b/crates/ui2/src/components/button/button_like.rs @@ -5,20 +5,23 @@ use crate::h_stack; use crate::prelude::*; pub trait ButtonCommon: Clickable + Disableable { - /// A unique element id to help identify the button. + /// A unique element ID to identify the button. fn id(&self) -> &ElementId; + /// The visual style of the button. /// - /// Mosty commonly will be `ButtonStyle::Subtle`, or `ButtonStyle::Filled` + /// Mosty commonly will be [`ButtonStyle::Subtle`], or [`ButtonStyle::Filled`] /// for an emphasized button. fn style(self, style: ButtonStyle) -> Self; + /// The size of the button. /// /// Most buttons will use the default size. /// - /// ButtonSize can also be used to help build non-button elements + /// [`ButtonSize`] can also be used to help build non-button elements /// that are consistently sized with buttons. fn size(self, size: ButtonSize) -> Self; + /// The tooltip that shows when a user hovers over the button. /// /// Nearly all interactable elements should have a tooltip. Some example @@ -31,15 +34,18 @@ pub enum ButtonStyle { /// A filled button with a solid background color. Provides emphasis versus /// the more common subtle button. Filled, + /// 🚧 Under construction 🚧 /// /// Used to emphasize a button in some way, like a selected state, or a semantic /// coloring like an error or success button. Tinted, + /// The default button style, used for most buttons. Has a transparent background, /// but has a background color to indicate states like hover and active. #[default] Subtle, + /// Used for buttons that only change forground color on hover and active states. /// /// TODO: Better docs for this. From 12b58f5b60159b8e3b0c4248f64bc37098c6eb7a Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Fri, 1 Dec 2023 13:10:53 -0500 Subject: [PATCH 2/2] Add `selected_icon` to `IconButton` --- crates/ui2/src/components/button/icon_button.rs | 14 +++++++++++++- crates/ui2/src/components/stories/icon_button.rs | 8 ++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/ui2/src/components/button/icon_button.rs b/crates/ui2/src/components/button/icon_button.rs index a62832059d148c0d5ed9a1ec34aa6352695217f0..e405d2296ccb30e2491e63f76db162eb74c74dce 100644 --- a/crates/ui2/src/components/button/icon_button.rs +++ b/crates/ui2/src/components/button/icon_button.rs @@ -9,6 +9,7 @@ pub struct IconButton { icon: Icon, icon_size: IconSize, icon_color: Color, + selected_icon: Option, } impl IconButton { @@ -18,6 +19,7 @@ impl IconButton { icon, icon_size: IconSize::default(), icon_color: Color::Default, + selected_icon: None, } } @@ -31,6 +33,11 @@ impl IconButton { self } + pub fn selected_icon(mut self, icon: impl Into>) -> Self { + self.selected_icon = icon.into(); + self + } + pub fn action(self, action: Box) -> Self { self.on_click(move |_event, cx| cx.dispatch_action(action.boxed_clone())) } @@ -85,6 +92,11 @@ impl RenderOnce for IconButton { type Rendered = ButtonLike; fn render(self, _cx: &mut WindowContext) -> Self::Rendered { + let icon = self + .selected_icon + .filter(|_| self.base.selected) + .unwrap_or(self.icon); + let icon_color = if self.base.disabled { Color::Disabled } else if self.base.selected { @@ -94,7 +106,7 @@ impl RenderOnce for IconButton { }; self.base.child( - IconElement::new(self.icon) + IconElement::new(icon) .size(self.icon_size) .color(icon_color), ) diff --git a/crates/ui2/src/components/stories/icon_button.rs b/crates/ui2/src/components/stories/icon_button.rs index 04f8ab7c168b0558e6789ad11ed3b0761ff499b0..3c4d68f8aff5500225be0f6cf83c096a5c6c6dff 100644 --- a/crates/ui2/src/components/stories/icon_button.rs +++ b/crates/ui2/src/components/stories/icon_button.rs @@ -20,6 +20,14 @@ impl Render for IconButtonStory { .w_8() .child(IconButton::new("icon_a", Icon::Hash).selected(true)), ) + .child(Story::label("Selected with `selected_icon`")) + .child( + div().w_8().child( + IconButton::new("icon_a", Icon::AudioOn) + .selected(true) + .selected_icon(Icon::AudioOff), + ), + ) .child(Story::label("Disabled")) .child( div()