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
use gio::prelude::ApplicationExtManual;
use gio::ApplicationExt;
use crate::prelude::LifecycleState;
use std::sync::mpsc::TryRecvError;
pub fn run<F, A, S>(store: crate::store::Store<A, S>, window: F)
where
F: Fn(crate::store::Store<A, S>) -> gtk::ApplicationWindow + 'static,
A: Send + Clone + Eq + 'static,
S: LifecycleState + Clone + Eq + Default + 'static,
{
if gtk::init().is_err() {
eprintln!("Failed to initialize GTK.");
return;
}
#[cfg(feature = "with_libhandy")]
libhandy::init();
store.register(
|s1, s2| s1.running() != s2.running(),
|s| {
if !s.running() {
gtk::main_quit()
}
},
);
{
let store = store.clone();
glib::timeout_add_local(100, move || {
glib::Continue(match store.try_receive() {
Ok(a) => {
store.dispatch(a);
store.select(|s| s.running())
}
Err(TryRecvError::Empty) => true,
Err(TryRecvError::Disconnected) => false,
})
});
}
let gtk_app = gtk::ApplicationBuilder::new().build();
gtk_app.connect_startup(move |_| {
window(store.clone());
});
gtk_app.connect_activate(move |_| {
gtk::main();
});
gtk_app.run(std::env::args().collect::<Vec<String>>().as_slice());
}