-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathapplication.rs
More file actions
193 lines (166 loc) · 6.33 KB
/
application.rs
File metadata and controls
193 lines (166 loc) · 6.33 KB
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
use async_std::channel::{Receiver, Sender};
use gettextrs::gettext;
use tracing::{debug, info};
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{gdk, gio, glib};
use super::{ViewModel, window::ExampleApplicationWindow};
use crate::config::{APP_ID, PKGDATADIR, PROFILE, VERSION};
use crate::gui::view_model::{ViewEvent, ViewUpdate};
mod imp {
use crate::gui::view_model::gtk::ModelState;
use super::*;
use glib::{WeakRef, clone};
use std::{
cell::{OnceCell, RefCell},
time::Duration,
};
#[derive(Debug, Default)]
pub struct ExampleApplication {
pub window: OnceCell<WeakRef<ExampleApplicationWindow>>,
pub(super) tx: RefCell<Option<Sender<ViewEvent>>>,
pub(super) rx: RefCell<Option<Receiver<ViewUpdate>>>,
}
#[glib::object_subclass]
impl ObjectSubclass for ExampleApplication {
const NAME: &'static str = "ExampleApplication";
type Type = super::ExampleApplication;
type ParentType = gtk::Application;
}
impl ObjectImpl for ExampleApplication {}
impl ApplicationImpl for ExampleApplication {
fn activate(&self) {
debug!("GtkApplication<ExampleApplication>::activate");
self.parent_activate();
let app = self.obj();
if let Some(window) = self.window.get() {
let window = window.upgrade().unwrap();
window.present();
return;
}
let tx = self.tx.take().expect("sender to be initiated");
let rx = self.rx.take().expect("receiver to be initiated");
let view_model = ViewModel::new(tx, rx);
let vm2 = view_model.clone();
let window = ExampleApplicationWindow::new(&app, view_model);
let window2 = window.clone();
vm2.clone().connect_completed_notify(move |vm| {
if vm.completed() {
glib::spawn_future_local(clone!(
#[weak]
window2,
async move {
// Wait to show confirmation before closing.
async_std::task::sleep(Duration::from_millis(500)).await;
gtk::prelude::WidgetExt::activate_action(&window2, "window.close", None)
.unwrap()
}
));
}
});
let window3 = window.clone();
// TODO: merge these state callbacks into a single function
vm2.clone().connect_state_notify(move |vm| {
if let ModelState::Cancelled = vm.state() {
glib::spawn_future_local(clone!(
#[weak]
window3,
async move {
gtk::prelude::WidgetExt::activate_action(&window3, "window.close", None)
.unwrap()
}
));
}
});
self.window
.set(window.downgrade())
.expect("Window already set.");
app.main_window().present();
}
fn startup(&self) {
debug!("GtkApplication<ExampleApplication>::startup");
self.parent_startup();
let app = self.obj();
// Set icons for shell
gtk::Window::set_default_icon_name(APP_ID);
app.setup_css();
app.setup_gactions();
app.setup_accels();
}
}
impl GtkApplicationImpl for ExampleApplication {}
}
glib::wrapper! {
pub struct ExampleApplication(ObjectSubclass<imp::ExampleApplication>)
@extends gio::Application, gtk::Application,
@implements gio::ActionMap, gio::ActionGroup;
}
impl ExampleApplication {
fn main_window(&self) -> ExampleApplicationWindow {
self.imp().window.get().unwrap().upgrade().unwrap()
}
fn setup_gactions(&self) {
// Quit
let action_quit = gio::ActionEntry::builder("quit")
.activate(move |app: &Self, _, _| {
// This is needed to trigger the delete event and saving the window state
app.main_window().close();
app.quit();
})
.build();
// About
let action_about = gio::ActionEntry::builder("about")
.activate(|app: &Self, _, _| {
app.show_about_dialog();
})
.build();
self.add_action_entries([action_quit, action_about]);
}
// Sets up keyboard shortcuts
fn setup_accels(&self) {
self.set_accels_for_action("app.quit", &["<Control>q"]);
self.set_accels_for_action("window.close", &["<Control>w"]);
}
fn setup_css(&self) {
let provider = gtk::CssProvider::new();
provider.load_from_resource("/xyz/iinuwa/credentialsd/CredentialsUi/style.css");
if let Some(display) = gdk::Display::default() {
gtk::style_context_add_provider_for_display(
&display,
&provider,
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
);
}
}
fn show_about_dialog(&self) {
let dialog = gtk::AboutDialog::builder()
.logo_icon_name(APP_ID)
.license_type(gtk::License::Lgpl30Only)
.website("https://github.com/linux-credentials/linux-webauthn-portal-api")
.version(VERSION)
.transient_for(&self.main_window())
.translator_credits(gettext("translator-credits"))
.modal(true)
.authors(vec!["Isaiah Inuwa <isaiah.inuwa@gmail.com>"])
.build();
dialog.present();
}
pub fn run(&self) -> glib::ExitCode {
info!("Credentials UI ({})", APP_ID);
info!("Version: {} ({})", VERSION, PROFILE);
info!("Datadir: {}", PKGDATADIR);
ApplicationExtManual::run(self)
}
pub(crate) fn new(tx: Sender<ViewEvent>, rx: Receiver<ViewUpdate>) -> Self {
let app: Self = glib::Object::builder()
.property("application-id", APP_ID)
.property(
"resource-base-path",
"/xyz/iinuwa/credentialsd/CredentialUI/",
)
.build();
app.imp().tx.replace(Some(tx));
app.imp().rx.replace(Some(rx));
app
}
}