creddy/src-tauri/src/main.rs

94 lines
2.8 KiB
Rust
Raw Normal View History

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
)]
use tauri::{AppHandle, Manager};
use once_cell::sync::OnceCell;
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
use state::AppState;
2022-08-14 20:27:41 +00:00
pub static APP: OnceCell<AppHandle> = OnceCell::new();
2022-08-14 20:27:41 +00:00
fn main() {
2022-12-14 05:50:34 +00:00
let initial_state = match state::AppState::new() {
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,
ipc::get_config,
2022-12-14 05:50:34 +00:00
])
.setup(|app| {
APP.set(app.handle()).unwrap();
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()));
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();
}
_ => ()
}
_ => ()
})
}
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;