From 95cff18d95cb78700358fc2765a3114b06139e0c Mon Sep 17 00:00:00 2001 From: Brandon Minnick <13558917+brminnick@users.noreply.github.com> Date: Thu, 3 Oct 2024 16:37:35 -0700 Subject: [PATCH] Add Popup Support for MediaElement on iOS + MacCatalyst (#2251) * Support Popup * Update comments * Update description in comments * Update comment descriptions * Update comments * Update MauiMediaElement.macios.cs --------- Co-authored-by: James Crutchley --- .../Views/Popup/MauiPopup.macios.cs | 8 ++++---- .../Extensions/ElementExtensions.shared.cs | 18 ++++++++++++++++++ .../Views/MauiMediaElement.macios.cs | 12 ++++++++++-- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/CommunityToolkit.Maui.Core/Views/Popup/MauiPopup.macios.cs b/src/CommunityToolkit.Maui.Core/Views/Popup/MauiPopup.macios.cs index b910f6d2bd..1c3618f83f 100644 --- a/src/CommunityToolkit.Maui.Core/Views/Popup/MauiPopup.macios.cs +++ b/src/CommunityToolkit.Maui.Core/Views/Popup/MauiPopup.macios.cs @@ -60,8 +60,8 @@ public override void ViewDidLayoutSubviews() if (VirtualView is not null) { - PopupExtensions.SetSize(this, VirtualView); - PopupExtensions.SetLayout(this, VirtualView); + this.SetSize(VirtualView); + this.SetLayout(VirtualView); } } @@ -76,8 +76,8 @@ public override void ViewWillTransitionToSize(CGSize toSize, IUIViewControllerTr // After screen rotate if (VirtualView is not null) { - PopupExtensions.SetSize(this, VirtualView); - PopupExtensions.SetLayout(this, VirtualView); + this.SetSize(VirtualView); + this.SetLayout(VirtualView); } }); diff --git a/src/CommunityToolkit.Maui.MediaElement/Extensions/ElementExtensions.shared.cs b/src/CommunityToolkit.Maui.MediaElement/Extensions/ElementExtensions.shared.cs index 95b7410c49..da7d88180c 100644 --- a/src/CommunityToolkit.Maui.MediaElement/Extensions/ElementExtensions.shared.cs +++ b/src/CommunityToolkit.Maui.MediaElement/Extensions/ElementExtensions.shared.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using Microsoft.Maui.Platform; namespace CommunityToolkit.Maui.Extensions; static class ElementExtensions @@ -19,4 +20,21 @@ public static bool TryFindParent(this Element? child, [NotNullWhen(true)] out parent = null; return false; } + + public static bool TryFindParentPlatformView(this Element? child, [NotNullWhen(true)] out T? parentPlatformView) + { + while (child is not null) + { + if (child.Parent?.Handler?.PlatformView is T element) + { + parentPlatformView = element; + return true; + } + + child = child.Parent; + } + + parentPlatformView = default; + return false; + } } \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.macios.cs b/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.macios.cs index 1b1a7ff24b..82fa4cc342 100644 --- a/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.macios.cs +++ b/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.macios.cs @@ -29,8 +29,16 @@ public MauiMediaElement(AVPlayerViewController playerViewController, MediaElemen UIViewController? viewController; - // If Parent is not null, we can retrieve the PageHandler using the traversing `MediaElement.Parent` until the Page is located - if (virtualView.TryFindParent(out var page) + // If any of the Parents in the VisualTree of MediaElement uses a UIViewController for their PlatformView, use it as the child ViewController + // This enables support for UI controls like CommunityToolkit.Maui.Popup whose PlatformView is a UIViewController (e.g. `public class MauiPopup : UIViewController`) + // To find the UIViewController, we traverse `MediaElement.Parent` until a Parent using UIViewController is located + if (virtualView.TryFindParentPlatformView(out UIViewController? parentUIViewController)) + { + viewController = parentUIViewController; + } + // If none of the Parents in the VisualTree of MediaElement use a UIViewController, we can use the ViewController in the PageHandler + // To find the PageHandler, we traverse `MediaElement.Parent` until the Page is located + else if (virtualView.TryFindParent(out var page) && page.Handler is PageHandler { ViewController: not null } pageHandler) { viewController = pageHandler.ViewController;