Skip to content

Commit

Permalink
feat: allow toggler to be disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
acolombier committed Sep 8, 2024
1 parent 71cd25c commit 856cc5b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion iced
24 changes: 24 additions & 0 deletions src/theme/style/iced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
}
}
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/widget/settings/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
}
2 changes: 1 addition & 1 deletion src/widget/toggler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use iced_core::text;
pub fn toggler<'a, Message, Theme: iced_widget::toggler::StyleSheet, Renderer>(
label: impl Into<Option<String>>,
is_checked: bool,
f: impl Fn(bool) -> Message + 'a,
f: Option<impl Fn(bool) -> Message + 'a>,
) -> widget::Toggler<'a, Message, Theme, Renderer>
where
Renderer: iced_core::Renderer + text::Renderer,
Expand Down

0 comments on commit 856cc5b

Please sign in to comment.