Skip to content

Commit

Permalink
feat(double): Unify .IsFinite() method extension for all platforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
carldebilly committed Feb 25, 2021
1 parent 1d0901a commit 29f9f2a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 30 deletions.
30 changes: 30 additions & 0 deletions src/Uno.UI/Extensions/DoubleExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;

namespace Uno.Extensions
{
internal static class DoubleExtensions
{
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool IsFinite(this double value)
{
#if !XAMARIN
return !double.IsInfinity(value) && !double.IsNaN(value);
#else
return double.IsFinite(value);
#endif
}

[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool IsFinite(this float value)
{
#if !XAMARIN
return !float.IsInfinity(value) && !float.IsNaN(value);
#else
return float.IsFinite(value);
#endif
}
}
}
10 changes: 3 additions & 7 deletions src/Uno.UI/Microsoft/UI/Xaml/Controls/Repeater/FlowLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Uno.Extensions;
using static Microsoft.UI.Xaml.Controls._Tracing;
#if !XAMARIN
using _Float = Uno.UI.LayoutHelper;
#else
using _Float = System.Single;
#endif

namespace Microsoft.UI.Xaml.Controls
{
Expand Down Expand Up @@ -227,7 +223,7 @@ protected virtual Rect GetExtent(
// If the available size is infinite, we will have realized all the items in one line.
// In that case, the extent in the non virtualizing direction should be based on the
// right/bottom of the last realized element.
SetMinorSize(ref extent, _Float.IsFinite(availableSizeMinor) ? availableSizeMinor : Math.Max(0.0f, MinorEnd(lastRealizedLayoutBounds)));
SetMinorSize(ref extent, availableSizeMinor.IsFinite() ? availableSizeMinor : Math.Max(0.0f, MinorEnd(lastRealizedLayoutBounds)));
}
else
{
Expand All @@ -236,7 +232,7 @@ protected virtual Rect GetExtent(
// We dont have anything realized. make an educated guess.
int numLines = (int)Math.Ceiling(itemsCount / averageItemsPerLine);
extent =
_Float.IsFinite(availableSizeMinor)
availableSizeMinor.IsFinite()
? MinorMajorRect(0, 0, availableSizeMinor, Math.Max(0.0f, (float)(numLines * averageLineSize - lineSpacing)))
: MinorMajorRect(
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
using System.Collections.Specialized;
using Windows.Foundation;
using Windows.UI.Xaml;
using Uno.Extensions;
using static Microsoft.UI.Xaml.Controls._Tracing;
#if !XAMARIN
using _Double = Uno.UI.LayoutHelper;
#else
using _Double = System.Double;
#endif

namespace Microsoft.UI.Xaml.Controls
{
Expand Down Expand Up @@ -391,7 +387,7 @@ void Generate(
{
// Does not fit, wrap to the previous row
var availableSizeMinor = Minor(availableSize);
SetMinorStart(ref currentBounds, _Double.IsFinite(availableSizeMinor) ? availableSizeMinor - Minor(desiredSize) : 0.0f);
SetMinorStart(ref currentBounds, availableSizeMinor.IsFinite() ? availableSizeMinor - Minor(desiredSize) : 0.0f);
SetMajorStart(ref currentBounds, lineOffset - Major(desiredSize) - (float)(lineSpacing));

if (lineNeedsReposition)
Expand Down
8 changes: 2 additions & 6 deletions src/Uno.UI/Microsoft/UI/Xaml/Controls/Repeater/StackLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Uno.Extensions;
using static Microsoft.UI.Xaml.Controls._Tracing;
#if !XAMARIN
using _Double = Uno.UI.LayoutHelper;
#else
using _Double = System.Double;
#endif

namespace Microsoft.UI.Xaml.Controls
{
Expand Down Expand Up @@ -227,7 +223,7 @@ Size IFlowLayoutAlgorithmDelegates.Algorithm_GetProvisionalArrangeSize(int index
{
var measureSizeMinor = Minor(measureSize);
return MinorMajorSize(
(float) (_Double.IsFinite(measureSizeMinor) ? Math.Max(measureSizeMinor, Minor(desiredSize)) : Minor(desiredSize)),
(float) (measureSizeMinor.IsFinite() ? Math.Max(measureSizeMinor, Minor(desiredSize)) : Minor(desiredSize)),
(float) Major(desiredSize));
}

Expand Down
11 changes: 0 additions & 11 deletions src/Uno.UI/UI/LayoutHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Windows.Foundation;
using Windows.UI.Xaml;
using static System.Double;
using static System.Math;

namespace Uno.UI
{
Expand Down Expand Up @@ -261,16 +260,6 @@ internal static Size NumberOrDefault(this Size value, Size defaultValue)
);
}

#if !XAMARIN
[Pure]
internal static bool IsFinite(this double value)
=> !IsInfinity(value) && !IsNaN(value);

[Pure]
internal static bool IsFinite(this float value)
=> !float.IsInfinity(value) && !float.IsNaN(value);
#endif

[Pure]
internal static double FiniteOrDefault(this double value, double defaultValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Windows.UI.Xaml.Automation.Peers;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Uno.Extensions;
using Uno.UI;

namespace Windows.UI.Xaml.Controls.Primitives
Expand Down

0 comments on commit 29f9f2a

Please sign in to comment.