Skip to content

Commit

Permalink
[Tizen] Adds ApplicationHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
rookiejava committed Oct 6, 2021
1 parent 938be8f commit d84c486
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 98 deletions.
25 changes: 0 additions & 25 deletions src/Compatibility/Core/src/Tizen/Forms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,6 @@ public static TOut GetHandlerForObject<TOut>(object obj, params object[] args) w

public static void Init(IActivationState activationState, InitializationOptions options) => Init(activationState.Context, options);

public static void Init(CoreApplication application) => Init(new MauiContext(CoreUIAppContext.GetInstance(application)));

public static void Init(IMauiContext context, InitializationOptions options = null)
{
if (options != null && options.DisplayResolutionUnit != null)
Expand All @@ -412,29 +410,6 @@ public static void Init(IMauiContext context, InitializationOptions options = nu
SetupInit(context, options);
}

public static void Init(CoreApplication application, bool useDeviceIndependentPixel)
{
DisplayResolutionUnit = DisplayResolutionUnit.FromInit(useDeviceIndependentPixel);
SetupInit(new MauiContext(CoreUIAppContext.GetInstance(application)));
}

public static void Init(CoreApplication application, DisplayResolutionUnit unit)
{
DisplayResolutionUnit = unit ?? DisplayResolutionUnit.Pixel();
SetupInit(new MauiContext(CoreUIAppContext.GetInstance(application)));
}

public static void Init(InitializationOptions options)
{
if (options == null)
{
throw new ArgumentException("Must be set options", nameof(options));
}

DisplayResolutionUnit = options.DisplayResolutionUnit ?? DisplayResolutionUnit.FromInit(options.UseDeviceIndependentPixel);
SetupInit(new MauiContext(CoreUIAppContext.GetInstance(options.Context)), options);
}

static void SetupInit(IMauiContext context, InitializationOptions options = null)
{
Context = context.Context.CurrentApplication;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal partial class AlertManager
internal void Subscribe(Window window)
{
IMauiContext mauiContext = window?.MauiContext;
EWindow nativeWindow = mauiContext?.Window;
EWindow nativeWindow = mauiContext.Context?.MainWindow;

if (mauiContext == null || nativeWindow == null)
return;
Expand All @@ -39,7 +39,7 @@ internal void Subscribe(Window window)
internal void Unsubscribe(Window window)
{
IMauiContext mauiContext = window?.MauiContext;
EWindow nativeWindow = mauiContext?.Window;
EWindow nativeWindow = mauiContext.Context?.MainWindow;

var toRemove = Subscriptions.Where(s => s.Window == nativeWindow).ToList();

Expand Down
12 changes: 12 additions & 0 deletions src/Core/src/Handlers/Application/ApplicationHandler.Tizen.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Tizen.Applications;

namespace Microsoft.Maui.Handlers
{
public partial class ApplicationHandler : ElementHandler<IApplication, CoreUIApplication>
{
public static void MapTerminate(ApplicationHandler handler, IApplication application, object? args)
{
handler.NativeView.Exit();
}
}
}
2 changes: 2 additions & 0 deletions src/Core/src/Handlers/Application/ApplicationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using NativeView = Android.App.Application;
#elif WINDOWS
using NativeView = Microsoft.UI.Xaml.Application;
#elif TIZEN
using NativeView = Tizen.Applications.CoreUIApplication;
#endif

namespace Microsoft.Maui.Handlers
Expand Down
1 change: 0 additions & 1 deletion src/Core/src/Platform/IMauiContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public interface IMauiContext
Android.Content.Context? Context { get; }
#elif TIZEN
CoreUIAppContext? Context { get; }
ElmSharp.Window? Window { get; }
#endif
}
}
39 changes: 0 additions & 39 deletions src/Core/src/Platform/MauiContext.Tizen.cs

This file was deleted.

16 changes: 16 additions & 0 deletions src/Core/src/Platform/MauiContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ public MauiContext(IServiceProvider services, UI.Xaml.Application application, I
{
AddSpecific(application);
}
#elif TIZEN
public MauiContext(IServiceProvider services, Tizen.Applications.CoreUIApplication application, CoreUIAppContext context, IMauiContext? parent = null)
: this(services, parent)
{
AddSpecific(application);
AddWeakSpecific(context.MainWindow);
}

public MauiContext(IServiceProvider services, CoreUIAppContext context, IMauiContext? parent = null)
: this(services, parent)
{
AddWeakSpecific(context);
}
#endif

public MauiContext(IMauiContext parent)
Expand All @@ -59,6 +72,9 @@ public MauiContext(IServiceProvider services, IMauiContext? parent = null)
#if __ANDROID__
public Android.Content.Context? Context =>
Services.GetService<Android.Content.Context>();
#elif TIZEN
public CoreUIAppContext? Context =>
Services.GetService<CoreUIAppContext>();
#endif

internal void AddSpecific<TService>(TService instance)
Expand Down
14 changes: 0 additions & 14 deletions src/Core/src/Platform/Tizen/ActivationState.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/Core/src/Platform/Tizen/ContentCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ protected void OnLayoutUpdated(object? sender, LayoutEventArgs e)
var nativeGeometry = Geometry.ToDP();

var measured = CrossPlatformMeasure!(nativeGeometry.Width, nativeGeometry.Height);
if (measured != _measureCache)
if (measured != _measureCache && _virtualView?.Parent is IView parentView)
{
_virtualView?.Parent?.InvalidateMeasure();
parentView?.InvalidateMeasure();
}
_measureCache = measured;

Expand Down
31 changes: 21 additions & 10 deletions src/Core/src/Platform/Tizen/HandlerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,36 @@ public static EvasObject ToNative(this IElement view, IMauiContext context)
return result;
}

public static void SetWindow(this Window nativeWindow, IWindow window, IMauiContext mauiContext)
public static void SetApplicationHandler(this CoreUIApplication nativeApplication, IApplication application, IMauiContext context)
{
_ = nativeApplication ?? throw new ArgumentNullException(nameof(nativeApplication));
SetHandler(application, context);
}

public static void SetWindowHandler(this Window nativeWindow, IWindow window, IMauiContext context)
{
_ = nativeWindow ?? throw new ArgumentNullException(nameof(nativeWindow));
_ = window ?? throw new ArgumentNullException(nameof(window));
_ = mauiContext ?? throw new ArgumentNullException(nameof(mauiContext));
SetHandler(window, context);
}

var handler = window.Handler;
static void SetHandler(IElement element, IMauiContext context)
{
_ = element ?? throw new ArgumentNullException(nameof(element));
_ = context ?? throw new ArgumentNullException(nameof(context));

var handler = element.Handler;
if (handler == null)
handler = mauiContext.Handlers.GetHandler(window.GetType());
handler = context.Handlers.GetHandler(element.GetType());

if (handler == null)
throw new Exception($"Handler not found for view {window}.");
throw new Exception($"Handler not found for view {element}.");

handler.SetMauiContext(mauiContext);
handler.SetMauiContext(context);

window.Handler = handler;
element.Handler = handler;

if (handler.VirtualView != window)
handler.SetVirtualView(window);
if (handler.VirtualView != element)
handler.SetVirtualView(element);
}
}
}
4 changes: 2 additions & 2 deletions src/Core/src/Platform/Tizen/LayoutCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ protected void OnLayoutUpdated(object? sender, LayoutEventArgs e)
var nativeGeometry = Geometry.ToDP();

