Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(macOS): Cache DisplayInformation properties #9114

Merged
merged 2 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 50 additions & 23 deletions src/Uno.UWP/Graphics/Display/DisplayInformation.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if __IOS__
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -36,37 +35,37 @@ public sealed partial class DisplayInformation

public uint ScreenHeightInRawPixels
{
get
{
var screenSize = UIScreen.MainScreen.Bounds.Size;
var scale = UIScreen.MainScreen.NativeScale;
return (uint)(screenSize.Height * scale);
}
get;
private set;
}

public uint ScreenWidthInRawPixels
{
get
{
var screenSize = UIScreen.MainScreen.Bounds.Size;
var scale = UIScreen.MainScreen.NativeScale;
return (uint)(screenSize.Width * scale);
}
get;
private set;
}

public double RawPixelsPerViewPixel => UIScreen.MainScreen.NativeScale;
public double RawPixelsPerViewPixel
{
get;
private set;
}

/// <summary>
/// Scale of 1 is considered @1x, which is the equivalent of 96.0 or 100% for UWP.
/// https://developer.apple.com/documentation/uikit/uiscreen/1617836-scale
/// </summary>
public float LogicalDpi
{
get
{
// Scale of 1 is considered @1x, which is the equivalent of 96.0 or 100% for UWP.
// https://developer.apple.com/documentation/uikit/uiscreen/1617836-scale
return (float)(UIScreen.MainScreen.Scale * BaseDpi);
}
get;
private set;
}

public ResolutionScale ResolutionScale => (ResolutionScale)(int)(UIScreen.MainScreen.Scale * 100.0);
public ResolutionScale ResolutionScale
{
get;
private set;
}

/// <summary>
/// Sets the NativeOrientation property
Expand Down Expand Up @@ -106,6 +105,35 @@ private DisplayOrientations GetCurrentOrientation()
}
}

private void Update()
{
var screen = UIScreen.MainScreen;
var bounds = screen.Bounds;

RawPixelsPerViewPixel = screen.NativeScale;
ScreenHeightInRawPixels = (uint)(bounds.Height * RawPixelsPerViewPixel);
ScreenWidthInRawPixels = (uint)(bounds.Width * RawPixelsPerViewPixel);

LogicalDpi = (float)(RawPixelsPerViewPixel * BaseDpi);

ResolutionScale = (ResolutionScale)(int)(RawPixelsPerViewPixel * 100.0);
}

partial void Initialize()
{
NSNotificationCenter
.DefaultCenter
.AddObserver(
UIScreen.ModeDidChangeNotification,
n =>
{
Update();
}
);
// we are notified on changes but we're already on a screen so let's initialize
Update();
}

partial void StartOrientationChanged() => ObserveDisplayMetricsChanges();

partial void StopOrientationChanged() => UnobserveDisplayMetricsChanges();
Expand Down Expand Up @@ -170,4 +198,3 @@ static partial void SetOrientationPartial(DisplayOrientations orientations)
}
}
}
#endif
79 changes: 53 additions & 26 deletions src/Uno.UWP/Graphics/Display/DisplayInformation.macOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,8 @@ public sealed partial class DisplayInformation

public DisplayOrientations CurrentOrientation
{
get
{
if (NSScreen.MainScreen.Frame.Width > NSScreen.MainScreen.Frame.Height)
{
return DisplayOrientations.Landscape;
}
else
{
return DisplayOrientations.Portrait;
}
}
get;
private set;
}

/// <summary>
Expand All @@ -36,33 +27,69 @@ public DisplayOrientations CurrentOrientation

public uint ScreenHeightInRawPixels
{
get
{
var screenSize = NSScreen.MainScreen.Frame.Size;
var scale = NSScreen.MainScreen.BackingScaleFactor;
return (uint)(screenSize.Height * scale);
}
get;
private set;
}

public uint ScreenWidthInRawPixels
{
get
{
var screenSize = NSScreen.MainScreen.Frame.Size;
var scale = NSScreen.MainScreen.BackingScaleFactor;
return (uint)(screenSize.Width * scale);
}
get;
private set;
}

public double RawPixelsPerViewPixel => NSScreen.MainScreen.BackingScaleFactor;
public double RawPixelsPerViewPixel
{
get;
private set;
}

/// <summary>
/// Scale of 1 is considered @1x, which is the equivalent of 96.0 or 100% for UWP.
/// https://developer.apple.com/documentation/uikit/uiscreen/1617836-scale
/// </summary>
public float LogicalDpi => (float)(NSScreen.MainScreen.BackingScaleFactor * BaseDpi);
public float LogicalDpi
{
get;
private set;
}

public ResolutionScale ResolutionScale
{
get;
private set;
}

private void Update()
{
var screen = NSScreen.MainScreen;
var rect = screen.ConvertRectToBacking(screen.Frame);

ScreenHeightInRawPixels = (uint)rect.Height;
ScreenWidthInRawPixels = (uint)rect.Width;
RawPixelsPerViewPixel = screen.BackingScaleFactor;

CurrentOrientation = ScreenWidthInRawPixels > ScreenHeightInRawPixels
? DisplayOrientations.Landscape : DisplayOrientations.Portrait;

public ResolutionScale ResolutionScale => (ResolutionScale)(int)(NSScreen.MainScreen.BackingScaleFactor * 100.0);
LogicalDpi = (float)(RawPixelsPerViewPixel * BaseDpi);

ResolutionScale = (ResolutionScale)(int)(RawPixelsPerViewPixel * 100.0);
}

partial void Initialize()
{
NSNotificationCenter
.DefaultCenter
.AddObserver(
NSWindow.DidChangeScreenNotification,
n =>
{
Update();
}
);
// we are notified on changes but we're already on a screen so let's initialize
Update();
}

partial void StartOrientationChanged() => ObserveDisplayMetricsChanges();

Expand Down