From 2e11d62de5ac6c120891b27061c1d97c58f5955d Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Wed, 13 Sep 2023 17:26:19 +0200 Subject: [PATCH] feat(widget): custom style variant for button widget --- src/theme/style/button.rs | 26 +++++++++++++++++++++++++- src/widget/button/icon.rs | 2 -- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/theme/style/button.rs b/src/theme/style/button.rs index 0d092f36..f41237a2 100644 --- a/src/theme/style/button.rs +++ b/src/theme/style/button.rs @@ -9,8 +9,14 @@ use palette::{rgb::Rgb, Alpha}; use crate::widget::button::{Appearance, StyleSheet}; -#[derive(Copy, Clone, Debug, Default)] +#[derive(Default)] pub enum Button { + Custom { + active: Box Appearance>, + disabled: Box Appearance>, + hovered: Box Appearance>, + pressed: Box Appearance>, + }, Destructive, Link, Icon, @@ -69,6 +75,8 @@ pub fn appearance( appearance.text_color = cosmic.accent.base.into(); corner_radii = &cosmic.corner_radii.radius_0; } + + Button::Custom { .. } => (), } appearance.border_radius = (*corner_radii).into(); @@ -87,6 +95,10 @@ impl StyleSheet for crate::Theme { type Style = Button; fn active(&self, focused: bool, style: &Self::Style) -> Appearance { + if let Button::Custom { active, .. } = style { + return active(focused, self); + } + appearance(self, focused, style, |component| { ( component.base.into(), @@ -97,6 +109,10 @@ impl StyleSheet for crate::Theme { } fn disabled(&self, style: &Self::Style) -> Appearance { + if let Button::Custom { disabled, .. } = style { + return disabled(self); + } + appearance(self, false, style, |component| { let mut background = Color::from(component.base); background.a *= 0.5; @@ -113,6 +129,10 @@ impl StyleSheet for crate::Theme { } fn hovered(&self, focused: bool, style: &Self::Style) -> Appearance { + if let Button::Custom { hovered, .. } = style { + return hovered(focused, self); + } + appearance(self, focused, style, |component| { ( component.hover.into(), @@ -123,6 +143,10 @@ impl StyleSheet for crate::Theme { } fn pressed(&self, focused: bool, style: &Self::Style) -> Appearance { + if let Button::Custom { pressed, .. } = style { + return pressed(focused, self); + } + appearance(self, focused, style, |component| { ( component.pressed.into(), diff --git a/src/widget/button/icon.rs b/src/widget/button/icon.rs index 1fcd6455..9a70a510 100644 --- a/src/widget/button/icon.rs +++ b/src/widget/button/icon.rs @@ -162,8 +162,6 @@ impl<'a, Message: Clone + 'static> From> for Element<'a, Mes let button = if builder.variant.vertical { crate::widget::column::with_children(content) .padding(builder.padding) - // .width(builder.width) - // .height(builder.height) .spacing(builder.spacing) .align_items(Alignment::Center) .apply(button)