Skip to content

Commit

Permalink
perf: Add DisplayInformation bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
ebariche committed Apr 17, 2023
1 parent d077eaf commit b7d0279
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 12 deletions.
20 changes: 20 additions & 0 deletions src/Uno.UI/ts/Windows/Graphics/Display/DisplayInformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@

private static lockingSupported: boolean | null;

public static getDevicePixelRatio() : number {
return globalThis.devicePixelRatio;
}

public static getScreenWidth(): number {
return globalThis.screen.width;
}

public static getScreenHeight(): number {
return globalThis.screen.height;
}

public static getScreenOrientationAngle(): number | null {
return globalThis.screen.orientation?.angle;
}

public static getScreenOrientationType(): string | null {
return globalThis.screen.orientation?.type;
}

public static startOrientationChanged() {
window.screen.orientation.addEventListener("change", DisplayInformation.onOrientationChange);
}
Expand Down
41 changes: 41 additions & 0 deletions src/Uno.UWP/Graphics/Display/DisplayInformation.Interop.wasm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#if NET7_0_OR_GREATER
using System.Runtime.InteropServices.JavaScript;

namespace __Windows.Graphics.Display
{
internal partial class DisplayInformation
{
internal static partial class NativeMethods
{
private const string JsType = "globalThis.Windows.Graphics.Display.DisplayInformation";

[JSImport($"{JsType}.getDevicePixelRatio")]
internal static partial float GetDevicePixelRatio();

[JSImport($"{JsType}.getScreenHeight")]
internal static partial float GetScreenHeight();

[JSImport($"{JsType}.getScreenOrientationAngle")]
internal static partial int? GetScreenOrientationAngle();

[JSImport($"{JsType}.getScreenOrientationType")]
internal static partial string GetScreenOrientationType();

[JSImport($"{JsType}.getScreenWidth")]
internal static partial float GetScreenWidth();

[JSImport($"{JsType}.startDpiChanged")]
internal static partial void StartDpiChanged();

[JSImport($"{JsType}.startOrientationChanged")]
internal static partial void StartOrientationChanged();

[JSImport($"{JsType}.stopDpiChanged")]
internal static partial void StopDpiChanged();

[JSImport($"{JsType}.stopOrientationChanged")]
internal static partial void StopOrientationChanged();
}
}
}
#endif
92 changes: 80 additions & 12 deletions src/Uno.UWP/Graphics/Display/DisplayInformation.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
using Uno;
using Uno.Foundation;

#if NET7_0_OR_GREATER
using NativeMethods = __Windows.Graphics.Display.DisplayInformation.NativeMethods;
#endif

