Skip to content

Commit

Permalink
feat: Initialize WindowManager asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
ebariche committed Apr 13, 2023
1 parent d010280 commit 09be506
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 66 deletions.
31 changes: 22 additions & 9 deletions src/Uno.UI/UI/Xaml/Application.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Uno.Extensions;
using Uno.Foundation.Logging;
using System.Threading;
using System.Threading.Tasks;
using Uno.UI;
using Uno.UI.Xaml;
using Uno;
Expand Down Expand Up @@ -84,19 +85,31 @@ public static int DispatchVisibilityChange(bool isVisible)
return 0;
}

static partial void StartPartial(ApplicationInitializationCallback callback)
static async partial void StartPartial(ApplicationInitializationCallback callback)
{
_startInvoked = true;
try
{
_startInvoked = true;

SynchronizationContext.SetSynchronizationContext(
new CoreDispatcherSynchronizationContext(CoreDispatcher.Main, CoreDispatcherPriority.Normal)
);

var isLoadEventsEnabled = !FeatureConfiguration.FrameworkElement.WasmUseManagedLoadedUnloaded;
WindowManagerInterop.Init(isLoadEventsEnabled);
Windows.Storage.ApplicationData.Init();
var isLoadEventsEnabled = !FeatureConfiguration.FrameworkElement.WasmUseManagedLoadedUnloaded;

SynchronizationContext.SetSynchronizationContext(
new CoreDispatcherSynchronizationContext(CoreDispatcher.Main, CoreDispatcherPriority.Normal)
);
await WindowManagerInterop.InitAsync(isLoadEventsEnabled);

callback(new ApplicationInitializationCallbackParams());
Windows.Storage.ApplicationData.Init();

callback(new ApplicationInitializationCallbackParams());
}
catch (Exception exception)
{
if (typeof(Application).Log().IsEnabled(LogLevel.Error))
{
typeof(Application).Log().LogError("Application initialization failed.", exception);
}
}
}

partial void ObserveSystemThemeChanges()
Expand Down
29 changes: 10 additions & 19 deletions src/Uno.UI/UI/Xaml/WindowManagerInterop.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,13 @@ internal partial class WindowManagerInterop
//When users set double.MaxValue to scroll to the end of the page Javascript doesn't scroll.
private const double MAX_SCROLLING_OFFSET = 1_000_000_000_000_000_000;

#region Init
internal static void Init(bool isLoadEventsEnabled)
{
var parms = new WindowManagerInitParams
{
IsLoadEventsEnabled = isLoadEventsEnabled
};

TSInteropMarshaller.InvokeJS("UnoStatic:initNative", parms);
}

[TSInteropMessage]
[StructLayout(LayoutKind.Sequential, Pack = 4)]
private struct WindowManagerInitParams
{
public bool IsLoadEventsEnabled;
}

#endregion
internal static Task InitAsync(bool isLoadEventsEnabled)
=>
#if NET7_0_OR_GREATER
NativeMethods.InitAsync(isLoadEventsEnabled);
#else
WebAssemblyRuntime.InvokeAsync($"Uno.UI.WindowManager.init({(isLoadEventsEnabled ? "true" : "false")})");
#endif

internal static string FindLaunchArguments()
=>
Expand Down Expand Up @@ -1314,6 +1302,9 @@ internal static partial void ArrangeElement(
[JSImport("globalThis.Uno.UI.WindowManager.current.getProperty")]
internal static partial string GetProperty(IntPtr htmlId, string name);

[JSImport("globalThis.Uno.UI.WindowManager.init")]
internal static partial Task InitAsync(bool isLoadEventsEnabled);

[JSImport("globalThis.Uno.UI.WindowManager.current.measureViewNativeFast")]
internal static partial void MeasureView(IntPtr htmlId, double availableWidth, double availableHeight, bool measureContent, IntPtr pReturn);

Expand Down
1 change: 0 additions & 1 deletion src/Uno.UI/ts/MonoSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ namespace MonoSupport {
}
else {
if (!jsCallDispatcher._isUnoRegistered) {
jsCallDispatcher.registerScope("UnoStatic", Uno.UI.WindowManager);
jsCallDispatcher.registerScope("UnoStatic_Windows_Storage_StorageFolder", Windows.Storage.StorageFolder);
jsCallDispatcher.registerScope("UnoStatic_Windows_Storage_ApplicationDataContainer", Windows.Storage.ApplicationDataContainer);
jsCallDispatcher.registerScope("UnoStatic_Windows_ApplicationModel_DataTransfer_DragDrop_Core_DragDropExtension", Windows.ApplicationModel.DataTransfer.DragDrop.Core.DragDropExtension);
Expand Down
32 changes: 10 additions & 22 deletions src/Uno.UI/ts/WindowManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ namespace Uno.UI {
private static readonly unoUnarrangedClassName = "uno-unarranged";
private static readonly unoCollapsedClassName = "uno-visibility-collapsed";

private static _cctor = (() => {
WindowManager.initMethods();
HtmlDom.initPolyfills();
})();

/**
* Initialize the WindowManager
* @param containerElementId The ID of the container element for the Xaml UI
* @param loadingElementId The ID of the loading element to remove once ready
*/
public static init(isLoadEventsEnabled: boolean, containerElementId: string = "uno-body", loadingElementId: string = "uno-loading"): void {
public static async init(isLoadEventsEnabled: boolean, containerElementId: string = "uno-body", loadingElementId: string = "uno-loading") {

HtmlDom.initPolyfills();

await WindowManager.initMethods();

WindowManager._isLoadEventsEnabled = isLoadEventsEnabled;

Expand All @@ -48,7 +47,7 @@ namespace Uno.UI {
private static buildReadyPromise(): Promise<boolean> {
return new Promise<boolean>(resolve => {
Promise.all(
[WindowManager.buildSplashScreen(), ExportManager.initialize()]
[WindowManager.buildSplashScreen()]
).then(() => resolve(true))
});
}
Expand Down Expand Up @@ -112,20 +111,6 @@ namespace Uno.UI {
});
}

/**
* Initialize the WindowManager
* @param containerElementId The ID of the container element for the Xaml UI
* @param loadingElementId The ID of the loading element to remove once ready
*/
public static initNative(pParams: number): boolean {

const params = WindowManagerInitParams.unmarshal(pParams);

WindowManager.init(params.IsLoadEventsEnabled);

return true;
}

private containerElement: HTMLDivElement;
private rootElement: HTMLElement;

Expand Down Expand Up @@ -1582,7 +1567,10 @@ namespace Uno.UI {
);
}

private static initMethods() {
private static async initMethods() {

await ExportManager.initialize();

if (!WindowManager.resizeMethod) {
WindowManager.resizeMethod = (<any>Module).mono_bind_static_method("[Uno.UI] Windows.UI.Xaml.Window:Resize");
}
Expand Down
15 changes: 0 additions & 15 deletions src/Uno.UI/tsBindings/WindowManagerInitParams.ts

This file was deleted.

0 comments on commit 09be506

Please sign in to comment.