Skip to content

Commit

Permalink
Add Popup Support for MediaElement on iOS + MacCatalyst (#2251)
Browse files Browse the repository at this point in the history
* Support Popup

* Update comments

* Update description in comments

* Update comment descriptions

* Update comments

* Update MauiMediaElement.macios.cs

---------

Co-authored-by: James Crutchley <[email protected]>
  • Loading branch information
brminnick and ne0rrmatrix authored Oct 3, 2024
1 parent 9ba5965 commit 95cff18
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.Maui.Platform;
namespace CommunityToolkit.Maui.Extensions;

static class ElementExtensions
Expand All @@ -19,4 +20,21 @@ public static bool TryFindParent<T>(this Element? child, [NotNullWhen(true)] out
parent = null;
return false;
}

public static bool TryFindParentPlatformView<T>(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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Page>(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<Page>(out var page)
&& page.Handler is PageHandler { ViewController: not null } pageHandler)
{
viewController = pageHandler.ViewController;
Expand Down

0 comments on commit 95cff18

Please sign in to comment.