Skip to content

Commit

Permalink
Merge pull request #6818 from Youssef1313/issues/1929
Browse files Browse the repository at this point in the history
fix: Hide open flyouts when MessageDialog is shown
  • Loading branch information
mergify[bot] authored Sep 28, 2021
2 parents 32b81e6 + 7bcd8eb commit ee16529
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;
using static Private.Infrastructure.TestServices;

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Popups
{
[TestClass]
public class Given_MessageDialog
{
#if !__WASM__
[TestMethod]
[RunsOnUIThread]
public async Task Should_Close_Open_Popups()
{
var button = new Windows.UI.Xaml.Controls.Button();
var flyout = new Flyout();
FlyoutBase.SetAttachedFlyout(button, flyout);
WindowHelper.WindowContent = button;
Assert.AreEqual(0, VisualTreeHelper.GetOpenPopups(Window.Current).Count);
FlyoutBase.ShowAttachedFlyout(button);
Assert.AreEqual(1, VisualTreeHelper.GetOpenPopups(Window.Current).Count);
var messageDialog = new MessageDialog("Hello");
var asyncOperation = messageDialog.ShowAsync();
Assert.AreEqual(0, VisualTreeHelper.GetOpenPopups(Window.Current).Count);
asyncOperation.Cancel();
}
#endif
}
}
3 changes: 2 additions & 1 deletion src/Uno.UI/UI/Xaml/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ static Application()
ApiInformation.RegisterAssembly(typeof(Application).Assembly);
ApiInformation.RegisterAssembly(typeof(Windows.Storage.ApplicationData).Assembly);

Uno.DispatcherTimerHelper.SetDispatcherTimerGetter(() => new DispatcherTimer());
Uno.Helpers.DispatcherTimerProxy.SetDispatcherTimerGetter(() => new DispatcherTimer());
Uno.Helpers.VisualTreeHelperProxy.SetCloseAllPopupsAction(() => Media.VisualTreeHelper.CloseAllPopups());

InitializePartialStatic();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Text;

namespace Uno
namespace Uno.Helpers
{
internal static class DispatcherTimerHelper
internal static class DispatcherTimerProxy
{
private static Func<IDispatcherTimer>? _getter;

Expand Down
15 changes: 15 additions & 0 deletions src/Uno.UWP/Helpers/VisualTreeHelperProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#nullable enable

using System;

namespace Uno.Helpers
{
internal static class VisualTreeHelperProxy
{
private static Action? _closeAllPopups;

public static void CloseAllPopups() => _closeAllPopups?.Invoke();

public static void SetCloseAllPopupsAction(Action closeAllPopups) => _closeAllPopups = closeAllPopups;
}
}
3 changes: 3 additions & 0 deletions src/Uno.UWP/UI/Popups/MessageDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Uno.Helpers;
using Windows.Foundation;
using Windows.UI.Core;

Expand Down Expand Up @@ -52,6 +53,8 @@ public MessageDialog(string content, string title)

public IAsyncOperation<IUICommand> ShowAsync()
{
VisualTreeHelperProxy.CloseAllPopups();

var invokedCommand = new TaskCompletionSource<IUICommand>();

return AsyncOperation.FromTask<IUICommand>(async ct =>
Expand Down
3 changes: 3 additions & 0 deletions src/Uno.UWP/UI/Popups/MessageDialog.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using Uno.Extensions;
using Uno.Helpers;
using Windows.Foundation;
using Windows.UI.Core;

Expand All @@ -16,6 +17,8 @@ public partial class MessageDialog

public IAsyncOperation<IUICommand> ShowAsync()
{
VisualTreeHelperProxy.CloseAllPopups();

var command = $"Uno.UI.WindowManager.current.alert(\"{Uno.Foundation.WebAssemblyRuntime.EscapeJs(Content)}\");";
Uno.Foundation.WebAssemblyRuntime.InvokeJS(command);

Expand Down

0 comments on commit ee16529

Please sign in to comment.