Skip to content

Commit

Permalink
fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
Misieq01 committed Dec 11, 2024
1 parent ba5a778 commit 6c3b168
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions src-tauri/src/auto_launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AutoLauncher> = LazyLock::new(AutoLauncher::new);

pub enum CurrentOperatingSystem {
Windows,
Linux,
MacOS,
}

pub struct AutoLauncher {
auto_launcher: RwLock<Option<AutoLaunch>>,
}
Expand All @@ -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<AutoLaunch, anyhow::Error> {
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)
Expand Down Expand Up @@ -100,21 +84,28 @@ impl AutoLauncher {
}
}

fn toggle_auto_launcher(
async fn toggle_auto_launcher(
&self,
auto_launcher: &AutoLaunch,
config_is_auto_launcher_enabled: bool,
) -> Result<(), anyhow::Error> {
let is_auto_launcher_enabled = auto_launcher.is_enabled().unwrap_or(false);

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()?;
}
Expand All @@ -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()?;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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 => {
Expand Down

0 comments on commit 6c3b168

Please sign in to comment.