From baba2c2467e59c52d391b0caec7dec4186d64d99 Mon Sep 17 00:00:00 2001
From: Emmanuel Hansen <emmausssss@gmail.com>
Date: Sun, 23 Oct 2022 09:15:45 +0000
Subject: [PATCH] Avalonia: Use overlay dialog for controller applet (#3777)

* use overlay dialog for controller applet

* Update Ryujinx.Ava/Ui/Controls/ContentDialogHelper.cs

Co-authored-by: riperiperi <rhy3756547@hotmail.com>

Co-authored-by: riperiperi <rhy3756547@hotmail.com>
---
 .../Ui/Controls/ContentDialogHelper.cs        | 56 +++++++++----------
 1 file changed, 26 insertions(+), 30 deletions(-)

diff --git a/Ryujinx.Ava/Ui/Controls/ContentDialogHelper.cs b/Ryujinx.Ava/Ui/Controls/ContentDialogHelper.cs
index e774a09a0a..9fcc2d2ec0 100644
--- a/Ryujinx.Ava/Ui/Controls/ContentDialogHelper.cs
+++ b/Ryujinx.Ava/Ui/Controls/ContentDialogHelper.cs
@@ -4,6 +4,7 @@ using Avalonia.Controls.ApplicationLifetimes;
 using Avalonia.Controls.Primitives;
 using Avalonia.Media;
 using Avalonia.Threading;
+using FluentAvalonia.Core;
 using FluentAvalonia.UI.Controls;
 using Ryujinx.Ava.Common.Locale;
 using Ryujinx.Ava.Ui.Models;
@@ -27,7 +28,10 @@ namespace Ryujinx.Ava.Ui.Controls
             string secondaryButton,
             string closeButton,
             int iconSymbol,
-            UserResult primaryButtonResult = UserResult.Ok)
+            UserResult primaryButtonResult = UserResult.Ok,
+            ManualResetEvent deferResetEvent = null,
+            Func<Window, Task> doWhileDeferred = null,
+            TypedEventHandler<ContentDialog, ContentDialogButtonClickEventArgs> deferCloseAction = null)
         {
             UserResult result = UserResult.None;
 
@@ -110,12 +114,19 @@ namespace Ryujinx.Ava.Ui.Controls
                 contentDialog.SecondaryButtonCommand = MiniCommand.Create(() =>
                 {
                     result = UserResult.No;
+                    contentDialog.PrimaryButtonClick -= deferCloseAction;
                 });
                 contentDialog.CloseButtonCommand = MiniCommand.Create(() =>
                 {
                     result = UserResult.Cancel;
+                    contentDialog.PrimaryButtonClick -= deferCloseAction;
                 });
 
+                if (deferResetEvent != null)
+                {
+                    contentDialog.PrimaryButtonClick += deferCloseAction;
+                }
+
                 await contentDialog.ShowAsync(ContentDialogPlacement.Popup);
 
                 overlay?.Close();
@@ -143,35 +154,20 @@ namespace Ryujinx.Ava.Ui.Controls
             Func<Window, Task> doWhileDeferred = null)
         {
             bool startedDeferring = false;
-
             UserResult result = UserResult.None;
 
-            ContentDialog contentDialog = new ContentDialog
-            {
-                Title = title,
-                PrimaryButtonText = primaryButton,
-                SecondaryButtonText = secondaryButton,
-                CloseButtonText = closeButton,
-                Content = CreateDialogTextContent(primaryText, secondaryText, iconSymbol),
-                PrimaryButtonCommand = MiniCommand.Create(() =>
-                {
-                    result = primaryButton == LocaleManager.Instance["InputDialogYes"] ? UserResult.Yes : UserResult.Ok;
-                }),
-            };
-            contentDialog.SecondaryButtonCommand = MiniCommand.Create(() =>
-            {
-                contentDialog.PrimaryButtonClick -= DeferClose;
-                result = UserResult.No;
-            });
-            contentDialog.CloseButtonCommand = MiniCommand.Create(() =>
-            {
-                contentDialog.PrimaryButtonClick -= DeferClose;
-                result = UserResult.Cancel;
-            });
-            contentDialog.PrimaryButtonClick += DeferClose;
-            await contentDialog.ShowAsync(ContentDialogPlacement.Popup);
-
-            return result;
+            return await ShowContentDialog(
+                title,
+                primaryText,
+                secondaryText,
+                primaryButton,
+                secondaryButton,
+                closeButton,
+                iconSymbol,
+                primaryButton == LocaleManager.Instance["InputDialogYes"] ? UserResult.Yes : UserResult.Ok,
+                deferResetEvent,
+                doWhileDeferred,
+                DeferClose);
 
             async void DeferClose(ContentDialog sender, ContentDialogButtonClickEventArgs args)
             {
@@ -180,7 +176,7 @@ namespace Ryujinx.Ava.Ui.Controls
                     return;
                 }
 
-                contentDialog.PrimaryButtonClick -= DeferClose;
+                sender.PrimaryButtonClick -= DeferClose;
 
                 startedDeferring = true;
 
@@ -188,7 +184,7 @@ namespace Ryujinx.Ava.Ui.Controls
 
                 result = primaryButton == LocaleManager.Instance["InputDialogYes"] ? UserResult.Yes : UserResult.Ok;
 
-                contentDialog.PrimaryButtonClick -= DeferClose;
+                sender.PrimaryButtonClick -= DeferClose;
 
 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                 Task.Run(() =>