From fe38e14ae86c018e0d7315f89731c96fc90cf6d5 Mon Sep 17 00:00:00 2001
From: DismissedLight <1686188646@qq.com>
Date: Sat, 15 Jun 2024 15:54:04 +0800
Subject: [PATCH] code style
---
.../Snap.Hutao/Control/Loading.xaml | 2 +-
.../NotifyIcon/NotifyIconController.cs | 8 ++-
.../Core/Windowing/XamlWindowController.cs | 3 +-
.../ContentDialog/ContentDialogFactory.cs | 71 +++++++++++--------
.../ContentDialog/IContentDialogFactory.cs | 4 +-
5 files changed, 50 insertions(+), 38 deletions(-)
diff --git a/src/Snap.Hutao/Snap.Hutao/Control/Loading.xaml b/src/Snap.Hutao/Snap.Hutao/Control/Loading.xaml
index d7567bf5..f2052287 100644
--- a/src/Snap.Hutao/Snap.Hutao/Control/Loading.xaml
+++ b/src/Snap.Hutao/Snap.Hutao/Control/Loading.xaml
@@ -24,7 +24,7 @@
x:Name="ContentGrid"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
- x:Load="False">
+ x:Load="True">
diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/NotifyIcon/NotifyIconController.cs b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/NotifyIcon/NotifyIconController.cs
index 6f924ebb..901341be 100644
--- a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/NotifyIcon/NotifyIconController.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/NotifyIcon/NotifyIconController.cs
@@ -4,6 +4,7 @@
using Snap.Hutao.Core.ExceptionService;
using Snap.Hutao.Win32.Foundation;
using Snap.Hutao.Win32.UI.WindowsAndMessaging;
+using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
@@ -26,9 +27,10 @@ internal sealed class NotifyIconController : IDisposable
{
lazyMenu = new(() => new(serviceProvider));
- StorageFile iconFile = StorageFile.GetFileFromApplicationUriAsync("ms-appx:///Assets/Logo.ico".ToUri()).AsTask().GetAwaiter().GetResult();
- icon = new(iconFile.Path);
- id = Unsafe.As(ref MemoryMarshal.GetArrayDataReference(MD5.HashData(Encoding.UTF8.GetBytes(iconFile.Path))));
+ RuntimeOptions runtimeOptions = serviceProvider.GetRequiredService();
+ string iconPath = Path.Combine(runtimeOptions.InstalledLocation, "Assets/Logo.ico");
+ icon = new(iconPath);
+ id = Unsafe.As(ref MemoryMarshal.GetArrayDataReference(MD5.HashData(Encoding.UTF8.GetBytes(iconPath))));
xamlHostWindow = new(serviceProvider);
xamlHostWindow.MoveAndResize(default);
diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/XamlWindowController.cs b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/XamlWindowController.cs
index 5636a872..8be2e106 100644
--- a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/XamlWindowController.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/XamlWindowController.cs
@@ -100,8 +100,7 @@ internal sealed class XamlWindowController
private void OnWindowClosed(object sender, WindowEventArgs args)
{
- IContentDialogFactory contentDialogFactory = serviceProvider.GetRequiredService();
- contentDialogFactory.HideAllDialogs();
+ serviceProvider.GetRequiredService().PropertyChanged -= OnOptionsPropertyChanged;
if (XamlLifetime.ApplicationLaunchedWithNotifyIcon && !XamlLifetime.ApplicationExiting)
{
diff --git a/src/Snap.Hutao/Snap.Hutao/Factory/ContentDialog/ContentDialogFactory.cs b/src/Snap.Hutao/Snap.Hutao/Factory/ContentDialog/ContentDialogFactory.cs
index 6c5680de..4ea5e8b5 100644
--- a/src/Snap.Hutao/Snap.Hutao/Factory/ContentDialog/ContentDialogFactory.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Factory/ContentDialog/ContentDialogFactory.cs
@@ -4,6 +4,7 @@
using Microsoft.UI.Xaml.Controls;
using Snap.Hutao.Core.LifeCycle;
using Snap.Hutao.Service;
+using System.Collections.Concurrent;
namespace Snap.Hutao.Factory.ContentDialog;
@@ -18,12 +19,12 @@ internal sealed partial class ContentDialogFactory : IContentDialogFactory
private readonly ITaskContext taskContext;
private readonly AppOptions appOptions;
- private Microsoft.UI.Xaml.Controls.ContentDialog? currentContentDialog;
+ private readonly ConcurrentQueue> dialogQueue = [];
+ private bool isDialogShowing;
///
public async ValueTask CreateForConfirmAsync(string title, string content)
{
- await HideAllDialogsAsync().ConfigureAwait(false);
await taskContext.SwitchToMainThreadAsync();
Microsoft.UI.Xaml.Controls.ContentDialog dialog = new()
@@ -36,15 +37,12 @@ internal sealed partial class ContentDialogFactory : IContentDialogFactory
RequestedTheme = appOptions.ElementTheme,
};
- dialog.Closed += OnContentDialogClosed;
- currentContentDialog = dialog;
return await dialog.ShowAsync();
}
///
public async ValueTask CreateForConfirmCancelAsync(string title, string content, ContentDialogButton defaultButton = ContentDialogButton.Close)
{
- await HideAllDialogsAsync().ConfigureAwait(false);
await taskContext.SwitchToMainThreadAsync();
Microsoft.UI.Xaml.Controls.ContentDialog dialog = new()
@@ -58,15 +56,12 @@ internal sealed partial class ContentDialogFactory : IContentDialogFactory
RequestedTheme = appOptions.ElementTheme,
};
- dialog.Closed += OnContentDialogClosed;
- currentContentDialog = dialog;
return await dialog.ShowAsync();
}
///
public async ValueTask CreateForIndeterminateProgressAsync(string title)
{
- await HideAllDialogsAsync().ConfigureAwait(false);
await taskContext.SwitchToMainThreadAsync();
Microsoft.UI.Xaml.Controls.ContentDialog dialog = new()
@@ -77,54 +72,72 @@ internal sealed partial class ContentDialogFactory : IContentDialogFactory
RequestedTheme = appOptions.ElementTheme,
};
- dialog.Closed += OnContentDialogClosed;
- currentContentDialog = dialog;
return dialog;
}
public async ValueTask CreateInstanceAsync(params object[] parameters)
where TContentDialog : Microsoft.UI.Xaml.Controls.ContentDialog
{
- await HideAllDialogsAsync().ConfigureAwait(false);
await taskContext.SwitchToMainThreadAsync();
TContentDialog contentDialog = serviceProvider.CreateInstance(parameters);
contentDialog.XamlRoot = currentWindowReference.GetXamlRoot();
contentDialog.RequestedTheme = appOptions.ElementTheme;
- contentDialog.Closed += OnContentDialogClosed;
- currentContentDialog = contentDialog;
return contentDialog;
}
public TContentDialog CreateInstance(params object[] parameters)
where TContentDialog : Microsoft.UI.Xaml.Controls.ContentDialog
{
- HideAllDialogs();
-
TContentDialog contentDialog = serviceProvider.CreateInstance(parameters);
contentDialog.XamlRoot = currentWindowReference.GetXamlRoot();
contentDialog.RequestedTheme = appOptions.ElementTheme;
- contentDialog.Closed += OnContentDialogClosed;
- currentContentDialog = contentDialog;
return contentDialog;
}
- public void HideAllDialogs()
+ [SuppressMessage("", "SH003")]
+ public Task EnqueueAndShowAsync(Microsoft.UI.Xaml.Controls.ContentDialog contentDialog)
{
- currentContentDialog?.Hide();
- }
+ TaskCompletionSource dialogShowCompletionSource = new();
- public async ValueTask HideAllDialogsAsync()
- {
- await taskContext.SwitchToMainThreadAsync();
- currentContentDialog?.Hide();
- }
+ dialogQueue.Enqueue(async () =>
+ {
+ try
+ {
+ ContentDialogResult result = await contentDialog.ShowAsync();
+ dialogShowCompletionSource.SetResult(result);
+ }
+ catch (Exception ex)
+ {
+ dialogShowCompletionSource.SetException(ex);
+ }
+ finally
+ {
+ await ShowNextDialog().ConfigureAwait(false);
+ }
+ });
- private void OnContentDialogClosed(Microsoft.UI.Xaml.Controls.ContentDialog sender, ContentDialogClosedEventArgs args)
- {
- currentContentDialog = null;
- sender.Closed -= OnContentDialogClosed;
+ if (!isDialogShowing)
+ {
+ ShowNextDialog();
+ }
+
+ return dialogShowCompletionSource.Task;
+
+ Task ShowNextDialog()
+ {
+ if (dialogQueue.TryDequeue(out Func? showNextDialogAsync))
+ {
+ isDialogShowing = true;
+ return showNextDialogAsync();
+ }
+ else
+ {
+ isDialogShowing = false;
+ return Task.CompletedTask;
+ }
+ }
}
}
\ No newline at end of file
diff --git a/src/Snap.Hutao/Snap.Hutao/Factory/ContentDialog/IContentDialogFactory.cs b/src/Snap.Hutao/Snap.Hutao/Factory/ContentDialog/IContentDialogFactory.cs
index f7420929..703378df 100644
--- a/src/Snap.Hutao/Snap.Hutao/Factory/ContentDialog/IContentDialogFactory.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Factory/ContentDialog/IContentDialogFactory.cs
@@ -41,7 +41,5 @@ internal interface IContentDialogFactory
ValueTask CreateInstanceAsync(params object[] parameters)
where TContentDialog : Microsoft.UI.Xaml.Controls.ContentDialog;
- void HideAllDialogs();
-
- ValueTask HideAllDialogsAsync();
+ Task EnqueueAndShowAsync(Microsoft.UI.Xaml.Controls.ContentDialog contentDialog);
}
\ No newline at end of file