squash(autostart): added adenmgb's autostart feature
Squashed commit of the following: commit 085cd9481dee748ee84c1a8f9dcd8ef0b01105ab Author: Aden Lindsay <140392385+AdenMGB@users.noreply.github.com> Date: Mon Dec 30 16:29:41 2024 +1030 Update lib.rs for the DB sync of autostart commit 86f2fb19bde3933ec6b5cd82701de0d306121659 Author: Aden Lindsay <140392385+AdenMGB@users.noreply.github.com> Date: Mon Dec 30 16:29:13 2024 +1030 Update db.rs to accomidate the settings sync commit ece11e7581c9aef55588b2e829379b1224021a45 Author: Aden Lindsay <140392385+AdenMGB@users.noreply.github.com> Date: Mon Dec 30 16:27:48 2024 +1030 Update autostart.rs to include DB commit 7ea8a24fdc2ff98379694ce8e347a40fcfd5fea3 Author: Aden Lindsay <140392385+AdenMGB@users.noreply.github.com> Date: Mon Dec 30 15:17:38 2024 +1030 Add files via upload commit af2f232d94fa449d9e20df737ce81ebded87fd50 Author: Aden Lindsay <140392385+AdenMGB@users.noreply.github.com> Date: Mon Dec 30 15:17:09 2024 +1030 Delete src-tauri/Cargo.toml commit 5d27b65612457de6eb30835d5423b9d96fd5a596 Author: Aden Lindsay <140392385+AdenMGB@users.noreply.github.com> Date: Mon Dec 30 15:15:42 2024 +1030 Add files via upload commit 2eea7b97a876e23cc0d6daec37f1b75af9bf3ae2 Author: Aden Lindsay <140392385+AdenMGB@users.noreply.github.com> Date: Mon Dec 30 15:15:31 2024 +1030 Delete src-tauri/src/lib.rs commit 9a635a10d1340f86c74812113284b115b34b9bbe Author: Aden Lindsay <140392385+AdenMGB@users.noreply.github.com> Date: Mon Dec 30 15:14:49 2024 +1030 Add files via upload commit 2fb049531a082fbdd217aba694819b7a3f954a55 Author: Aden Lindsay <140392385+AdenMGB@users.noreply.github.com> Date: Mon Dec 30 15:13:37 2024 +1030 Add files via upload commit ea1be4d7505a9ab16bda338491c0ec313d0bc586 Author: Aden Lindsay <140392385+AdenMGB@users.noreply.github.com> Date: Mon Dec 30 15:13:20 2024 +1030 Delete pages/settings/index.vue
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
use log::info;
|
||||
use tauri::AppHandle;
|
||||
use tauri_plugin_autostart::ManagerExt;
|
||||
use crate::DB;
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn toggle_autostart(app: AppHandle, enabled: bool) -> Result<(), String> {
|
||||
let manager = app.autolaunch();
|
||||
if enabled {
|
||||
manager.enable().map_err(|e| e.to_string())?;
|
||||
info!("Enabled autostart");
|
||||
} else {
|
||||
manager.disable().map_err(|e| e.to_string())?;
|
||||
info!("Disabled autostart");
|
||||
}
|
||||
|
||||
// Store the state in DB
|
||||
let mut db_handle = DB.borrow_data_mut().map_err(|e| e.to_string())?;
|
||||
db_handle.settings.autostart = enabled;
|
||||
drop(db_handle);
|
||||
DB.save().map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_autostart_enabled(app: AppHandle) -> Result<bool, String> {
|
||||
// First check DB state
|
||||
let db_handle = DB.borrow_data().map_err(|e| e.to_string())?;
|
||||
let db_state = db_handle.settings.autostart;
|
||||
drop(db_handle);
|
||||
|
||||
// Get actual system state
|
||||
let manager = app.autolaunch();
|
||||
let system_state = manager.is_enabled().map_err(|e| e.to_string())?;
|
||||
|
||||
// If they don't match, sync to DB state
|
||||
if db_state != system_state {
|
||||
if db_state {
|
||||
manager.enable().map_err(|e| e.to_string())?;
|
||||
} else {
|
||||
manager.disable().map_err(|e| e.to_string())?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(db_state)
|
||||
}
|
||||
|
||||
// New function to sync state on startup
|
||||
pub fn sync_autostart_on_startup(app: &AppHandle) -> Result<(), String> {
|
||||
let db_handle = DB.borrow_data().map_err(|e| e.to_string())?;
|
||||
let should_be_enabled = db_handle.settings.autostart;
|
||||
drop(db_handle);
|
||||
|
||||
let manager = app.autolaunch();
|
||||
let current_state = manager.is_enabled().map_err(|e| e.to_string())?;
|
||||
|
||||
if current_state != should_be_enabled {
|
||||
if should_be_enabled {
|
||||
manager.enable().map_err(|e| e.to_string())?;
|
||||
info!("Synced autostart: enabled");
|
||||
} else {
|
||||
manager.disable().map_err(|e| e.to_string())?;
|
||||
info!("Synced autostart: disabled");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -72,9 +72,24 @@ pub struct DatabaseGames {
|
||||
pub transient_statuses: HashMap<String, GameTransientStatus>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct Settings {
|
||||
pub autostart: bool,
|
||||
// ... other settings ...
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
autostart: false,
|
||||
// ... other settings defaults ...
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct Database {
|
||||
pub settings: Settings,
|
||||
pub auth: Option<DatabaseAuth>,
|
||||
pub base_url: String,
|
||||
pub games: DatabaseGames,
|
||||
@@ -118,13 +133,14 @@ impl DatabaseImpls for DatabaseInterface {
|
||||
debug!("Creating logs directory");
|
||||
create_dir_all(logs_root_dir.clone()).unwrap();
|
||||
|
||||
#[allow(clippy::let_and_return)]
|
||||
let exists = fs::exists(db_path.clone()).unwrap();
|
||||
|
||||
match exists {
|
||||
true => PathDatabase::load_from_path(db_path).expect("Database loading failed"),
|
||||
true => PathDatabase::load_from_path(db_path)
|
||||
.expect("Database loading failed"),
|
||||
false => {
|
||||
let default = Database {
|
||||
settings: Settings::default(),
|
||||
auth: None,
|
||||
base_url: "".to_string(),
|
||||
games: DatabaseGames {
|
||||
|
||||
@@ -9,6 +9,7 @@ mod remote;
|
||||
mod state;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
mod autostart;
|
||||
|
||||
use crate::db::DatabaseImpls;
|
||||
use auth::{auth_initiate, generate_authorization_header, manual_recieve_handshake, recieve_handshake, retry_connect};
|
||||
@@ -46,6 +47,7 @@ use tauri::menu::{Menu, MenuItem, PredefinedMenuItem};
|
||||
use tauri::tray::TrayIconBuilder;
|
||||
use tauri::{AppHandle, Manager, RunEvent, WindowEvent};
|
||||
use tauri_plugin_deep_link::DeepLinkExt;
|
||||
use crate::autostart::{get_autostart_enabled, toggle_autostart};
|
||||
|
||||
#[derive(Clone, Copy, Serialize)]
|
||||
pub enum AppStatus {
|
||||
@@ -178,6 +180,11 @@ fn setup(handle: AppHandle) -> AppState<'static> {
|
||||
drop(db_handle);
|
||||
info!("finished setup!");
|
||||
|
||||
// Sync autostart state
|
||||
if let Err(e) = autostart::sync_autostart_on_startup(&handle) {
|
||||
warn!("Failed to sync autostart state: {}", e);
|
||||
}
|
||||
|
||||
AppState {
|
||||
status: app_status,
|
||||
user,
|
||||
@@ -234,10 +241,16 @@ pub fn run() {
|
||||
uninstall_game,
|
||||
// Processes
|
||||
launch_game,
|
||||
kill_game
|
||||
kill_game,
|
||||
toggle_autostart,
|
||||
get_autostart_enabled,
|
||||
])
|
||||
.plugin(tauri_plugin_shell::init())
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.plugin(tauri_plugin_autostart::init(
|
||||
tauri_plugin_autostart::MacosLauncher::LaunchAgent,
|
||||
Some(vec!["--minimize"])
|
||||
))
|
||||
.setup(|app| {
|
||||
let handle = app.handle().clone();
|
||||
let state = setup(handle);
|
||||
|
||||
Reference in New Issue
Block a user