store config in database, macro for state access
This commit is contained in:
@ -3,7 +3,8 @@
|
||||
windows_subsystem = "windows"
|
||||
)]
|
||||
|
||||
use tauri::Manager;
|
||||
use tauri::{AppHandle, Manager};
|
||||
use once_cell::sync::OnceCell;
|
||||
|
||||
mod config;
|
||||
mod errors;
|
||||
@ -16,6 +17,8 @@ mod tray;
|
||||
use state::AppState;
|
||||
|
||||
|
||||
pub static APP: OnceCell<AppHandle> = OnceCell::new();
|
||||
|
||||
fn main() {
|
||||
let initial_state = match state::AppState::new() {
|
||||
Ok(state) => state,
|
||||
@ -31,8 +34,10 @@ fn main() {
|
||||
ipc::respond,
|
||||
ipc::get_session_status,
|
||||
ipc::save_credentials,
|
||||
ipc::get_config,
|
||||
])
|
||||
.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);
|
||||
@ -51,4 +56,38 @@ fn main() {
|
||||
}
|
||||
_ => ()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
|
Reference in New Issue
Block a user