From 70f37dcf210908eb8ddbf64af13a43deaebe0cc6 Mon Sep 17 00:00:00 2001 From: Rustam Sayfutdinov Date: Fri, 19 Jun 2020 13:02:28 +0300 Subject: [PATCH] Revert "Remove duplicate code for RoundLayoutValue" This reverts commit 2edcb3b676bc1ce3d366e6b72e0433040fefb792. --- .../Presenters/ContentPresenter.cs | 7 +-- src/Avalonia.Layout/LayoutHelper.cs | 43 +++++++++++++++++-- src/Avalonia.Layout/Layoutable.cs | 8 ++-- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/Avalonia.Controls/Presenters/ContentPresenter.cs b/src/Avalonia.Controls/Presenters/ContentPresenter.cs index eeed2c6812..f75184c686 100644 --- a/src/Avalonia.Controls/Presenters/ContentPresenter.cs +++ b/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 = diff --git a/src/Avalonia.Layout/LayoutHelper.cs b/src/Avalonia.Layout/LayoutHelper.cs index d5215b6b69..d8fa00deb7 100644 --- a/src/Avalonia.Layout/LayoutHelper.cs +++ b/src/Avalonia.Layout/LayoutHelper.cs @@ -98,11 +98,48 @@ namespace Avalonia.Layout /// 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)); } + /// + /// 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 the 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.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; + } + + /// /// Calculates the min and max height for a control. Ported from WPF. /// diff --git a/src/Avalonia.Layout/Layoutable.cs b/src/Avalonia.Layout/Layoutable.cs index d897402d96..8d2a825fa0 100644 --- a/src/Avalonia.Layout/Layoutable.cs +++ b/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);