Browse Source

Remove duplicate code for RoundLayoutValue

pull/4136/head
Rustam Sayfutdinov 6 years ago
parent
commit
8414ace5cf
  1. 33
      src/Avalonia.Base/Utilities/MathUtilities.cs
  2. 6
      src/Avalonia.Controls/Grid.cs
  3. 2
      src/Avalonia.Layout/LayoutHelper.cs
  4. 19
      tests/Avalonia.Base.UnitTests/Utilities/MathUtilitiesTests.cs
  5. 27
      tests/Avalonia.Layout.UnitTests/LayoutHelperTests.cs

33
src/Avalonia.Base/Utilities/MathUtilities.cs

@ -206,39 +206,6 @@ namespace Avalonia.Utilities
}
}
/// <summary>
/// Calculates the value to be used for layout rounding at high DPI.
/// </summary>
/// <param name="value">Input value to be rounded.</param>
/// <param name="dpiScale">Ratio of screen's DPI to layout DPI</param>
/// <returns>Adjusted value that will produce layout rounding on screen at high dpi.</returns>
/// <remarks>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.</remarks>
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;
}
/// <summary>
/// Clamps a value between a minimum and maximum value.
/// </summary>

6
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;

2
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;

19
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);
}
}
}

27
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);
}
}
}
Loading…
Cancel
Save