Skip to content

Commit

Permalink
code style
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Jun 15, 2024
1 parent a174493 commit fe38e14
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/Snap.Hutao/Snap.Hutao/Control/Loading.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
x:Name="ContentGrid"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
x:Load="False">
x:Load="True">
<ContentPresenter.RenderTransform>
<CompositeTransform/>
</ContentPresenter.RenderTransform>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,9 +27,10 @@ public NotifyIconController(IServiceProvider serviceProvider)
{
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ private void InitializeCore()

private void OnWindowClosed(object sender, WindowEventArgs args)
{
IContentDialogFactory contentDialogFactory = serviceProvider.GetRequiredService<IContentDialogFactory>();
contentDialogFactory.HideAllDialogs();
serviceProvider.GetRequiredService<AppOptions>().PropertyChanged -= OnOptionsPropertyChanged;

if (XamlLifetime.ApplicationLaunchedWithNotifyIcon && !XamlLifetime.ApplicationExiting)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()
Expand All @@ -36,15 +37,12 @@ public async ValueTask<ContentDialogResult> CreateForConfirmAsync(string title,
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()
Expand All @@ -58,15 +56,12 @@ public async ValueTask<ContentDialogResult> CreateForConfirmCancelAsync(string t
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()
Expand All @@ -77,54 +72,72 @@ public async ValueTask<Microsoft.UI.Xaml.Controls.ContentDialog> CreateForIndete
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);
}
});

if (!isDialogShowing)
{
ShowNextDialog();
}

private void OnContentDialogClosed(Microsoft.UI.Xaml.Controls.ContentDialog sender, ContentDialogClosedEventArgs args)
{
currentContentDialog = null;
sender.Closed -= OnContentDialogClosed;
return dialogShowCompletionSource.Task;

Task ShowNextDialog()
{
if (dialogQueue.TryDequeue(out Func<Task>? showNextDialogAsync))
{
isDialogShowing = true;
return showNextDialogAsync();
}
else
{
isDialogShowing = false;
return Task.CompletedTask;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,5 @@ TContentDialog CreateInstance<TContentDialog>(params object[] parameters)
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);
}

0 comments on commit fe38e14

Please sign in to comment.