From fe70a72a626a74d0ef88c0d359a2f448642329e1 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Thu, 22 May 2025 22:04:47 +0800 Subject: [PATCH] Add admin mode configuration in general page --- Flow.Launcher/Languages/en.xaml | 4 ++ .../SettingsPaneGeneralViewModel.cs | 59 ++++++++++++++++++- .../Views/SettingsPaneGeneral.xaml | 31 ++++++---- 3 files changed, 83 insertions(+), 11 deletions(-) diff --git a/Flow.Launcher/Languages/en.xaml b/Flow.Launcher/Languages/en.xaml index 2abeb28f5..de9ba2f27 100644 --- a/Flow.Launcher/Languages/en.xaml +++ b/Flow.Launcher/Languages/en.xaml @@ -132,6 +132,10 @@ Show History Results in Home Page Maximum History Results Shown in Home Page This can only be edited if plugin supports Home feature and Home Page is enabled. + Always run as administrator + Run Flow Launcher as administrator on system startup + Administrator Mode Change + Do you want to restart to apply administrator mode change? Search Plugin diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs index eed81fe17..edb2aacf7 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Windows; using System.Windows.Forms; using CommunityToolkit.Mvvm.Input; using Flow.Launcher.Core; @@ -22,6 +23,8 @@ public partial class SettingsPaneGeneralViewModel : BaseModel private readonly Portable _portable; private readonly Internationalization _translater; + private static readonly bool _isAdministrator = Win32Helper.IsAdministrator(); + public SettingsPaneGeneralViewModel(Settings settings, Updater updater, Portable portable, Internationalization translater) { Settings = settings; @@ -65,6 +68,13 @@ public partial class SettingsPaneGeneralViewModel : BaseModel { App.API.ShowMsg(App.API.GetTranslation("setAutoStartFailed"), e.Message); } + + // If we have enabled logon task startup, we need to check if we need to restart the app + // even if we encounter an error while setting the startup method + if (value && UseLogonTaskForStartup) + { + CheckAdminChangeAndAskForRestart(); + } } } @@ -92,10 +102,57 @@ public partial class SettingsPaneGeneralViewModel : BaseModel { App.API.ShowMsg(App.API.GetTranslation("setAutoStartFailed"), e.Message); } - } + } + + // If we have enabled logon task startup, we need to check if we need to restart the app + // even if we encounter an error while setting the startup method + if (StartFlowLauncherOnSystemStartup && value) + { + CheckAdminChangeAndAskForRestart(); + } } } + public bool AlwaysRunAsAdministrator + { + get => Settings.AlwaysRunAsAdministrator; + set + { + Settings.AlwaysRunAsAdministrator = value; + + if (StartFlowLauncherOnSystemStartup && UseLogonTaskForStartup) + { + try + { + AutoStartup.ChangeToViaLogonTask(value); + } + catch (Exception e) + { + App.API.ShowMsg(App.API.GetTranslation("setAutoStartFailed"), e.Message); + } + + // If we have enabled logon task startup, we need to check if we need to restart the app + // even if we encounter an error while setting the startup method + CheckAdminChangeAndAskForRestart(); + } + } + } + + private void CheckAdminChangeAndAskForRestart() + { + if ((AlwaysRunAsAdministrator && !_isAdministrator) || // Change from non-admin to admin + (!AlwaysRunAsAdministrator && _isAdministrator)) // Change from admin to non-admin + { + if (App.API.ShowMsgBox( + App.API.GetTranslation("runAsAdministratorChangeAndRestart"), + App.API.GetTranslation("runAsAdministratorChange"), + MessageBoxButton.YesNo) == MessageBoxResult.Yes) + { + App.API.RestartApp(AlwaysRunAsAdministrator ? "runas" : string.Empty); + } + } + } + public List SearchWindowScreens { get; } = DropdownDataGeneric.GetValues("SearchWindowScreen"); diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml b/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml index c0c5613de..1871a92ff 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml @@ -43,15 +43,27 @@ OffContent="{DynamicResource disable}" OnContent="{DynamicResource enable}" /> - - - + + + + + + + + + -