From e96ea59e844511335e3f43c8ef20f089d2839ced Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20=C3=98yvind=20Flu=C3=B8r?= Date: Fri, 21 Jun 2024 09:56:04 +0200 Subject: [PATCH] Open popups on the main application window --- .../Platforms/MacCatalyst/MacOSMopups.cs | 13 +++++++++++-- Mopups/Mopups.Maui/Platforms/iOS/iOSMopups.cs | 6 +++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Mopups/Mopups.Maui/Platforms/MacCatalyst/MacOSMopups.cs b/Mopups/Mopups.Maui/Platforms/MacCatalyst/MacOSMopups.cs index 42b2832..c657902 100644 --- a/Mopups/Mopups.Maui/Platforms/MacCatalyst/MacOSMopups.cs +++ b/Mopups/Mopups.Maui/Platforms/MacCatalyst/MacOSMopups.cs @@ -1,7 +1,7 @@ using CoreGraphics; using Microsoft.Maui.Controls.Compatibility.Platform.iOS; - +using Microsoft.Maui.Platform; using Mopups.Interfaces; using Mopups.Pages; using Mopups.Platforms.MacCatalyst; @@ -36,7 +36,16 @@ public Task AddAsync(PopupPage page) if (IsiOS13OrNewer) { - var connectedScene = UIApplication.SharedApplication.ConnectedScenes.ToArray().FirstOrDefault(x => x.ActivationState == UISceneActivationState.ForegroundActive); + UIScene connectedScene = null; + + if(page.Parent != null && page.Parent.Handler != null && page.Parent.Handler.MauiContext != null) { + var nativeMainPage = page.Parent.ToPlatform(page.Parent.Handler.MauiContext); + connectedScene = nativeMainPage.Window.WindowScene; + } + + if(connectedScene == null) + connectedScene = UIApplication.SharedApplication.ConnectedScenes.ToArray().FirstOrDefault(x => x.ActivationState == UISceneActivationState.ForegroundActive); + if (connectedScene != null && connectedScene is UIWindowScene windowScene) window = new PopupWindow(windowScene); else diff --git a/Mopups/Mopups.Maui/Platforms/iOS/iOSMopups.cs b/Mopups/Mopups.Maui/Platforms/iOS/iOSMopups.cs index 06a66b5..e29fde2 100644 --- a/Mopups/Mopups.Maui/Platforms/iOS/iOSMopups.cs +++ b/Mopups/Mopups.Maui/Platforms/iOS/iOSMopups.cs @@ -30,7 +30,10 @@ public Task AddAsync(PopupPage page) if (IsiOS13OrNewer) { - var connectedScene = UIApplication.SharedApplication.ConnectedScenes.ToArray().FirstOrDefault(x => x.ActivationState == UISceneActivationState.ForegroundActive); + var connectedScene = UIApplication.SharedApplication.ConnectedScenes.ToArray() + .Where(scene => scene.Session.Role == UIWindowSceneSessionRole.Application) + .FirstOrDefault(x => x.ActivationState == UISceneActivationState.ForegroundActive); + if (connectedScene != null && connectedScene is UIWindowScene windowScene) window = new PopupWindow(windowScene); else @@ -65,6 +68,7 @@ UIWindow GetKeyWindow(UIApplication application) .ConnectedScenes .ToArray() .OfType() + .Where(scene => scene.Session.Role == UIWindowSceneSessionRole.Application) .SelectMany(scene => scene.Windows) .FirstOrDefault(window => window.IsKeyWindow);