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 7, 2024
1 parent 44407b8 commit 3d0867e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
31 changes: 31 additions & 0 deletions style/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions style/src/toggler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion widget/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down
14 changes: 8 additions & 6 deletions widget/src/toggler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub struct Toggler<
#[cfg(feature = "a11y")]
labeled_by_widget: Option<Vec<iced_accessibility::accesskit::NodeId>>,
is_toggled: bool,
on_toggle: Box<dyn Fn(bool) -> Message + 'a>,
on_toggle: Option<Box<dyn Fn(bool) -> Message + 'a>>,
label: Option<String>,
width: Length,
size: f32,
Expand Down Expand Up @@ -86,7 +86,7 @@ where
pub fn new<F>(
label: impl Into<Option<String>>,
is_toggled: bool,
f: F,
f: Option<F>,
) -> Self
where
F: 'a + Fn(bool) -> Message,
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3d0867e

Please sign in to comment.