aboutsummaryrefslogtreecommitdiff
path: root/example/src/main.rs
blob: 6b0a363e66eaea148259c7ca33615cb041643849 (plain)
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
use iced::{
    Element, Length, Task,
    widget::{center, column, text},
};
use iced_dialog::{button, dialog};

#[derive(Default)]
struct State {
    is_open: bool,
    action_text: String,
}

#[derive(Debug, Clone)]
enum Message {
    OpenDialog,
    Saved,
    Cancelled,
}

fn main() -> iced::Result {
    iced::run(State::update, State::view)
}

impl State {
    fn update(&mut self, message: Message) -> Task<Message> {
        match message {
            Message::OpenDialog => self.is_open = true,
            Message::Saved => {
                self.action_text = "User saved their work".to_owned();
                self.is_open = false;
            }
            Message::Cancelled => {
                self.action_text = "User cancelled the dialog".to_owned();
                self.is_open = false;
            }
        }
        Task::none()
    }

    fn view(&self) -> Element<'_, Message> {
        let base = center(
            column![
                text(&self.action_text),
                iced::widget::button("Open Dialog")
                    .on_press(Message::OpenDialog)
            ]
            .spacing(14.0),
        )
        .width(Length::Fill)
        .height(Length::Fill);

        let dialog_content = text("Do you want to save?");

        dialog(self.is_open, base, dialog_content)
            .title("Save")
            .push_button(button("Save").on_press(Message::Saved))
            .push_button(button("Cancel").on_press(Message::Cancelled))
            .width(350)
            .height(234)
            .into()
    }
}