diff options
-rw-r--r-- | examples/qr_code/src/main.rs | 48 | ||||
-rw-r--r-- | widget/src/qr_code.rs | 16 |
2 files changed, 54 insertions, 10 deletions
diff --git a/examples/qr_code/src/main.rs b/examples/qr_code/src/main.rs index f1b654e0..638f31c0 100644 --- a/examples/qr_code/src/main.rs +++ b/examples/qr_code/src/main.rs @@ -1,6 +1,12 @@ -use iced::widget::{center, column, pick_list, qr_code, row, text, text_input}; +use iced::widget::{ + center, column, pick_list, qr_code, row, slider, text, text_input, toggler, +}; use iced::{Center, Element, Theme}; +const QR_CODE_EXACT_SIZE_MIN_PX: u32 = 200; +const QR_CODE_EXACT_SIZE_MAX_PX: u32 = 400; +const QR_CODE_EXACT_SIZE_SLIDER_STEPS: u8 = 100; + pub fn main() -> iced::Result { iced::application( "QR Code Generator - Iced", @@ -15,12 +21,16 @@ pub fn main() -> iced::Result { struct QRGenerator { data: String, qr_code: Option<qr_code::Data>, + display_with_fixed_size: bool, + fixed_size_slider_value: u8, theme: Theme, } #[derive(Debug, Clone)] enum Message { DataChanged(String), + SetDisplayWithFixedSize(bool), + FixedSizeSliderChanged(u8), ThemeChanged(Theme), } @@ -38,6 +48,12 @@ impl QRGenerator { self.data = data; } + Message::SetDisplayWithFixedSize(exact_size) => { + self.display_with_fixed_size = exact_size; + } + Message::FixedSizeSliderChanged(value) => { + self.fixed_size_slider_value = value; + } Message::ThemeChanged(theme) => { self.theme = theme; } @@ -61,11 +77,33 @@ impl QRGenerator { .align_y(Center); let content = column![title, input, choose_theme] - .push_maybe( - self.qr_code - .as_ref() - .map(|data| qr_code(data).cell_size(10)), + .push( + toggler(self.display_with_fixed_size) + .on_toggle(Message::SetDisplayWithFixedSize) + .label("Fixed Size"), ) + .push_maybe(self.display_with_fixed_size.then(|| { + slider( + 1..=QR_CODE_EXACT_SIZE_SLIDER_STEPS, + self.fixed_size_slider_value, + Message::FixedSizeSliderChanged, + ) + })) + .push_maybe(self.qr_code.as_ref().map(|data| { + if self.display_with_fixed_size { + // Convert the slider value to a size in pixels. + let qr_code_size_px = (self.fixed_size_slider_value as f32 + / QR_CODE_EXACT_SIZE_SLIDER_STEPS as f32) + * (QR_CODE_EXACT_SIZE_MAX_PX + - QR_CODE_EXACT_SIZE_MIN_PX) + as f32 + + QR_CODE_EXACT_SIZE_MIN_PX as f32; + + qr_code(data).total_size(qr_code_size_px) + } else { + qr_code(data).cell_size(10.0) + } + })) .width(700) .spacing(20) .align_x(Center); diff --git a/widget/src/qr_code.rs b/widget/src/qr_code.rs index 21dee6b1..6a8f5ed7 100644 --- a/widget/src/qr_code.rs +++ b/widget/src/qr_code.rs @@ -34,7 +34,7 @@ use crate::Renderer; use std::cell::RefCell; use thiserror::Error; -const DEFAULT_CELL_SIZE: u16 = 4; +const DEFAULT_CELL_SIZE: f32 = 4.0; const QUIET_ZONE: usize = 2; /// A type of matrix barcode consisting of squares arranged in a grid which @@ -66,7 +66,7 @@ where Theme: Catalog, { data: &'a Data, - cell_size: u16, + cell_size: f32, class: Theme::Class<'a>, } @@ -84,11 +84,17 @@ where } /// Sets the size of the squares of the grid cell of the [`QRCode`]. - pub fn cell_size(mut self, cell_size: u16) -> Self { + pub fn cell_size(mut self, cell_size: f32) -> Self { self.cell_size = cell_size; self } + /// Sets the size of the entire [`QRCode`]. + pub fn total_size(mut self, total_size: f32) -> Self { + self.cell_size = total_size / (self.data.width + 2 * QUIET_ZONE) as f32; + self + } + /// Sets the style of the [`QRCode`]. #[must_use] pub fn style(mut self, style: impl Fn(&Theme) -> Style + 'a) -> Self @@ -133,8 +139,8 @@ where _renderer: &Renderer, _limits: &layout::Limits, ) -> layout::Node { - let side_length = (self.data.width + 2 * QUIET_ZONE) as f32 - * f32::from(self.cell_size); + let side_length = + (self.data.width + 2 * QUIET_ZONE) as f32 * self.cell_size; layout::Node::new(Size::new(side_length, side_length)) } |