Skip to content

Commit 1d02c30

Browse files
authored
Merge pull request #1299 from nachmore/bug_1269
Fix crash when user does not have access to registry startup keys
2 parents 96d9309 + ae177a3 commit 1d02c30

File tree

9 files changed

+114
-55
lines changed

9 files changed

+114
-55
lines changed

Flow.Launcher/App.xaml.cs

Lines changed: 13 additions & 5 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,14 +104,22 @@ await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () =>
104104
});
105105
}
106106

107-
108107
private void AutoStartup()
109108
{
110-
if (_settings.StartFlowLauncherOnSystemStartup)
109+
// we try to enable auto-startup on first launch, or reenable if it was removed
110+
// but the user still has the setting set
111+
if (_settings.StartFlowLauncherOnSystemStartup && !Helper.AutoStartup.IsEnabled)
111112
{
112-
if (!SettingWindow.StartupSet())
113+
try
114+
{
115+
Helper.AutoStartup.Enable();
116+
}
117+
catch (Exception e)
113118
{
114-
SettingWindow.SetStartup();
119+
// but if it fails (permissions, etc) then don't keep retrying
120+
// this also gives the user a visual indication in the Settings widget
121+
_settings.StartFlowLauncherOnSystemStartup = false;
122+
Notification.Show(InternationalizationManager.Instance.GetTranslation("setAutoStartFailed"), e.Message);
115123
}
116124
}
117125
}

Flow.Launcher/Helper/AutoStartup.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 (querying if enabled): {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", $"Failed to disable auto-startup: {e}");
45+
throw;
46+
}
47+
}
48+
49+
internal static void Enable()
50+
{
51+
try
52+
{
53+
using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true);
54+
key?.SetValue(Constant.FlowLauncher, Constant.ExecutablePath);
55+
}
56+
catch (Exception e)
57+
{
58+
Log.Error("AutoStartup", $"Failed to enable auto-startup: {e}");
59+
throw;
60+
}
61+
}
62+
}
63+
}

Flow.Launcher/Languages/en.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<system:String x:Key="portableMode">Portable Mode</system:String>
3131
<system:String x:Key="portableModeToolTIp">Store all settings and user data in one folder (Useful when used with removable drives or cloud services).</system:String>
3232
<system:String x:Key="startFlowLauncherOnSystemStartup">Start Flow Launcher on system startup</system:String>
33+
<system:String x:Key="setAutoStartFailed">Error setting launch on startup</system:String>
3334
<system:String x:Key="hideFlowLauncherWhenLoseFocus">Hide Flow Launcher when focus is lost</system:String>
3435
<system:String x:Key="dontPromptUpdateMsg">Do not show new version notifications</system:String>
3536
<system:String x:Key="rememberLastLocation">Remember last launch location</system:String>

Flow.Launcher/Notification.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal static void Uninstall()
1818
}
1919

2020
[System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
21-
public static void Show(string title, string subTitle, string iconPath)
21+
public static void Show(string title, string subTitle, string iconPath = null)
2222
{
2323
// Handle notification for win7/8/early win10
2424
if (legacy)
@@ -45,4 +45,4 @@ private static void LegacyShow(string title, string subTitle, string iconPath)
4545
msg.Show(title, subTitle, iconPath);
4646
}
4747
}
48-
}
48+
}

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 2 additions & 2 deletions
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.Linq;
44
using System.Net;
@@ -286,4 +286,4 @@ private bool KListener_hookedKeyboardCallback(KeyEvent keyevent, int vkcode, Spe
286286

287287
#endregion
288288
}
289-
}
289+
}

Flow.Launcher/SettingWindow.xaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,10 +598,8 @@
598598
<TextBlock Style="{DynamicResource SettingTitleLabel}" Text="{DynamicResource startFlowLauncherOnSystemStartup}" />
599599
</StackPanel>
600600
<CheckBox
601-
Checked="OnAutoStartupChecked"
602-
IsChecked="{Binding Settings.StartFlowLauncherOnSystemStartup}"
603-
Style="{DynamicResource SideControlCheckBox}"
604-
Unchecked="OnAutoStartupUncheck" />
601+
IsChecked="{Binding StartFlowLauncherOnSystemStartup}"
602+
Style="{DynamicResource SideControlCheckBox}" />
605603
<TextBlock Style="{StaticResource Glyph}">
606604
&#xe8fc;
607605
</TextBlock>

