Implement `qr_code` example
This commit is contained in:
parent
3296be845c
commit
f259d44186
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "qr_code"
|
||||
version = "0.1.0"
|
||||
authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
|
||||
edition = "2018"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
iced = { path = "../..", features = ["qr_code"] }
|
|
@ -0,0 +1,18 @@
|
|||
## QR Code Generator
|
||||
|
||||
A basic QR code generator that showcases the `QRCode` widget.
|
||||
|
||||
The __[`main`]__ file contains all the code of the example.
|
||||
|
||||
<div align="center">
|
||||
<a href="https://gfycat.com/heavyexhaustedaracari">
|
||||
<img src="https://thumbs.gfycat.com/HeavyExhaustedAracari-size_restricted.gif">
|
||||
</a>
|
||||
</div>
|
||||
|
||||
You can run it with `cargo run`:
|
||||
```
|
||||
cargo run --package qr_code
|
||||
```
|
||||
|
||||
[`main`]: src/main.rs
|
|
@ -0,0 +1,81 @@
|
|||
use iced::qr_code::{self, QRCode};
|
||||
use iced::text_input::{self, TextInput};
|
||||
use iced::{
|
||||
Align, Column, Container, Element, Length, Sandbox, Settings, Text,
|
||||
};
|
||||
|
||||
pub fn main() -> iced::Result {
|
||||
QRGenerator::run(Settings::default())
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct QRGenerator {
|
||||
data: String,
|
||||
input: text_input::State,
|
||||
qr_code: Option<qr_code::State>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum Message {
|
||||
DataChanged(String),
|
||||
}
|
||||
|
||||
impl Sandbox for QRGenerator {
|
||||
type Message = Message;
|
||||
|
||||
fn new() -> Self {
|
||||
QRGenerator {
|
||||
qr_code: qr_code::State::new("").ok(),
|
||||
..Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn title(&self) -> String {
|
||||
String::from("QR Code Generator - Iced")
|
||||
}
|
||||
|
||||
fn update(&mut self, message: Message) {
|
||||
match message {
|
||||
Message::DataChanged(mut data) => {
|
||||
data.truncate(100);
|
||||
|
||||
self.qr_code = qr_code::State::new(&data).ok();
|
||||
self.data = data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn view(&mut self) -> Element<Message> {
|
||||
let title = Text::new("QR Code Generator")
|
||||
.size(70)
|
||||
.color([0.5, 0.5, 0.5]);
|
||||
|
||||
let input = TextInput::new(
|
||||
&mut self.input,
|
||||
"Type the data of your QR code here...",
|
||||
&self.data,
|
||||
Message::DataChanged,
|
||||
)
|
||||
.size(30)
|
||||
.padding(15);
|
||||
|
||||
let mut content = Column::new()
|
||||
.width(Length::Units(700))
|
||||
.spacing(20)
|
||||
.align_items(Align::Center)
|
||||
.push(title)
|
||||
.push(input);
|
||||
|
||||
if let Some(qr_code) = self.qr_code.as_mut() {
|
||||
content = content.push(QRCode::new(qr_code).cell_size(10));
|
||||
}
|
||||
|
||||
Container::new(content)
|
||||
.width(Length::Fill)
|
||||
.height(Length::Fill)
|
||||
.padding(20)
|
||||
.center_x()
|
||||
.center_y()
|
||||
.into()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue