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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use gtk::prelude::{BoxExt, ButtonExt, Cast, GtkWindowExt, OrientableExt};
use relm4::{gtk, send, AppUpdate, Model, RelmApp, RelmComponent, Sender, WidgetPlus, Widgets};
use relm4_components::alert::{AlertConfig, AlertModel, AlertMsg, AlertParent, AlertSettings};
use relm4_components::ParentWindow;

#[derive(Default)]
struct AppModel {
    counter: u8,
    alert_toggle: bool,
}

enum AppMsg {
    Increment,
    Decrement,
    CloseRequest,
    Save,
    Close,
    Ignore,
}

impl Model for AppModel {
    type Msg = AppMsg;
    type Widgets = AppWidgets;
    type Components = AppComponents;
}

impl AppUpdate for AppModel {
    fn update(&mut self, msg: AppMsg, components: &AppComponents, _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Increment => {
                self.counter = self.counter.wrapping_add(1);
            }
            AppMsg::Decrement => {
                self.counter = self.counter.wrapping_sub(1);
            }
            AppMsg::CloseRequest => {
                if self.counter == 42 {
                    return false;
                } else {
                    self.alert_toggle = !self.alert_toggle;
                    if self.alert_toggle {
                        components.dialog.send(AlertMsg::Show).unwrap();
                    } else {
                        components.second_dialog.send(AlertMsg::Show).unwrap();
                    }
                }
            }
            AppMsg::Save => {
                println!("* Open save dialog here *");
            }
            AppMsg::Close => {
                return false;
            }
            AppMsg::Ignore => (),
        }

        true
    }
}

struct FirstAlert {}

impl AlertConfig for FirstAlert {
    type Model = AppModel;

    fn alert_config(_model: &AppModel) -> AlertSettings {
        AlertSettings {
            text: "Do you want to quit without saving? (First alert)",
            secondary_text: Some("Your counter hasn't reached 42 yet"),
            confirm_label: "Close without saving",
            cancel_label: "Cancel",
            option_label: Some("Save"),
            is_modal: true,
            destructive_accept: true,
        }
    }
}

struct SecondAlert {}

impl AlertConfig for SecondAlert {
    type Model = AppModel;

    fn alert_config(_model: &AppModel) -> AlertSettings {
        AlertSettings {
            text: "Do you want to quit without saving? (Second alert)",
            secondary_text: Some("Your counter hasn't reached 42 yet"),
            confirm_label: "Close without saving",
            cancel_label: "Cancel",
            option_label: Some("Save"),
            is_modal: true,
            destructive_accept: true,
        }
    }
}

impl AlertParent for AppModel {
    fn confirm_msg() -> Self::Msg {
        AppMsg::Close
    }

    fn cancel_msg() -> Self::Msg {
        AppMsg::Ignore
    }

    fn option_msg() -> Self::Msg {
        AppMsg::Save
    }
}

impl ParentWindow for AppWidgets {
    fn parent_window(&self) -> Option<gtk::Window> {
        Some(self.main_window.clone().upcast::<gtk::Window>())
    }
}

#[derive(relm4::Components)]
pub struct AppComponents {
    dialog: RelmComponent<AlertModel<FirstAlert>, AppModel>,
    second_dialog: RelmComponent<AlertModel<SecondAlert>, AppModel>,
}

#[relm4::widget]
impl Widgets<AppModel, ()> for AppWidgets {
    view! {
        main_window = gtk::ApplicationWindow {
            set_title: Some("Simple app"),
            set_default_width: 300,
            set_default_height: 100,
            connect_close_request(sender) => move |_| {
                send!(sender, AppMsg::CloseRequest);
                gtk::Inhibit(true)
            },
            set_child = Some(&gtk::Box) {
                set_orientation: gtk::Orientation::Vertical,
                set_margin_all: 5,
                set_spacing: 5,

                append = &gtk::Button {
                    set_label: "Increment",
                    connect_clicked(sender) => move |_| {
                        send!(sender, AppMsg::Increment);
                    },
                },
                append = &gtk::Button {
                    set_label: "Decrement",
                    connect_clicked(sender) => move |_| {
                        send!(sender, AppMsg::Decrement);
                    },
                },
                append = &gtk::Label {
                    set_margin_all: 5,
                    set_label: watch! { &format!("Counter: {}", model.counter) },
                },
                append = &gtk::Button {
                    set_label: "Close",
                    connect_clicked(sender) => move |_| {
                        send!(sender, AppMsg::CloseRequest);
                    },
                },
            },
        }
    }
}

fn main() {
    let model = AppModel::default();
    let app = RelmApp::new(model);
    app.run();
}