From 8414ace5cfa20dcd7c0258a5014fff92e4b73ae7 Mon Sep 17 00:00:00 2001 From: Rustam Sayfutdinov Date: Fri, 19 Jun 2020 13:46:57 +0300 Subject: [PATCH] Remove duplicate code for RoundLayoutValue --- src/Avalonia.Base/Utilities/MathUtilities.cs | 33 ------------------- src/Avalonia.Controls/Grid.cs | 6 ++-- src/Avalonia.Layout/LayoutHelper.cs | 2 +- .../Utilities/MathUtilitiesTests.cs | 19 ----------- .../LayoutHelperTests.cs | 27 +++++++++++++++ 5 files changed, 30 insertions(+), 57 deletions(-) create mode 100644 tests/Avalonia.Layout.UnitTests/LayoutHelperTests.cs diff --git a/src/Avalonia.Base/Utilities/MathUtilities.cs b/src/Avalonia.Base/Utilities/MathUtilities.cs index 06a1cd4ae5..2a92e75f58 100644 --- a/src/Avalonia.Base/Utilities/MathUtilities.cs +++ b/src/Avalonia.Base/Utilities/MathUtilities.cs @@ -206,39 +206,6 @@ namespace Avalonia.Utilities } } - /// - /// Calculates the value to be used for layout rounding at high DPI. - /// - /// Input value to be rounded. - /// Ratio of screen's DPI to layout DPI - /// Adjusted value that will produce layout rounding on screen at high dpi. - /// This is a layout helper method. It takes DPI into account and also does not return - /// the rounded value if it is unacceptable for layout, e.g. Infinity or NaN. It's a helper associated with - /// UseLayoutRounding property and should not be used as a general rounding utility. - public static double RoundLayoutValue(double value, double dpiScale) - { - double newValue; - - // If DPI == 1, don't use DPI-aware rounding. - if (!MathUtilities.IsOne(dpiScale)) - { - newValue = Math.Round(value * dpiScale) / dpiScale; - // If rounding produces a value unacceptable to layout (NaN, Infinity or MaxValue), use the original value. - if (double.IsNaN(newValue) || - double.IsInfinity(newValue) || - MathUtilities.AreClose(newValue, double.MaxValue)) - { - newValue = value; - } - } - else - { - newValue = Math.Round(value); - } - - return newValue; - } - /// /// Clamps a value between a minimum and maximum value. /// diff --git a/src/Avalonia.Controls/Grid.cs b/src/Avalonia.Controls/Grid.cs index e10d78917e..6357ec98a8 100644 --- a/src/Avalonia.Controls/Grid.cs +++ b/src/Avalonia.Controls/Grid.cs @@ -8,10 +8,8 @@ using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Diagnostics; -using System.Linq; using System.Threading; -using Avalonia; -using Avalonia.Collections; +using Avalonia.Layout; using Avalonia.Media; using Avalonia.Utilities; using Avalonia.VisualTree; @@ -2103,7 +2101,7 @@ namespace Avalonia.Controls for (int i = 0; i < definitions.Count; ++i) { DefinitionBase def = definitions[i]; - double roundedSize = MathUtilities.RoundLayoutValue(def.SizeCache, dpi); + double roundedSize = LayoutHelper.RoundLayoutValue(def.SizeCache, dpi); roundingErrors[i] = (roundedSize - def.SizeCache); def.SizeCache = roundedSize; roundedTakenSize += roundedSize; diff --git a/src/Avalonia.Layout/LayoutHelper.cs b/src/Avalonia.Layout/LayoutHelper.cs index d8fa00deb7..3708748ad1 100644 --- a/src/Avalonia.Layout/LayoutHelper.cs +++ b/src/Avalonia.Layout/LayoutHelper.cs @@ -118,7 +118,7 @@ namespace Avalonia.Layout double newValue; // If DPI == 1, don't use DPI-aware rounding. - if (!MathUtilities.AreClose(dpiScale, 1.0)) + if (!MathUtilities.IsOne(dpiScale)) { newValue = Math.Round(value * dpiScale) / dpiScale; diff --git a/tests/Avalonia.Base.UnitTests/Utilities/MathUtilitiesTests.cs b/tests/Avalonia.Base.UnitTests/Utilities/MathUtilitiesTests.cs index a12d07b8ef..de3fadd157 100644 --- a/tests/Avalonia.Base.UnitTests/Utilities/MathUtilitiesTests.cs +++ b/tests/Avalonia.Base.UnitTests/Utilities/MathUtilitiesTests.cs @@ -199,24 +199,5 @@ namespace Avalonia.Base.UnitTests.Utilities var actual = MathUtilities.GreaterThanOrClose(1f, 1f); Assert.True(actual); } - - [Fact] - public void Round_Layout_Value_Without_DPI_Aware() - { - const double value = 42.5; - var expectedValue = Math.Round(value); - var actualValue = MathUtilities.RoundLayoutValue(value, 1.0); - Assert.Equal(expectedValue, actualValue); - } - - [Fact] - public void Round_Layout_Value_With_DPI_Aware() - { - const double dpiScale = 1.25; - const double value = 42.5; - var expectedValue = Math.Round(value * dpiScale) / dpiScale; - var actualValue = MathUtilities.RoundLayoutValue(value, dpiScale); - Assert.Equal(expectedValue, actualValue); - } } } diff --git a/tests/Avalonia.Layout.UnitTests/LayoutHelperTests.cs b/tests/Avalonia.Layout.UnitTests/LayoutHelperTests.cs new file mode 100644 index 0000000000..f9187edc42 --- /dev/null +++ b/tests/Avalonia.Layout.UnitTests/LayoutHelperTests.cs @@ -0,0 +1,27 @@ +using System; +using Xunit; + +namespace Avalonia.Layout.UnitTests +{ + public class LayoutHelperTests + { + [Fact] + public void Round_Layout_Value_Without_DPI_Aware() + { + const double value = 42.5; + var expectedValue = Math.Round(value); + var actualValue = LayoutHelper.RoundLayoutValue(value, 1.0); + Assert.Equal(expectedValue, actualValue); + } + + [Fact] + public void Round_Layout_Value_With_DPI_Aware() + { + const double dpiScale = 1.25; + const double value = 42.5; + var expectedValue = Math.Round(value * dpiScale) / dpiScale; + var actualValue = LayoutHelper.RoundLayoutValue(value, dpiScale); + Assert.Equal(expectedValue, actualValue); + } + } +}