feat(插件): 添加应用重启服务接口及实现

添加 IAppRestartService 接口及其实现 AppRestartService,用于插件系统调用应用重启功能
将原 StartupPage 中的重启逻辑提取到 AppRestartHelper 工具类中
在 App.xaml.cs 中注册 AppRestartService 供插件使用
This commit is contained in:
PrefacedCorg
2026-04-21 02:16:58 +08:00
parent cab703b98e
commit 259ce3dd11
5 changed files with 183 additions and 47 deletions
+11
View File
@@ -1113,6 +1113,17 @@ namespace Ink_Canvas
LogHelper.WriteLogToFile($"Failed to register InkCanvasService: {ex.Message}", LogHelper.LogType.Error);
}
try
{
var appRestartService = new Plugins.AppRestartService();
Plugins.PluginManager.Instance.RegisterService<Plugins.IAppRestartService>(appRestartService);
LogHelper.WriteLogToFile("AppRestartService registered for plugins");
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"Failed to register AppRestartService: {ex.Message}", LogHelper.LogType.Error);
}
// 主窗口加载完成后关闭启动画面
mainWindow.Loaded += (s, args) =>
{
+106
View File
@@ -0,0 +1,106 @@
using Ink_Canvas.Windows.SettingsViews.Helpers;
using System;
using System.Diagnostics;
using System.Security.Principal;
using System.Windows;
namespace Ink_Canvas.Helpers
{
public static class AppRestartHelper
{
public static bool IsRunningAsAdmin()
{
try
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch
{
return false;
}
}
public static void RestartApp(bool asAdmin)
{
try
{
App.IsAppExitByUser = true;
(Application.Current as App)?.ReleaseMutexForRestart();
string exePath = Process.GetCurrentProcess().MainModule.FileName;
if (asAdmin)
{
var psi = new ProcessStartInfo(exePath) { UseShellExecute = true, Verb = "runas" };
Process.Start(psi);
}
else
{
Process.Start("explorer.exe", "\"" + exePath + "\"");
}
Application.Current.Shutdown();
}
catch (Exception ex)
{
Debug.WriteLine($"重启应用时出错: {ex.Message}");
}
}
public static void RestartWithCurrentPrivileges()
{
RestartApp(IsRunningAsAdmin());
}
public static void RestartAsAdmin()
{
RestartApp(true);
}
public static void RestartAsNormal()
{
RestartApp(false);
}
public static void SwitchToUIATopMostAndRestart()
{
try
{
SettingsManager.Settings.Advanced.EnableUIAccessTopMost = true;
if (!SettingsManager.Settings.Advanced.IsAlwaysOnTop)
{
SettingsManager.Settings.Advanced.IsAlwaysOnTop = true;
}
SettingsManager.SaveSettingsToFile();
App.IsUIAccessTopMostEnabled = true;
RestartApp(true);
}
catch (Exception ex)
{
Debug.WriteLine($"切换到UIA置顶模式时出错: {ex.Message}");
}
}
public static void SwitchToNormalTopMostAndRestart()
{
try
{
SettingsManager.Settings.Advanced.EnableUIAccessTopMost = false;
SettingsManager.SaveSettingsToFile();
App.IsUIAccessTopMostEnabled = false;
RestartApp(IsRunningAsAdmin());
}
catch (Exception ex)
{
Debug.WriteLine($"切换到普通置顶模式时出错: {ex.Message}");
}
}
}
}
+40
View File
@@ -0,0 +1,40 @@
using Ink_Canvas.Helpers;
using Ink_Canvas.Plugins;
namespace Ink_Canvas.Plugins
{
public class AppRestartService : IAppRestartService
{
public bool IsRunningAsAdmin => AppRestartHelper.IsRunningAsAdmin();
public void RestartApp(bool asAdmin)
{
AppRestartHelper.RestartApp(asAdmin);
}
public void RestartWithCurrentPrivileges()
{
AppRestartHelper.RestartWithCurrentPrivileges();
}
public void RestartAsAdmin()
{
AppRestartHelper.RestartAsAdmin();
}
public void RestartAsNormal()
{
AppRestartHelper.RestartAsNormal();
}
public void SwitchToUIATopMostAndRestart()
{
AppRestartHelper.SwitchToUIATopMostAndRestart();
}
public void SwitchToNormalTopMostAndRestart()
{
AppRestartHelper.SwitchToNormalTopMostAndRestart();
}
}
}
@@ -1,8 +1,8 @@
using Ink_Canvas.Helpers;
using Ink_Canvas.Windows.SettingsViews.Helpers;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Security.Principal;
using System.Windows;
using System.Windows.Controls;
@@ -28,52 +28,10 @@ namespace Ink_Canvas.Windows.SettingsViews.Pages
_isLoaded = true;
}
private static bool IsRunningAsAdmin()
{
try
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch
{
return false;
}
}
private void RestartApp(bool asAdmin)
{
try
{
App.IsAppExitByUser = true;
(Application.Current as App)?.ReleaseMutexForRestart();
string exePath = Process.GetCurrentProcess().MainModule.FileName;
if (asAdmin)
{
var psi = new ProcessStartInfo(exePath) { UseShellExecute = true, Verb = "runas" };
Process.Start(psi);
}
else
{
Process.Start("explorer.exe", "\"" + exePath + "\"");
}
Application.Current.Shutdown();
}
catch (Exception ex)
{
Debug.WriteLine($"重启应用时出错: {ex.Message}");
}
}
private void LoadSettings()
{
_isLoaded = false;
_isAdmin = IsRunningAsAdmin();
_isAdmin = AppRestartHelper.IsRunningAsAdmin();
try
{
@@ -257,7 +215,7 @@ namespace Ink_Canvas.Windows.SettingsViews.Pages
if (result == MessageBoxResult.Yes)
{
RestartApp(_isAdmin);
AppRestartHelper.RestartWithCurrentPrivileges();
}
}
catch (Exception ex)
@@ -288,7 +246,7 @@ namespace Ink_Canvas.Windows.SettingsViews.Pages
if (result == MessageBoxResult.Yes)
{
App.IsUIAccessTopMostEnabled = true;
RestartApp(true);
AppRestartHelper.RestartAsAdmin();
}
}
catch (Exception ex)
@@ -301,7 +259,7 @@ namespace Ink_Canvas.Windows.SettingsViews.Pages
{
if (sender is Button btn && btn.Tag is bool asAdmin)
{
RestartApp(asAdmin);
AppRestartHelper.RestartApp(asAdmin);
}
}
+21
View File
@@ -0,0 +1,21 @@
using System;
namespace Ink_Canvas.Plugins
{
public interface IAppRestartService
{
bool IsRunningAsAdmin { get; }
void RestartApp(bool asAdmin);
void RestartWithCurrentPrivileges();
void RestartAsAdmin();
void RestartAsNormal();
void SwitchToUIATopMostAndRestart();
void SwitchToNormalTopMostAndRestart();
}
}