var measured = CrossPlatformMeasure!(nativeGeometry.Width, nativeGeometry.Height);
if (measured != _measureCache)
if (measured != _measureCache && _virtualView?.Parent is IView parentView)
{
_virtualView?.Parent?.InvalidateMeasure();
parentView?.InvalidateMeasure();
}
_measureCache = measured;

Expand Down
12 changes: 9 additions & 3 deletions src/Core/src/Platform/Tizen/MauiApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ protected override void OnPreCreate()

var mauiApp = CreateMauiApp();

MauiApplicationContext = new MauiContext(mauiApp.Services, this, CoreUIAppContext.GetInstance(this));

Services = mauiApp.Services;

Current.Services?.InvokeLifecycleEvents<TizenLifecycle.OnPreCreate>(del => del(this));
Expand All @@ -48,6 +50,8 @@ protected override void OnCreate()
if (Application == null)
throw new InvalidOperationException($"The {nameof(IApplication)} instance was not found.");

this.SetApplicationHandler(Application, MauiApplicationContext);

MainWindow = CreateNativeWindow();
MainWindow.Show();

Expand All @@ -57,11 +61,11 @@ protected override void OnCreate()
Window CreateNativeWindow()
{
var context = CoreUIAppContext.GetInstance(this);
var mauiContext = new MauiContext(Services, context);
var mauiContext = MauiApplicationContext.MakeScoped(context);

Services.InvokeLifecycleEvents<TizenLifecycle.OnMauiContextCreated>(del => del(mauiContext));

var tizenWindow = mauiContext.Window;
var tizenWindow = mauiContext.Context?.MainWindow;

if (tizenWindow == null)
throw new InvalidOperationException($"The {nameof(tizenWindow)} instance was not found.");
Expand All @@ -70,7 +74,7 @@ Window CreateNativeWindow()
var window = Application.CreateWindow(activationState);

_virtualWindow = new WeakReference<IWindow>(window);
tizenWindow.SetWindow(window, mauiContext);
tizenWindow.SetWindowHandler(window, mauiContext);

return tizenWindow;
}
Expand Down Expand Up @@ -131,6 +135,8 @@ protected override void OnTerminate()

public static new MauiApplication Current { get; private set; } = null!;

internal IMauiContext MauiApplicationContext { get; private set; } = null!;

public Window MainWindow { get; protected set; } = null!;

public IServiceProvider Services { get; protected set; } = null!;
Expand Down
26 changes: 26 additions & 0 deletions src/Core/src/Platform/Tizen/MauiContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.Extensions.DependencyInjection;
using ElmSharp;

namespace Microsoft.Maui
{
internal static partial class MauiContextExtensions
{

public static Window GetNativeWindow(this IMauiContext mauiContext) =>
mauiContext.Services.GetRequiredService<Window>();

public static IMauiContext MakeScoped(this IMauiContext mauiContext, Window nativeWindow)
{
var scopedContext = new MauiContext(mauiContext);
scopedContext.AddSpecific(nativeWindow);
return scopedContext;
}

public static IMauiContext MakeScoped(this IMauiContext mauiContext, CoreUIAppContext context)
{
var scopedContext = new MauiContext(mauiContext.Services, context, mauiContext);
scopedContext.AddSpecific(context);
return scopedContext;
}
}
}

0 comments on commit d84c486

Please sign in to comment.