From 856cc5b14d76d20f8d00ff2e5a74505b8a418c04 Mon Sep 17 00:00:00 2001 From: Antoine C Date: Sat, 7 Sep 2024 01:59:45 +0100 Subject: [PATCH] feat: allow toggler to be disabled --- iced | 2 +- src/theme/style/iced.rs | 24 ++++++++++++++++++++++++ src/widget/settings/item.rs | 2 +- src/widget/toggler.rs | 2 +- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/iced b/iced index 44407b80435..1e48a5c8cf8 160000 --- a/iced +++ b/iced @@ -1 +1 @@ -Subproject commit 44407b80435788a612159613829d41a80782de9a +Subproject commit 1e48a5c8cf83bdb38d22494d46d91cac05f50a41 diff --git a/src/theme/style/iced.rs b/src/theme/style/iced.rs index 6c79823ea8e..66943d503ea 100644 --- a/src/theme/style/iced.rs +++ b/src/theme/style/iced.rs @@ -5,6 +5,7 @@ use crate::theme::{CosmicComponent, Theme, TRANSPARENT_COMPONENT}; use cosmic_theme::composite::over; +use iced::color; use iced_core::{Background, Border, Color, Shadow, Vector}; use iced_style::application; use iced_style::button as iced_button; @@ -858,6 +859,29 @@ impl toggler::StyleSheet for Theme { ..self.active(style, is_active) } } + + fn disabled(&self, style: &Self::Style, is_active: bool) -> toggler::Appearance { + let theme = self.cosmic(); + const HANDLE_MARGIN: f32 = 2.0; + let background: Color = if is_active { + theme.accent.base.into() + } else { + theme.palette.neutral_5.into() + }; + let foreground: Color = theme.palette.neutral_2.into(); + toggler::Appearance { + background: Color::from_rgba(background.r, background.g, background.b, 0.5), + background_border: None, + foreground: Color::from_rgba(foreground.r, foreground.g, foreground.b, 0.5), + foreground_border: None, + border_radius: theme.radius_xl().into(), + handle_radius: theme + .radius_xl() + .map(|x| (x - HANDLE_MARGIN).max(0.0)) + .into(), + handle_margin: HANDLE_MARGIN, + } + } } /* diff --git a/src/widget/settings/item.rs b/src/widget/settings/item.rs index 3de4975f09f..c0a6eb84bae 100644 --- a/src/widget/settings/item.rs +++ b/src/widget/settings/item.rs @@ -123,6 +123,6 @@ impl<'a, Message: 'static> Item<'a, Message> { is_checked: bool, message: impl Fn(bool) -> Message + 'static, ) -> Row<'a, Message> { - self.control(crate::widget::toggler(None, is_checked, message)) + self.control(crate::widget::toggler(None, is_checked, Some(message))) } } diff --git a/src/widget/toggler.rs b/src/widget/toggler.rs index 0831c9c4ee7..9fa74d58d66 100644 --- a/src/widget/toggler.rs +++ b/src/widget/toggler.rs @@ -7,7 +7,7 @@ use iced_core::text; pub fn toggler<'a, Message, Theme: iced_widget::toggler::StyleSheet, Renderer>( label: impl Into>, is_checked: bool, - f: impl Fn(bool) -> Message + 'a, + f: Option Message + 'a>, ) -> widget::Toggler<'a, Message, Theme, Renderer> where Renderer: iced_core::Renderer + text::Renderer,