11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4+ using System . Windows ;
45using System . Windows . Forms ;
56using CommunityToolkit . Mvvm . Input ;
67using 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
0 commit comments