From cc41037a2b1d4cc4e2a915760a6b6a514bdc5c4a Mon Sep 17 00:00:00 2001 From: DismissedLight <1686188646@qq.com> Date: Mon, 4 Sep 2023 23:27:01 +0800 Subject: [PATCH] fix a issue where taskbaricon launchgame will open mainwindow --- .../Snap.Hutao/Core/LifeCycle/Activation.cs | 25 +++++++------------ .../Core/LifeCycle/CurrentWindowReference.cs | 22 ++++++++++++++++ .../Snap.Hutao/Core/LifeCycle/IActivation.cs | 1 + .../Core/LifeCycle/ICurrentWindowReference.cs | 11 ++++++++ .../Factory/ContentDialogFactory.cs | 9 ++++--- .../Snap.Hutao/Properties/launchSettings.json | 2 +- 6 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 src/Snap.Hutao/Snap.Hutao/Core/LifeCycle/CurrentWindowReference.cs create mode 100644 src/Snap.Hutao/Snap.Hutao/Core/LifeCycle/ICurrentWindowReference.cs diff --git a/src/Snap.Hutao/Snap.Hutao/Core/LifeCycle/Activation.cs b/src/Snap.Hutao/Snap.Hutao/Core/LifeCycle/Activation.cs index 46a1381b..13442cb5 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/LifeCycle/Activation.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/LifeCycle/Activation.cs @@ -3,6 +3,7 @@ using CommunityToolkit.WinUI.Notifications; using Microsoft.Extensions.Caching.Memory; +using Microsoft.UI.Xaml; using Microsoft.Windows.AppLifecycle; using Snap.Hutao.Core.Setting; using Snap.Hutao.Service.DailyNote; @@ -18,9 +19,10 @@ namespace Snap.Hutao.Core.LifeCycle; /// 激活 /// [HighQuality] +[ConstructorGenerated] [Injection(InjectAs.Singleton, typeof(IActivation))] [SuppressMessage("", "CA1001")] -internal sealed class Activation : IActivation +internal sealed partial class Activation : IActivation { /// /// 操作 @@ -48,20 +50,11 @@ internal sealed class Activation : IActivation private const string UrlActionRefresh = "/REFRESH"; private readonly IServiceProvider serviceProvider; + private readonly ICurrentWindowReference currentWindowReference; private readonly ITaskContext taskContext; - private readonly WeakReference mainWindowReference = new(default!); + //private readonly WeakReference mainWindowReference = new(default!); private readonly SemaphoreSlim activateSemaphore = new(1); - /// - /// 构造一个新的激活 - /// - /// 服务提供器 - public Activation(IServiceProvider serviceProvider) - { - taskContext = serviceProvider.GetRequiredService(); - this.serviceProvider = serviceProvider; - } - /// public void Activate(object? sender, AppActivationArguments args) { @@ -163,11 +156,11 @@ internal sealed class Activation : IActivation private async ValueTask WaitMainWindowAsync() { - if (!mainWindowReference.TryGetTarget(out _)) + if (currentWindowReference.Window is null) { await taskContext.SwitchToMainThreadAsync(); - mainWindowReference.SetTarget(serviceProvider.GetRequiredService()); + currentWindowReference.Window = serviceProvider.GetRequiredService(); serviceProvider .GetRequiredService() @@ -272,9 +265,9 @@ internal sealed class Activation : IActivation await taskContext.SwitchToMainThreadAsync(); - if (!mainWindowReference.TryGetTarget(out _)) + if (currentWindowReference.Window is null) { - _ = serviceProvider.GetRequiredService(); + currentWindowReference.Window = serviceProvider.GetRequiredService(); } else { diff --git a/src/Snap.Hutao/Snap.Hutao/Core/LifeCycle/CurrentWindowReference.cs b/src/Snap.Hutao/Snap.Hutao/Core/LifeCycle/CurrentWindowReference.cs new file mode 100644 index 00000000..ce9cbdda --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Core/LifeCycle/CurrentWindowReference.cs @@ -0,0 +1,22 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using CommunityToolkit.WinUI.Notifications; +using Microsoft.Extensions.Caching.Memory; +using Microsoft.UI.Xaml; +using Microsoft.Windows.AppLifecycle; +using Snap.Hutao.Core.Setting; +using Snap.Hutao.Service.DailyNote; +using Snap.Hutao.Service.Hutao; +using Snap.Hutao.Service.Metadata; +using Snap.Hutao.Service.Navigation; +using Snap.Hutao.ViewModel.Guide; +using System.Diagnostics; + +namespace Snap.Hutao.Core.LifeCycle; + +[Injection(InjectAs.Singleton, typeof(ICurrentWindowReference))] +internal sealed class CurrentWindowReference : ICurrentWindowReference +{ + public Window Window { get; set; } = default!; +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Core/LifeCycle/IActivation.cs b/src/Snap.Hutao/Snap.Hutao/Core/LifeCycle/IActivation.cs index 4859192d..0ddc4b9a 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/LifeCycle/IActivation.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/LifeCycle/IActivation.cs @@ -1,6 +1,7 @@ // Copyright (c) DGP Studio. All rights reserved. // Licensed under the MIT license. +using Microsoft.UI.Xaml; using Microsoft.Windows.AppLifecycle; namespace Snap.Hutao.Core.LifeCycle; diff --git a/src/Snap.Hutao/Snap.Hutao/Core/LifeCycle/ICurrentWindowReference.cs b/src/Snap.Hutao/Snap.Hutao/Core/LifeCycle/ICurrentWindowReference.cs new file mode 100644 index 00000000..0999b288 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Core/LifeCycle/ICurrentWindowReference.cs @@ -0,0 +1,11 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using Microsoft.UI.Xaml; + +namespace Snap.Hutao.Core.LifeCycle; + +internal interface ICurrentWindowReference +{ + public Window Window { get; set; } +} diff --git a/src/Snap.Hutao/Snap.Hutao/Factory/ContentDialogFactory.cs b/src/Snap.Hutao/Snap.Hutao/Factory/ContentDialogFactory.cs index c6fc6374..ccb40ed0 100644 --- a/src/Snap.Hutao/Snap.Hutao/Factory/ContentDialogFactory.cs +++ b/src/Snap.Hutao/Snap.Hutao/Factory/ContentDialogFactory.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. using Microsoft.UI.Xaml.Controls; +using Snap.Hutao.Core.LifeCycle; using Snap.Hutao.Factory.Abstraction; namespace Snap.Hutao.Factory; @@ -14,7 +15,7 @@ internal sealed partial class ContentDialogFactory : IContentDialogFactory { private readonly IServiceProvider serviceProvider; private readonly ITaskContext taskContext; - private readonly MainWindow mainWindow; + private readonly ICurrentWindowReference currentWindowReference; /// public async ValueTask CreateForConfirmAsync(string title, string content) @@ -22,7 +23,7 @@ internal sealed partial class ContentDialogFactory : IContentDialogFactory await taskContext.SwitchToMainThreadAsync(); ContentDialog dialog = new() { - XamlRoot = mainWindow.Content.XamlRoot, + XamlRoot = currentWindowReference.Window.Content.XamlRoot, Title = title, Content = content, DefaultButton = ContentDialogButton.Primary, @@ -38,7 +39,7 @@ internal sealed partial class ContentDialogFactory : IContentDialogFactory await taskContext.SwitchToMainThreadAsync(); ContentDialog dialog = new() { - XamlRoot = mainWindow.Content.XamlRoot, + XamlRoot = currentWindowReference.Window.Content.XamlRoot, Title = title, Content = content, DefaultButton = defaultButton, @@ -55,7 +56,7 @@ internal sealed partial class ContentDialogFactory : IContentDialogFactory await taskContext.SwitchToMainThreadAsync(); ContentDialog dialog = new() { - XamlRoot = mainWindow.Content.XamlRoot, + XamlRoot = currentWindowReference.Window.Content.XamlRoot, Title = title, Content = new ProgressBar() { IsIndeterminate = true }, }; diff --git a/src/Snap.Hutao/Snap.Hutao/Properties/launchSettings.json b/src/Snap.Hutao/Snap.Hutao/Properties/launchSettings.json index a5fb2a14..7ae76b9e 100644 --- a/src/Snap.Hutao/Snap.Hutao/Properties/launchSettings.json +++ b/src/Snap.Hutao/Snap.Hutao/Properties/launchSettings.json @@ -3,7 +3,7 @@ "Snap.Hutao": { "commandName": "MsixPackage", "nativeDebugging": false, - "doNotLaunchApp": false, + "doNotLaunchApp": true, "allowLocalNetworkLoopbackProperty": true }, "[Unpackaged] Snap.Hutao": {