Skip to content

Commit a5919a8

Browse files
committed
perf(iOS): Cache DisplayInformation properties
1 parent 67e02bd commit a5919a8

File tree

1 file changed

+50
-23
lines changed

1 file changed

+50
-23
lines changed

src/Uno.UWP/Graphics/Display/DisplayInformation.iOS.cs

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#if __IOS__
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Linq;
54
using System.Text;
@@ -36,37 +35,37 @@ public sealed partial class DisplayInformation
3635

3736
public uint ScreenHeightInRawPixels
3837
{
39-
get
40-
{
41-
var screenSize = UIScreen.MainScreen.Bounds.Size;
42-
var scale = UIScreen.MainScreen.NativeScale;
43-
return (uint)(screenSize.Height * scale);
44-
}
38+
get;
39+
private set;
4540
}
4641

4742
public uint ScreenWidthInRawPixels
4843
{
49-
get
50-
{
51-
var screenSize = UIScreen.MainScreen.Bounds.Size;
52-
var scale = UIScreen.MainScreen.NativeScale;
53-
return (uint)(screenSize.Width * scale);
54-
}
44+
get;
45+
private set;
5546
}
5647

57-
public double RawPixelsPerViewPixel => UIScreen.MainScreen.NativeScale;
48+
public double RawPixelsPerViewPixel
49+
{
50+
get;
51+
private set;
52+
}
5853

54+
/// <summary>
55+
/// Scale of 1 is considered @1x, which is the equivalent of 96.0 or 100% for UWP.
56+
/// https://developer.apple.com/documentation/uikit/uiscreen/1617836-scale
57+
/// </summary>
5958
public float LogicalDpi
6059
{
61-
get
62-
{
63-
// Scale of 1 is considered @1x, which is the equivalent of 96.0 or 100% for UWP.
64-
// https://developer.apple.com/documentation/uikit/uiscreen/1617836-scale
65-
return (float)(UIScreen.MainScreen.Scale * BaseDpi);
66-
}
60+
get;
61+
private set;
6762
}
6863

69-
public ResolutionScale ResolutionScale => (ResolutionScale)(int)(UIScreen.MainScreen.Scale * 100.0);
64+
public ResolutionScale ResolutionScale
65+
{
66+
get;
67+
private set;
68+
}
7069

7170
/// <summary>
7271
/// Sets the NativeOrientation property
@@ -106,6 +105,35 @@ private DisplayOrientations GetCurrentOrientation()
106105
}
107106
}
108107

108+
private void Update()
109+
{
110+
var screen = UIScreen.MainScreen;
111+
var bounds = screen.Bounds;
112+
113+
RawPixelsPerViewPixel = screen.NativeScale;
114+
ScreenHeightInRawPixels = (uint)(bounds.Height * RawPixelsPerViewPixel);
115+
ScreenWidthInRawPixels = (uint)(bounds.Width * RawPixelsPerViewPixel);
116+
117+
LogicalDpi = (float)(RawPixelsPerViewPixel * BaseDpi);
118+
119+
ResolutionScale = (ResolutionScale)(int)(RawPixelsPerViewPixel * 100.0);
120+
}
121+
122+
partial void Initialize()
123+
{
124+
NSNotificationCenter
125+
.DefaultCenter
126+
.AddObserver(
127+
UIScreen.ModeDidChangeNotification,
128+
n =>
129+
{
130+
Update();
131+
}
132+
);
133+
// we are notified on changes but we're already on a screen so let's initialize
134+
Update();
135+
}
136+
109137
partial void StartOrientationChanged() => ObserveDisplayMetricsChanges();
110138

111139
partial void StopOrientationChanged() => UnobserveDisplayMetricsChanges();
@@ -170,4 +198,3 @@ static partial void SetOrientationPartial(DisplayOrientations orientations)
170198
}
171199
}
172200
}
173-
#endif

0 commit comments

Comments
 (0)