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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use gtk::gio;
use gtk::glib::FromVariant;
use gtk::prelude::{ActionExt, ActionMapExt, StaticVariantType, ToVariant};
use std::marker::PhantomData;
pub mod traits;
pub use traits::*;
#[macro_export]
macro_rules! new_action_group {
($vis:vis $ty:ident, $name:expr) => {
$vis struct $ty;
impl relm4::actions::ActionGroupName for $ty {
const NAME: &'static str = $name;
}
};
}
#[macro_export]
macro_rules! new_stateless_action {
($vis:vis $ty:ident, $group:ty, $name:expr) => {
$vis struct $ty;
impl relm4::actions::ActionName for $ty {
type Group = $group;
type Target = ();
type State = ();
const NAME: &'static str = $name;
}
};
}
#[macro_export]
macro_rules! new_stateful_action {
($vis:vis $ty:ident, $group:ty, $name:expr, $value:ty, $state:ty) => {
$vis struct $ty;
impl relm4::actions::ActionName for $ty {
type Group = $group;
type Target = $value;
type State = $state;
const NAME: &'static str = $name;
}
};
}
#[derive(Debug)]
pub struct RelmAction<Name: ActionName> {
name: PhantomData<Name>,
action: gio::SimpleAction,
}
impl<Name: ActionName> RelmAction<Name>
where
Name::State: ToVariant + FromVariant,
Name::Target: ToVariant + FromVariant,
{
pub fn new_stateful_with_target_value<
Callback: Fn(&gio::SimpleAction, &mut Name::State, Name::Target) + 'static,
>(
start_value: &Name::State,
callback: Callback,
) -> Self {
let ty = Name::Target::static_variant_type();
let action =
gio::SimpleAction::new_stateful(Name::NAME, Some(&ty), &start_value.to_variant());
action.connect_activate(move |action, variant| {
let value = variant.unwrap().get().unwrap();
let mut state = action.state().unwrap().get().unwrap();
callback(action, &mut state, value);
action.set_state(&state.to_variant());
});
Self {
name: PhantomData,
action,
}
}
}
impl<Name: ActionName> RelmAction<Name>
where
Name::State: ToVariant + FromVariant,
Name::Target: EmptyType,
{
pub fn new_stateful<Callback: Fn(&gio::SimpleAction, &mut Name::State) + 'static>(
start_value: &Name::State,
callback: Callback,
) -> Self {
let action = gio::SimpleAction::new_stateful(Name::NAME, None, &start_value.to_variant());
action.connect_activate(move |action, _variant| {
let mut state = action.state().unwrap().get().unwrap();
callback(action, &mut state);
action.set_state(&state.to_variant());
});
Self {
name: PhantomData,
action,
}
}
}
impl<Name: ActionName> RelmAction<Name>
where
Name::State: EmptyType,
Name::Target: ToVariant + FromVariant,
{
pub fn new_with_target_value<Callback: Fn(&gio::SimpleAction, Name::Target) + 'static>(
callback: Callback,
) -> Self {
let ty = Name::Target::static_variant_type();
let action = gio::SimpleAction::new(Name::NAME, Some(&ty));
action.connect_activate(move |action, variant| {
let value = variant.unwrap().get().unwrap();
callback(action, value);
});
Self {
name: PhantomData,
action,
}
}
}
impl<Name: ActionName> RelmAction<Name>
where
Name::Target: EmptyType,
Name::State: EmptyType,
{
pub fn new_stateless<Callback: Fn(&gio::SimpleAction) + 'static>(callback: Callback) -> Self {
let action = gio::SimpleAction::new(Name::NAME, None);
action.connect_activate(move |action, _variant| {
callback(action);
});
Self {
name: PhantomData,
action,
}
}
}
impl<Name: ActionName> RelmAction<Name>
where
Name::Target: ToVariant + FromVariant,
{
pub fn to_menu_item_with_target_value(
label: &str,
target_value: &Name::Target,
) -> gio::MenuItem {
let menu_item = gio::MenuItem::new(Some(label), Some(&Name::action_name()));
menu_item.set_action_and_target_value(
Some(&Name::action_name()),
Some(&target_value.to_variant()),
);
menu_item
}
}
impl<Name: ActionName> RelmAction<Name>
where
Name::Target: EmptyType,
{
#[must_use]
pub fn to_menu_item(label: &str) -> gio::MenuItem {
gio::MenuItem::new(Some(label), Some(&Name::action_name()))
}
}
#[derive(Debug)]
pub struct RelmActionGroup<GroupName: ActionGroupName> {
group_name: PhantomData<GroupName>,
group: gio::SimpleActionGroup,
}
impl<GroupName: ActionGroupName> RelmActionGroup<GroupName> {
pub fn add_action<Name: ActionName>(&self, action: &RelmAction<Name>) {
self.group.add_action(&action.action);
}
#[must_use]
pub fn into_action_group(self) -> gio::SimpleActionGroup {
self.group
}
#[must_use]
pub fn new() -> Self {
Self {
group_name: PhantomData,
group: gio::SimpleActionGroup::new(),
}
}
}
impl<GroupName: ActionGroupName> Default for RelmActionGroup<GroupName> {
fn default() -> Self {
Self::new()
}
}