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
use gtk::prelude::{DialogExt, GtkWindowExt, WidgetExt};
use relm4::{gtk, send, ComponentUpdate, Model, Sender};
use crate::ParentWindow;
use std::marker::PhantomData;
pub struct AlertSettings {
pub text: &'static str,
pub secondary_text: Option<&'static str>,
pub is_modal: bool,
pub destructive_accept: bool,
pub confirm_label: &'static str,
pub cancel_label: &'static str,
pub option_label: Option<&'static str>,
}
pub struct AlertModel<Conf: AlertConfig> {
settings: AlertSettings,
is_active: bool,
_conf_provider: PhantomData<*const Conf>,
}
pub enum AlertMsg {
Show,
#[doc(hidden)]
Response(gtk::ResponseType),
}
impl<C: AlertConfig> Model for AlertModel<C> {
type Msg = AlertMsg;
type Widgets = AlertWidgets;
type Components = ();
}
pub trait AlertConfig {
type Model: Model;
fn alert_config(model: &Self::Model) -> AlertSettings;
}
pub trait AlertParent: Model
where
Self::Widgets: ParentWindow,
{
fn confirm_msg() -> Self::Msg;
fn cancel_msg() -> Self::Msg;
fn option_msg() -> Self::Msg;
}
impl<ParentModel, Conf> ComponentUpdate<ParentModel> for AlertModel<Conf>
where
ParentModel: AlertParent,
ParentModel::Widgets: ParentWindow,
Conf: AlertConfig<Model = ParentModel>,
{
fn init_model(parent_model: &ParentModel) -> Self {
AlertModel {
settings: Conf::alert_config(parent_model),
is_active: false,
_conf_provider: PhantomData,
}
}
fn update(
&mut self,
msg: AlertMsg,
_components: &(),
_sender: Sender<AlertMsg>,
parent_sender: Sender<ParentModel::Msg>,
) {
match msg {
AlertMsg::Show => {
self.is_active = true;
}
AlertMsg::Response(ty) => {
self.is_active = false;
parent_sender
.send(match ty {
gtk::ResponseType::Accept => ParentModel::confirm_msg(),
gtk::ResponseType::Other(_) => ParentModel::option_msg(),
_ => ParentModel::cancel_msg(),
})
.unwrap();
}
}
}
}
#[relm4::widget(pub)]
impl<ParentModel, Conf> relm4::Widgets<AlertModel<Conf>, ParentModel> for AlertWidgets
where
ParentModel: AlertParent,
ParentModel::Widgets: ParentWindow,
Conf: AlertConfig,
{
view! {
dialog = gtk::MessageDialog {
set_transient_for: parent!(parent_widgets.parent_window().as_ref()),
set_message_type: gtk::MessageType::Question,
set_visible: watch!(model.is_active),
connect_response(sender) => move |_, response| {
send!(sender, AlertMsg::Response(response));
},
set_text: Some(model.settings.text),
set_secondary_text: model.settings.secondary_text,
set_modal: model.settings.is_modal,
add_button: args!(model.settings.confirm_label, gtk::ResponseType::Accept),
add_button: args!(model.settings.cancel_label, gtk::ResponseType::Cancel),
}
}
fn post_init() {
if let Some(option_label) = &model.settings.option_label {
dialog.add_button(option_label, gtk::ResponseType::Other(0));
}
if model.settings.destructive_accept {
let accept_widget = dialog
.widget_for_response(gtk::ResponseType::Accept)
.expect("No button for accept response set");
accept_widget.add_css_class("destructive-action");
}
}
}