summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-03-05 21:16:22 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-03-05 21:16:22 +0100
commit330a6252054b729e4d4d3f5a5d09f32e06cec282 (patch)
tree9dd78f556c514142de71fe5fae37622be3b1eb07
parent704ec9cb5cdc1d44f2df2f15de700b0af330b1d7 (diff)
downloadiced-330a6252054b729e4d4d3f5a5d09f32e06cec282.tar.gz
iced-330a6252054b729e4d4d3f5a5d09f32e06cec282.tar.bz2
iced-330a6252054b729e4d4d3f5a5d09f32e06cec282.zip
Simplify theming for `QRCode` widget
-rw-r--r--style/src/lib.rs1
-rw-r--r--style/src/qr_code.rs20
-rw-r--r--style/src/theme.rs41
-rw-r--r--widget/src/helpers.rs2
-rw-r--r--widget/src/qr_code.rs68
5 files changed, 48 insertions, 84 deletions
diff --git a/style/src/lib.rs b/style/src/lib.rs
index 5906fa0e..7a6be69a 100644
--- a/style/src/lib.rs
+++ b/style/src/lib.rs
@@ -21,7 +21,6 @@ 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 slider;
diff --git a/style/src/qr_code.rs b/style/src/qr_code.rs
deleted file mode 100644
index 02c4709a..00000000
--- a/style/src/qr_code.rs
+++ /dev/null
@@ -1,20 +0,0 @@
-//! Change the appearance of a QR code.
-use crate::core::Color;
-
-/// 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,
-}
-
-/// 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 57dfe3c7..9038d730 100644
--- a/style/src/theme.rs
+++ b/style/src/theme.rs
@@ -9,7 +9,6 @@ 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::slider;
@@ -662,46 +661,6 @@ 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 {
diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs
index 2153ed50..8f0dcd5c 100644
--- a/widget/src/helpers.rs
+++ b/widget/src/helpers.rs
@@ -396,7 +396,7 @@ where
#[cfg(feature = "qr_code")]
pub fn qr_code<Theme>(data: &crate::qr_code::Data) -> crate::QRCode<'_, Theme>
where
- Theme: crate::qr_code::StyleSheet,
+ Theme: crate::qr_code::Style,
{
crate::QRCode::new(data)
}
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<Theme::Style>) -> Self {
- self.style = style.into();
+ pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self {
+ self.style = style;
self
}
}
-impl<'a, Message, Theme> Widget<Message, Theme, Renderer> for QRCode<'a, Theme>
-where
- Theme: StyleSheet,
+impl<'a, Message, Theme> Widget<Message, Theme, Renderer>
+ for QRCode<'a, Theme>
{
fn tag(&self) -> tree::Tag {
tree::Tag::of::<State>()
@@ -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<QRCode<'a, Theme>>
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<qrcode::types::QrError> for Error {
struct State {
last_appearance: RefCell<Option<Appearance>>,
}
+
+/// 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,
+ }
+}