diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 87677f58a..87698a545 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -202,18 +202,11 @@ namespace Flow.Launcher { // we try to enable auto-startup on first launch, or reenable if it was removed // but the user still has the setting set - if (_settings.StartFlowLauncherOnSystemStartup && !Helper.AutoStartup.IsEnabled) + if (_settings.StartFlowLauncherOnSystemStartup) { try { - if (_settings.UseLogonTaskForStartup) - { - Helper.AutoStartup.EnableViaLogonTask(); - } - else - { - Helper.AutoStartup.EnableViaRegistry(); - } + Helper.AutoStartup.CheckIsEnabled(_settings.UseLogonTaskForStartup); } catch (Exception e) { diff --git a/Flow.Launcher/Helper/AutoStartup.cs b/Flow.Launcher/Helper/AutoStartup.cs index 81568875e..b83bb5948 100644 --- a/Flow.Launcher/Helper/AutoStartup.cs +++ b/Flow.Launcher/Helper/AutoStartup.cs @@ -16,29 +16,37 @@ public class AutoStartup private const string LogonTaskName = $"{Constant.FlowLauncher} Startup"; private const string LogonTaskDesc = $"{Constant.FlowLauncher} Auto Startup"; - public static bool IsEnabled + public static void CheckIsEnabled(bool useLogonTaskForStartup) { - get + // We need to check both because if both of them are enabled, + // Hide Flow Launcher on startup will not work since the later one will trigger main window show event + var logonTaskEnabled = CheckLogonTask(); + var registryEnabled = CheckRegistry(); + if (useLogonTaskForStartup) { - // Check if logon task is enabled - if (CheckLogonTask()) + // Enable logon task + if (!logonTaskEnabled) { - return true; + Enable(true); } - - // Check if registry is enabled - try + // Disable registry + if (registryEnabled) { - using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); - var path = key?.GetValue(Constant.FlowLauncher) as string; - return path == Constant.ExecutablePath; + Disable(false); } - catch (Exception e) + } + else + { + // Enable registry + if (!registryEnabled) { - App.API.LogError(ClassName, $"Ignoring non-critical registry error (querying if enabled): {e}"); + Enable(false); + } + // Disable logon task + if (logonTaskEnabled) + { + Disable(true); } - - return false; } } @@ -69,22 +77,28 @@ public class AutoStartup return false; } + private static bool CheckRegistry() + { + try + { + using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); + var path = key?.GetValue(Constant.FlowLauncher) as string; + return path == Constant.ExecutablePath; + } + catch (Exception e) + { + App.API.LogError(ClassName, $"Ignoring non-critical registry error (querying if enabled): {e}"); + } + + return false; + } + public static void DisableViaLogonTaskAndRegistry() { Disable(true); Disable(false); } - public static void EnableViaLogonTask() - { - Enable(true); - } - - public static void EnableViaRegistry() - { - Enable(false); - } - public static void ChangeToViaLogonTask() { Disable(false); diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs index 021c9d7fe..6b56caf5e 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs @@ -48,11 +48,11 @@ public partial class SettingsPaneGeneralViewModel : BaseModel { if (UseLogonTaskForStartup) { - AutoStartup.EnableViaLogonTask(); + AutoStartup.ChangeToViaLogonTask(); } else { - AutoStartup.EnableViaRegistry(); + AutoStartup.ChangeToViaRegistry(); } } else