Skip to content

Commit

Permalink
fix(ContentDialog): Handle back button being pressed
Browse files Browse the repository at this point in the history
  • Loading branch information
kazo0 committed Oct 7, 2022
1 parent 2c93ac1 commit 17633b7
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Uno.UI.RuntimeTests.Extensions;
using Uno.UI.RuntimeTests.Helpers;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Tests.Enterprise;
using static Private.Infrastructure.TestServices;

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls
Expand Down Expand Up @@ -267,6 +270,60 @@ public async Task When_Soft_Keyboard_And_VisibleBounds()
}
}

[DataTestMethod]
[DataRow(true)]
[DataRow(false)]
public async Task When_BackButton_Pressed_With_CloseButton(bool isCloseButtonEnabled)
{
var closeButtonClickEvent = new Event();
var closedEvent = new Event();
var openedEvent = new Event();
var closeButtonClickRegistration = new SafeEventRegistration<ContentDialog, TypedEventHandler<ContentDialog, ContentDialogButtonClickEventArgs>>("CloseButtonClick");
var closedRegistration = new SafeEventRegistration<ContentDialog, TypedEventHandler<ContentDialog, ContentDialogClosedEventArgs>>("Closed");
var openedRegistration = new SafeEventRegistration<ContentDialog, TypedEventHandler<ContentDialog, ContentDialogOpenedEventArgs>>("Opened");

var SUT = new MyContentDialog
{
Title = "Dialog title",
Content = "Dialog content",
CloseButtonText = "Close",
};

if (!isCloseButtonEnabled)
{
var disabledStyle = new Style(typeof(Button));
disabledStyle.Setters.Add(new Setter(FrameworkElement.IsEnabledProperty, false));

SUT.CloseButtonStyle = disabledStyle;
}


closeButtonClickRegistration.Attach(SUT, (s, e) => closeButtonClickEvent.Set());
closedRegistration.Attach(SUT, (s, e) => closedEvent.Set());
openedRegistration.Attach(SUT, (s, e) => openedEvent.Set());

try
{
await ShowDialog(SUT);

await openedEvent.WaitForDefault();
VERIFY_IS_TRUE(SUT._popup.IsOpen);

SystemNavigationManager.GetForCurrentView().RequestBack();

await closeButtonClickEvent.WaitForDefault();
await closedEvent.WaitForDefault();

Assert.AreEqual(isCloseButtonEnabled, closeButtonClickEvent.HasFired());
VERIFY_IS_TRUE(closedEvent.HasFired());
VERIFY_IS_FALSE(SUT._popup.IsOpen);
}
finally
{
SUT.Hide();
}
}

#if __ANDROID__
// Fails because keyboard does not appear when TextBox is programmatically focussed, or appearance is not correctly registered - https://github.com/unoplatform/uno/issues/7995
[Ignore()]
Expand Down
30 changes: 30 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/ContentDialog/ContentDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Uno.UI.DataBinding;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.ViewManagement;
using Windows.UI.Core;

#if HAS_UNO_WINUI
using WindowSizeChangedEventArgs = Microsoft.UI.Xaml.WindowSizeChangedEventArgs;
Expand Down Expand Up @@ -345,8 +346,37 @@ private void RegisterEvents()
window.SizeChanged -= WindowSizeChanged;
});

// Here we are relying on the BackRequested event to be able to close the ContentDialog on back button pressed.
// This diverges from Windows behaviour as they do not allow BackRequested to be raised if the back button was pressed
// while a ContentDialog was open.
if (SystemNavigationManager.GetForCurrentView() is { } navManager)
{
navManager.BackRequested += OnBackRequested;

d.Add(() =>
{
navManager.BackRequested -= OnBackRequested;
});
}

_subscriptions.Disposable = d;
}
private void OnBackRequested(object sender, BackRequestedEventArgs e)
{
// Match Windows behavior:
// If we have a clickable close button, then invoke it, otherwise just
// return a result of None.
if (m_tpCloseButtonPart is { IsEnabled: true } closeButton)
{
closeButton.RaiseClick();
}
else
{
Hide();
}

e.Handled = true;
}

private void OnCloseButtonClicked(object sender, RoutedEventArgs e)
=> ProcessCloseButton();
Expand Down

0 comments on commit 17633b7

Please sign in to comment.