2022-09-19 14:05:26 -04:00
|
|
|
using Avalonia;
|
2022-05-15 07:30:15 -04:00
|
|
|
using Avalonia.Controls;
|
2022-09-19 14:05:26 -04:00
|
|
|
using Avalonia.Controls.ApplicationLifetimes;
|
|
|
|
using Avalonia.Media;
|
2022-05-15 07:30:15 -04:00
|
|
|
using Avalonia.Threading;
|
2022-10-23 05:15:45 -04:00
|
|
|
using FluentAvalonia.Core;
|
2022-05-15 07:30:15 -04:00
|
|
|
using FluentAvalonia.UI.Controls;
|
|
|
|
using Ryujinx.Ava.Common.Locale;
|
2022-12-29 09:24:05 -05:00
|
|
|
using Ryujinx.Ava.UI.Controls;
|
|
|
|
using Ryujinx.Ava.UI.Windows;
|
2022-05-15 07:30:15 -04:00
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
using System;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
2022-12-29 09:24:05 -05:00
|
|
|
namespace Ryujinx.Ava.UI.Helpers
|
2022-05-15 07:30:15 -04:00
|
|
|
{
|
|
|
|
public static class ContentDialogHelper
|
|
|
|
{
|
|
|
|
private static bool _isChoiceDialogOpen;
|
|
|
|
|
|
|
|
private async static Task<UserResult> ShowContentDialog(
|
|
|
|
string title,
|
|
|
|
string primaryText,
|
|
|
|
string secondaryText,
|
|
|
|
string primaryButton,
|
|
|
|
string secondaryButton,
|
|
|
|
string closeButton,
|
|
|
|
int iconSymbol,
|
2022-10-23 05:15:45 -04:00
|
|
|
UserResult primaryButtonResult = UserResult.Ok,
|
|
|
|
ManualResetEvent deferResetEvent = null,
|
|
|
|
Func<Window, Task> doWhileDeferred = null,
|
|
|
|
TypedEventHandler<ContentDialog, ContentDialogButtonClickEventArgs> deferCloseAction = null)
|
2022-05-15 07:30:15 -04:00
|
|
|
{
|
|
|
|
UserResult result = UserResult.None;
|
|
|
|
|
2022-09-19 14:05:26 -04:00
|
|
|
bool useOverlay = false;
|
|
|
|
Window mainWindow = null;
|
2022-05-15 07:30:15 -04:00
|
|
|
|
2022-09-19 14:05:26 -04:00
|
|
|
if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime al)
|
|
|
|
{
|
|
|
|
foreach (var item in al.Windows)
|
|
|
|
{
|
|
|
|
if (item.IsActive && item is MainWindow window && window.ViewModel.IsGameRunning)
|
|
|
|
{
|
|
|
|
mainWindow = window;
|
|
|
|
useOverlay = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentDialog contentDialog = null;
|
|
|
|
ContentDialogOverlayWindow overlay = null;
|
|
|
|
|
|
|
|
if (useOverlay)
|
|
|
|
{
|
|
|
|
overlay = new ContentDialogOverlayWindow()
|
|
|
|
{
|
|
|
|
Height = mainWindow.Bounds.Height,
|
|
|
|
Width = mainWindow.Bounds.Width,
|
|
|
|
Position = mainWindow.PointToScreen(new Point())
|
|
|
|
};
|
|
|
|
|
|
|
|
mainWindow.PositionChanged += OverlayOnPositionChanged;
|
|
|
|
|
|
|
|
void OverlayOnPositionChanged(object sender, PixelPointEventArgs e)
|
|
|
|
{
|
|
|
|
overlay.Position = mainWindow.PointToScreen(new Point());
|
|
|
|
}
|
|
|
|
|
|
|
|
contentDialog = overlay.ContentDialog;
|
|
|
|
|
|
|
|
bool opened = false;
|
|
|
|
|
|
|
|
overlay.Opened += OverlayOnActivated;
|
|
|
|
|
|
|
|
async void OverlayOnActivated(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
if (opened)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
opened = true;
|
|
|
|
|
|
|
|
overlay.Position = mainWindow.PointToScreen(new Point());
|
|
|
|
|
|
|
|
await ShowDialog();
|
|
|
|
}
|
|
|
|
|
|
|
|
await overlay.ShowDialog(mainWindow);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
contentDialog = new ContentDialog();
|
|
|
|
|
|
|
|
await ShowDialog();
|
|
|
|
}
|
2022-05-15 07:30:15 -04:00
|
|
|
|
|
|
|
async Task ShowDialog()
|
|
|
|
{
|
2022-07-24 13:38:38 -04:00
|
|
|
contentDialog.Title = title;
|
|
|
|
contentDialog.PrimaryButtonText = primaryButton;
|
|
|
|
contentDialog.SecondaryButtonText = secondaryButton;
|
|
|
|
contentDialog.CloseButtonText = closeButton;
|
|
|
|
contentDialog.Content = CreateDialogTextContent(primaryText, secondaryText, iconSymbol);
|
2022-05-15 07:30:15 -04:00
|
|
|
|
2022-07-24 13:38:38 -04:00
|
|
|
contentDialog.PrimaryButtonCommand = MiniCommand.Create(() =>
|
|
|
|
{
|
|
|
|
result = primaryButtonResult;
|
|
|
|
});
|
|
|
|
contentDialog.SecondaryButtonCommand = MiniCommand.Create(() =>
|
|
|
|
{
|
|
|
|
result = UserResult.No;
|
2022-10-23 05:15:45 -04:00
|
|
|
contentDialog.PrimaryButtonClick -= deferCloseAction;
|
2022-07-24 13:38:38 -04:00
|
|
|
});
|
|
|
|
contentDialog.CloseButtonCommand = MiniCommand.Create(() =>
|
|
|
|
{
|
|
|
|
result = UserResult.Cancel;
|
2022-10-23 05:15:45 -04:00
|
|
|
contentDialog.PrimaryButtonClick -= deferCloseAction;
|
2022-07-24 13:38:38 -04:00
|
|
|
});
|
2022-05-15 07:30:15 -04:00
|
|
|
|
2022-10-23 05:15:45 -04:00
|
|
|
if (deferResetEvent != null)
|
|
|
|
{
|
|
|
|
contentDialog.PrimaryButtonClick += deferCloseAction;
|
|
|
|
}
|
|
|
|
|
2022-11-29 00:33:46 -05:00
|
|
|
if (useOverlay)
|
|
|
|
{
|
|
|
|
await contentDialog.ShowAsync(overlay, ContentDialogPlacement.Popup);
|
2022-09-19 14:05:26 -04:00
|
|
|
|
2022-11-29 00:33:46 -05:00
|
|
|
overlay!.Close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
await contentDialog.ShowAsync(ContentDialogPlacement.Popup);
|
|
|
|
}
|
2022-09-19 14:05:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (useOverlay)
|
|
|
|
{
|
|
|
|
overlay.Content = null;
|
|
|
|
overlay.Close();
|
2022-05-15 07:30:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async static Task<UserResult> ShowDeferredContentDialog(
|
|
|
|
StyleableWindow window,
|
|
|
|
string title,
|
|
|
|
string primaryText,
|
|
|
|
string secondaryText,
|
|
|
|
string primaryButton,
|
|
|
|
string secondaryButton,
|
|
|
|
string closeButton,
|
|
|
|
int iconSymbol,
|
|
|
|
ManualResetEvent deferResetEvent,
|
|
|
|
Func<Window, Task> doWhileDeferred = null)
|
|
|
|
{
|
|
|
|
bool startedDeferring = false;
|
|
|
|
UserResult result = UserResult.None;
|
|
|
|
|
2022-10-23 05:15:45 -04:00
|
|
|
return await ShowContentDialog(
|
|
|
|
title,
|
|
|
|
primaryText,
|
|
|
|
secondaryText,
|
|
|
|
primaryButton,
|
|
|
|
secondaryButton,
|
|
|
|
closeButton,
|
|
|
|
iconSymbol,
|
|
|
|
primaryButton == LocaleManager.Instance["InputDialogYes"] ? UserResult.Yes : UserResult.Ok,
|
|
|
|
deferResetEvent,
|
|
|
|
doWhileDeferred,
|
|
|
|
DeferClose);
|
2022-05-15 07:30:15 -04:00
|
|
|
|
|
|
|
async void DeferClose(ContentDialog sender, ContentDialogButtonClickEventArgs args)
|
|
|
|
{
|
|
|
|
if (startedDeferring)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-23 05:15:45 -04:00
|
|
|
sender.PrimaryButtonClick -= DeferClose;
|
2022-07-05 14:06:31 -04:00
|
|
|
|
2022-05-15 07:30:15 -04:00
|
|
|
startedDeferring = true;
|
|
|
|
|
|
|
|
var deferral = args.GetDeferral();
|
|
|
|
|
|
|
|
result = primaryButton == LocaleManager.Instance["InputDialogYes"] ? UserResult.Yes : UserResult.Ok;
|
|
|
|
|
2022-10-23 05:15:45 -04:00
|
|
|
sender.PrimaryButtonClick -= DeferClose;
|
2022-05-15 07:30:15 -04:00
|
|
|
|
|
|
|
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
|
|
|
|
Task.Run(() =>
|
|
|
|
{
|
|
|
|
deferResetEvent.WaitOne();
|
|
|
|
|
|
|
|
Dispatcher.UIThread.Post(() =>
|
|
|
|
{
|
|
|
|
deferral.Complete();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
|
|
|
|
|
|
|
|
if (doWhileDeferred != null)
|
|
|
|
{
|
2022-07-24 13:38:38 -04:00
|
|
|
await doWhileDeferred(window);
|
2022-05-15 07:30:15 -04:00
|
|
|
|
|
|
|
deferResetEvent.Set();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Grid CreateDialogTextContent(string primaryText, string secondaryText, int symbol)
|
|
|
|
{
|
|
|
|
Grid content = new Grid();
|
|
|
|
content.RowDefinitions = new RowDefinitions() { new RowDefinition(), new RowDefinition() };
|
|
|
|
content.ColumnDefinitions = new ColumnDefinitions() { new ColumnDefinition(GridLength.Auto), new ColumnDefinition() };
|
|
|
|
|
|
|
|
content.MinHeight = 80;
|
|
|
|
|
2022-12-10 15:21:13 -05:00
|
|
|
SymbolIcon icon = new SymbolIcon { Symbol = (Symbol)symbol, Margin = new Thickness(10) };
|
2022-05-15 07:30:15 -04:00
|
|
|
icon.FontSize = 40;
|
|
|
|
icon.VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center;
|
|
|
|
Grid.SetColumn(icon, 0);
|
|
|
|
Grid.SetRowSpan(icon, 2);
|
|
|
|
Grid.SetRow(icon, 0);
|
|
|
|
|
|
|
|
TextBlock primaryLabel = new TextBlock()
|
|
|
|
{
|
|
|
|
Text = primaryText,
|
2022-12-10 15:21:13 -05:00
|
|
|
Margin = new Thickness(5),
|
|
|
|
TextWrapping = TextWrapping.Wrap,
|
2022-05-15 07:30:15 -04:00
|
|
|
MaxWidth = 450
|
|
|
|
};
|
|
|
|
TextBlock secondaryLabel = new TextBlock()
|
|
|
|
{
|
|
|
|
Text = secondaryText,
|
2022-12-10 15:21:13 -05:00
|
|
|
Margin = new Thickness(5),
|
|
|
|
TextWrapping = TextWrapping.Wrap,
|
2022-05-15 07:30:15 -04:00
|
|
|
MaxWidth = 450
|
|
|
|
};
|
|
|
|
|
|
|
|
Grid.SetColumn(primaryLabel, 1);
|
|
|
|
Grid.SetColumn(secondaryLabel, 1);
|
|
|
|
Grid.SetRow(primaryLabel, 0);
|
|
|
|
Grid.SetRow(secondaryLabel, 1);
|
|
|
|
|
|
|
|
content.Children.Add(icon);
|
|
|
|
content.Children.Add(primaryLabel);
|
|
|
|
content.Children.Add(secondaryLabel);
|
|
|
|
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async Task<UserResult> CreateInfoDialog(
|
|
|
|
string primary,
|
|
|
|
string secondaryText,
|
|
|
|
string acceptButton,
|
|
|
|
string closeButton,
|
|
|
|
string title)
|
|
|
|
{
|
|
|
|
return await ShowContentDialog(
|
|
|
|
title,
|
|
|
|
primary,
|
|
|
|
secondaryText,
|
|
|
|
acceptButton,
|
|
|
|
"",
|
|
|
|
closeButton,
|
|
|
|
(int)Symbol.Important);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static async Task<UserResult> CreateConfirmationDialog(
|
|
|
|
string primaryText,
|
|
|
|
string secondaryText,
|
|
|
|
string acceptButtonText,
|
|
|
|
string cancelButtonText,
|
|
|
|
string title,
|
|
|
|
UserResult primaryButtonResult = UserResult.Yes)
|
|
|
|
{
|
|
|
|
return await ShowContentDialog(
|
|
|
|
string.IsNullOrWhiteSpace(title) ? LocaleManager.Instance["DialogConfirmationTitle"] : title,
|
|
|
|
primaryText,
|
|
|
|
secondaryText,
|
|
|
|
acceptButtonText,
|
|
|
|
"",
|
|
|
|
cancelButtonText,
|
|
|
|
(int)Symbol.Help,
|
|
|
|
primaryButtonResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static UpdateWaitWindow CreateWaitingDialog(string mainText, string secondaryText)
|
|
|
|
{
|
|
|
|
return new(mainText, secondaryText);
|
|
|
|
}
|
|
|
|
|
2022-07-24 13:38:38 -04:00
|
|
|
internal static async Task CreateUpdaterInfoDialog(string primary, string secondaryText)
|
2022-05-15 07:30:15 -04:00
|
|
|
{
|
|
|
|
await ShowContentDialog(
|
|
|
|
LocaleManager.Instance["DialogUpdaterTitle"],
|
|
|
|
primary,
|
|
|
|
secondaryText,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
LocaleManager.Instance["InputDialogOk"],
|
|
|
|
(int)Symbol.Important);
|
|
|
|
}
|
|
|
|
|
2022-07-24 13:38:38 -04:00
|
|
|
internal static async Task CreateWarningDialog(string primary, string secondaryText)
|
2022-05-15 07:30:15 -04:00
|
|
|
{
|
|
|
|
await ShowContentDialog(
|
|
|
|
LocaleManager.Instance["DialogWarningTitle"],
|
|
|
|
primary,
|
|
|
|
secondaryText,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
LocaleManager.Instance["InputDialogOk"],
|
|
|
|
(int)Symbol.Important);
|
|
|
|
}
|
|
|
|
|
2022-07-24 13:38:38 -04:00
|
|
|
internal static async Task CreateErrorDialog(string errorMessage, string secondaryErrorMessage = "")
|
2022-05-15 07:30:15 -04:00
|
|
|
{
|
|
|
|
Logger.Error?.Print(LogClass.Application, errorMessage);
|
|
|
|
|
|
|
|
await ShowContentDialog(
|
|
|
|
LocaleManager.Instance["DialogErrorTitle"],
|
|
|
|
LocaleManager.Instance["DialogErrorMessage"],
|
|
|
|
errorMessage,
|
|
|
|
secondaryErrorMessage,
|
|
|
|
"",
|
|
|
|
LocaleManager.Instance["InputDialogOk"],
|
|
|
|
(int)Symbol.Dismiss);
|
|
|
|
}
|
|
|
|
|
2022-07-24 13:38:38 -04:00
|
|
|
internal static async Task<bool> CreateChoiceDialog(string title, string primary, string secondaryText)
|
2022-05-15 07:30:15 -04:00
|
|
|
{
|
|
|
|
if (_isChoiceDialogOpen)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isChoiceDialogOpen = true;
|
|
|
|
|
|
|
|
UserResult response =
|
|
|
|
await ShowContentDialog(
|
|
|
|
title,
|
|
|
|
primary,
|
|
|
|
secondaryText,
|
|
|
|
LocaleManager.Instance["InputDialogYes"],
|
|
|
|
"",
|
|
|
|
LocaleManager.Instance["InputDialogNo"],
|
|
|
|
(int)Symbol.Help,
|
|
|
|
UserResult.Yes);
|
|
|
|
|
|
|
|
_isChoiceDialogOpen = false;
|
|
|
|
|
|
|
|
return response == UserResult.Yes;
|
|
|
|
}
|
|
|
|
|
2022-07-24 13:38:38 -04:00
|
|
|
internal static async Task<bool> CreateExitDialog()
|
2022-05-15 07:30:15 -04:00
|
|
|
{
|
|
|
|
return await CreateChoiceDialog(
|
|
|
|
LocaleManager.Instance["DialogExitTitle"],
|
|
|
|
LocaleManager.Instance["DialogExitMessage"],
|
|
|
|
LocaleManager.Instance["DialogExitSubMessage"]);
|
|
|
|
}
|
|
|
|
|
2022-07-24 13:38:38 -04:00
|
|
|
internal static async Task<bool> CreateStopEmulationDialog()
|
2022-05-15 07:30:15 -04:00
|
|
|
{
|
|
|
|
return await CreateChoiceDialog(
|
|
|
|
LocaleManager.Instance["DialogStopEmulationTitle"],
|
|
|
|
LocaleManager.Instance["DialogStopEmulationMessage"],
|
|
|
|
LocaleManager.Instance["DialogExitSubMessage"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static async Task<string> CreateInputDialog(
|
|
|
|
string title,
|
|
|
|
string mainText,
|
|
|
|
string subText,
|
|
|
|
uint maxLength = int.MaxValue,
|
|
|
|
string input = "")
|
|
|
|
{
|
|
|
|
var result = await InputDialog.ShowInputDialog(
|
|
|
|
title,
|
|
|
|
mainText,
|
|
|
|
input,
|
|
|
|
subText,
|
|
|
|
maxLength);
|
|
|
|
|
|
|
|
if (result.Result == UserResult.Ok)
|
|
|
|
{
|
|
|
|
return result.Input;
|
|
|
|
}
|
|
|
|
|
|
|
|
return string.Empty;
|
|
|
|
}
|
|
|
|
}
|
2022-11-29 00:33:46 -05:00
|
|
|
}
|