Skip to content

Commit fe70a72

Browse files
committed
Add admin mode configuration in general page
1 parent 40cf413 commit fe70a72

File tree

3 files changed

+83
-11
lines changed

3 files changed

+83
-11
lines changed

Flow.Launcher/Languages/en.xaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@
132132
<system:String x:Key="historyResultsForHomePage">Show History Results in Home Page</system:String>
133133
<system:String x:Key="historyResultsCountForHomePage">Maximum History Results Shown in Home Page</system:String>
134134
<system:String x:Key="homeToggleBoxToolTip">This can only be edited if plugin supports Home feature and Home Page is enabled.</system:String>
135+
<system:String x:Key="alwaysRunAsAdministrator">Always run as administrator</system:String>
136+
<system:String x:Key="alwaysRunAsAdministratorToolTip">Run Flow Launcher as administrator on system startup</system:String>
137+
<system:String x:Key="runAsAdministratorChange">Administrator Mode Change</system:String>
138+
<system:String x:Key="runAsAdministratorChangeAndRestart">Do you want to restart to apply administrator mode change?</system:String>
135139

136140
<!-- Setting Plugin -->
137141
<system:String x:Key="searchplugin">Search Plugin</system:String>

Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Windows;
45
using System.Windows.Forms;
56
using CommunityToolkit.Mvvm.Input;
67
using Flow.Launcher.Core;
@@ -22,6 +23,8 @@ public partial class SettingsPaneGeneralViewModel : BaseModel
2223
private readonly Portable _portable;
2324
private readonly Internationalization _translater;
2425

26+
private static readonly bool _isAdministrator = Win32Helper.IsAdministrator();
27+
2528
public SettingsPaneGeneralViewModel(Settings settings, Updater updater, Portable portable, Internationalization translater)
2629
{
2730
Settings = settings;
@@ -65,6 +68,13 @@ public bool StartFlowLauncherOnSystemStartup
6568
{
6669
App.API.ShowMsg(App.API.GetTranslation("setAutoStartFailed"), e.Message);
6770
}
71+
72+
// If we have enabled logon task startup, we need to check if we need to restart the app
73+
// even if we encounter an error while setting the startup method
74+
if (value && UseLogonTaskForStartup)
75+
{
76+
CheckAdminChangeAndAskForRestart();
77+
}
6878
}
6979
}
7080

@@ -92,10 +102,57 @@ public bool UseLogonTaskForStartup
92102
{
93103
App.API.ShowMsg(App.API.GetTranslation("setAutoStartFailed"), e.Message);
94104
}
95-
}
105+
}
106+
107+
// If we have enabled logon task startup, we need to check if we need to restart the app
108+
// even if we encounter an error while setting the startup method
109+
if (StartFlowLauncherOnSystemStartup && value)
110+
{
111+
CheckAdminChangeAndAskForRestart();
112+
}
96113
}
97114
}
98115

116+
public bool AlwaysRunAsAdministrator
117+
{
118+
get => Settings.AlwaysRunAsAdministrator;
119+
set
120+
{
121+
Settings.AlwaysRunAsAdministrator = value;
122+
123+
if (StartFlowLauncherOnSystemStartup && UseLogonTaskForStartup)
124+
{
125+
try
126+
{
127+
AutoStartup.ChangeToViaLogonTask(value);
128+
}
129+
catch (Exception e)
130+
{
131+
App.API.ShowMsg(App.API.GetTranslation("setAutoStartFailed"), e.Message);
132+
}
133+
134+
// If we have enabled logon task startup, we need to check if we need to restart the app
135+
// even if we encounter an error while setting the startup method
136+
CheckAdminChangeAndAskForRestart();
137+
}
138+
}
139+
}
140+
141+
private void CheckAdminChangeAndAskForRestart()
142+
{
143+
if ((AlwaysRunAsAdministrator && !_isAdministrator) || // Change from non-admin to admin
144+
(!AlwaysRunAsAdministrator && _isAdministrator)) // Change from admin to non-admin
145+
{
146+
if (App.API.ShowMsgBox(
147+
App.API.GetTranslation("runAsAdministratorChangeAndRestart"),
148+
App.API.GetTranslation("runAsAdministratorChange"),
149+
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
150+
{
151+
App.API.RestartApp(AlwaysRunAsAdministrator ? "runas" : string.Empty);
152+
}
153+
}
154+
}
155+
99156
public List<SearchWindowScreenData> SearchWindowScreens { get; } =
100157
DropdownDataGeneric<SearchWindowScreens>.GetValues<SearchWindowScreenData>("SearchWindowScreen");
101158

Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,27 @@
4343
OffContent="{DynamicResource disable}"
4444
OnContent="{DynamicResource enable}" />
4545
</cc:ExCard.SideContent>
46-
<cc:Card
47-
Title="{DynamicResource useLogonTaskForStartup}"
48-
Sub="{DynamicResource useLogonTaskForStartupTooltip}"
49-
Type="InsideFit">
50-
<ui:ToggleSwitch
51-
IsOn="{Binding UseLogonTaskForStartup}"
52-
OffContent="{DynamicResource disable}"
53-
OnContent="{DynamicResource enable}" />
54-
</cc:Card>
46+
47+
<cc:CardGroup>
48+
<cc:Card
49+
Title="{DynamicResource useLogonTaskForStartup}"
50+
Icon="&#xE823;"
51+
Sub="{DynamicResource useLogonTaskForStartupTooltip}">
52+
<ui:ToggleSwitch
53+
IsOn="{Binding UseLogonTaskForStartup}"
54+
OffContent="{DynamicResource disable}"
55+
OnContent="{DynamicResource enable}" />
56+
</cc:Card>
57+
<cc:Card
58+
Title="{DynamicResource alwaysRunAsAdministrator}"
59+
Icon="&#xE7EF;"
60+
Sub="{DynamicResource alwaysRunAsAdministratorToolTip}">
61+
<ui:ToggleSwitch
62+
IsOn="{Binding AlwaysRunAsAdministrator}"
63+
OffContent="{DynamicResource disable}"
64+
OnContent="{DynamicResource enable}" />
65+
</cc:Card>
66+
</cc:CardGroup>
5567
</cc:ExCard>
5668
<cc:Card
5769
Title="{DynamicResource hideOnStartup}"
@@ -69,7 +81,6 @@
6981
OffContent="{DynamicResource disable}"
7082
OnContent="{DynamicResource enable}" />
7183
</cc:Card>
72-
7384
<cc:Card Title="{DynamicResource hideNotifyIcon}" Sub="{DynamicResource hideNotifyIconToolTip}">
7485
<ui:ToggleSwitch
7586
IsOn="{Binding Settings.HideNotifyIcon}"

0 commit comments

Comments
 (0)