提交 b7d02799 编写于 作者: E Elie Bariche

perf: Add DisplayInformation bindings

上级 d077eaf8
......@@ -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);
}
......
#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
......@@ -6,6 +6,10 @@ using System.Threading.Tasks;
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
......@@ -38,7 +42,7 @@ namespace Windows.Graphics.Display
{
get
{
var jsOrientation = ReadJsString("window.screen.orientation.type");
var jsOrientation = ReadOrientationType();
return ParseJsOrientation(jsOrientation);
}
}
......@@ -52,9 +56,9 @@ namespace Windows.Graphics.Display
{
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);
......@@ -86,7 +90,7 @@ namespace Windows.Graphics.Display
{
get
{
if (TryReadJsFloat("window.screen.height", out var height))
if (TryReadScreenHeight(out var height))
{
var scale = (double)LogicalDpi / BaseDpi;
return (uint)(height * scale);
......@@ -99,7 +103,7 @@ namespace Windows.Graphics.Display
{
get
{
if (TryReadJsFloat("window.screen.width", out var width))
if (TryReadScreenWidth(out var width))
{
var scale = (double)LogicalDpi / BaseDpi;
return (uint)(width * scale);
......@@ -112,7 +116,7 @@ namespace Windows.Graphics.Display
{
get
{
if (TryReadJsFloat("window.devicePixelRatio", out var devicePixelRatio))
if (TryReadDevicePixelRatio(out var devicePixelRatio))
{
return devicePixelRatio * BaseDpi;
}
......@@ -136,7 +140,7 @@ namespace Windows.Graphics.Display
{
get
{
if (TryReadJsFloat("window.devicePixelRatio", out var devicePixelRatio))
if (TryReadDevicePixelRatio(out var devicePixelRatio))
{
return (ResolutionScale)(int)(devicePixelRatio * 100);
}
......@@ -150,27 +154,45 @@ namespace Windows.Graphics.Display
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)
......@@ -180,13 +202,59 @@ namespace Windows.Graphics.Display
(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)
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册