Repository navigation
Description
Current behavior
This code does not always work as expected:
#if __IOS__
public static readonly nfloat MainScreenScale = UIScreen.MainScreen.Scale;
public static readonly bool IsRetinaDisplay = UIScreen.MainScreen.Scale > 1.0f;
#elif __MACOS__
public static readonly nfloat MainScreenScale = NSScreen.MainScreen.UserSpaceScaleFactor;
public static readonly bool IsRetinaDisplay = NSScreen.MainScreen.UserSpaceScaleFactor > 1.0f;
#endifBoth fields are evaluated and assigned only when the (static) constructor is executed.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/readonly
Which means they will always match the then current (at execution time) screen/monitor.
IOW if the app starts on a 4K monitor and is moved to an HD monitor the (cached) values will still reflect the 4K monitor.
Note: this is also a problem for iOS / iPadOS where multiple screens are becoming more commons.
Expected behavior
Calling the fields should return the correct values for the currently used screen/monitor (at time of the call).
How to reproduce it (as minimally and precisely as possible)
No response
Workaround
Avoid using the fields and call the API.
Works on UWP/WinUI
No response
Environment
Uno.UI / Uno.UI.WebAssembly / Uno.UI.Skia
NuGet package version(s)
No response
Affected platforms
iOS, macOS
IDE
No response
IDE version
No response
Relevant plugins
No response
Anything else we need to know?
Since the fields are public removing them would be a breaking change.
However they will never work as someone would expect them since they can only be assigned inside the constructor.
Proposal
-
Decorate the readonly fields with
[Obsolete]. This avoids the breaking changes and will notify developers to use something else; -
Provide new properties (not fields) that can be used to replace both fields;
-
Ensure the old fields are not used inside any calculation inside Uno;
Activity