From b041fd837cdab394cf90e20ca346def1162335fd Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Wed, 17 Apr 2024 19:58:25 -0400 Subject: [PATCH] Extend ContentDialogHelper to work on multiple windows --- src/Ryujinx/UI/Helpers/ContentDialogHelper.cs | 125 ++++++++++-------- 1 file changed, 70 insertions(+), 55 deletions(-) diff --git a/src/Ryujinx/UI/Helpers/ContentDialogHelper.cs b/src/Ryujinx/UI/Helpers/ContentDialogHelper.cs index 15b7ddd14..2ff57322e 100644 --- a/src/Ryujinx/UI/Helpers/ContentDialogHelper.cs +++ b/src/Ryujinx/UI/Helpers/ContentDialogHelper.cs @@ -28,7 +28,8 @@ namespace Ryujinx.Ava.UI.Helpers string closeButton, UserResult primaryButtonResult = UserResult.Ok, ManualResetEvent deferResetEvent = null, - TypedEventHandler deferCloseAction = null) + TypedEventHandler deferCloseAction = null, + Window parent = null) { UserResult result = UserResult.None; @@ -62,7 +63,7 @@ namespace Ryujinx.Ava.UI.Helpers contentDialog.PrimaryButtonClick += deferCloseAction; } - await ShowAsync(contentDialog); + await ShowAsync(contentDialog, parent); return result; } @@ -77,11 +78,21 @@ namespace Ryujinx.Ava.UI.Helpers int iconSymbol, UserResult primaryButtonResult = UserResult.Ok, ManualResetEvent deferResetEvent = null, - TypedEventHandler deferCloseAction = null) + TypedEventHandler deferCloseAction = null, + Window parent = null) { 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 ShowDeferredContentDialog( @@ -94,7 +105,8 @@ namespace Ryujinx.Ava.UI.Helpers string closeButton, int iconSymbol, ManualResetEvent deferResetEvent, - Func doWhileDeferred = null) + Func doWhileDeferred = null, + Window parent = null) { bool startedDeferring = false; @@ -108,7 +120,8 @@ namespace Ryujinx.Ava.UI.Helpers iconSymbol, primaryButton == LocaleManager.Instance[LocaleKeys.InputDialogYes] ? UserResult.Yes : UserResult.Ok, deferResetEvent, - DeferClose); + DeferClose, + parent); async void DeferClose(ContentDialog sender, ContentDialogButtonClickEventArgs args) { @@ -199,7 +212,8 @@ namespace Ryujinx.Ava.UI.Helpers string secondaryText, string acceptButton, string closeButton, - string title) + string title, + Window parent = null) { return await ShowTextDialog( title, @@ -208,7 +222,8 @@ namespace Ryujinx.Ava.UI.Helpers acceptButton, "", closeButton, - (int)Symbol.Important); + (int)Symbol.Important, + parent: parent); } internal static async Task CreateConfirmationDialog( @@ -217,7 +232,8 @@ namespace Ryujinx.Ava.UI.Helpers string acceptButtonText, string cancelButtonText, string title, - UserResult primaryButtonResult = UserResult.Yes) + UserResult primaryButtonResult = UserResult.Yes, + Window parent = null) { return await ShowTextDialog( string.IsNullOrWhiteSpace(title) ? LocaleManager.Instance[LocaleKeys.DialogConfirmationTitle] : title, @@ -227,7 +243,8 @@ namespace Ryujinx.Ava.UI.Helpers "", cancelButtonText, (int)Symbol.Help, - primaryButtonResult); + primaryButtonResult, + parent: parent); } internal static async Task CreateUpdaterInfoDialog(string primary, string secondaryText) @@ -268,7 +285,11 @@ namespace Ryujinx.Ava.UI.Helpers (int)Symbol.Dismiss); } - internal static async Task CreateChoiceDialog(string title, string primary, string secondaryText) + internal static async Task CreateChoiceDialog( + string title, + string primary, + string secondaryText, + Window parent = null) { if (_isChoiceDialogOpen) { @@ -285,7 +306,8 @@ namespace Ryujinx.Ava.UI.Helpers "", LocaleManager.Instance[LocaleKeys.InputDialogNo], (int)Symbol.Help, - UserResult.Yes); + UserResult.Yes, + parent: parent); _isChoiceDialogOpen = false; @@ -308,69 +330,62 @@ namespace Ryujinx.Ava.UI.Helpers LocaleManager.Instance[LocaleKeys.DialogExitSubMessage]); } - public static async Task ShowAsync(ContentDialog contentDialog) + public static async Task ShowAsync(ContentDialog contentDialog, Window parent = null) { ContentDialogResult result; bool isTopDialog = true; - Window parent = GetMainWindow(); + parent ??= GetMainWindow(); if (_contentDialogOverlayWindow != null) { 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, - 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()); + return; } - _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 _contentDialogOverlayWindow.ShowDialog(parent); + _contentDialogOverlayWindow.Position = parent.PointToScreen(new Point()); } - 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 _contentDialogOverlayWindow.ShowDialog(parent); + async Task ShowDialog() { if (_contentDialogOverlayWindow is not null)