From 81c08fa59692077b254f15ebfa2e336c02c3804a Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Thu, 9 May 2024 04:19:03 -0500 Subject: [PATCH 1/3] Fix PopoverPresentationController for action sheet --- .../Issues/Issues16321.xaml | 2 + .../Issues/Issues16321.xaml.cs | 46 ++++++++++++++++--- .../Platform/AlertManager/AlertManager.iOS.cs | 26 ++++++----- .../tests/UITests/Tests/Issues/Issue16321.cs | 25 ++++------ 4 files changed, 64 insertions(+), 35 deletions(-) diff --git a/src/Controls/samples/Controls.Sample.UITests/Issues/Issues16321.xaml b/src/Controls/samples/Controls.Sample.UITests/Issues/Issues16321.xaml index 0a1fc6ae9c3e..ef5aa49882a5 100644 --- a/src/Controls/samples/Controls.Sample.UITests/Issues/Issues16321.xaml +++ b/src/Controls/samples/Controls.Sample.UITests/Issues/Issues16321.xaml @@ -6,6 +6,8 @@ + + \ No newline at end of file diff --git a/src/Controls/samples/Controls.Sample.UITests/Issues/Issues16321.xaml.cs b/src/Controls/samples/Controls.Sample.UITests/Issues/Issues16321.xaml.cs index ea75962617fe..0be91fe30696 100644 --- a/src/Controls/samples/Controls.Sample.UITests/Issues/Issues16321.xaml.cs +++ b/src/Controls/samples/Controls.Sample.UITests/Issues/Issues16321.xaml.cs @@ -4,6 +4,8 @@ using Microsoft.Maui; using System.Linq; using System; +using System.Threading.Tasks; + #if IOS using UIKit; @@ -38,8 +40,18 @@ async void OpenAlertWithModals(System.Object sender, System.EventArgs e) await Navigation.PopModalAsync(); } + async void OpenActionSheetWithModals(System.Object sender, System.EventArgs e) + { + await Navigation.PushModalAsync(new ContentPage()); + await Navigation.PushModalAsync(new ContentPage()); + await this.DisplayActionSheet("hello", "message", "Cancel", "Option 1", "Option 2"); + await Navigation.PopModalAsync(); + await Navigation.PopModalAsync(); + } + #if IOS - async void OpenAlertWithNewUIWindow(System.Object sender, System.EventArgs e) + + async void OpenPrompt(System.Object sender, System.EventArgs e, Func promptAction) { var uIWindow = new UIWindow(); var keyWindow = (this.Window.Handler.PlatformView as UIWindow); @@ -59,7 +71,7 @@ async void OpenAlertWithNewUIWindow(System.Object sender, System.EventArgs e) popupVC.ModalTransitionStyle = UIModalTransitionStyle.CoverVertical; await uIWindow.RootViewController.PresentViewControllerAsync(popupVC, false); - await page.DisplayAlert("hello", "message", "Cancel"); + await promptAction.Invoke(page); var rvc = uIWindow.RootViewController; @@ -74,11 +86,33 @@ async void OpenAlertWithNewUIWindow(System.Object sender, System.EventArgs e) keyWindow.WindowLevel = UIWindowLevel.Normal; this.RemoveLogicalChild(page); } -#else - async void OpenAlertWithNewUIWindow(System.Object sender, System.EventArgs e) + + void OpenAlertWithNewUIWindow(System.Object sender, System.EventArgs e) + { + OpenPrompt(sender, e, (page) => { - await this.DisplayAlert("hello", "message", "Cancel"); - } + return page.DisplayAlert("hello", "message", "Cancel"); + }); + } + + + void OpenActionSheetWithNewUIWindow(System.Object sender, System.EventArgs e) + { + OpenPrompt(sender, e, (page) => + { + return page.DisplayActionSheet("hello", "message", "Cancel", "Option 1", "Option 2"); + }); + } +#else + async void OpenActionSheetWithNewUIWindow(System.Object sender, System.EventArgs e) + { + await page.DisplayActionSheet("hello", "message", "Cancel", "Option 1", "Option 2"); + } + + async void OpenAlertWithNewUIWindow(System.Object sender, System.EventArgs e) + { + await this.DisplayAlert("hello", "message", "Cancel"); + } #endif } } \ No newline at end of file diff --git a/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs b/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs index 2729181c83c0..e4323b096bbf 100644 --- a/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs +++ b/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs @@ -177,11 +177,22 @@ void PresentActionSheet(Page sender, ActionSheetArguments arguments) static void PresentPopUp(Page sender, Window virtualView, UIWindow platformView, UIAlertController alert, ActionSheetArguments arguments = null) { + UIWindow presentingWindow = platformView; + + if (sender.Handler is IPlatformViewHandler pvh && + pvh.PlatformView?.Window is UIWindow senderPageWindow && + senderPageWindow != platformView && + senderPageWindow.RootViewController is not null) + { + presentingWindow = senderPageWindow; + } + if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad && arguments != null) { + var topViewController = GetTopUIViewController(presentingWindow); UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications(); var observer = NSNotificationCenter.DefaultCenter.AddObserver(UIDevice.OrientationDidChangeNotification, - n => { alert.PopoverPresentationController.SourceRect = platformView.RootViewController.View.Bounds; }); + n => { alert.PopoverPresentationController.SourceRect = topViewController.View.Bounds; }); arguments.Result.Task.ContinueWith(t => { @@ -189,20 +200,11 @@ static void PresentPopUp(Page sender, Window virtualView, UIWindow platformView, UIDevice.CurrentDevice.EndGeneratingDeviceOrientationNotifications(); }, TaskScheduler.FromCurrentSynchronizationContext()); - alert.PopoverPresentationController.SourceView = platformView.RootViewController.View; - alert.PopoverPresentationController.SourceRect = platformView.RootViewController.View.Bounds; + alert.PopoverPresentationController.SourceView = topViewController.View; + alert.PopoverPresentationController.SourceRect = topViewController.View.Bounds; alert.PopoverPresentationController.PermittedArrowDirections = 0; // No arrow } - UIWindow presentingWindow = platformView; - if (sender.Handler is IPlatformViewHandler pvh && - pvh.PlatformView?.Window is UIWindow senderPageWindow && - senderPageWindow != platformView && - senderPageWindow.RootViewController is not null) - { - presentingWindow = senderPageWindow; - } - presentingWindow.BeginInvokeOnMainThread(() => { GetTopUIViewController(presentingWindow) diff --git a/src/Controls/tests/UITests/Tests/Issues/Issue16321.cs b/src/Controls/tests/UITests/Tests/Issues/Issue16321.cs index 0c02e35076d4..2552915bd472 100644 --- a/src/Controls/tests/UITests/Tests/Issues/Issue16321.cs +++ b/src/Controls/tests/UITests/Tests/Issues/Issue16321.cs @@ -11,29 +11,20 @@ public Issue16321(TestDevice device) : base(device) { } public override string Issue => "Alerts Open on top of current presented view"; [Test] + [TestCase("OpenAlertWithModals")] + [TestCase("OpenAlertWithNewUIWindow")] + [TestCase("OpenActionSheetWithNewUIWindow")] + [TestCase("OpenActionSheetWithModals")] [Category(UITestCategories.DisplayAlert)] - public void OpenAlertWithModals() + public void OpenAlertWithModals(string testCase) { this.IgnoreIfPlatforms(new[] { TestDevice.Mac, TestDevice.Windows, TestDevice.Android }); - App.WaitForElement("OpenAlertWithModals").Click(); - App.WaitForElement("Cancel").Click(); - } - - [Test] - [Category(UITestCategories.DisplayAlert)] - public void OpenAlertWithNewUIWindow() - { - this.IgnoreIfPlatforms(new[] - { - TestDevice.Mac, TestDevice.Windows, TestDevice.Android - }); - - App.WaitForElement("OpenAlertWithNewUIWindow").Click(); - App.WaitForElement("Cancel").Click(); + App.WaitForElement(testCase).Tap(); + App.WaitForElement("Cancel").Tap(); } } -} +} \ No newline at end of file From dadbeee107145dc9c2d2c61661d374eec07502f5 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Thu, 9 May 2024 05:00:39 -0500 Subject: [PATCH 2/3] - fix --- .../samples/Controls.Sample.UITests/Issues/Issues16321.xaml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controls/samples/Controls.Sample.UITests/Issues/Issues16321.xaml.cs b/src/Controls/samples/Controls.Sample.UITests/Issues/Issues16321.xaml.cs index 0be91fe30696..012702f7e6c0 100644 --- a/src/Controls/samples/Controls.Sample.UITests/Issues/Issues16321.xaml.cs +++ b/src/Controls/samples/Controls.Sample.UITests/Issues/Issues16321.xaml.cs @@ -106,7 +106,7 @@ void OpenActionSheetWithNewUIWindow(System.Object sender, System.EventArgs e) #else async void OpenActionSheetWithNewUIWindow(System.Object sender, System.EventArgs e) { - await page.DisplayActionSheet("hello", "message", "Cancel", "Option 1", "Option 2"); + await this.DisplayActionSheet("hello", "message", "Cancel", "Option 1", "Option 2"); } async void OpenAlertWithNewUIWindow(System.Object sender, System.EventArgs e) From 58f1a464e8ff2c194f76d952beef41486aa76999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Thu, 9 May 2024 12:03:59 +0200 Subject: [PATCH 3/3] Fixed build error --- .../samples/Controls.Sample.UITests/Issues/Issues16321.xaml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controls/samples/Controls.Sample.UITests/Issues/Issues16321.xaml.cs b/src/Controls/samples/Controls.Sample.UITests/Issues/Issues16321.xaml.cs index 0be91fe30696..012702f7e6c0 100644 --- a/src/Controls/samples/Controls.Sample.UITests/Issues/Issues16321.xaml.cs +++ b/src/Controls/samples/Controls.Sample.UITests/Issues/Issues16321.xaml.cs @@ -106,7 +106,7 @@ void OpenActionSheetWithNewUIWindow(System.Object sender, System.EventArgs e) #else async void OpenActionSheetWithNewUIWindow(System.Object sender, System.EventArgs e) { - await page.DisplayActionSheet("hello", "message", "Cancel", "Option 1", "Option 2"); + await this.DisplayActionSheet("hello", "message", "Cancel", "Option 1", "Option 2"); } async void OpenAlertWithNewUIWindow(System.Object sender, System.EventArgs e)