Add new api RestartAppAsAdmin

This commit is contained in:
Jack251970 2025-06-13 18:07:42 +08:00
parent f2d3ba712f
commit 3a921022f3
3 changed files with 18 additions and 20 deletions

View file

@ -30,10 +30,15 @@ namespace Flow.Launcher.Plugin
void ChangeQuery(string query, bool requery = false);
/// <summary>
/// Restart Flow Launcher
/// Restart Flow Launcher without changing the user privileges.
/// </summary>
void RestartApp();
/// <summary>
/// Restart Flow Launcher as administrator.
/// </summary>
void RestartAppAsAdmin();
/// <summary>
/// Run a shell command
/// </summary>

View file

@ -72,8 +72,12 @@ namespace Flow.Launcher
_mainVM.ChangeQueryText(query, requery);
}
public void RestartApp() => RestartApp(false);
public void RestartAppAsAdmin() => RestartApp(true);
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD100:Avoid async void methods", Justification = "<Pending>")]
public async void RestartApp()
private async void RestartApp(bool runAsAdmin)
{
_mainVM.Hide();
@ -88,7 +92,7 @@ namespace Flow.Launcher
// Restart requires Squirrel's Update.exe to be present in the parent folder,
// it is only published from the project's release pipeline. When debugging without it,
// the project may not restart or just terminates. This is expected.
App.RestartApp();
App.RestartApp(runAsAdmin);
}
public void ShowMainWindow() => _mainVM.Show();

View file

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using CommunityToolkit.Mvvm.Input;
@ -10,7 +9,6 @@ using Flow.Launcher.Core.Configuration;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Image;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
using Flow.Launcher.Plugin.SharedModels;
@ -75,7 +73,7 @@ public partial class SettingsPaneGeneralViewModel : BaseModel
// even if we encounter an error while setting the startup method
if (value && UseLogonTaskForStartup)
{
_ = CheckAdminChangeAndAskForRestartAsync();
CheckAdminChangeAndAskForRestart();
}
}
}
@ -112,7 +110,7 @@ public partial class SettingsPaneGeneralViewModel : BaseModel
// even if we encounter an error while setting the startup method
if (StartFlowLauncherOnSystemStartup && value)
{
_ = CheckAdminChangeAndAskForRestartAsync();
CheckAdminChangeAndAskForRestart();
}
}
}
@ -139,12 +137,12 @@ public partial class SettingsPaneGeneralViewModel : BaseModel
// 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
_ = CheckAdminChangeAndAskForRestartAsync();
CheckAdminChangeAndAskForRestart();
}
}
}
private async Task CheckAdminChangeAndAskForRestartAsync()
private void CheckAdminChangeAndAskForRestart()
{
// When we change from non-admin to admin, we need to restart the app as administrator to apply the changes
// Under non-administrator, we cannot delete or set the logon task which is run as administrator
@ -155,17 +153,8 @@ public partial class SettingsPaneGeneralViewModel : BaseModel
App.API.GetTranslation("runAsAdministratorChange"),
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
App.API.HideMainWindow();
// We must manually save because of Environment.Exit(0)
// which will cause ungraceful exit
App.API.SaveAppAllSettings();
// Wait for all image caches to be saved before restarting
await ImageLoader.WaitSaveAsync();
// Restart the app as administrator
App.RestartApp(true);
App.API.RestartAppAsAdmin();
}
}
}
@ -195,7 +184,7 @@ public partial class SettingsPaneGeneralViewModel : BaseModel
}
// This is only required to set at startup. When portable mode enabled/disabled a restart is always required
private static bool _portableMode = DataLocation.PortableDataLocationInUse();
private static readonly bool _portableMode = DataLocation.PortableDataLocationInUse();
public bool PortableMode
{