add ability to create desktop shortcut

This commit is contained in:
Lightczx
2023-09-07 17:01:41 +08:00
parent a7bf82d7e7
commit 4e972f35dd
9 changed files with 124 additions and 14 deletions

View File

@@ -74,10 +74,10 @@ public sealed partial class App : Application
private void LogDiagnosticInformation()
{
RuntimeOptions hutaoOptions = serviceProvider.GetRequiredService<RuntimeOptions>();
RuntimeOptions runtimeOptions = serviceProvider.GetRequiredService<RuntimeOptions>();
logger.LogInformation("FamilyName: {name}", hutaoOptions.FamilyName);
logger.LogInformation("Version: {version}", hutaoOptions.Version);
logger.LogInformation("LocalCache: {folder}", hutaoOptions.LocalCache);
logger.LogInformation("FamilyName: {name}", runtimeOptions.FamilyName);
logger.LogInformation("Version: {version}", runtimeOptions.Version);
logger.LogInformation("LocalCache: {folder}", runtimeOptions.LocalCache);
}
}

View File

@@ -0,0 +1,9 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
namespace Snap.Hutao.Core.Shell;
internal interface IShellLinkInterop
{
void CreateDesktopShoutcutForElevatedLaunch();
}

View File

@@ -0,0 +1,35 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using System.IO;
using Windows.Win32;
using Windows.Win32.System.Com;
using Windows.Win32.UI.Shell;
using Windows.Win32.UI.WindowsAndMessaging;
namespace Snap.Hutao.Core.Shell;
[ConstructorGenerated]
[Injection(InjectAs.Transient, typeof(IShellLinkInterop))]
internal sealed partial class ShellLinkInterop : IShellLinkInterop
{
private readonly RuntimeOptions runtimeOptions;
public void CreateDesktopShoutcutForElevatedLaunch()
{
IShellLinkW shellLink = (IShellLinkW)new ShellLink();
shellLink.SetPath("powershell");
shellLink.SetArguments($"""
-Command "Start-Process shell:AppsFolder\{runtimeOptions.FamilyName}!App -verb runas"
""");
shellLink.SetShowCmd(SHOW_WINDOW_CMD.SW_SHOWMINNOACTIVE);
string iconPath = Path.Combine(runtimeOptions.InstalledLocation, "Snap.Hutao.exe");
shellLink.SetIconLocation(iconPath, 0);
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string target = Path.Combine(desktop, $"{SH.AppNameAndVersion.Format(runtimeOptions.Version)}.lnk");
IPersistFile persistFile = (IPersistFile)shellLink;
persistFile.Save(target, false);
}
}

View File

@@ -41,6 +41,12 @@ GetWindowThreadProcessId
ReleaseDC
SetForegroundWindow
// COM
IPersistFile
IShellLinkW
ShellLink
SHELL_LINK_DATA_FLAGS
// WinRT
IMemoryBufferByteAccess
@@ -60,6 +66,4 @@ LPTHREAD_START_ROUTINE
MINMAXINFO
// System.Com
CWMO_FLAGS
//IShellLinkW
CWMO_FLAGS

View File

@@ -3354,6 +3354,15 @@ namespace Snap.Hutao.Resource.Localization {
}
}
/// <summary>
/// 查找类似 操作完成 的本地化字符串。
/// </summary>
internal static string ViewModelSettingActionComplete {
get {
return ResourceManager.GetString("ViewModelSettingActionComplete", resourceCulture);
}
}
/// <summary>
/// 查找类似 清除失败,文件目录权限不足,请使用管理员模式重试 的本地化字符串。
/// </summary>
@@ -5109,6 +5118,33 @@ namespace Snap.Hutao.Resource.Localization {
}
}
/// <summary>
/// 查找类似 创建 的本地化字符串。
/// </summary>
internal static string ViewPageSettingCreateDesktopShortcutAction {
get {
return ResourceManager.GetString("ViewPageSettingCreateDesktopShortcutAction", resourceCulture);
}
}
/// <summary>
/// 查找类似 在桌面上创建默认以管理员方式启动的快捷方式,更新后需要重新创建 的本地化字符串。
/// </summary>
internal static string ViewPageSettingCreateDesktopShortcutDescription {
get {
return ResourceManager.GetString("ViewPageSettingCreateDesktopShortcutDescription", resourceCulture);
}
}
/// <summary>
/// 查找类似 创建快捷方式 的本地化字符串。
/// </summary>
internal static string ViewPageSettingCreateDesktopShortcutHeader {
get {
return ResourceManager.GetString("ViewPageSettingCreateDesktopShortcutHeader", resourceCulture);
}
}
/// <summary>
/// 查找类似 执行 的本地化字符串。
/// </summary>

