From 3ac615c4e31e2cc96ff76c67f0d06b6daaf285d3 Mon Sep 17 00:00:00 2001 From: Dariusz Komosinski Date: Sun, 18 Apr 2021 17:06:12 +0200 Subject: [PATCH] Make GritSplitter calculations match the ones in WPF. --- src/Avalonia.Base/Utilities/MathUtilities.cs | 15 +++++++++++++++ src/Avalonia.Controls/GridSplitter.cs | 4 ++-- src/Avalonia.Layout/LayoutHelper.cs | 6 ++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Base/Utilities/MathUtilities.cs b/src/Avalonia.Base/Utilities/MathUtilities.cs index 446b366dc8..1ea369954a 100644 --- a/src/Avalonia.Base/Utilities/MathUtilities.cs +++ b/src/Avalonia.Base/Utilities/MathUtilities.cs @@ -30,6 +30,21 @@ namespace Avalonia.Utilities return (-eps < delta) && (eps > delta); } + /// + /// AreClose - Returns whether or not two doubles are "close". That is, whether or + /// not they are within epsilon of each other. + /// + /// The first double to compare. + /// The second double to compare. + /// The fixed epsilon value used to compare. + public static bool AreClose(double value1, double value2, double eps) + { + //in case they are Infinities (then epsilon check does not work) + if (value1 == value2) return true; + double delta = value1 - value2; + return (-eps < delta) && (eps > delta); + } + /// /// AreClose - Returns whether or not two floats are "close". That is, whether or /// not they are within epsilon of each other. diff --git a/src/Avalonia.Controls/GridSplitter.cs b/src/Avalonia.Controls/GridSplitter.cs index a38ee739bc..26c8464576 100644 --- a/src/Avalonia.Controls/GridSplitter.cs +++ b/src/Avalonia.Controls/GridSplitter.cs @@ -632,11 +632,11 @@ namespace Avalonia.Controls double actualLength2 = GetActualLength(definition2); // When splitting, Check to see if the total pixels spanned by the definitions - // is the same asbefore starting resize. If not cancel the drag + // is the same as before starting resize. If not cancel the drag. if (_resizeData.SplitBehavior == SplitBehavior.Split && !MathUtilities.AreClose( actualLength1 + actualLength2, - _resizeData.OriginalDefinition1ActualLength + _resizeData.OriginalDefinition2ActualLength)) + _resizeData.OriginalDefinition1ActualLength + _resizeData.OriginalDefinition2ActualLength, LayoutHelper.LayoutEpsilon)) { CancelResize(); diff --git a/src/Avalonia.Layout/LayoutHelper.cs b/src/Avalonia.Layout/LayoutHelper.cs index 8ad972059f..35eae0f54c 100644 --- a/src/Avalonia.Layout/LayoutHelper.cs +++ b/src/Avalonia.Layout/LayoutHelper.cs @@ -9,6 +9,12 @@ namespace Avalonia.Layout /// public static class LayoutHelper { + /// + /// Epsilon value used for certain layout calculations. + /// Based on the value in WPF LayoutDoubleUtil. + /// + public const double LayoutEpsilon = 0.00000153; + /// /// Calculates a control's size based on its , /// , ,