Refactor auto-startup registry code into its own helper

This commit is contained in:
Oren Nachman 2022-07-29 00:09:33 -07:00
parent c3a67d46f6
commit bde85c87a7
4 changed files with 83 additions and 56 deletions

View file

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

View file

@ -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}");
}
}
}
}

View file

@ -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);
}
}
}
}

View file

@ -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";