summaryrefslogtreecommitdiffstats
path: root/widget/src/checkbox.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-03-06 20:30:58 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-03-06 20:30:58 +0100
commit34e7c6593a9e0f56cee5db18b7258717cf6bc11b (patch)
tree7c65a58e9052f2f95a0025355679b13c7002eeab /widget/src/checkbox.rs
parent8a63774b24488f71147a728123551ae72c080d14 (diff)
downloadiced-34e7c6593a9e0f56cee5db18b7258717cf6bc11b.tar.gz
iced-34e7c6593a9e0f56cee5db18b7258717cf6bc11b.tar.bz2
iced-34e7c6593a9e0f56cee5db18b7258717cf6bc11b.zip
Use `Style` struct pattern instead of trait for all widgets
Diffstat (limited to 'widget/src/checkbox.rs')
-rw-r--r--widget/src/checkbox.rs40
1 files changed, 27 insertions, 13 deletions
diff --git a/widget/src/checkbox.rs b/widget/src/checkbox.rs
index 91838291..e4fc2232 100644
--- a/widget/src/checkbox.rs
+++ b/widget/src/checkbox.rs
@@ -53,13 +53,12 @@ pub struct Checkbox<
text_shaping: text::Shaping,
font: Option<Renderer::Font>,
icon: Icon<Renderer::Font>,
- style: fn(&Theme, Status) -> Appearance,
+ style: Style<Theme>,
}
impl<'a, Message, Theme, Renderer> Checkbox<'a, Message, Theme, Renderer>
where
Renderer: text::Renderer,
- Theme: Style,
{
/// The default size of a [`Checkbox`].
const DEFAULT_SIZE: f32 = 15.0;
@@ -72,7 +71,10 @@ where
/// It expects:
/// * the label of the [`Checkbox`]
/// * a boolean describing whether the [`Checkbox`] is checked or not
- pub fn new(label: impl Into<String>, is_checked: bool) -> Self {
+ pub fn new(label: impl Into<String>, is_checked: bool) -> Self
+ where
+ Style<Theme>: Default,
+ {
Checkbox {
is_checked,
on_toggle: None,
@@ -91,7 +93,7 @@ where
line_height: text::LineHeight::default(),
shaping: text::Shaping::Basic,
},
- style: Theme::style(),
+ style: Style::default(),
}
}
@@ -175,7 +177,7 @@ where
/// Sets the style of the [`Checkbox`].
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self {
- self.style = style.into();
+ self.style = Style(style);
self
}
}
@@ -301,7 +303,7 @@ where
Status::Active { is_checked }
};
- let appearance = (self.style)(theme, status);
+ let appearance = (self.style.0)(theme, status);
{
let layout = children.next().unwrap();
@@ -423,15 +425,27 @@ pub struct Appearance {
pub text_color: Option<Color>,
}
-/// A set of rules that dictate the style of a checkbox.
-pub trait Style {
- /// The supported style of the [`StyleSheet`].
- fn style() -> fn(&Self, Status) -> Appearance;
+/// The style of a [`Checkbox`].
+#[derive(Debug, PartialEq, Eq)]
+pub struct Style<Theme>(fn(&Theme, Status) -> Appearance);
+
+impl<Theme> Clone for Style<Theme> {
+ fn clone(&self) -> Self {
+ *self
+ }
+}
+
+impl<Theme> Copy for Style<Theme> {}
+
+impl Default for Style<Theme> {
+ fn default() -> Self {
+ Style(primary)
+ }
}
-impl Style for Theme {
- fn style() -> fn(&Self, Status) -> Appearance {
- primary
+impl<Theme> From<fn(&Theme, Status) -> Appearance> for Style<Theme> {
+ fn from(f: fn(&Theme, Status) -> Appearance) -> Self {
+ Style(f)
}
}