2022-08-14 20:27:41 +00:00
|
|
|
#![cfg_attr(
|
2022-12-14 05:50:34 +00:00
|
|
|
all(not(debug_assertions), target_os = "windows"),
|
|
|
|
windows_subsystem = "windows"
|
2022-08-14 20:27:41 +00:00
|
|
|
)]
|
|
|
|
|
2022-12-23 19:34:17 +00:00
|
|
|
use tauri::{AppHandle, Manager, async_runtime as rt};
|
2022-12-23 00:36:32 +00:00
|
|
|
use once_cell::sync::OnceCell;
|
2022-11-28 06:03:15 +00:00
|
|
|
|
2022-12-21 21:42:12 +00:00
|
|
|
mod config;
|
2022-11-29 00:16:33 +00:00
|
|
|
mod errors;
|
2022-12-04 05:47:09 +00:00
|
|
|
mod clientinfo;
|
2022-11-29 00:16:33 +00:00
|
|
|
mod ipc;
|
|
|
|
mod state;
|
|
|
|
mod server;
|
2022-12-21 22:49:01 +00:00
|
|
|
mod tray;
|
2022-08-14 20:27:41 +00:00
|
|
|
|
2022-12-23 19:34:17 +00:00
|
|
|
use crate::errors::*;
|
2022-12-21 21:42:12 +00:00
|
|
|
use state::AppState;
|
|
|
|
|
2022-08-14 20:27:41 +00:00
|
|
|
|
2022-12-23 00:36:32 +00:00
|
|
|
pub static APP: OnceCell<AppHandle> = OnceCell::new();
|
|
|
|
|
2022-08-14 20:27:41 +00:00
|
|
|
fn main() {
|
2022-12-23 19:34:17 +00:00
|
|
|
let initial_state = match rt::block_on(state::AppState::load()) {
|
2022-12-14 05:50:34 +00:00
|
|
|
Ok(state) => state,
|
|
|
|
Err(e) => {eprintln!("{}", e); return;}
|
|
|
|
};
|
|
|
|
|
|
|
|
tauri::Builder::default()
|
|
|
|
.manage(initial_state)
|
2022-12-21 22:49:01 +00:00
|
|
|
.system_tray(tray::create())
|
|
|
|
.on_system_tray_event(tray::handle_event)
|
2022-12-14 05:50:34 +00:00
|
|
|
.invoke_handler(tauri::generate_handler![
|
|
|
|
ipc::unlock,
|
|
|
|
ipc::respond,
|
|
|
|
ipc::get_session_status,
|
|
|
|
ipc::save_credentials,
|
2022-12-23 00:36:32 +00:00
|
|
|
ipc::get_config,
|
2022-12-14 05:50:34 +00:00
|
|
|
])
|
|
|
|
.setup(|app| {
|
2022-12-23 00:36:32 +00:00
|
|
|
APP.set(app.handle()).unwrap();
|
2022-12-21 21:42:12 +00:00
|
|
|
let state = app.state::<AppState>();
|
|
|
|
let config = state.config.read().unwrap();
|
|
|
|
let addr = std::net::SocketAddrV4::new(config.listen_addr, config.listen_port);
|
2022-12-14 05:50:34 +00:00
|
|
|
tauri::async_runtime::spawn(server::serve(addr, app.handle()));
|
2022-12-23 19:34:17 +00:00
|
|
|
|
|
|
|
if !config.start_minimized {
|
|
|
|
app.get_window("main")
|
|
|
|
.ok_or(RequestError::NoMainWindow)?
|
|
|
|
.show()?;
|
|
|
|
}
|
2022-12-14 05:50:34 +00:00
|
|
|
Ok(())
|
|
|
|
})
|
2022-12-21 22:49:01 +00:00
|
|
|
.build(tauri::generate_context!())
|
|
|
|
.expect("error while running tauri application")
|
|
|
|
.run(|app, run_event| match run_event {
|
|
|
|
tauri::RunEvent::WindowEvent { label, event, .. } => match event {
|
|
|
|
tauri::WindowEvent::CloseRequested { api, .. } => {
|
|
|
|
let _ = app.get_window(&label).map(|w| w.hide());
|
|
|
|
api.prevent_close();
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
})
|
2022-12-23 00:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
macro_rules! get_state {
|
|
|
|
($prop:ident as $name:ident) => {
|
|
|
|
use tauri::Manager;
|
|
|
|
let app = crate::APP.get().unwrap(); // as long as the app is running, this is fine
|
|
|
|
let state = app.state::<crate::state::AppState>();
|
|
|
|
let $name = state.$prop.read().unwrap(); // only panics if another thread has already panicked
|
|
|
|
};
|
|
|
|
(config.$prop:ident as $name:ident) => {
|
|
|
|
use tauri::Manager;
|
|
|
|
let app = crate::APP.get().unwrap();
|
|
|
|
let state = app.state::<crate::state::AppState>();
|
|
|
|
let config = state.config.read().unwrap();
|
|
|
|
let $name = config.$prop;
|
|
|
|
};
|
|
|
|
|
|
|
|
(mut $prop:ident as $name:ident) => {
|
|
|
|
use tauri::Manager;
|
|
|
|
let app = crate::APP.get().unwrap();
|
|
|
|
let state = app.state::<crate::state::AppState>();
|
|
|
|
let $name = state.$prop.write().unwrap();
|
|
|
|
};
|
|
|
|
(mut config.$prop:ident as $name:ident) => {
|
|
|
|
use tauri::Manager;
|
|
|
|
let app = crate::APP.get().unwrap();
|
|
|
|
let state = app.state::<crate::state::AppState>();
|
|
|
|
let config = state.config.write().unwrap();
|
|
|
|
let $name = config.$prop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub(crate) use get_state;
|