From 8309a5c5aeb90a223222792f9c72f82b834ed7bd Mon Sep 17 00:00:00 2001 From: Elie Bariche <33458222+ebariche@users.noreply.github.com> Date: Tue, 4 Apr 2023 15:22:01 -0400 Subject: [PATCH] perf: Add GetBootTime binding --- .../UI/Xaml/Input/PointerRoutedEventArgs.wasm.cs | 2 +- src/Uno.UI/UI/Xaml/WindowManagerInterop.wasm.cs | 11 +++++++++++ src/Uno.UI/ts/WindowManager.ts | 7 +++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Uno.UI/UI/Xaml/Input/PointerRoutedEventArgs.wasm.cs b/src/Uno.UI/UI/Xaml/Input/PointerRoutedEventArgs.wasm.cs index d4d9f1676598..934cc147e652 100644 --- a/src/Uno.UI/UI/Xaml/Input/PointerRoutedEventArgs.wasm.cs +++ b/src/Uno.UI/UI/Xaml/Input/PointerRoutedEventArgs.wasm.cs @@ -110,7 +110,7 @@ private PointerPointProperties GetProperties() private static ulong ToTimeStamp(double timestamp) { - _bootTime ??= (ulong)(double.Parse(WebAssemblyRuntime.InvokeJS("Date.now() - performance.now()"), CultureInfo.InvariantCulture) * TimeSpan.TicksPerMillisecond); + _bootTime ??= (ulong)(WindowManagerInterop.GetBootTime() * TimeSpan.TicksPerMillisecond); return _bootTime.Value + (ulong)(timestamp * TimeSpan.TicksPerMillisecond); } diff --git a/src/Uno.UI/UI/Xaml/WindowManagerInterop.wasm.cs b/src/Uno.UI/UI/Xaml/WindowManagerInterop.wasm.cs index 86c82f1a9070..3c50f35a2d89 100644 --- a/src/Uno.UI/UI/Xaml/WindowManagerInterop.wasm.cs +++ b/src/Uno.UI/UI/Xaml/WindowManagerInterop.wasm.cs @@ -55,6 +55,14 @@ internal static string FindLaunchArguments() WebAssemblyRuntime.InvokeJS("Uno.UI.WindowManager.findLaunchArguments()"); #endif + internal static double GetBootTime() + => +#if NET7_0_OR_GREATER + NativeMethods.GetBootTime(); +#else + double.Parse(WebAssemblyRuntime.InvokeJS("Date.now() - performance.now()"), CultureInfo.InvariantCulture); +#endif + #region CreateContent internal static void CreateContent(IntPtr htmlId, string htmlTag, IntPtr handle, int uiElementRegistrationId, bool htmlTagIsSvg, bool isFocusable) { @@ -1294,6 +1302,9 @@ internal static partial void ArrangeElement( [JSImport("globalThis.Uno.UI.WindowManager.findLaunchArguments")] internal static partial string FindLaunchArguments(); + [JSImport("globalThis.Uno.UI.WindowManager.getBootTime")] + internal static partial double GetBootTime(); + [JSImport("globalThis.Uno.UI.WindowManager.current.focusView")] internal static partial void FocusView(IntPtr htmlId); diff --git a/src/Uno.UI/ts/WindowManager.ts b/src/Uno.UI/ts/WindowManager.ts index 93c34c08dbe7..4d334d88b60d 100644 --- a/src/Uno.UI/ts/WindowManager.ts +++ b/src/Uno.UI/ts/WindowManager.ts @@ -205,6 +205,13 @@ namespace Uno.UI { } } + /** + * Estimated application startup time + */ + public static getBootTime(): number { + return Date.now() - performance.now(); + } + /** * Create a html DOM element representing a Xaml element. *