Flow.Launcher/SettingWindow.xaml.cs

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
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;
55
using Flow.Launcher.Infrastructure;
66
using Flow.Launcher.Infrastructure.Hotkey;
7+
using Flow.Launcher.Infrastructure.Logger;
78
using Flow.Launcher.Infrastructure.UserSettings;
89
using Flow.Launcher.Plugin;
910
using Flow.Launcher.Plugin.SharedCommands;
@@ -30,8 +31,6 @@ namespace Flow.Launcher
3031
{
3132
public partial class SettingWindow
3233
{
33-
private const string StartupPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
34-
3534
public readonly IPublicAPI API;
3635
private Settings settings;
3736
private SettingWindowViewModel viewModel;
@@ -58,39 +57,6 @@ private void OnLoaded(object sender, RoutedEventArgs e)
5857
hwndTarget.RenderMode = RenderMode.SoftwareOnly;
5958
}
6059

61-
private void OnAutoStartupChecked(object sender, RoutedEventArgs e)
62-
{
63-
SetStartup();
64-
}
65-
66-
private void OnAutoStartupUncheck(object sender, RoutedEventArgs e)
67-
{
68-
RemoveStartup();
69-
}
70-
71-
public static void SetStartup()
72-
{
73-
using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true);
74-
key?.SetValue(Constant.FlowLauncher, Constant.ExecutablePath);
75-
}
76-
77-
private void RemoveStartup()
78-
{
79-
using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true);
80-
key?.DeleteValue(Constant.FlowLauncher, false);
81-
}
82-
83-
public static bool StartupSet()
84-
{
85-
using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true);
86-
var path = key?.GetValue(Constant.FlowLauncher) as string;
87-
if (path != null)
88-
{
89-
return path == Constant.ExecutablePath;
90-
}
91-
return false;
92-
}
93-
9460
private void OnSelectPythonDirectoryClick(object sender, RoutedEventArgs e)
9561
{
9662
var dlg = new FolderBrowserDialog
@@ -384,4 +350,4 @@ private void ItemSizeChanged(object sender, SizeChangedEventArgs e)
384350
Plugins.ScrollIntoView(Plugins.SelectedItem);
385351
}
386352
}
387-
}
353+
}

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 4 additions & 4 deletions
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.Linq;
44
using System.Threading;
@@ -306,8 +306,8 @@ private void InitializeKeyCommands()
306306
{
307307
Notification.Show(
308308
InternationalizationManager.Instance.GetTranslation("success"),
309-
InternationalizationManager.Instance.GetTranslation("completedSuccessfully"),
310-
"");
309+
InternationalizationManager.Instance.GetTranslation("completedSuccessfully")
310+
);
311311
}))
312312
.ConfigureAwait(false);
313313
});
@@ -927,4 +927,4 @@ public void ResultCopy(string stringToCopy)
927927

928928
#endregion
929929
}
930-
}
930+
}

Flow.Launcher/ViewModel/SettingWindowViewModel.cs

Lines changed: 24 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;
@@ -60,7 +60,30 @@ public bool AutoUpdates
6060
Settings.AutoUpdates = value;
6161

6262
if (value)
63+
{
6364
UpdateApp();
65+
}
66+
}
67+
}
68+
69+
public bool StartFlowLauncherOnSystemStartup
70+
{
71+
get => Settings.StartFlowLauncherOnSystemStartup;
72+
set
73+
{
74+
Settings.StartFlowLauncherOnSystemStartup = value;
75+
76+
try
77+
{
78+
if (value)
79+
AutoStartup.Enable();
80+
else
81+
AutoStartup.Disable();
82+
}
83+
catch (Exception e)
84+
{
85+
Notification.Show(InternationalizationManager.Instance.GetTranslation("setAutoStartFailed"), e.Message);
86+
}
6487
}
6588
}
6689

0 commit comments

Comments
 (0)