From 10d5ec550874857ba6d8c0b2b62edbbb5da64b58 Mon Sep 17 00:00:00 2001 From: Sebastien Pouliot Date: Fri, 24 Jun 2022 10:35:46 -0400 Subject: [PATCH 1/2] fix(iOSmacOS): Obsolete the use of readonly fields --- .../Extensions/UIViewExtensions.iOSmacOS.cs | 9 ++---- src/Uno.UI/Extensions/ViewHelper.iOSmacOS.cs | 28 +++++++++++-------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/Uno.UI/Extensions/UIViewExtensions.iOSmacOS.cs b/src/Uno.UI/Extensions/UIViewExtensions.iOSmacOS.cs index e9c9d7a3f6ed..7cf5a6200cc5 100644 --- a/src/Uno.UI/Extensions/UIViewExtensions.iOSmacOS.cs +++ b/src/Uno.UI/Extensions/UIViewExtensions.iOSmacOS.cs @@ -487,17 +487,14 @@ public static T AddTo(this T thisView, _View toAddTo) where T : _View public static void AddBorder(this _View thisButton, float borderThickness = 1, _Color borderColor = null) { + var layer = thisButton.Layer; if (borderColor != null) { - thisButton.Layer.BorderColor = borderColor.CGColor; + layer.BorderColor = borderColor.CGColor; } if (borderThickness > 0) { - if (Math.Abs(borderThickness - 1f) < float.Epsilon && ViewHelper.IsRetinaDisplay) - { - borderThickness = (float)ViewHelper.OnePixel; - } - thisButton.Layer.BorderWidth = borderThickness; + layer.BorderWidth = ViewHelper.GetConvertedPixel(borderThickness); } } diff --git a/src/Uno.UI/Extensions/ViewHelper.iOSmacOS.cs b/src/Uno.UI/Extensions/ViewHelper.iOSmacOS.cs index 69ddd71bae97..99c2c428ec78 100644 --- a/src/Uno.UI/Extensions/ViewHelper.iOSmacOS.cs +++ b/src/Uno.UI/Extensions/ViewHelper.iOSmacOS.cs @@ -5,6 +5,7 @@ using Uno.UI.Extensions; using Uno.Foundation.Logging; using Uno.Extensions; +using Windows.Graphics.Display; using Foundation; using CoreGraphics; @@ -27,15 +28,19 @@ namespace Uno.UI public static class ViewHelper { #if __IOS__ + [Obsolete("This return the value from the original screen. Use 'DisplayInformation.RawPixelsPerViewPixel' to get the value for the current screen.")] public static readonly nfloat MainScreenScale = UIScreen.MainScreen.Scale; - public static readonly bool IsRetinaDisplay = UIScreen.MainScreen.Scale > 1.0f; + [Obsolete("This return the value from the original screen. Use 'DisplayInformation.RawPixelsPerViewPixel > 1.0f' for the current screen.")] + public static readonly bool IsRetinaDisplay = MainScreenScale > 1.0f; #elif __MACOS__ + [Obsolete("This return the value from the original screen. Use 'DisplayInformation.RawPixelsPerViewPixel' to get the value for the current screen.")] public static readonly nfloat MainScreenScale = NSScreen.MainScreen.BackingScaleFactor; - public static readonly bool IsRetinaDisplay = NSScreen.MainScreen.BackingScaleFactor > 1.0f; + [Obsolete("This return the value from the original screen. Use 'DisplayInformation.RawPixelsPerViewPixel > 1.0f' for the current screen.")] + public static readonly bool IsRetinaDisplay = MainScreenScale > 1.0f; #endif private static double _rectangleRoundingEpsilon = 0.05; - private static double _scaledRectangleRoundingEpsilon = _rectangleRoundingEpsilon * MainScreenScale; + private static double _scaledRectangleRoundingEpsilon = _rectangleRoundingEpsilon * DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel; /// /// This is used to correct some errors when using Floor and Ceiling in LogicalToPhysicalPixels for CGRect. @@ -46,7 +51,7 @@ public static double RectangleRoundingEpsilon set { _rectangleRoundingEpsilon = value; - _scaledRectangleRoundingEpsilon = value * MainScreenScale; + _scaledRectangleRoundingEpsilon = value * DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel; } } @@ -57,7 +62,7 @@ static ViewHelper() { if (typeof(ViewHelper).Log().IsEnabled(Uno.Foundation.Logging.LogLevel.Debug)) { - typeof(ViewHelper).Log().DebugFormat("Display scale is {0}", MainScreenScale); + typeof(ViewHelper).Log().DebugFormat("Display scale is {0}", DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel); } } @@ -65,7 +70,7 @@ public static nfloat OnePixel { get { - return (1.0f / MainScreenScale); + return (nfloat)(1.0d / DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel); } } @@ -139,12 +144,13 @@ public static CGRect LogicalToPhysicalPixels(this CGRect size) // and its size upward to the nearest whole integers, // such that the result contains the original rectangle. + var scale = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel; return new CGRect ( - (nfloat)FloorWithEpsilon(size.X * MainScreenScale) / MainScreenScale, - (nfloat)FloorWithEpsilon(size.Y * MainScreenScale) / MainScreenScale, - (nfloat)CeilingWithEpsilon(size.Width * MainScreenScale) / MainScreenScale, - (nfloat)CeilingWithEpsilon(size.Height * MainScreenScale) / MainScreenScale + (nfloat)FloorWithEpsilon(size.X * scale) / scale, + (nfloat)FloorWithEpsilon(size.Y * scale) / scale, + (nfloat)CeilingWithEpsilon(size.Width * scale) / scale, + (nfloat)CeilingWithEpsilon(size.Height * scale) / scale ); } @@ -182,7 +188,7 @@ private static double FloorWithEpsilon(double value) public static nfloat GetConvertedPixel(float thickness) { - if (IsRetinaDisplay && thickness > 0 && thickness <= 1) + if (thickness > 0 && thickness <= 1 && (DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel > 1.0f)) { return OnePixel; } From 99104f7a1bc8f7161c61a338c47bb3409b18a10e Mon Sep 17 00:00:00 2001 From: Sebastien Pouliot Date: Sat, 25 Jun 2022 10:08:56 -0400 Subject: [PATCH 2/2] chore: Switch [Obsolete] to [EditorBrowsable(.Never)] --- src/Uno.UI/Extensions/ViewHelper.iOSmacOS.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Uno.UI/Extensions/ViewHelper.iOSmacOS.cs b/src/Uno.UI/Extensions/ViewHelper.iOSmacOS.cs index 99c2c428ec78..6e79596ecd92 100644 --- a/src/Uno.UI/Extensions/ViewHelper.iOSmacOS.cs +++ b/src/Uno.UI/Extensions/ViewHelper.iOSmacOS.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Drawing; using System.Runtime.CompilerServices; using Uno.UI.Extensions; @@ -28,14 +29,18 @@ namespace Uno.UI public static class ViewHelper { #if __IOS__ - [Obsolete("This return the value from the original screen. Use 'DisplayInformation.RawPixelsPerViewPixel' to get the value for the current screen.")] + // This return the value from the original screen. Use 'DisplayInformation.RawPixelsPerViewPixel' to get the value for the current screen. + [EditorBrowsable(EditorBrowsableState.Never)] public static readonly nfloat MainScreenScale = UIScreen.MainScreen.Scale; - [Obsolete("This return the value from the original screen. Use 'DisplayInformation.RawPixelsPerViewPixel > 1.0f' for the current screen.")] + // This return the value from the original screen. Use 'DisplayInformation.RawPixelsPerViewPixel > 1.0f' for the current screen. + [EditorBrowsable(EditorBrowsableState.Never)] public static readonly bool IsRetinaDisplay = MainScreenScale > 1.0f; #elif __MACOS__ - [Obsolete("This return the value from the original screen. Use 'DisplayInformation.RawPixelsPerViewPixel' to get the value for the current screen.")] + // This return the value from the original screen. Use 'DisplayInformation.RawPixelsPerViewPixel' to get the value for the current screen. + [EditorBrowsable(EditorBrowsableState.Never)] public static readonly nfloat MainScreenScale = NSScreen.MainScreen.BackingScaleFactor; - [Obsolete("This return the value from the original screen. Use 'DisplayInformation.RawPixelsPerViewPixel > 1.0f' for the current screen.")] + // This return the value from the original screen. Use 'DisplayInformation.RawPixelsPerViewPixel > 1.0f' for the current screen. + [EditorBrowsable(EditorBrowsableState.Never)] public static readonly bool IsRetinaDisplay = MainScreenScale > 1.0f; #endif