Styling for QR Code using theme framework

This commit is contained in:
Clark Moody 2024-02-01 16:23:41 -06:00 committed by Héctor Ramón Jiménez
parent 99a3f25c5f
commit c2d82833a0
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
4 changed files with 94 additions and 20 deletions

View file

@ -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;

20
style/src/qr_code.rs Normal file
View file

@ -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;
}

View file

@ -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<T: Fn(&Theme) -> 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<dyn qr_code::StyleSheet<Style = Theme>>),
}
impl<T: Fn(&Theme) -> qr_code::Appearance + 'static> From<T> 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<T: Fn(&Theme) -> 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 {