Merge pull request #2606 from tvolk131/qr_code_fixed_size
feat: set total size of QRCode
This commit is contained in:
commit
4a080e27ab
2 changed files with 60 additions and 18 deletions
|
|
@ -1,6 +1,10 @@
|
||||||
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};
|
||||||
|
|
||||||
|
use std::ops::RangeInclusive;
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::application(
|
iced::application(
|
||||||
"QR Code Generator - Iced",
|
"QR Code Generator - Iced",
|
||||||
|
|
@ -15,16 +19,21 @@ 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>,
|
||||||
|
total_size: Option<f32>,
|
||||||
theme: Theme,
|
theme: Theme,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
enum Message {
|
enum Message {
|
||||||
DataChanged(String),
|
DataChanged(String),
|
||||||
|
ToggleTotalSize(bool),
|
||||||
|
TotalSizeChanged(f32),
|
||||||
ThemeChanged(Theme),
|
ThemeChanged(Theme),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl QRGenerator {
|
impl QRGenerator {
|
||||||
|
const SIZE_RANGE: RangeInclusive<f32> = 200.0..=400.0;
|
||||||
|
|
||||||
fn update(&mut self, message: Message) {
|
fn update(&mut self, message: Message) {
|
||||||
match message {
|
match message {
|
||||||
Message::DataChanged(mut data) => {
|
Message::DataChanged(mut data) => {
|
||||||
|
|
@ -38,6 +47,16 @@ impl QRGenerator {
|
||||||
|
|
||||||
self.data = data;
|
self.data = data;
|
||||||
}
|
}
|
||||||
|
Message::ToggleTotalSize(enabled) => {
|
||||||
|
self.total_size = enabled.then_some(
|
||||||
|
Self::SIZE_RANGE.start()
|
||||||
|
+ (Self::SIZE_RANGE.end() - Self::SIZE_RANGE.start())
|
||||||
|
/ 2.0,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Message::TotalSizeChanged(total_size) => {
|
||||||
|
self.total_size = Some(total_size);
|
||||||
|
}
|
||||||
Message::ThemeChanged(theme) => {
|
Message::ThemeChanged(theme) => {
|
||||||
self.theme = theme;
|
self.theme = theme;
|
||||||
}
|
}
|
||||||
|
|
@ -53,6 +72,10 @@ impl QRGenerator {
|
||||||
.size(30)
|
.size(30)
|
||||||
.padding(15);
|
.padding(15);
|
||||||
|
|
||||||
|
let toggle_total_size = toggler(self.total_size.is_some())
|
||||||
|
.on_toggle(Message::ToggleTotalSize)
|
||||||
|
.label("Limit Total Size");
|
||||||
|
|
||||||
let choose_theme = row![
|
let choose_theme = row![
|
||||||
text("Theme:"),
|
text("Theme:"),
|
||||||
pick_list(Theme::ALL, Some(&self.theme), Message::ThemeChanged,)
|
pick_list(Theme::ALL, Some(&self.theme), Message::ThemeChanged,)
|
||||||
|
|
@ -60,15 +83,26 @@ impl QRGenerator {
|
||||||
.spacing(10)
|
.spacing(10)
|
||||||
.align_y(Center);
|
.align_y(Center);
|
||||||
|
|
||||||
let content = column![title, input, choose_theme]
|
let content = column![
|
||||||
.push_maybe(
|
title,
|
||||||
self.qr_code
|
input,
|
||||||
.as_ref()
|
row![toggle_total_size, choose_theme]
|
||||||
.map(|data| qr_code(data).cell_size(10)),
|
.spacing(20)
|
||||||
)
|
.align_y(Center)
|
||||||
.width(700)
|
]
|
||||||
.spacing(20)
|
.push_maybe(self.total_size.map(|total_size| {
|
||||||
.align_x(Center);
|
slider(Self::SIZE_RANGE, total_size, Message::TotalSizeChanged)
|
||||||
|
}))
|
||||||
|
.push_maybe(self.qr_code.as_ref().map(|data| {
|
||||||
|
if let Some(total_size) = self.total_size {
|
||||||
|
qr_code(data).total_size(total_size)
|
||||||
|
} else {
|
||||||
|
qr_code(data).cell_size(10.0)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
.width(700)
|
||||||
|
.spacing(20)
|
||||||
|
.align_x(Center);
|
||||||
|
|
||||||
center(content).padding(20).into()
|
center(content).padding(20).into()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,15 +26,15 @@ use crate::core::mouse;
|
||||||
use crate::core::renderer::{self, Renderer as _};
|
use crate::core::renderer::{self, Renderer as _};
|
||||||
use crate::core::widget::tree::{self, Tree};
|
use crate::core::widget::tree::{self, Tree};
|
||||||
use crate::core::{
|
use crate::core::{
|
||||||
Color, Element, Layout, Length, Point, Rectangle, Size, Theme, Vector,
|
Color, Element, Layout, Length, Pixels, Point, Rectangle, Size, Theme,
|
||||||
Widget,
|
Vector, Widget,
|
||||||
};
|
};
|
||||||
use crate::Renderer;
|
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,8 +84,16 @@ 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: impl Into<Pixels>) -> Self {
|
||||||
self.cell_size = cell_size;
|
self.cell_size = cell_size.into().0;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the size of the entire [`QRCode`].
|
||||||
|
pub fn total_size(mut self, total_size: impl Into<Pixels>) -> Self {
|
||||||
|
self.cell_size =
|
||||||
|
total_size.into().0 / (self.data.width + 2 * QUIET_ZONE) as f32;
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -133,8 +141,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