Extend ContentDialogHelper to work on multiple windows

This commit is contained in:
Isaac Marovitz 2024-04-17 19:58:25 -04:00
parent eb212aa91b
commit b041fd837c
No known key found for this signature in database
GPG key ID: 97250B2B09A132E1

View file

@ -28,7 +28,8 @@ namespace Ryujinx.Ava.UI.Helpers
string closeButton, string closeButton,
UserResult primaryButtonResult = UserResult.Ok, UserResult primaryButtonResult = UserResult.Ok,
ManualResetEvent deferResetEvent = null, ManualResetEvent deferResetEvent = null,
TypedEventHandler<ContentDialog, ContentDialogButtonClickEventArgs> deferCloseAction = null) TypedEventHandler<ContentDialog, ContentDialogButtonClickEventArgs> deferCloseAction = null,
Window parent = null)
{ {
UserResult result = UserResult.None; UserResult result = UserResult.None;
@ -62,7 +63,7 @@ namespace Ryujinx.Ava.UI.Helpers
contentDialog.PrimaryButtonClick += deferCloseAction; contentDialog.PrimaryButtonClick += deferCloseAction;
} }
await ShowAsync(contentDialog); await ShowAsync(contentDialog, parent);
return result; return result;
} }
@ -77,11 +78,21 @@ namespace Ryujinx.Ava.UI.Helpers
int iconSymbol, int iconSymbol,
UserResult primaryButtonResult = UserResult.Ok, UserResult primaryButtonResult = UserResult.Ok,
ManualResetEvent deferResetEvent = null, ManualResetEvent deferResetEvent = null,
TypedEventHandler<ContentDialog, ContentDialogButtonClickEventArgs> deferCloseAction = null) TypedEventHandler<ContentDialog, ContentDialogButtonClickEventArgs> deferCloseAction = null,
Window parent = null)
{ {
Grid content = CreateTextDialogContent(primaryText, secondaryText, iconSymbol); Grid content = CreateTextDialogContent(primaryText, secondaryText, iconSymbol);
return await ShowContentDialog(title, content, primaryButton, secondaryButton, closeButton, primaryButtonResult, deferResetEvent, deferCloseAction); return await ShowContentDialog(
title,
content,
primaryButton,
secondaryButton,
closeButton,
primaryButtonResult,
deferResetEvent,
deferCloseAction,
parent);
} }
public async static Task<UserResult> ShowDeferredContentDialog( public async static Task<UserResult> ShowDeferredContentDialog(
@ -94,7 +105,8 @@ namespace Ryujinx.Ava.UI.Helpers
string closeButton, string closeButton,
int iconSymbol, int iconSymbol,
ManualResetEvent deferResetEvent, ManualResetEvent deferResetEvent,
Func<Window, Task> doWhileDeferred = null) Func<Window, Task> doWhileDeferred = null,
Window parent = null)
{ {
bool startedDeferring = false; bool startedDeferring = false;
@ -108,7 +120,8 @@ namespace Ryujinx.Ava.UI.Helpers
iconSymbol, iconSymbol,
primaryButton == LocaleManager.Instance[LocaleKeys.InputDialogYes] ? UserResult.Yes : UserResult.Ok, primaryButton == LocaleManager.Instance[LocaleKeys.InputDialogYes] ? UserResult.Yes : UserResult.Ok,
deferResetEvent, deferResetEvent,
DeferClose); DeferClose,
parent);
async void DeferClose(ContentDialog sender, ContentDialogButtonClickEventArgs args) async void DeferClose(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{ {
@ -199,7 +212,8 @@ namespace Ryujinx.Ava.UI.Helpers
string secondaryText, string secondaryText,
string acceptButton, string acceptButton,
string closeButton, string closeButton,
string title) string title,
Window parent = null)
{ {
return await ShowTextDialog( return await ShowTextDialog(
title, title,
@ -208,7 +222,8 @@ namespace Ryujinx.Ava.UI.Helpers
acceptButton, acceptButton,
"", "",
closeButton, closeButton,
(int)Symbol.Important); (int)Symbol.Important,
parent: parent);
} }
internal static async Task<UserResult> CreateConfirmationDialog( internal static async Task<UserResult> CreateConfirmationDialog(
@ -217,7 +232,8 @@ namespace Ryujinx.Ava.UI.Helpers
string acceptButtonText, string acceptButtonText,
string cancelButtonText, string cancelButtonText,
string title, string title,
UserResult primaryButtonResult = UserResult.Yes) UserResult primaryButtonResult = UserResult.Yes,
Window parent = null)
{ {
return await ShowTextDialog( return await ShowTextDialog(
string.IsNullOrWhiteSpace(title) ? LocaleManager.Instance[LocaleKeys.DialogConfirmationTitle] : title, string.IsNullOrWhiteSpace(title) ? LocaleManager.Instance[LocaleKeys.DialogConfirmationTitle] : title,
@ -227,7 +243,8 @@ namespace Ryujinx.Ava.UI.Helpers
"", "",
cancelButtonText, cancelButtonText,
(int)Symbol.Help, (int)Symbol.Help,
primaryButtonResult); primaryButtonResult,
parent: parent);
} }
internal static async Task CreateUpdaterInfoDialog(string primary, string secondaryText) internal static async Task CreateUpdaterInfoDialog(string primary, string secondaryText)
@ -268,7 +285,11 @@ namespace Ryujinx.Ava.UI.Helpers
(int)Symbol.Dismiss); (int)Symbol.Dismiss);
} }
internal static async Task<bool> CreateChoiceDialog(string title, string primary, string secondaryText) internal static async Task<bool> CreateChoiceDialog(
string title,
string primary,
string secondaryText,
Window parent = null)
{ {
if (_isChoiceDialogOpen) if (_isChoiceDialogOpen)
{ {
@ -285,7 +306,8 @@ namespace Ryujinx.Ava.UI.Helpers
"", "",
LocaleManager.Instance[LocaleKeys.InputDialogNo], LocaleManager.Instance[LocaleKeys.InputDialogNo],
(int)Symbol.Help, (int)Symbol.Help,
UserResult.Yes); UserResult.Yes,
parent: parent);
_isChoiceDialogOpen = false; _isChoiceDialogOpen = false;
@ -308,69 +330,62 @@ namespace Ryujinx.Ava.UI.Helpers
LocaleManager.Instance[LocaleKeys.DialogExitSubMessage]); LocaleManager.Instance[LocaleKeys.DialogExitSubMessage]);
} }
public static async Task<ContentDialogResult> ShowAsync(ContentDialog contentDialog) public static async Task<ContentDialogResult> ShowAsync(ContentDialog contentDialog, Window parent = null)
{ {
ContentDialogResult result; ContentDialogResult result;
bool isTopDialog = true; bool isTopDialog = true;
Window parent = GetMainWindow(); parent ??= GetMainWindow();
if (_contentDialogOverlayWindow != null) if (_contentDialogOverlayWindow != null)
{ {
isTopDialog = false; isTopDialog = false;
} }
if (parent is MainWindow window) parent.Activate();
_contentDialogOverlayWindow = new ContentDialogOverlayWindow
{ {
parent.Activate(); Height = parent.Bounds.Height,
Width = parent.Bounds.Width,
Position = parent.PointToScreen(new Point()),
ShowInTaskbar = false,
};
_contentDialogOverlayWindow = new ContentDialogOverlayWindow parent.PositionChanged += OverlayOnPositionChanged;
void OverlayOnPositionChanged(object sender, PixelPointEventArgs e)
{
if (_contentDialogOverlayWindow is null)
{ {
Height = parent.Bounds.Height, return;
Width = parent.Bounds.Width,
Position = parent.PointToScreen(new Point()),
ShowInTaskbar = false,
};
parent.PositionChanged += OverlayOnPositionChanged;
void OverlayOnPositionChanged(object sender, PixelPointEventArgs e)
{
if (_contentDialogOverlayWindow is null)
{
return;
}
_contentDialogOverlayWindow.Position = parent.PointToScreen(new Point());
} }
_contentDialogOverlayWindow.ContentDialog = contentDialog; _contentDialogOverlayWindow.Position = parent.PointToScreen(new Point());
bool opened = false;
_contentDialogOverlayWindow.Opened += OverlayOnActivated;
async void OverlayOnActivated(object sender, EventArgs e)
{
if (opened)
{
return;
}
opened = true;
_contentDialogOverlayWindow.Position = parent.PointToScreen(new Point());
result = await ShowDialog();
}
result = await _contentDialogOverlayWindow.ShowDialog<ContentDialogResult>(parent);
} }
else
_contentDialogOverlayWindow.ContentDialog = contentDialog;
bool opened = false;
_contentDialogOverlayWindow.Opened += OverlayOnActivated;
async void OverlayOnActivated(object sender, EventArgs e)
{ {
if (opened)
{
return;
}
opened = true;
_contentDialogOverlayWindow.Position = parent.PointToScreen(new Point());
result = await ShowDialog(); result = await ShowDialog();
} }
result = await _contentDialogOverlayWindow.ShowDialog<ContentDialogResult>(parent);
async Task<ContentDialogResult> ShowDialog() async Task<ContentDialogResult> ShowDialog()
{ {
if (_contentDialogOverlayWindow is not null) if (_contentDialogOverlayWindow is not null)