From c2d82833a0d56660b66bb06a9fb6360f425416af Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Thu, 1 Feb 2024 16:23:41 -0600 Subject: Styling for QR Code using theme framework --- style/src/lib.rs | 1 + style/src/qr_code.rs | 20 ++++++++++++++++++++ style/src/theme.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 style/src/qr_code.rs (limited to 'style') diff --git a/style/src/lib.rs b/style/src/lib.rs index e4097434..3c2865eb 100644 --- a/style/src/lib.rs +++ b/style/src/lib.rs @@ -24,6 +24,7 @@ pub mod menu; pub mod pane_grid; pub mod pick_list; pub mod progress_bar; +pub mod qr_code; pub mod radio; pub mod rule; pub mod scrollable; diff --git a/style/src/qr_code.rs b/style/src/qr_code.rs new file mode 100644 index 00000000..a024506c --- /dev/null +++ b/style/src/qr_code.rs @@ -0,0 +1,20 @@ +//! Change the appearance of a QR code. +use crate::core::Color; + +/// The appearance of a QR code. +#[derive(Debug, Clone, Copy)] +pub struct Appearance { + /// The color of the QR code data cells + pub cell: Color, + /// The color of the QR code background + pub background: Color, +} + +/// A set of rules that dictate the style of a QR code. +pub trait StyleSheet { + /// The supported style of the [`StyleSheet`]. + type Style: Default; + + /// Produces the style of a QR code. + fn appearance(&self, style: &Self::Style) -> Appearance; +} diff --git a/style/src/theme.rs b/style/src/theme.rs index e579a1c2..afb4d027 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -12,6 +12,7 @@ use crate::menu; use crate::pane_grid; use crate::pick_list; use crate::progress_bar; +use crate::qr_code; use crate::radio; use crate::rule; use crate::scrollable; @@ -956,6 +957,46 @@ impl progress_bar::Appearance> progress_bar::StyleSheet for T { } } +/// The style of a QR Code. +#[derive(Default)] +pub enum QRCode { + /// The default style. + #[default] + Default, + /// A custom style. + Custom(Box>), +} + +impl qr_code::Appearance + 'static> From for QRCode { + fn from(f: T) -> Self { + Self::Custom(Box::new(f)) + } +} + +impl qr_code::StyleSheet for Theme { + type Style = QRCode; + + fn appearance(&self, style: &Self::Style) -> qr_code::Appearance { + let palette = self.palette(); + + match style { + QRCode::Default => qr_code::Appearance { + cell: palette.text, + background: palette.background, + }, + QRCode::Custom(custom) => custom.appearance(self), + } + } +} + +impl qr_code::Appearance> qr_code::StyleSheet for T { + type Style = Theme; + + fn appearance(&self, style: &Self::Style) -> qr_code::Appearance { + (self)(style) + } +} + /// The style of a rule. #[derive(Default)] pub enum Rule { -- cgit From b535f7ae385658829f57f742eb1cc6f2f40cddf5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 9 Feb 2024 23:57:11 +0100 Subject: Invalidate `QRCode` cache on `Appearance` change --- style/src/qr_code.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'style') diff --git a/style/src/qr_code.rs b/style/src/qr_code.rs index a024506c..02c4709a 100644 --- a/style/src/qr_code.rs +++ b/style/src/qr_code.rs @@ -2,7 +2,7 @@ use crate::core::Color; /// The appearance of a QR code. -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq)] pub struct Appearance { /// The color of the QR code data cells pub cell: Color, -- cgit