code style

This commit is contained in:
DismissedLight
2024-06-15 15:54:04 +08:00
parent a174493819
commit fe38e14ae8
5 changed files with 50 additions and 38 deletions

View File

@@ -24,7 +24,7 @@
x:Name="ContentGrid"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
x:Load="False">
x:Load="True">
<ContentPresenter.RenderTransform>
<CompositeTransform/>
</ContentPresenter.RenderTransform>

View File

@@ -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<byte, Guid>(ref MemoryMarshal.GetArrayDataReference(MD5.HashData(Encoding.UTF8.GetBytes(iconFile.Path))));
RuntimeOptions runtimeOptions = serviceProvider.GetRequiredService<RuntimeOptions>();
string iconPath = Path.Combine(runtimeOptions.InstalledLocation, "Assets/Logo.ico");
icon = new(iconPath);
id = Unsafe.As<byte, Guid>(ref MemoryMarshal.GetArrayDataReference(MD5.HashData(Encoding.UTF8.GetBytes(iconPath))));
xamlHostWindow = new(serviceProvider);
xamlHostWindow.MoveAndResize(default);

View File

@@ -100,8 +100,7 @@ internal sealed class XamlWindowController
private void OnWindowClosed(object sender, WindowEventArgs args)
{
IContentDialogFactory contentDialogFactory = serviceProvider.GetRequiredService<IContentDialogFactory>();
contentDialogFactory.HideAllDialogs();
serviceProvider.GetRequiredService<AppOptions>().PropertyChanged -= OnOptionsPropertyChanged;
if (XamlLifetime.ApplicationLaunchedWithNotifyIcon && !XamlLifetime.ApplicationExiting)
{

View File

@@ -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<Func<Task>> dialogQueue = [];
private bool isDialogShowing;
/// <inheritdoc/>
public async ValueTask<ContentDialogResult> 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();
}
/// <inheritdoc/>
public async ValueTask<ContentDialogResult> 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();
}
/// <inheritdoc/>
public async ValueTask<Microsoft.UI.Xaml.Controls.ContentDialog> 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<TContentDialog> CreateInstanceAsync<TContentDialog>(params object[] parameters)
where TContentDialog : Microsoft.UI.Xaml.Controls.ContentDialog
{
await HideAllDialogsAsync().ConfigureAwait(false);
await taskContext.SwitchToMainThreadAsync();
TContentDialog contentDialog = serviceProvider.CreateInstance<TContentDialog>(parameters);
contentDialog.XamlRoot = currentWindowReference.GetXamlRoot();
contentDialog.RequestedTheme = appOptions.ElementTheme;
contentDialog.Closed += OnContentDialogClosed;
currentContentDialog = contentDialog;
return contentDialog;
}
public TContentDialog CreateInstance<TContentDialog>(params object[] parameters)
where TContentDialog : Microsoft.UI.Xaml.Controls.ContentDialog
{
HideAllDialogs();
TContentDialog contentDialog = serviceProvider.CreateInstance<TContentDialog>(parameters);
contentDialog.XamlRoot = currentWindowReference.GetXamlRoot();
contentDialog.RequestedTheme = appOptions.ElementTheme;
contentDialog.Closed += OnContentDialogClosed;
currentContentDialog = contentDialog;
return contentDialog;
}
public void HideAllDialogs()
[SuppressMessage("", "SH003")]
public Task<ContentDialogResult> EnqueueAndShowAsync(Microsoft.UI.Xaml.Controls.ContentDialog contentDialog)
{
currentContentDialog?.Hide();
}
TaskCompletionSource<ContentDialogResult> 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<Task>? showNextDialogAsync))
{
isDialogShowing = true;
return showNextDialogAsync();
}
else
{
isDialogShowing = false;
return Task.CompletedTask;
}
}
}
}

View File

@@ -41,7 +41,5 @@ internal interface IContentDialogFactory
ValueTask<TContentDialog> CreateInstanceAsync<TContentDialog>(params object[] parameters)
where TContentDialog : Microsoft.UI.Xaml.Controls.ContentDialog;
void HideAllDialogs();
ValueTask HideAllDialogsAsync();
Task<ContentDialogResult> EnqueueAndShowAsync(Microsoft.UI.Xaml.Controls.ContentDialog contentDialog);
}