Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions examples/qr_code/src/main.rs
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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),
}

Expand All @@ -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;
}
Expand All @@ -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);
Expand Down
16 changes: 11 additions & 5 deletions widget/src/qr_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,7 +66,7 @@ where
Theme: Catalog,
{
data: &'a Data,
cell_size: u16,
cell_size: f32,
class: Theme::Class<'a>,
}

Expand All @@ -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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for reviewer: This is a breaking change to the public API. Let me know if that's a problem! We can easily keep the rest of the changes and revert this back to accepting a u16.

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
Expand Down Expand Up @@ -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))
}
Expand Down