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

fix(iOSmacOS): Obsolete the use of readonly fields #9126

Merged
merged 2 commits into from
Jun 26, 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
9 changes: 3 additions & 6 deletions src/Uno.UI/Extensions/UIViewExtensions.iOSmacOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,17 +487,14 @@ public static T AddTo<T>(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);
}
}

Expand Down
33 changes: 22 additions & 11 deletions src/Uno.UI/Extensions/ViewHelper.iOSmacOS.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;
using Uno.UI.Extensions;
using Uno.Foundation.Logging;
using Uno.Extensions;
using Windows.Graphics.Display;

using Foundation;
using CoreGraphics;
Expand All @@ -27,15 +29,23 @@ namespace Uno.UI
public static class ViewHelper
{
#if __IOS__
// 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;
public static readonly bool IsRetinaDisplay = UIScreen.MainScreen.Scale > 1.0f;
// 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__
// 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;
public static readonly bool IsRetinaDisplay = NSScreen.MainScreen.BackingScaleFactor > 1.0f;
// 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

private static double _rectangleRoundingEpsilon = 0.05;
private static double _scaledRectangleRoundingEpsilon = _rectangleRoundingEpsilon * MainScreenScale;
private static double _scaledRectangleRoundingEpsilon = _rectangleRoundingEpsilon * DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;

/// <summary>
/// This is used to correct some errors when using Floor and Ceiling in LogicalToPhysicalPixels for CGRect.
Expand All @@ -46,7 +56,7 @@ public static double RectangleRoundingEpsilon
set
{
_rectangleRoundingEpsilon = value;
_scaledRectangleRoundingEpsilon = value * MainScreenScale;
_scaledRectangleRoundingEpsilon = value * DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
}
}

Expand All @@ -57,15 +67,15 @@ 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);
}
}

public static nfloat OnePixel
{
get
{
return (1.0f / MainScreenScale);
return (nfloat)(1.0d / DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel);
}
}

Expand Down Expand Up @@ -139,12 +149,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
);
}

Expand Down Expand Up @@ -182,7 +193,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;
}
Expand Down