feat: set total size of QRCode
This commit is contained in:
parent
75548373a7
commit
a949e59f3e
2 changed files with 54 additions and 10 deletions
|
|
@ -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};
|
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 {
|
pub fn main() -> iced::Result {
|
||||||
iced::application(
|
iced::application(
|
||||||
"QR Code Generator - Iced",
|
"QR Code Generator - Iced",
|
||||||
|
|
@ -15,12 +21,16 @@ pub fn main() -> iced::Result {
|
||||||
struct QRGenerator {
|
struct QRGenerator {
|
||||||
data: String,
|
data: String,
|
||||||
qr_code: Option<qr_code::Data>,
|
qr_code: Option<qr_code::Data>,
|
||||||
|
display_with_fixed_size: bool,
|
||||||
|
fixed_size_slider_value: u8,
|
||||||
theme: Theme,
|
theme: Theme,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
enum Message {
|
enum Message {
|
||||||
DataChanged(String),
|
DataChanged(String),
|
||||||
|
SetDisplayWithFixedSize(bool),
|
||||||
|
FixedSizeSliderChanged(u8),
|
||||||
ThemeChanged(Theme),
|
ThemeChanged(Theme),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -38,6 +48,12 @@ impl QRGenerator {
|
||||||
|
|
||||||
self.data = data;
|
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) => {
|
Message::ThemeChanged(theme) => {
|
||||||
self.theme = theme;
|
self.theme = theme;
|
||||||
}
|
}
|
||||||
|
|
@ -61,11 +77,33 @@ impl QRGenerator {
|
||||||
.align_y(Center);
|
.align_y(Center);
|
||||||
|
|
||||||
let content = column![title, input, choose_theme]
|
let content = column![title, input, choose_theme]
|
||||||
.push_maybe(
|
.push(
|
||||||
self.qr_code
|
toggler(self.display_with_fixed_size)
|
||||||
.as_ref()
|
.on_toggle(Message::SetDisplayWithFixedSize)
|
||||||
.map(|data| qr_code(data).cell_size(10)),
|
.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)
|
.width(700)
|
||||||
.spacing(20)
|
.spacing(20)
|
||||||
.align_x(Center);
|
.align_x(Center);
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ use crate::Renderer;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
const DEFAULT_CELL_SIZE: u16 = 4;
|
const DEFAULT_CELL_SIZE: f32 = 4.0;
|
||||||
const QUIET_ZONE: usize = 2;
|
const QUIET_ZONE: usize = 2;
|
||||||
|
|
||||||
/// A type of matrix barcode consisting of squares arranged in a grid which
|
/// A type of matrix barcode consisting of squares arranged in a grid which
|
||||||
|
|
@ -66,7 +66,7 @@ where
|
||||||
Theme: Catalog,
|
Theme: Catalog,
|
||||||
{
|
{
|
||||||
data: &'a Data,
|
data: &'a Data,
|
||||||
cell_size: u16,
|
cell_size: f32,
|
||||||
class: Theme::Class<'a>,
|
class: Theme::Class<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -84,11 +84,17 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the size of the squares of the grid cell of the [`QRCode`].
|
/// 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.cell_size = cell_size;
|
||||||
self
|
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`].
|
/// Sets the style of the [`QRCode`].
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn style(mut self, style: impl Fn(&Theme) -> Style + 'a) -> Self
|
pub fn style(mut self, style: impl Fn(&Theme) -> Style + 'a) -> Self
|
||||||
|
|
@ -133,8 +139,8 @@ where
|
||||||
_renderer: &Renderer,
|
_renderer: &Renderer,
|
||||||
_limits: &layout::Limits,
|
_limits: &layout::Limits,
|
||||||
) -> layout::Node {
|
) -> layout::Node {
|
||||||
let side_length = (self.data.width + 2 * QUIET_ZONE) as f32
|
let side_length =
|
||||||
* f32::from(self.cell_size);
|
(self.data.width + 2 * QUIET_ZONE) as f32 * self.cell_size;
|
||||||
|
|
||||||
layout::Node::new(Size::new(side_length, side_length))
|
layout::Node::new(Size::new(side_length, side_length))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue