From 330a6252054b729e4d4d3f5a5d09f32e06cec282 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 5 Mar 2024 21:16:22 +0100 Subject: Simplify theming for `QRCode` widget --- widget/src/qr_code.rs | 68 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 21 deletions(-) (limited to 'widget/src/qr_code.rs') diff --git a/widget/src/qr_code.rs b/widget/src/qr_code.rs index eeb1526f..f13c9102 100644 --- a/widget/src/qr_code.rs +++ b/widget/src/qr_code.rs @@ -5,41 +5,37 @@ use crate::core::mouse; use crate::core::renderer::{self, Renderer as _}; use crate::core::widget::tree::{self, Tree}; use crate::core::{ - Element, Layout, Length, Point, Rectangle, Size, Vector, Widget, + Color, Element, Layout, Length, Point, Rectangle, Size, Vector, Widget, }; use crate::graphics::geometry::Renderer as _; +use crate::style::Theme; use crate::Renderer; use std::cell::RefCell; use thiserror::Error; -pub use crate::style::qr_code::{Appearance, StyleSheet}; - const DEFAULT_CELL_SIZE: u16 = 4; const QUIET_ZONE: usize = 2; /// A type of matrix barcode consisting of squares arranged in a grid which /// can be read by an imaging device, such as a camera. #[derive(Debug)] -pub struct QRCode<'a, Theme = crate::Theme> -where - Theme: StyleSheet, -{ +pub struct QRCode<'a, Theme = crate::Theme> { data: &'a Data, cell_size: u16, - style: Theme::Style, + style: fn(&Theme) -> Appearance, } -impl<'a, Theme> QRCode<'a, Theme> -where - Theme: StyleSheet, -{ +impl<'a, Theme> QRCode<'a, Theme> { /// Creates a new [`QRCode`] with the provided [`Data`]. - pub fn new(data: &'a Data) -> Self { + pub fn new(data: &'a Data) -> Self + where + Theme: Style, + { Self { data, cell_size: DEFAULT_CELL_SIZE, - style: Default::default(), + style: Theme::style(), } } @@ -50,15 +46,14 @@ where } /// Sets the style of the [`QRCode`]. - pub fn style(mut self, style: impl Into) -> Self { - self.style = style.into(); + pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { + self.style = style; self } } -impl<'a, Message, Theme> Widget for QRCode<'a, Theme> -where - Theme: StyleSheet, +impl<'a, Message, Theme> Widget + for QRCode<'a, Theme> { fn tag(&self) -> tree::Tag { tree::Tag::of::() @@ -102,7 +97,7 @@ where let bounds = layout.bounds(); let side_length = self.data.width + 2 * QUIET_ZONE; - let appearance = theme.appearance(&self.style); + let appearance = (self.style)(theme); let mut last_appearance = state.last_appearance.borrow_mut(); if Some(appearance) != *last_appearance { @@ -156,7 +151,7 @@ where impl<'a, Message, Theme> From> for Element<'a, Message, Theme, Renderer> where - Theme: StyleSheet + 'a, + Theme: 'a, { fn from(qr_code: QRCode<'a, Theme>) -> Self { Self::new(qr_code) @@ -330,3 +325,34 @@ impl From for Error { struct State { last_appearance: RefCell>, } + +/// The appearance of a QR code. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Appearance { + /// The color of the QR code data cells + pub cell: Color, + /// The color of the QR code background + 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; +} + +impl Style for Theme { + fn style() -> fn(&Self) -> Appearance { + default + } +} + +/// The default style of a [`QRCode`]. +pub fn default(theme: &Theme) -> Appearance { + let palette = theme.palette(); + + Appearance { + cell: palette.text, + background: palette.background, + } +} -- cgit 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 From 905f2160e6eb7504f52d9bd62c7bfa42c8ec2902 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 00:14:41 +0100 Subject: Move `Theme` type to `iced_core` --- widget/src/qr_code.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'widget/src/qr_code.rs') diff --git a/widget/src/qr_code.rs b/widget/src/qr_code.rs index b94e95f6..66513775 100644 --- a/widget/src/qr_code.rs +++ b/widget/src/qr_code.rs @@ -5,10 +5,10 @@ use crate::core::mouse; use crate::core::renderer::{self, Renderer as _}; use crate::core::widget::tree::{self, Tree}; use crate::core::{ - Color, Element, Layout, Length, Point, Rectangle, Size, Vector, Widget, + Color, Element, Layout, Length, Point, Rectangle, Size, Theme, Vector, + Widget, }; use crate::graphics::geometry::Renderer as _; -use crate::style::Theme; use crate::Renderer; use std::cell::RefCell; -- cgit From 833538ee7f3a60a839304762dfc29b0881d19094 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 20:11:32 +0100 Subject: Leverage `DefaultStyle` traits instead of `Default` --- widget/src/qr_code.rs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) (limited to 'widget/src/qr_code.rs') diff --git a/widget/src/qr_code.rs b/widget/src/qr_code.rs index 66513775..41bcb83e 100644 --- a/widget/src/qr_code.rs +++ b/widget/src/qr_code.rs @@ -30,12 +30,12 @@ impl<'a, Theme> QRCode<'a, Theme> { /// Creates a new [`QRCode`] with the provided [`Data`]. pub fn new(data: &'a Data) -> Self where - Style: Default, + Theme: DefaultStyle, { Self { data, cell_size: DEFAULT_CELL_SIZE, - style: Style::default(), + style: Theme::default_style(), } } @@ -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.0)(theme); + let appearance = (self.style)(theme); let mut last_appearance = state.last_appearance.borrow_mut(); if Some(appearance) != *last_appearance { @@ -336,26 +336,23 @@ pub struct Appearance { } /// The style of a [`QRCode`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style(fn(&Theme) -> Appearance); +pub type Style = fn(&Theme) -> Appearance; -impl Clone for Style { - fn clone(&self) -> Self { - *self - } +/// The default style of a [`QRCode`]. +pub trait DefaultStyle { + /// Returns the default style of a [`QRCode`]. + fn default_style() -> Style; } -impl Copy for Style {} - -impl Default for Style { - fn default() -> Self { - Style(default) +impl DefaultStyle for Theme { + fn default_style() -> Style { + default } } -impl From Appearance> for Style { - fn from(f: fn(&Theme) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style { + |appearance| *appearance } } -- cgit