Browse Source

Revert "Remove duplicate code for RoundLayoutValue"

This reverts commit 2edcb3b676.
pull/4136/head
Rustam Sayfutdinov 6 years ago
parent
commit
70f37dcf21
  1. 7
      src/Avalonia.Controls/Presenters/ContentPresenter.cs
  2. 43
      src/Avalonia.Layout/LayoutHelper.cs
  3. 8
      src/Avalonia.Layout/Layoutable.cs

7
src/Avalonia.Controls/Presenters/ContentPresenter.cs

@ -2,11 +2,12 @@ using System;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Controls.Utils;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Layout;
using Avalonia.LogicalTree;
using Avalonia.Media;
using Avalonia.Metadata;
using Avalonia.Utilities;
namespace Avalonia.Controls.Presenters
{
@ -390,8 +391,8 @@ namespace Avalonia.Controls.Presenters
if (useLayoutRounding)
{
originX = MathUtilities.RoundLayoutValue(originX, scale);
originY = MathUtilities.RoundLayoutValue(originY, scale);
originX = LayoutHelper.RoundLayoutValue(originX, scale);
originY = LayoutHelper.RoundLayoutValue(originY, scale);
}
var boundsForChild =

43
src/Avalonia.Layout/LayoutHelper.cs

@ -98,11 +98,48 @@ namespace Avalonia.Layout
/// </remarks>
public static Size RoundLayoutSize(Size size, double dpiScaleX, double dpiScaleY)
{
var width = MathUtilities.RoundLayoutValue(size.Width, dpiScaleX);
var height = MathUtilities.RoundLayoutValue(size.Height, dpiScaleY);
return new Size(width, height);
return new Size(RoundLayoutValue(size.Width, dpiScaleX), RoundLayoutValue(size.Height, dpiScaleY));
}
/// <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 the 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.AreClose(dpiScale, 1.0))
{
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>
/// Calculates the min and max height for a control. Ported from WPF.
/// </summary>

8
src/Avalonia.Layout/Layoutable.cs

@ -566,8 +566,8 @@ namespace Avalonia.Layout
if (UseLayoutRounding)
{
var scale = GetLayoutScale();
width = MathUtilities.RoundLayoutValue(width, scale);
height = MathUtilities.RoundLayoutValue(height, scale);
width = LayoutHelper.RoundLayoutValue(width, scale);
height = LayoutHelper.RoundLayoutValue(height, scale);
}
return NonNegative(new Size(width, height).Inflate(margin));
@ -674,8 +674,8 @@ namespace Avalonia.Layout
if (useLayoutRounding)
{
originX = MathUtilities.RoundLayoutValue(originX, scale);
originY = MathUtilities.RoundLayoutValue(originY, scale);
originX = LayoutHelper.RoundLayoutValue(originX, scale);
originY = LayoutHelper.RoundLayoutValue(originY, scale);
}
Bounds = new Rect(originX, originY, size.Width, size.Height);

Loading…
Cancel
Save