Skip to content

Commit

Permalink
Window lifecycle (#1754)
Browse files Browse the repository at this point in the history
* Window Life Cycle

* - fix benchmark stubs

* - fix device tests

* - keep reference to associated window

* - fix window call

* - fix window
  • Loading branch information
PureWeen committed Jul 22, 2021
1 parent 8516b15 commit f5acc96
Show file tree
Hide file tree
Showing 33 changed files with 683 additions and 265 deletions.
36 changes: 36 additions & 0 deletions src/Compatibility/Core/src/AppHostBuilderExtensions.Android.cs
@@ -0,0 +1,36 @@
using Microsoft.Maui.LifecycleEvents;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Controls.Compatibility;

namespace Microsoft.Maui.Controls.Hosting
{
public static partial class AppHostBuilderExtensions
{
internal static IAppHostBuilder ConfigureCompatibilityLifecycleEvents(this IAppHostBuilder builder) =>
builder.ConfigureLifecycleEvents(events => events.AddAndroid(OnConfigureLifeCycle));

static void OnConfigureLifeCycle(IAndroidLifecycleBuilder android)
{
android
.OnApplicationCreating((app) =>
{
// This is the initial Init to set up any system services registered by
// Forms.Init(). This happens in the Application's OnCreate - before
// any UI has appeared.
// This creates a dummy MauiContext that wraps the Application.
var services = MauiApplication.Current.Services;
var mauiContext = new MauiContext(services, app);
var state = new ActivationState(mauiContext);
Forms.Init(state, new InitializationOptions { Flags = InitializationFlags.SkipRenderers });
})
.OnMauiContextCreated((mauiContext) =>
{
// This is the final Init that sets up the real context from the activity.
var state = new ActivationState(mauiContext);
Forms.Init(state);
});
}
}
}
12 changes: 12 additions & 0 deletions src/Compatibility/Core/src/AppHostBuilderExtensions.Standard.cs
@@ -0,0 +1,12 @@
using Microsoft.Maui.Hosting;
using Microsoft.Maui.LifecycleEvents;
using System;

namespace Microsoft.Maui.Controls.Hosting
{
public static partial class AppHostBuilderExtensions
{
internal static IAppHostBuilder ConfigureCompatibilityLifecycleEvents(this IAppHostBuilder builder) =>
builder;
}
}
50 changes: 50 additions & 0 deletions src/Compatibility/Core/src/AppHostBuilderExtensions.Windows.cs
@@ -0,0 +1,50 @@
#nullable enable
using Microsoft.Maui.Controls.Compatibility.Platform.UWP;
using Microsoft.Maui.Graphics.Win2D;
using BoxRenderer = Microsoft.Maui.Controls.Compatibility.Platform.UWP.BoxViewBorderRenderer;
using CellRenderer = Microsoft.Maui.Controls.Compatibility.Platform.UWP.TextCellRenderer;
using Deserializer = Microsoft.Maui.Controls.Compatibility.Platform.UWP.WindowsSerializer;
using ResourcesProvider = Microsoft.Maui.Controls.Compatibility.Platform.UWP.WindowsResourcesProvider;
using StreamImagesourceHandler = Microsoft.Maui.Controls.Compatibility.Platform.UWP.StreamImageSourceHandler;
using ImageLoaderSourceHandler = Microsoft.Maui.Controls.Compatibility.Platform.UWP.UriImageSourceHandler;
using DefaultRenderer = Microsoft.Maui.Controls.Compatibility.Platform.UWP.DefaultRenderer;
using Microsoft.Maui.LifecycleEvents;
using Microsoft.Maui.Controls.Compatibility;
using System;
using Microsoft.Maui.Hosting;

namespace Microsoft.Maui.Controls.Hosting
{
public static partial class AppHostBuilderExtensions
{
internal static IAppHostBuilder ConfigureCompatibilityLifecycleEvents(this IAppHostBuilder builder) =>
builder.ConfigureLifecycleEvents(events => events.AddWindows(OnConfigureLifeCycle));

static void OnConfigureLifeCycle(IWindowsLifecycleBuilder windows)
{
windows.OnLaunching((app, args) =>
{
// This is the initial Init to set up any system services registered by
// Forms.Init(). This happens before any UI has appeared.
// This creates a dummy MauiContext.
// We need to call this so the Window and Root Page can new up successfully
// The dispatcher that's inside of Forms.Init needs to be setup before the initial
// window and root page start creating.
// Inside OnLaunched we grab the MauiContext that's on the window so we can have the correct
// MauiContext inside Forms
var services = MauiWinUIApplication.Current.Services;
var mauiContext = new MauiContext(services);
var state = new ActivationState(mauiContext, args);
Forms.Init(state, new InitializationOptions { Flags = InitializationFlags.SkipRenderers });
})
.OnMauiContextCreated((mauiContext) =>
{
// This is the final Init that sets up the real context from the application.
var state = new ActivationState(mauiContext);
Forms.Init(state);
});
}
}
}
76 changes: 3 additions & 73 deletions src/Compatibility/Core/src/AppHostBuilderExtensions.cs
Expand Up @@ -40,7 +40,7 @@

namespace Microsoft.Maui.Controls.Hosting
{
public static class AppHostBuilderExtensions
public static partial class AppHostBuilderExtensions
{
public static IAppHostBuilder UseMauiApp<TApp>(this IAppHostBuilder builder)
where TApp : class, IApplication
Expand All @@ -67,81 +67,11 @@ public static IAppHostBuilder UseMauiApp<TApp>(this IAppHostBuilder builder, Fun

return builder;
}


static IAppHostBuilder SetupDefaults(this IAppHostBuilder builder)
{
builder.ConfigureLifecycleEvents(events =>
{
#if __ANDROID__
events.AddAndroid(android => android
.OnApplicationCreating((app) =>
{
// This is the initial Init to set up any system services registered by
// Forms.Init(). This happens in the Application's OnCreate - before
// any UI has appeared.
// This creates a dummy MauiContext that wraps the Application.
var services = MauiApplication.Current.Services;
var mauiContext = new MauiContext(services, app);
var state = new ActivationState(mauiContext);
Forms.Init(state, new InitializationOptions { Flags = InitializationFlags.SkipRenderers });
})
.OnMauiContextCreated((mauiContext) =>
{
// This is the final Init that sets up the real context from the activity.
var state = new ActivationState(mauiContext);
Forms.Init(state);
}));
#elif __IOS__
events.AddiOS(iOS => iOS
.WillFinishLaunching((app, options) =>
{
// This is the initial Init to set up any system services registered by
// Forms.Init(). This happens before any UI has appeared.
// This creates a dummy MauiContext.
var services = MauiUIApplicationDelegate.Current.Services;
var mauiContext = new MauiContext(services);
var state = new ActivationState(mauiContext);
Forms.Init(state, new InitializationOptions { Flags = InitializationFlags.SkipRenderers });
return true;
})
.OnMauiContextCreated((mauiContext) =>
{
// This is the final Init that sets up the real context from the application.
var state = new ActivationState(mauiContext);
Forms.Init(state);
}));
#elif WINDOWS
events.AddWindows(windows => windows
.OnLaunching((app, args) =>
{
// This is the initial Init to set up any system services registered by
// Forms.Init(). This happens before any UI has appeared.
// This creates a dummy MauiContext.
// We need to call this so the Window and Root Page can new up successfully
// The dispatcher that's inside of Forms.Init needs to be setup before the initial
// window and root page start creating.
// Inside OnLaunched we grab the MauiContext that's on the window so we can have the correct
// MauiContext inside Forms
var services = MauiWinUIApplication.Current.Services;
var mauiContext = new MauiContext(services);
var state = new ActivationState(mauiContext, args);
Forms.Init(state, new InitializationOptions { Flags = InitializationFlags.SkipRenderers });
})
.OnMauiContextCreated((mauiContext) =>
{
// This is the final Init that sets up the real context from the application.
var state = new ActivationState(mauiContext);
Forms.Init(state);
}));
#endif
});

builder.ConfigureCompatibilityLifecycleEvents();
builder
.ConfigureMauiHandlers(handlers =>
{
Expand Down
35 changes: 35 additions & 0 deletions src/Compatibility/Core/src/AppHostBuilderExtensions.iOS.cs
@@ -0,0 +1,35 @@
using Microsoft.Maui.Hosting;
using Microsoft.Maui.LifecycleEvents;
using Microsoft.Maui.Controls.Compatibility;

namespace Microsoft.Maui.Controls.Hosting
{
public static partial class AppHostBuilderExtensions
{
internal static IAppHostBuilder ConfigureCompatibilityLifecycleEvents(this IAppHostBuilder builder) =>
builder.ConfigureLifecycleEvents(events => events.AddiOS(OnConfigureLifeCycle));

static void OnConfigureLifeCycle(IiOSLifecycleBuilder iOS)
{
iOS.WillFinishLaunching((app, options) =>
{
// This is the initial Init to set up any system services registered by
// Forms.Init(). This happens before any UI has appeared.
// This creates a dummy MauiContext.
var services = MauiUIApplicationDelegate.Current.Services;
var mauiContext = new MauiContext(services);
var state = new ActivationState(mauiContext);
Forms.Init(state, new InitializationOptions { Flags = InitializationFlags.SkipRenderers });
return true;
})
.OnMauiContextCreated((mauiContext) =>
{
// This is the final Init that sets up the real context from the application.
var state = new ActivationState(mauiContext);
Forms.Init(state);
});
}
}
}
10 changes: 5 additions & 5 deletions src/Compatibility/Core/src/Windows/Forms.cs
Expand Up @@ -119,11 +119,11 @@ public static void Init(IActivationState state, InitializationOptions? options =
{
MainWindow = mainWindow;

if (mainWindow is WindowsBasePage windowsPage)
{
windowsPage.LoadApplication(windowsPage.CreateApplication());
windowsPage.Activate();
}
//if (mainWindow is WindowsBasePage windowsPage)
//{
// windowsPage.LoadApplication(windowsPage.CreateApplication());
// windowsPage.Activate();
//}
}

IsInitialized = true;
Expand Down
13 changes: 5 additions & 8 deletions src/Compatibility/Core/src/Windows/NavigationPageRenderer.cs
Expand Up @@ -35,11 +35,8 @@ public class NavigationPageRenderer : IVisualElementRenderer, ITitleProvider, IT
WImageSource _titleIcon;
VisualElementTracker<Page, PageControl> _tracker;
EntranceThemeTransition _transition;
Platform _platform;
bool _parentsLookedUp = false;

Platform Platform => _platform ?? (_platform = Platform.Current);

public NavigationPage Element { get; private set; }

protected VisualElementTracker<Page, PageControl> Tracker
Expand Down Expand Up @@ -676,7 +673,7 @@ void UpdateBackButton()
_container.SetBackButtonTitle(Element);
}

async void UpdateTitleOnParents()
void UpdateTitleOnParents()
{
if (Element == null || _currentPage == null)
return;
Expand Down Expand Up @@ -710,10 +707,10 @@ async void UpdateTitleOnParents()

if (_showTitle || (render != null && render.ShowTitle))
{
if (Platform != null)
{
await Platform.UpdateToolbarItems();
}
//if (Platform != null)
//{
// await Platform.UpdateToolbarItems();
//}
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/Compatibility/Core/src/Windows/Platform.cs
Expand Up @@ -109,15 +109,15 @@ public static IVisualElementRenderer CreateRenderer(VisualElement element)
return renderer;
}

internal static Platform Current
{
get
{
var frame = UI.Xaml.Window.Current?.Content as Microsoft.UI.Xaml.Controls.Frame;
var wbp = frame?.Content as WindowsBasePage;
return wbp?.Platform;
}
}
//internal static Platform Current
//{
// get
// {
// var frame = UI.Xaml.Window.Current?.Content as Microsoft.UI.Xaml.Controls.Frame;
// var wbp = frame?.Content as WindowsBasePage;
// return wbp?.Platform;
// }
//}

internal Platform(Microsoft.UI.Xaml.Window page)
{
Expand Down

0 comments on commit f5acc96

Please sign in to comment.