Add admin mode configuration in general page

This commit is contained in:
Jack251970 2025-05-22 22:04:47 +08:00
parent 40cf413898
commit fe70a72a62
3 changed files with 83 additions and 11 deletions

View file

@ -132,6 +132,10 @@
<system:String x:Key="historyResultsForHomePage">Show History Results in Home Page</system:String>
<system:String x:Key="historyResultsCountForHomePage">Maximum History Results Shown in Home Page</system:String>
<system:String x:Key="homeToggleBoxToolTip">This can only be edited if plugin supports Home feature and Home Page is enabled.</system:String>
<system:String x:Key="alwaysRunAsAdministrator">Always run as administrator</system:String>
<system:String x:Key="alwaysRunAsAdministratorToolTip">Run Flow Launcher as administrator on system startup</system:String>
<system:String x:Key="runAsAdministratorChange">Administrator Mode Change</system:String>
<system:String x:Key="runAsAdministratorChangeAndRestart">Do you want to restart to apply administrator mode change?</system:String>
<!-- Setting Plugin -->
<system:String x:Key="searchplugin">Search Plugin</system:String>

View file

@ -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<SearchWindowScreenData> SearchWindowScreens { get; } =
DropdownDataGeneric<SearchWindowScreens>.GetValues<SearchWindowScreenData>("SearchWindowScreen");

View file

@ -43,15 +43,27 @@
OffContent="{DynamicResource disable}"
OnContent="{DynamicResource enable}" />
</cc:ExCard.SideContent>
<cc:Card
Title="{DynamicResource useLogonTaskForStartup}"
Sub="{DynamicResource useLogonTaskForStartupTooltip}"
Type="InsideFit">
<ui:ToggleSwitch
IsOn="{Binding UseLogonTaskForStartup}"
OffContent="{DynamicResource disable}"
OnContent="{DynamicResource enable}" />
</cc:Card>
<cc:CardGroup>
<cc:Card
Title="{DynamicResource useLogonTaskForStartup}"
Icon="&#xE823;"
Sub="{DynamicResource useLogonTaskForStartupTooltip}">
<ui:ToggleSwitch
IsOn="{Binding UseLogonTaskForStartup}"
OffContent="{DynamicResource disable}"
OnContent="{DynamicResource enable}" />
</cc:Card>
<cc:Card
Title="{DynamicResource alwaysRunAsAdministrator}"
Icon="&#xE7EF;"
Sub="{DynamicResource alwaysRunAsAdministratorToolTip}">
<ui:ToggleSwitch
IsOn="{Binding AlwaysRunAsAdministrator}"
OffContent="{DynamicResource disable}"
OnContent="{DynamicResource enable}" />
</cc:Card>
</cc:CardGroup>
</cc:ExCard>
<cc:Card
Title="{DynamicResource hideOnStartup}"
@ -69,7 +81,6 @@
OffContent="{DynamicResource disable}"
OnContent="{DynamicResource enable}" />
</cc:Card>
<cc:Card Title="{DynamicResource hideNotifyIcon}" Sub="{DynamicResource hideNotifyIconToolTip}">
<ui:ToggleSwitch
IsOn="{Binding Settings.HideNotifyIcon}"