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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use gtk::glib;
use gtk::prelude::{ApplicationExt, ApplicationExtManual, Cast, GtkApplicationExt, IsA, WidgetExt};
use std::fmt::Debug;
use crate::component::{AsyncComponent, AsyncComponentBuilder, AsyncComponentController};
use crate::runtime_util::shutdown_all;
use crate::{Component, ComponentBuilder, ComponentController, MessageBroker, RUNTIME};
use std::cell::Cell;
/// A convenience wrapper around [`gtk::Application`] that launches a nrelm
/// component as the main window of the application.
///
/// `M` is the input message type of the root component.
#[derive(Debug)]
pub struct RelmApp<M: Debug + 'static> {
app: gtk::Application,
broker: Option<&'static MessageBroker<M>>,
args: Option<Vec<String>>,
visible: bool,
}
impl<M: Debug + 'static> RelmApp<M> {
/// Creates a new [`RelmApp`] with the given application ID.
///
/// This also initializes GTK and registers the application.
#[must_use]
pub fn new(app_id: &str) -> Self {
crate::init();
let app = crate::main_application();
app.set_application_id(Some(app_id));
Self {
app,
broker: None,
args: None,
visible: true,
}
}
/// Creates a new [`RelmApp`] from an existing [`gtk::Application`].
///
/// The application is registered as the main application, so that
/// [`crate::main_application`] returns it.
pub fn from_app(app: impl IsA<gtk::Application>) -> Self {
let app = app.upcast();
crate::set_main_application(app.clone());
Self {
app,
broker: None,
args: None,
visible: true,
}
}
/// Uses a global [`MessageBroker`] as the input channel for the root
/// component.
///
/// Messages sent to the broker are forwarded to the component that is
/// launched by [`run`](Self::run).
#[must_use]
pub fn with_broker(mut self, broker: &'static MessageBroker<M>) -> Self {
self.broker = Some(broker);
self
}
/// Passes command line arguments to the application.
#[must_use]
pub fn with_args(mut self, args: Vec<String>) -> Self {
self.args = Some(args);
self
}
/// Controls whether the main window is shown when the application is
/// activated. Defaults to `true`.
#[must_use]
pub fn visible_on_activate(mut self, visible: bool) -> Self {
self.visible = visible;
self
}
/// Sets whether multiple instances of the application may run at the same
/// time.
pub fn allow_multiple_instances(&self, allow: bool) {
let mut flags = self.app.flags();
if allow {
flags |= gtk::gio::ApplicationFlags::NON_UNIQUE;
} else {
flags &= !gtk::gio::ApplicationFlags::NON_UNIQUE;
}
self.app.set_flags(flags);
}
/// Sets global CSS styles with a custom priority.
///
/// This method is deprecated; use [`crate::set_global_css_with_priority`]
/// instead.
#[deprecated(note = "Use `nrelm::set_global_css_with_priority` instead")]
pub fn set_global_css_with_priority(&self, style_data: &str, priority: u32) {
crate::set_global_css_with_priority(style_data, priority);
}
/// Sets global CSS styles with the default application priority.
///
/// This method is deprecated; use [`crate::set_global_css`] instead.
#[deprecated(note = "Use `nrelm::set_global_css` instead")]
pub fn set_global_css(&self, style_data: &str) {
crate::set_global_css(style_data);
}
/// Reads CSS styles from a file and applies them with a custom priority.
///
/// This method is deprecated; use
/// [`crate::set_global_css_from_file_with_priority`] instead.
#[deprecated(note = "Use `nrelm::set_global_css_from_file_with_priority` instead")]
pub fn set_global_css_from_file_with_priority<P: AsRef<std::path::Path>>(
&self,
path: P,
priority: u32,
) -> Result<(), std::io::Error> {
crate::set_global_css_from_file_with_priority(path, priority)
}
/// Reads CSS styles from a file and applies them with the default
/// application priority.
///
/// This method is deprecated; use [`crate::set_global_css_from_file`]
/// instead.
#[deprecated(note = "Use `nrelm::set_global_css_from_file` instead")]
pub fn set_global_css_from_file<P: AsRef<std::path::Path>>(
&self,
path: P,
) -> Result<(), std::io::Error> {
crate::set_global_css_from_file(path)
}
/// Runs the application with a synchronous component as the root
/// component and enters the GTK main loop.
///
/// The root component's widget must be a [`gtk::Window`]; it is added to
/// the application and shown on activation.
pub fn run<C>(self, payload: C::Init)
where
C: Component<Input = M>,
C::Root: AsRef<gtk::Window>,
{
let Self {
app,
broker,
args,
visible,
} = self;
let payload = Cell::new(Some(payload));
app.connect_startup(move |app| {
if let Some(payload) = payload.take() {
let builder = ComponentBuilder::<C>::default();
let connector = match broker {
Some(broker) => builder.launch_with_broker(payload, broker),
None => builder.launch(payload),
};
crate::late_initialization::run_late_init();
let mut controller = connector.detach();
let window = controller.widget();
app.add_window(window.as_ref());
controller.detach_runtime();
}
});
app.connect_activate(move |app| {
if let Some(window) = app.active_window()
&& visible
{
window.set_visible(true);
}
});
let _guard = RUNTIME.enter();
if let Some(args) = args {
app.run_with_args(&args);
} else {
app.run();
}
shutdown_all();
glib::MainContext::ref_thread_default().iteration(true);
}
/// Runs the application with an asynchronous component as the root
/// component and enters the GTK main loop.
///
/// This works like [`run`](Self::run), but takes a component that
/// implements [`AsyncComponent`].
pub fn run_async<C>(self, payload: C::Init)
where
C: AsyncComponent<Input = M>,
C::Root: AsRef<gtk::Window>,
{
let Self {
app,
broker,
args,
visible: set_visible,
} = self;
let payload = Cell::new(Some(payload));
app.connect_startup(move |app| {
if let Some(payload) = payload.take() {
let builder = AsyncComponentBuilder::<C>::default();
let connector = match broker {
Some(broker) => builder.launch_with_broker(payload, broker),
None => builder.launch(payload),
};
crate::late_initialization::run_late_init();
let mut controller = connector.detach();
let window = controller.widget();
app.add_window(window.as_ref());
controller.detach_runtime();
}
});
app.connect_activate(move |app| {
if let Some(window) = app.active_window()
&& set_visible
{
window.set_visible(true);
}
});
let _guard = RUNTIME.enter();
if let Some(args) = args {
app.run_with_args(&args);
} else {
app.run();
}
shutdown_all();
glib::MainContext::ref_thread_default().iteration(true);
}
}