diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 4ebff16a9..755d206d5 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Diagnostics; using System.Text; using System.Threading.Tasks; @@ -104,15 +104,11 @@ namespace Flow.Launcher }); } - private void AutoStartup() { - if (_settings.StartFlowLauncherOnSystemStartup) + if (_settings.StartFlowLauncherOnSystemStartup && !Helper.AutoStartup.IsEnabled) { - if (!SettingWindow.StartupSet()) - { - SettingWindow.SetStartup(); - } + Helper.AutoStartup.Enable(); } } diff --git a/Flow.Launcher/Helper/AutoStartup.cs b/Flow.Launcher/Helper/AutoStartup.cs new file mode 100644 index 000000000..816adfaac --- /dev/null +++ b/Flow.Launcher/Helper/AutoStartup.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Flow.Launcher.Infrastructure; +using Flow.Launcher.Infrastructure.Logger; +using Microsoft.Win32; + +namespace Flow.Launcher.Helper +{ + public class AutoStartup + { + private const string StartupPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; + + public static bool IsEnabled + { + get + { + try + { + using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); + var path = key?.GetValue(Constant.FlowLauncher) as string; + return path == Constant.ExecutablePath; + } + catch (Exception e) + { + Log.Error("AutoStartup", $"Ignoring non-critical registry error (user permissions?): {e}"); + } + + return false; + } + } + + public static void Disable() + { + try + { + using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); + key?.DeleteValue(Constant.FlowLauncher, false); + } + catch (Exception e) + { + Log.Error("AutoStartup", $"Ignoring non-critical registry error (user permissions?): {e}"); + } + } + + internal static void Enable() + { + try + { + using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); + key?.SetValue(Constant.FlowLauncher, Constant.ExecutablePath); + } + catch (Exception e) + { + Log.Error("AutoStartup", $"Ignoring non-critical registry error (user permissions?): {e}"); + } + + } + } +} diff --git a/Flow.Launcher/SettingWindow.xaml.cs b/Flow.Launcher/SettingWindow.xaml.cs index 0dcbc6b1b..d5259d497 100644 --- a/Flow.Launcher/SettingWindow.xaml.cs +++ b/Flow.Launcher/SettingWindow.xaml.cs @@ -1,4 +1,4 @@ -using Flow.Launcher.Core.ExternalPlugins; +using Flow.Launcher.Core.ExternalPlugins; using Flow.Launcher.Core.Plugin; using Flow.Launcher.Core.Resource; using Flow.Launcher.Helper; @@ -31,8 +31,6 @@ namespace Flow.Launcher { public partial class SettingWindow { - private const string StartupPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; - public readonly IPublicAPI API; private Settings settings; private SettingWindowViewModel viewModel; @@ -61,54 +59,12 @@ namespace Flow.Launcher private void OnAutoStartupChecked(object sender, RoutedEventArgs e) { - SetStartup(); + viewModel.SetStartup(); } private void OnAutoStartupUncheck(object sender, RoutedEventArgs e) { - RemoveStartup(); - } - - public static void SetStartup() - { - try - { - using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); - key?.SetValue(Constant.FlowLauncher, Constant.ExecutablePath); - } - catch (Exception e) - { - Log.Error("SettingsWindow", $"Ignoring non-critical registry error (user permissions?): {e}"); - } - } - - private void RemoveStartup() - { - try - { - using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); - key?.DeleteValue(Constant.FlowLauncher, false); - } - catch (Exception e) - { - Log.Error("SettingsWindow", $"Ignoring non-critical registry error (user permissions?): {e}"); - } - } - - public static bool StartupSet() - { - try - { - using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); - var path = key?.GetValue(Constant.FlowLauncher) as string; - return path == Constant.ExecutablePath; - } - catch (Exception e) - { - Log.Error("SettingsWindow", $"Ignoring non-critical registry error (user permissions?): {e}"); - } - - return false; + viewModel.RemoveStartup(); } private void OnSelectPythonDirectoryClick(object sender, RoutedEventArgs e) @@ -404,4 +360,4 @@ namespace Flow.Launcher Plugins.ScrollIntoView(Plugins.SelectedItem); } } -} \ No newline at end of file +} diff --git a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs index 2fc6934d5..faa41c0f6 100644 --- a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs +++ b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -216,6 +216,19 @@ namespace Flow.Launcher.ViewModel #endregion + #region startup + + public void SetStartup() + { + AutoStartup.Enable(); + } + + public void RemoveStartup() + { + AutoStartup.Disable(); + } + #endregion + #region plugin public static string Plugin => @"https://github.com/Flow-Launcher/Flow.Launcher.PluginsManifest";