From 34e7c6593a9e0f56cee5db18b7258717cf6bc11b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 6 Mar 2024 20:30:58 +0100 Subject: Use `Style` struct pattern instead of trait for all widgets --- widget/src/qr_code.rs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'widget/src/qr_code.rs') diff --git a/widget/src/qr_code.rs b/widget/src/qr_code.rs index f13c9102..b94e95f6 100644 --- a/widget/src/qr_code.rs +++ b/widget/src/qr_code.rs @@ -23,19 +23,19 @@ const QUIET_ZONE: usize = 2; pub struct QRCode<'a, Theme = crate::Theme> { data: &'a Data, cell_size: u16, - style: fn(&Theme) -> Appearance, + style: Style, } impl<'a, Theme> QRCode<'a, Theme> { /// Creates a new [`QRCode`] with the provided [`Data`]. pub fn new(data: &'a Data) -> Self where - Theme: Style, + Style: Default, { Self { data, cell_size: DEFAULT_CELL_SIZE, - style: Theme::style(), + style: Style::default(), } } @@ -47,7 +47,7 @@ impl<'a, Theme> QRCode<'a, Theme> { /// Sets the style of the [`QRCode`]. pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { - self.style = style; + self.style = style.into(); self } } @@ -97,7 +97,7 @@ impl<'a, Message, Theme> Widget let bounds = layout.bounds(); let side_length = self.data.width + 2 * QUIET_ZONE; - let appearance = (self.style)(theme); + let appearance = (self.style.0)(theme); let mut last_appearance = state.last_appearance.borrow_mut(); if Some(appearance) != *last_appearance { @@ -335,15 +335,27 @@ pub struct Appearance { pub background: Color, } -/// The definiton of the default style of a [`QRCode`]. -pub trait Style { - /// Returns the default style of a [`QRCode`]. - fn style() -> fn(&Self) -> Appearance; +/// The style of a [`QRCode`]. +#[derive(Debug, PartialEq, Eq)] +pub struct Style(fn(&Theme) -> Appearance); + +impl Clone for Style { + fn clone(&self) -> Self { + *self + } +} + +impl Copy for Style {} + +impl Default for Style { + fn default() -> Self { + Style(default) + } } -impl Style for Theme { - fn style() -> fn(&Self) -> Appearance { - default +impl From Appearance> for Style { + fn from(f: fn(&Theme) -> Appearance) -> Self { + Style(f) } } -- cgit