namespace Windows.Graphics.Display
{
public sealed partial class DisplayInformation
Expand Down Expand Up @@ -38,7 +42,7 @@ public DisplayOrientations CurrentOrientation
{
get
{
var jsOrientation = ReadJsString("window.screen.orientation.type");
var jsOrientation = ReadOrientationType();
return ParseJsOrientation(jsOrientation);
}
}
Expand All @@ -52,9 +56,9 @@ public DisplayOrientations NativeOrientation
{
get
{
if (TryReadJsInt("window.screen.orientation.angle", out var angle))
if (TryReadOrientationAngle(out var angle))
{
var jsOrientation = ReadJsString("window.screen.orientation.type");
var jsOrientation = ReadOrientationType();

var isCurrentlyPortrait = jsOrientation.StartsWith("portrait", StringComparison.Ordinal);
var isCurrentlyLandscape = jsOrientation.StartsWith("landscape", StringComparison.Ordinal);
Expand Down Expand Up @@ -86,7 +90,7 @@ public uint ScreenHeightInRawPixels
{
get
{
if (TryReadJsFloat("window.screen.height", out var height))
if (TryReadScreenHeight(out var height))
{
var scale = (double)LogicalDpi / BaseDpi;
return (uint)(height * scale);
Expand All @@ -99,7 +103,7 @@ public uint ScreenWidthInRawPixels
{
get
{
if (TryReadJsFloat("window.screen.width", out var width))
if (TryReadScreenWidth(out var width))
{
var scale = (double)LogicalDpi / BaseDpi;
return (uint)(width * scale);
Expand All @@ -112,7 +116,7 @@ public float LogicalDpi
{
get
{
if (TryReadJsFloat("window.devicePixelRatio", out var devicePixelRatio))
if (TryReadDevicePixelRatio(out var devicePixelRatio))
{
return devicePixelRatio * BaseDpi;
}
Expand All @@ -136,7 +140,7 @@ public ResolutionScale ResolutionScale
{
get
{
if (TryReadJsFloat("window.devicePixelRatio", out var devicePixelRatio))
if (TryReadDevicePixelRatio(out var devicePixelRatio))
{
return (ResolutionScale)(int)(devicePixelRatio * 100);
}
Expand All @@ -150,27 +154,45 @@ public ResolutionScale ResolutionScale
partial void StartOrientationChanged()
{
_lastKnownOrientation = CurrentOrientation;

#if NET7_0_OR_GREATER
NativeMethods.StartOrientationChanged();
#else
var command = $"{JsType}.startOrientationChanged()";
WebAssemblyRuntime.InvokeJS(command);
#endif
}

partial void StopOrientationChanged()
{
#if NET7_0_OR_GREATER
NativeMethods.StopOrientationChanged();
#else
var command = $"{JsType}.stopOrientationChanged()";
WebAssemblyRuntime.InvokeJS(command);
#endif
}

partial void StartDpiChanged()
{
_lastKnownDpi = LogicalDpi;

#if NET7_0_OR_GREATER
NativeMethods.StartDpiChanged();
#else
var command = $"{JsType}.startDpiChanged()";
WebAssemblyRuntime.InvokeJS(command);
#endif
}

partial void StopDpiChanged()
{
#if NET7_0_OR_GREATER
NativeMethods.StopDpiChanged();
#else
var command = $"{JsType}.stopDpiChanged()";
WebAssemblyRuntime.InvokeJS(command);
#endif
}

static partial void SetOrientationPartial(DisplayOrientations orientations)
Expand All @@ -180,13 +202,59 @@ static partial void SetOrientationPartial(DisplayOrientations orientations)
(ct) => SetOrientationAsync(orientations, ct));
}

private static bool TryReadJsFloat(string property, out float value) =>
float.TryParse(WebAssemblyRuntime.InvokeJS(property), NumberStyles.Any, CultureInfo.InvariantCulture, out value);
private static bool TryReadDevicePixelRatio(out float value)
{
#if NET7_0_OR_GREATER
value = NativeMethods.GetDevicePixelRatio();

return true;
#else
return float.TryParse(WebAssemblyRuntime.InvokeJS("window.devicePixelRatio"), NumberStyles.Any, CultureInfo.InvariantCulture, out value);
#endif
}

private static bool TryReadScreenWidth(out float value)
{
#if NET7_0_OR_GREATER
value = NativeMethods.GetScreenWidth();

return true;
#else
return float.TryParse(WebAssemblyRuntime.InvokeJS("window.screen.width"), NumberStyles.Any, CultureInfo.InvariantCulture, out value);
#endif
}

private static bool TryReadScreenHeight(out float value)
{
#if NET7_0_OR_GREATER
value = NativeMethods.GetScreenHeight();

return true;
#else
return float.TryParse(WebAssemblyRuntime.InvokeJS("window.screen.height"), NumberStyles.Any, CultureInfo.InvariantCulture, out value);
#endif
}

private static bool TryReadOrientationAngle(out int value)
{
#if NET7_0_OR_GREATER
var angle = NativeMethods.GetScreenOrientationAngle();

private static bool TryReadJsInt(string property, out int value) =>
int.TryParse(WebAssemblyRuntime.InvokeJS(property), NumberStyles.Any, CultureInfo.InvariantCulture, out value);
value = angle ?? default;

private static string ReadJsString(string property) => WebAssemblyRuntime.InvokeJS(property);
return angle.HasValue;
#else
return int.TryParse(WebAssemblyRuntime.InvokeJS("window.screen.orientation.angle"), NumberStyles.Any, CultureInfo.InvariantCulture, out value);
#endif
}

private static string ReadOrientationType()
=>
#if NET7_0_OR_GREATER
NativeMethods.GetScreenOrientationType();
#else
WebAssemblyRuntime.InvokeJS("window.screen.orientation.type");
#endif

private static DisplayOrientations ParseJsOrientation(string jsOrientation)
{
Expand Down

0 comments on commit b7d0279

Please sign in to comment.