Skip to content

Commit

Permalink
Organize and centralize HandlerExtensions (#3716)
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen authored Dec 9, 2021
1 parent 7ebd79a commit ca1ea75
Show file tree
Hide file tree
Showing 16 changed files with 339 additions and 416 deletions.
13 changes: 13 additions & 0 deletions src/Core/src/Platform/Android/ElementExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using Android.App;
using Android.Content;
using AView = Android.Views.View;

namespace Microsoft.Maui.Platform
{
public static partial class ElementExtensions
{
public static AView ToContainerView(this IElement view, IMauiContext context) =>
new ContainerView(context) { CurrentView = view };
}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,31 @@
using System;
using Android.App;
using Android.Content;
using AView = Android.Views.View;
using System;
#if __IOS__ || MACCATALYST
using NativeView = UIKit.UIView;
using BasePlatformType = Foundation.NSObject;
using PlatformWindow = UIKit.UIWindow;
using PlatformApplication = UIKit.UIApplicationDelegate;
#elif MONOANDROID
using NativeView = Android.Views.View;
using BasePlatformType = Android.Content.Context;
using PlatformWindow = Android.App.Activity;
using PlatformApplication = Android.App.Application;
#elif WINDOWS
using NativeView = Microsoft.UI.Xaml.FrameworkElement;
using BasePlatformType = WinRT.IWinRTObject;
using PlatformWindow = Microsoft.UI.Xaml.Window;
using PlatformApplication = Microsoft.UI.Xaml.Application;
#elif NETSTANDARD || (NET6_0 && !IOS && !ANDROID)
using NativeView = System.Object;
using BasePlatformType = System.Object;
using INativeViewHandler = Microsoft.Maui.IViewHandler;
using PlatformWindow = System.Object;
using PlatformApplication = System.Object;
#endif

namespace Microsoft.Maui.Platform
{
public static class HandlerExtensions
public static partial class ElementExtensions
{
internal static AView? GetNative(this IElement view, bool returnWrappedIfPresent)
{
if (view.Handler is INativeViewHandler nativeHandler && nativeHandler.NativeView != null)
return nativeHandler.NativeView;

return (view.Handler?.NativeView as AView);

}

internal static AView ToNative(this IElement view, IMauiContext context, bool returnWrappedIfPresent)
{
var nativeView = view.ToNative(context);

if (view.Handler is INativeViewHandler nativeHandler && nativeHandler.NativeView != null)
return nativeHandler.NativeView;

return nativeView;

}

public static AView ToContainerView(this IElement view, IMauiContext context) =>
new ContainerView(context) { CurrentView = view };

public static AView ToNative(this IElement view, IMauiContext context)
{
var handler = view.ToHandler(context);

if (handler.NativeView is not AView result)
{
throw new InvalidOperationException($"Unable to convert {view} to {typeof(AView)}");
}
return result;
}

public static IElementHandler ToHandler(this IElement view, IMauiContext context)
{
_ = view ?? throw new ArgumentNullException(nameof(view));
Expand Down Expand Up @@ -68,16 +53,41 @@ public static IElementHandler ToHandler(this IElement view, IMauiContext context
if (handler.VirtualView != view)
handler.SetVirtualView(view);

return (IElementHandler)handler;
return handler;
}

public static void SetApplicationHandler(this Application nativeApplication, IApplication application, IMauiContext context) =>
SetHandler(nativeApplication, application, context);
internal static NativeView? GetNative(this IElement view, bool returnWrappedIfPresent)
{
if (view.Handler is INativeViewHandler nativeHandler && nativeHandler.NativeView != null)
return nativeHandler.NativeView;

return view.Handler?.NativeView as NativeView;

}

internal static NativeView ToNative(this IElement view, IMauiContext context, bool returnWrappedIfPresent)
{
var nativeView = view.ToNative(context);

if (view.Handler is INativeViewHandler nativeHandler && nativeHandler.NativeView != null)
return nativeHandler.NativeView;

return nativeView;

public static void SetWindowHandler(this Activity activity, IWindow window, IMauiContext context) =>
SetHandler(activity, window, context);
}

static void SetHandler(this Context nativeElement, IElement element, IMauiContext context)
public static NativeView ToNative(this IElement view, IMauiContext context)
{
var handler = view.ToHandler(context);

if (handler.NativeView is not NativeView result)
{
throw new InvalidOperationException($"Unable to convert {view} to {typeof(NativeView)}");
}
return result;
}

static void SetHandler(this BasePlatformType nativeElement, IElement element, IMauiContext context)
{
_ = nativeElement ?? throw new ArgumentNullException(nameof(nativeElement));
_ = element ?? throw new ArgumentNullException(nameof(element));
Expand All @@ -100,5 +110,11 @@ static void SetHandler(this Context nativeElement, IElement element, IMauiContex
if (handler.VirtualView != element)
handler.SetVirtualView(element);
}

public static void SetApplicationHandler(this PlatformApplication nativeApplication, IApplication application, IMauiContext context) =>
SetHandler(nativeApplication, application, context);

public static void SetWindowHandler(this PlatformWindow nativeWindow, IWindow window, IMauiContext context) =>
SetHandler(nativeWindow, window, context);
}
}
}
6 changes: 6 additions & 0 deletions src/Core/src/Platform/ViewExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Numerics;
using Microsoft.Maui.Graphics;
#if NETSTANDARD || (NET6_0 && !IOS && !ANDROID)
using INativeViewHandler = Microsoft.Maui.IViewHandler;
#endif

namespace Microsoft.Maui
{
Expand All @@ -13,5 +16,8 @@ public static partial class ViewExtensions
internal static double ExtractAngleInRadians(this Matrix4x4 matrix) => Math.Atan2(matrix.M21, matrix.M11);

internal static double ExtractAngleInDegrees(this Matrix4x4 matrix) => ExtractAngleInRadians(matrix) * 180 / Math.PI;

public static INativeViewHandler ToHandler(this IView view, IMauiContext context) =>
(INativeViewHandler)ElementExtensions.ToHandler(view, context);
}
}
88 changes: 0 additions & 88 deletions src/Core/src/Platform/Windows/HandlerExtensions.cs

This file was deleted.

27 changes: 27 additions & 0 deletions src/Core/src/Platform/iOS/ElementExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using Foundation;
using ObjCRuntime;
using UIKit;

namespace Microsoft.Maui.Platform
{
public static partial class ElementExtensions
{
const string UIApplicationSceneManifestKey = "UIApplicationSceneManifest";

public static UIViewController ToUIViewController(this IElement view, IMauiContext context)
{
var nativeView = view.ToNative(context);
if (view?.Handler is INativeViewHandler nvh && nvh.ViewController != null)
return nvh.ViewController;

return new ContainerViewController { CurrentView = view, Context = context };
}

// If < iOS 13 or the Info.plist does not have a scene manifest entry we need to assume no multi window, and no UISceneDelegate.
// We cannot check for iPads/Mac because even on the iPhone it uses the scene delegate if one is specified in the manifest.
public static bool HasSceneManifest(this UIApplicationDelegate nativeApplication) =>
UIDevice.CurrentDevice.CheckSystemVersion(13, 0) &&
NSBundle.MainBundle.InfoDictionary.ContainsKey(new NSString(UIApplicationSceneManifestKey));
}
}
112 changes: 0 additions & 112 deletions src/Core/src/Platform/iOS/HandlerExtensions.cs

This file was deleted.

Loading

0 comments on commit ca1ea75

Please sign in to comment.