Skip to content

Commit bde85c8

Browse files
committed
Refactor auto-startup registry code into its own helper
1 parent c3a67d4 commit bde85c8

File tree

4 files changed

+83
-56
lines changed

4 files changed

+83
-56
lines changed

Flow.Launcher/App.xaml.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Diagnostics;
33
using System.Text;
44
using System.Threading.Tasks;
@@ -104,15 +104,11 @@ await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () =>
104104
});
105105
}
106106

107-
108107
private void AutoStartup()
109108
{
110-
if (_settings.StartFlowLauncherOnSystemStartup)
109+
if (_settings.StartFlowLauncherOnSystemStartup && !Helper.AutoStartup.IsEnabled)
111110
{
112-
if (!SettingWindow.StartupSet())
113-
{
114-
SettingWindow.SetStartup();
115-
}
111+
Helper.AutoStartup.Enable();
116112
}
117113
}
118114

Flow.Launcher/Helper/AutoStartup.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Flow.Launcher.Infrastructure;
7+
using Flow.Launcher.Infrastructure.Logger;
8+
using Microsoft.Win32;
9+
10+
namespace Flow.Launcher.Helper
11+
{
12+
public class AutoStartup
13+
{
14+
private const string StartupPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
15+
16+
public static bool IsEnabled
17+
{
18+
get
19+
{
20+
try
21+
{
22+
using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true);
23+
var path = key?.GetValue(Constant.FlowLauncher) as string;
24+
return path == Constant.ExecutablePath;
25+
}
26+
catch (Exception e)
27+
{
28+
Log.Error("AutoStartup", $"Ignoring non-critical registry error (user permissions?): {e}");
29+
}
30+
31+
return false;
32+
}
33+
}
34+
35+
public static void Disable()
36+
{
37+
try
38+
{
39+
using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true);
40+
key?.DeleteValue(Constant.FlowLauncher, false);
41+
}
42+
catch (Exception e)
43+
{
44+
Log.Error("AutoStartup", $"Ignoring non-critical registry error (user permissions?): {e}");
45+
}
46+
}
47+
48+
internal static void Enable()
49+
{
50+
try
51+
{
52+
using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true);
53+
key?.SetValue(Constant.FlowLauncher, Constant.ExecutablePath);
54+
}
55+
catch (Exception e)
56+
{
57+
Log.Error("AutoStartup", $"Ignoring non-critical registry error (user permissions?): {e}");
58+
}
59+
60+
}
61+
}
62+
}

Flow.Launcher/SettingWindow.xaml.cs

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Flow.Launcher.Core.ExternalPlugins;
1+
using Flow.Launcher.Core.ExternalPlugins;
22
using Flow.Launcher.Core.Plugin;
33
using Flow.Launcher.Core.Resource;
44
using Flow.Launcher.Helper;
@@ -31,8 +31,6 @@ namespace Flow.Launcher
3131
{
3232
public partial class SettingWindow
3333
{
34-
private const string StartupPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
35-
3634
public readonly IPublicAPI API;
3735
private Settings settings;
3836
private SettingWindowViewModel viewModel;
@@ -61,54 +59,12 @@ private void OnLoaded(object sender, RoutedEventArgs e)
6159

6260
private void OnAutoStartupChecked(object sender, RoutedEventArgs e)
6361
{
64-
SetStartup();
62+
viewModel.SetStartup();
6563
}
6664

6765
private void OnAutoStartupUncheck(object sender, RoutedEventArgs e)
6866
{
69-
RemoveStartup();
70-
}
71-
72-
public static void SetStartup()
73-
{
74-
try
75-
{
76-
using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true);
77-
key?.SetValue(Constant.FlowLauncher, Constant.ExecutablePath);
78-
}
79-
catch (Exception e)
80-
{
81-
Log.Error("SettingsWindow", $"Ignoring non-critical registry error (user permissions?): {e}");
82-
}
83-
}
84-
85-
private void RemoveStartup()
86-
{
87-
try
88-
{
89-
using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true);
90-
key?.DeleteValue(Constant.FlowLauncher, false);
91-
}
92-
catch (Exception e)
93-
{
94-
Log.Error("SettingsWindow", $"Ignoring non-critical registry error (user permissions?): {e}");
95-
}
96-
}
97-
98-
public static bool StartupSet()
99-
{
100-
try
101-
{
102-
using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true);
103-
var path = key?.GetValue(Constant.FlowLauncher) as string;
104-
return path == Constant.ExecutablePath;
105-
}
106-
catch (Exception e)
107-
{
108-
Log.Error("SettingsWindow", $"Ignoring non-critical registry error (user permissions?): {e}");
109-
}
110-
111-
return false;
67+
viewModel.RemoveStartup();
11268
}
11369

11470
private void OnSelectPythonDirectoryClick(object sender, RoutedEventArgs e)
@@ -404,4 +360,4 @@ private void ItemSizeChanged(object sender, SizeChangedEventArgs e)
404360
Plugins.ScrollIntoView(Plugins.SelectedItem);
405361
}
406362
}
407-
}
363+
}

Flow.Launcher/ViewModel/SettingWindowViewModel.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
@@ -216,6 +216,19 @@ public string TestProxy()
216216

217217
#endregion
218218

219+
#region startup
220+
221+
public void SetStartup()
222+
{
223+
AutoStartup.Enable();
224+
}
225+
226+
public void RemoveStartup()
227+
{
228+
AutoStartup.Disable();
229+
}
230+
#endregion
231+
219232
#region plugin
220233

221234
public static string Plugin => @"https://github.com/Flow-Launcher/Flow.Launcher.PluginsManifest";

0 commit comments

Comments
 (0)