summaryrefslogtreecommitdiffstats
path: root/examples/qr_code/src/main.rs
blob: 37b4855dae3cc8f0a319ad4f2a80315e9f3f15e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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()
    }
}