View File

@@ -1271,6 +1271,9 @@
<data name="ViewModelLaunchGameSwitchGameAccountFail" xml:space="preserve">
<value>切换账号失败</value>
</data>
<data name="ViewModelSettingActionComplete" xml:space="preserve">
<value>操作完成</value>
</data>
<data name="ViewModelSettingClearWebCacheFail" xml:space="preserve">
<value>清除失败,文件目录权限不足,请使用管理员模式重试</value>
</data>
@@ -1856,6 +1859,15 @@
<data name="ViewPageSettingCopyDeviceIdAction" xml:space="preserve">
<value>复制</value>
</data>
<data name="ViewPageSettingCreateDesktopShortcutAction" xml:space="preserve">
<value>创建</value>
</data>
<data name="ViewPageSettingCreateDesktopShortcutDescription" xml:space="preserve">
<value>在桌面上创建默认以管理员方式启动的快捷方式,更新后需要重新创建</value>
</data>
<data name="ViewPageSettingCreateDesktopShortcutHeader" xml:space="preserve">
<value>创建快捷方式</value>
</data>
<data name="ViewPageSettingDangerousAction" xml:space="preserve">
<value>执行</value>
</data>

View File

@@ -2,12 +2,10 @@
// Licensed under the MIT license.
using Microsoft.Extensions.Primitives;
using Snap.Hutao.Core.ExceptionService;
using Snap.Hutao.Core.Windowing;
using Snap.Hutao.Model;
using Snap.Hutao.Model.Entity;
using Snap.Hutao.Service.Abstraction;
using Snap.Hutao.Service.Game;
using System.Globalization;
using System.IO;

View File

@@ -91,6 +91,13 @@
Description="{Binding HutaoOptions.WebView2Version}"
Header="{shcm:ResourceString Name=ViewPageSettingWebview2Header}"
HeaderIcon="{shcm:FontIcon Glyph=&#xECAA;}"/>
<cwc:SettingsCard
ActionIconToolTip="{shcm:ResourceString Name=ViewPageSettingCreateDesktopShortcutAction}"
Command="{Binding CreateDesktopShortcutCommand}"
Description="{shcm:ResourceString Name=ViewPageSettingCreateDesktopShortcutDescription}"
Header="{shcm:ResourceString Name=ViewPageSettingCreateDesktopShortcutHeader}"
HeaderIcon="{shcm:FontIcon Glyph=&#xE7EF;}"
IsClickEnabled="True"/>
<TextBlock Style="{StaticResource SettingsSectionHeaderTextBlockStyle}" Text="{shcm:ResourceString Name=ViewPageSettingApperanceHeader}"/>
<cwc:SettingsCard

View File

@@ -8,6 +8,7 @@ using Snap.Hutao.Core;
using Snap.Hutao.Core.IO;
using Snap.Hutao.Core.IO.DataTransfer;
using Snap.Hutao.Core.Setting;
using Snap.Hutao.Core.Shell;
using Snap.Hutao.Core.Windowing;
using Snap.Hutao.Factory.Abstraction;
using Snap.Hutao.Model;
@@ -36,17 +37,18 @@ namespace Snap.Hutao.ViewModel;
[Injection(InjectAs.Scoped)]
internal sealed partial class SettingViewModel : Abstraction.ViewModel
{
private readonly IGameLocatorFactory gameLocatorFactory;
private readonly IClipboardInterop clipboardInterop;
private readonly IContentDialogFactory contentDialogFactory;
private readonly IGameLocatorFactory gameLocatorFactory;
private readonly INavigationService navigationService;
private readonly IClipboardInterop clipboardInterop;
private readonly IShellLinkInterop shellLinkInterop;
private readonly HutaoUserOptions hutaoUserOptions;
private readonly IInfoBarService infoBarService;
private readonly RuntimeOptions runtimeOptions;
private readonly IPickerFactory pickerFactory;
private readonly IUserService userService;
private readonly IInfoBarService infoBarService;
private readonly ITaskContext taskContext;
private readonly AppOptions appOptions;
private readonly RuntimeOptions runtimeOptions;
private readonly HutaoUserOptions hutaoUserOptions;
private NameValue<BackdropType>? selectedBackdropType;
private NameValue<string>? selectedCulture;
@@ -254,4 +256,11 @@ internal sealed partial class SettingViewModel : Abstraction.ViewModel
infoBarService.Success("无感验证复合 Url 配置成功");
}
}
[Command("CreateDesktopShortcutCommand")]
private void CreateDesktopShortcutForElevatedLaunch()
{
shellLinkInterop.CreateDesktopShoutcutForElevatedLaunch();
infoBarService.Information(SH.ViewModelSettingActionComplete);
}
}