summaryrefslogtreecommitdiffstats
path: root/examples/exit/src/main.rs
blob: c3a190d8a89d9783c14021b43dc2cb0181f5d9dc (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
use iced::{
    button, Alignment, Button, Column, Element, Sandbox, Settings, Text,
};

pub fn main() -> iced::Result {
    Exit::run(Settings::default())
}

#[derive(Default)]
struct Exit {
    show_confirm: bool,
    exit: bool,
    confirm_button: button::State,
    exit_button: button::State,
}

#[derive(Debug, Clone, Copy)]
enum Message {
    Confirm,
    Exit,
}

impl Sandbox for Exit {
    type Message = Message;

    fn new() -> Self {
        Self::default()
    }

    fn title(&self) -> String {
        String::from("Exit - Iced")
    }

    fn should_exit(&self) -> bool {
        self.exit
    }

    fn update(&mut self, message: Message) {
        match message {
            Message::Confirm => {
                self.exit = true;
            }
            Message::Exit => {
                self.show_confirm = true;
            }
        }
    }

    fn view(&mut self) -> Element<Message> {
        if self.show_confirm {
            Column::new()
                .padding(20)
                .align_items(Alignment::Center)
                .push(Text::new("Are you sure you want to exit?"))
                .push(
                    Button::new(
                        &mut self.confirm_button,
                        Text::new("Yes, exit now"),
                    )
                    .on_press(Message::Confirm),
                )
                .into()
        } else {
            Column::new()
                .padding(20)
                .align_items(Alignment::Center)
                .push(Text::new("Click the button to exit"))
                .push(
                    Button::new(&mut self.exit_button, Text::new("Exit"))
                        .on_press(Message::Exit),
                )
                .into()
        }
    }
}