-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmod.rs
More file actions
49 lines (41 loc) · 1.65 KB
/
mod.rs
File metadata and controls
49 lines (41 loc) · 1.65 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
pub mod view_model;
use std::thread;
use std::{sync::Arc, thread::JoinHandle};
use async_std::{channel::Receiver, sync::Mutex as AsyncMutex};
use credentialsd_common::server::ViewRequest;
use credentialsd_common::{client::FlowController, model::ViewUpdate};
use view_model::ViewEvent;
pub(super) fn start_gui_thread<F: FlowController + Send + Sync + 'static>(
rx: Receiver<ViewRequest>,
flow_controller: F,
) -> Result<JoinHandle<()>, std::io::Error> {
thread::Builder::new().name("gui".into()).spawn(move || {
let flow_controller = Arc::new(AsyncMutex::new(flow_controller));
// D-Bus received a request and needs a window open
while let Ok(view_request) = rx.recv_blocking() {
run_gui(flow_controller.clone(), view_request);
}
})
}
fn run_gui<F: FlowController + Send + Sync + 'static>(
flow_controller: Arc<AsyncMutex<F>>,
request: ViewRequest,
) {
let operation = request.operation;
let (tx_update, rx_update) = async_std::channel::unbounded::<ViewUpdate>();
let (tx_event, rx_event) = async_std::channel::unbounded::<ViewEvent>();
let event_loop = async_std::task::spawn(async move {
let mut vm =
view_model::ViewModel::new(operation, flow_controller.clone(), rx_event, tx_update);
vm.start_event_loop().await;
tracing::debug!("Finishing user request.");
// If cancellation fails, that's fine.
let _ = flow_controller
.lock()
.await
.cancel_request(request.id)
.await;
});
view_model::gtk::start_gtk_app(tx_event, rx_update);
async_std::task::block_on(event_loop.cancel());
}