From a7efd1ac014e9482969f332840cd8d32c0e8b4c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Sua=CC=81rez=20Ruiz?= Date: Wed, 21 Apr 2021 20:11:30 +0200 Subject: [PATCH 1/2] Added Linux HandlerExtensions --- .../src/Platform/Linux/HandlerExtensions.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/Core/src/Platform/Linux/HandlerExtensions.cs diff --git a/src/Core/src/Platform/Linux/HandlerExtensions.cs b/src/Core/src/Platform/Linux/HandlerExtensions.cs new file mode 100644 index 000000000..1f13ad2c2 --- /dev/null +++ b/src/Core/src/Platform/Linux/HandlerExtensions.cs @@ -0,0 +1,37 @@ +using Gtk; +using System; + +namespace Microsoft.Maui +{ + public static class HandlerExtensions + { + public static Widget ToNative(this IView view, IMauiContext context) + { + _ = view ?? throw new ArgumentNullException(nameof(view)); + _ = context ?? throw new ArgumentNullException(nameof(context)); + + var handler = view.Handler; + + if (handler == null) + { + handler = context.Handlers.GetHandler(view.GetType()); + + if (handler == null) + throw new Exception($"Handler not found for view {view}"); + + handler.SetMauiContext(context); + + view.Handler = handler; + } + + handler.SetVirtualView(view); + + if (handler.NativeView is not Widget result) + { + throw new InvalidOperationException($"Unable to convert {view} to {typeof(Widget)}"); + } + + return result; + } + } +} \ No newline at end of file From 5ce87eb9040e7bd513a152ca1c0b8a9d5f7722c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Sua=CC=81rez=20Ruiz?= Date: Wed, 21 Apr 2021 20:37:53 +0200 Subject: [PATCH 2/2] Add Linux MauiWindow --- .../src/Platform/Linux/ActivationState.cs | 14 ++++++ src/Core/src/Platform/Linux/MauiContext.cs | 27 +++++++++++ src/Core/src/Platform/Linux/MauiWindow.cs | 47 +++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 src/Core/src/Platform/Linux/ActivationState.cs create mode 100644 src/Core/src/Platform/Linux/MauiContext.cs create mode 100644 src/Core/src/Platform/Linux/MauiWindow.cs diff --git a/src/Core/src/Platform/Linux/ActivationState.cs b/src/Core/src/Platform/Linux/ActivationState.cs new file mode 100644 index 000000000..6719e581c --- /dev/null +++ b/src/Core/src/Platform/Linux/ActivationState.cs @@ -0,0 +1,14 @@ +using System; + +namespace Microsoft.Maui +{ + public class ActivationState : IActivationState + { + public ActivationState(IMauiContext context) + { + Context = context ?? throw new ArgumentNullException(nameof(context)); + } + + public IMauiContext Context { get; } + } +} \ No newline at end of file diff --git a/src/Core/src/Platform/Linux/MauiContext.cs b/src/Core/src/Platform/Linux/MauiContext.cs new file mode 100644 index 000000000..e6549d784 --- /dev/null +++ b/src/Core/src/Platform/Linux/MauiContext.cs @@ -0,0 +1,27 @@ +using System; +using Microsoft.Extensions.DependencyInjection; + +namespace Microsoft.Maui +{ + public class MauiContext : IMauiContext + { + readonly IServiceProvider? _services; + readonly IMauiHandlersServiceProvider? _mauiHandlersServiceProvider; + + public MauiContext() + { + } + + public MauiContext(IServiceProvider services) + { + _services = services ?? throw new ArgumentNullException(nameof(services)); + _mauiHandlersServiceProvider = Services.GetRequiredService(); + } + + public IServiceProvider Services => + _services ?? throw new InvalidOperationException($"No service provider was specified during construction."); + + public IMauiHandlersServiceProvider Handlers => + _mauiHandlersServiceProvider ?? throw new InvalidOperationException($"No service provider was specified during construction."); + } +} \ No newline at end of file diff --git a/src/Core/src/Platform/Linux/MauiWindow.cs b/src/Core/src/Platform/Linux/MauiWindow.cs new file mode 100644 index 000000000..9c335b0ad --- /dev/null +++ b/src/Core/src/Platform/Linux/MauiWindow.cs @@ -0,0 +1,47 @@ +using System; +using Gtk; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Maui.Hosting; +using Microsoft.Maui.LifecycleEvents; + +namespace Microsoft.Maui +{ + public class MauiWindow : Window + where TStartup : IStartup, new() + { + public MauiWindow() : base(WindowType.Toplevel) + { + var startup = new TStartup(); + + var host = startup + .CreateAppHostBuilder() + .ConfigureServices(ConfigureNativeServices) + .ConfigureUsing(startup) + .Build(); + + Services = host.Services; + Application = Services.GetRequiredService(); + + var mauiContext = new MauiContext(Services); + + var activationState = new ActivationState(mauiContext); + var window = Application.CreateWindow(activationState); + window.MauiContext = mauiContext; + + var content = (window.Page as IView) ?? window.Page.View; + + Add(content.ToNative(window.MauiContext)); + Child.ShowAll(); + } + + public new IApplication Application { get; protected set; } = null!; + + public IServiceProvider Services { get; protected set; } = null!; + + // Configure native services like HandlersContext, ImageSourceHandlers etc.. + void ConfigureNativeServices(HostBuilderContext ctx, IServiceCollection services) + { + } + } +} \ No newline at end of file