From ff94ab4192ed54ea09de0107c3ae7993a41a9362 Mon Sep 17 00:00:00 2001 From: Antoine C Date: Sat, 7 Sep 2024 01:59:18 +0100 Subject: [PATCH] feat: allow toggler to be disabled --- style/src/theme.rs | 31 +++++++++++++++++++++++++++++++ style/src/toggler.rs | 5 +++++ widget/src/helpers.rs | 2 +- widget/src/toggler.rs | 14 ++++++++------ 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/style/src/theme.rs b/style/src/theme.rs index de7c2c7b5a..0652994eb4 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -745,6 +745,37 @@ impl toggler::StyleSheet for Theme { } } + fn disabled( + &self, + style: &Self::Style, + is_active: bool, + ) -> toggler::Appearance { + match style { + Toggler::Default => { + let palette = self.extended_palette(); + + toggler::Appearance { + background: if is_active { + palette.primary.strong.color + } else { + palette.background.strong.color + }, + background_border: None, + foreground: if is_active { + palette.primary.strong.text + } else { + palette.background.base.color + }, + foreground_border: None, + border_radius: Radius::from(8.0), + handle_radius: Radius::from(8.0), + handle_margin: 2.0, + } + } + Toggler::Custom(custom) => custom.active(self, is_active), + } + } + fn hovered( &self, style: &Self::Style, diff --git a/style/src/toggler.rs b/style/src/toggler.rs index 81c615fb2d..ff0dc0f3be 100644 --- a/style/src/toggler.rs +++ b/style/src/toggler.rs @@ -34,4 +34,9 @@ pub trait StyleSheet { /// /// [`Style`]: Self::Style fn hovered(&self, style: &Self::Style, is_active: bool) -> Appearance; + + /// Returns the disabled [`Appearance`] of the toggler for the provided [`Style`]. + /// + /// [`Style`]: Self::Style + fn disabled(&self, style: &Self::Style, is_active: bool) -> Appearance; } diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index bfc8abea91..75b0d03a05 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -202,7 +202,7 @@ where Renderer: core::text::Renderer, Theme: toggler::StyleSheet, { - Toggler::new(label, is_checked, f) + Toggler::new(label, is_checked, Some(f)) } /// Creates a new [`TextInput`]. diff --git a/widget/src/toggler.rs b/widget/src/toggler.rs index 20d34bf2d5..1c16150903 100644 --- a/widget/src/toggler.rs +++ b/widget/src/toggler.rs @@ -53,7 +53,7 @@ pub struct Toggler< #[cfg(feature = "a11y")] labeled_by_widget: Option>, is_toggled: bool, - on_toggle: Box Message + 'a>, + on_toggle: Option Message + 'a>>, label: Option, width: Length, size: f32, @@ -86,7 +86,7 @@ where pub fn new( label: impl Into>, is_toggled: bool, - f: F, + f: Option, ) -> Self where F: 'a + Fn(bool) -> Message, @@ -103,7 +103,7 @@ where #[cfg(feature = "a11y")] labeled_by_widget: None, is_toggled, - on_toggle: Box::new(f), + on_toggle: if f.is_some() { Some(Box::new(f.unwrap())) } else { None }, label, width: Length::Fill, size: Self::DEFAULT_SIZE, @@ -296,8 +296,8 @@ where | Event::Touch(touch::Event::FingerPressed { .. }) => { let mouse_over = cursor.is_over(layout.bounds()); - if mouse_over { - shell.publish((self.on_toggle)(!self.is_toggled)); + if self.on_toggle.is_some() && mouse_over { + shell.publish((self.on_toggle.as_ref().unwrap())(!self.is_toggled)); event::Status::Captured } else { @@ -352,7 +352,9 @@ where let bounds = toggler_layout.bounds(); let is_mouse_over = cursor.is_over(layout.bounds()); - let style = if is_mouse_over { + let style = if self.on_toggle.is_none() { + theme.disabled(&self.style, self.is_toggled) + } else if is_mouse_over { theme.hovered(&self.style, self.is_toggled) } else { theme.active(&self.style, self.is_toggled)