From 6c3b168d17a18fd65ae7b2373d8d6f126a77ea75 Mon Sep 17 00:00:00 2001 From: Misieq01 Date: Wed, 11 Dec 2024 09:15:02 +0100 Subject: [PATCH] fixes after review --- src-tauri/src/auto_launcher.rs | 49 +++++++++++++++------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/src-tauri/src/auto_launcher.rs b/src-tauri/src/auto_launcher.rs index 2c40bc08a..6a303230b 100644 --- a/src-tauri/src/auto_launcher.rs +++ b/src-tauri/src/auto_launcher.rs @@ -36,16 +36,12 @@ use tauri::utils::platform::current_exe; use tokio::sync::RwLock; use whoami::username; +use crate::utils::platform_utils::{CurrentOperatingSystem, PlatformUtils}; + const LOG_TARGET: &str = "tari::universe::auto_launcher"; static INSTANCE: LazyLock = LazyLock::new(AutoLauncher::new); -pub enum CurrentOperatingSystem { - Windows, - Linux, - MacOS, -} - pub struct AutoLauncher { auto_launcher: RwLock>, } @@ -57,22 +53,10 @@ impl AutoLauncher { } } - fn detect_current_os() -> CurrentOperatingSystem { - if cfg!(target_os = "windows") { - CurrentOperatingSystem::Windows - } else if cfg!(target_os = "linux") { - CurrentOperatingSystem::Linux - } else if cfg!(target_os = "macos") { - CurrentOperatingSystem::MacOS - } else { - panic!("Unsupported OS"); - } - } - fn build_auto_launcher(app_name: &str, app_path: &str) -> Result { info!(target: LOG_TARGET, "Building auto-launcher with app_name: {} and app_path: {}", app_name, app_path); - match AutoLauncher::detect_current_os() { + match PlatformUtils::detect_current_os() { CurrentOperatingSystem::Windows => { return AutoLaunchBuilder::new() .set_app_name(app_name) @@ -100,7 +84,8 @@ impl AutoLauncher { } } - fn toggle_auto_launcher( + async fn toggle_auto_launcher( + &self, auto_launcher: &AutoLaunch, config_is_auto_launcher_enabled: bool, ) -> Result<(), anyhow::Error> { @@ -108,13 +93,19 @@ impl AutoLauncher { if config_is_auto_launcher_enabled && !is_auto_launcher_enabled { info!(target: LOG_TARGET, "Enabling auto-launcher"); - match AutoLauncher::detect_current_os() { + match PlatformUtils::detect_current_os() { CurrentOperatingSystem::MacOS => { // This for some reason fixes the issue where macOS starts two instances of the app // when auto-launcher is enabled and when during shutdown user selects to reopen the apps after restart auto_launcher.disable()?; auto_launcher.enable()?; } + CurrentOperatingSystem::Windows => { + auto_launcher.enable()?; + // To startup application as admin on windows, we need to create a task scheduler + self.toggle_windows_admin_auto_launcher(is_auto_launcher_enabled) + .await?; + } _ => { auto_launcher.enable()?; } @@ -124,6 +115,14 @@ impl AutoLauncher { if !config_is_auto_launcher_enabled && is_auto_launcher_enabled { info!(target: LOG_TARGET, "Disabling auto-launcher"); + match PlatformUtils::detect_current_os() { + CurrentOperatingSystem::Windows => { + self.toggle_windows_admin_auto_launcher(is_auto_launcher_enabled) + .await?; + auto_launcher.disable()?; + } + _ => {} + } auto_launcher.disable()?; } @@ -221,9 +220,7 @@ impl AutoLauncher { info!(target: LOG_TARGET, "Building auto-launcher with app_name: {} and app_path: {}", app_name, app_path); let auto_launcher = AutoLauncher::build_auto_launcher(app_name, &app_path)?; - AutoLauncher::toggle_auto_launcher(&auto_launcher, is_auto_launcher_enabled)?; - #[cfg(target_os = "windows")] - self.toggle_windows_admin_auto_launcher(is_auto_launcher_enabled) + self.toggle_auto_launcher(&auto_launcher, is_auto_launcher_enabled) .await?; let _ = &self.auto_launcher.write().await.replace(auto_launcher); @@ -247,9 +244,7 @@ impl AutoLauncher { let auto_launcher_ref = auto_launcher.as_ref(); match auto_launcher_ref { Some(auto_launcher) => { - AutoLauncher::toggle_auto_launcher(auto_launcher, is_auto_launcher_enabled)?; - #[cfg(target_os = "windows")] - self.toggle_windows_admin_auto_launcher(is_auto_launcher_enabled) + self.toggle_auto_launcher(auto_launcher, is_auto_launcher_enabled) .await?; } None => {