2024-09-24 20:57:53 -05:00
|
|
|
use iced::widget::{
|
|
|
|
|
center, column, pick_list, qr_code, row, slider, text, text_input, toggler,
|
|
|
|
|
};
|
2024-07-12 18:12:34 +02:00
|
|
|
use iced::{Center, Element, Theme};
|
2020-11-20 10:26:08 +01:00
|
|
|
|
2024-10-02 17:08:53 +02:00
|
|
|
use std::ops::RangeInclusive;
|
2024-09-24 20:57:53 -05:00
|
|
|
|
2020-11-20 10:26:08 +01:00
|
|
|
pub fn main() -> iced::Result {
|
2024-06-19 01:53:40 +02:00
|
|
|
iced::application(
|
2025-03-12 02:10:42 +01:00
|
|
|
QRGenerator::default,
|
2024-03-16 16:12:07 +01:00
|
|
|
QRGenerator::update,
|
|
|
|
|
QRGenerator::view,
|
|
|
|
|
)
|
|
|
|
|
.theme(QRGenerator::theme)
|
|
|
|
|
.run()
|
2020-11-20 10:26:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct QRGenerator {
|
|
|
|
|
data: String,
|
2024-02-09 23:57:11 +01:00
|
|
|
qr_code: Option<qr_code::Data>,
|
2024-10-02 17:08:53 +02:00
|
|
|
total_size: Option<f32>,
|
2025-09-08 05:16:20 +02:00
|
|
|
theme: Option<Theme>,
|
2020-11-20 10:26:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
enum Message {
|
|
|
|
|
DataChanged(String),
|
2024-10-02 17:08:53 +02:00
|
|
|
ToggleTotalSize(bool),
|
|
|
|
|
TotalSizeChanged(f32),
|
2024-02-02 10:45:37 -06:00
|
|
|
ThemeChanged(Theme),
|
2020-11-20 10:26:08 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-16 05:33:47 +01:00
|
|
|
impl QRGenerator {
|
2024-10-02 17:08:53 +02:00
|
|
|
const SIZE_RANGE: RangeInclusive<f32> = 200.0..=400.0;
|
|
|
|
|
|
2020-11-20 10:26:08 +01:00
|
|
|
fn update(&mut self, message: Message) {
|
|
|
|
|
match message {
|
|
|
|
|
Message::DataChanged(mut data) => {
|
|
|
|
|
data.truncate(100);
|
|
|
|
|
|
2023-01-20 13:46:09 +08:00
|
|
|
self.qr_code = if data.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
2024-02-09 23:57:11 +01:00
|
|
|
qr_code::Data::new(&data).ok()
|
2023-01-20 13:56:02 +08:00
|
|
|
};
|
2023-02-17 13:57:04 +01:00
|
|
|
|
2020-11-20 10:26:08 +01:00
|
|
|
self.data = data;
|
|
|
|
|
}
|
2024-10-02 17:08:53 +02:00
|
|
|
Message::ToggleTotalSize(enabled) => {
|
|
|
|
|
self.total_size = enabled.then_some(
|
|
|
|
|
Self::SIZE_RANGE.start()
|
|
|
|
|
+ (Self::SIZE_RANGE.end() - Self::SIZE_RANGE.start())
|
|
|
|
|
/ 2.0,
|
|
|
|
|
);
|
2024-09-24 20:57:53 -05:00
|
|
|
}
|
2024-10-02 17:08:53 +02:00
|
|
|
Message::TotalSizeChanged(total_size) => {
|
|
|
|
|
self.total_size = Some(total_size);
|
2024-09-24 20:57:53 -05:00
|
|
|
}
|
2024-02-02 10:45:37 -06:00
|
|
|
Message::ThemeChanged(theme) => {
|
2025-09-08 05:16:20 +02:00
|
|
|
self.theme = Some(theme);
|
2024-02-02 10:45:37 -06:00
|
|
|
}
|
2020-11-20 10:26:08 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-24 14:29:36 +02:00
|
|
|
fn view(&self) -> Element<'_, Message> {
|
2024-02-02 10:45:37 -06:00
|
|
|
let title = text("QR Code Generator").size(70);
|
2020-11-20 10:26:08 +01:00
|
|
|
|
2023-03-03 10:01:49 +03:00
|
|
|
let input =
|
|
|
|
|
text_input("Type the data of your QR code here...", &self.data)
|
2023-04-12 04:13:36 +02:00
|
|
|
.on_input(Message::DataChanged)
|
2023-03-03 10:01:49 +03:00
|
|
|
.size(30)
|
|
|
|
|
.padding(15);
|
2020-11-20 10:26:08 +01:00
|
|
|
|
2024-10-02 17:08:53 +02:00
|
|
|
let toggle_total_size = toggler(self.total_size.is_some())
|
|
|
|
|
.on_toggle(Message::ToggleTotalSize)
|
|
|
|
|
.label("Limit Total Size");
|
|
|
|
|
|
2024-02-02 10:45:37 -06:00
|
|
|
let choose_theme = row![
|
|
|
|
|
text("Theme:"),
|
2025-09-08 05:16:20 +02:00
|
|
|
pick_list(Theme::ALL, self.theme.as_ref(), Message::ThemeChanged)
|
|
|
|
|
.placeholder("Theme")
|
2024-02-02 10:45:37 -06:00
|
|
|
]
|
|
|
|
|
.spacing(10)
|
2024-07-12 18:12:34 +02:00
|
|
|
.align_y(Center);
|
2024-02-02 10:45:37 -06:00
|
|
|
|
2024-10-02 17:08:53 +02:00
|
|
|
let content = column![
|
|
|
|
|
title,
|
|
|
|
|
input,
|
|
|
|
|
row![toggle_total_size, choose_theme]
|
|
|
|
|
.spacing(20)
|
2025-07-18 03:53:59 +02:00
|
|
|
.align_y(Center),
|
|
|
|
|
self.total_size.map(|total_size| {
|
|
|
|
|
slider(Self::SIZE_RANGE, total_size, Message::TotalSizeChanged)
|
|
|
|
|
}),
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
})
|
2024-10-02 17:08:53 +02:00
|
|
|
]
|
|
|
|
|
.width(700)
|
|
|
|
|
.spacing(20)
|
|
|
|
|
.align_x(Center);
|
2020-11-20 10:26:08 +01:00
|
|
|
|
2024-05-03 09:11:46 +02:00
|
|
|
center(content).padding(20).into()
|
2020-11-20 10:26:08 +01:00
|
|
|
}
|
2024-02-02 10:45:37 -06:00
|
|
|
|
2025-09-08 05:16:20 +02:00
|
|
|
fn theme(&self) -> Option<Theme> {
|
2024-02-02 10:45:37 -06:00
|
|
|
self.theme.clone()
|
|
|
|
|
}
|
2020-11-20 10:26:08 +01:00
|
|
|
}
|