Skip to content

Commit

Permalink
feat(itemsrepeater): Make the ItemRepeater available on netstandard 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dr1rrb committed Nov 16, 2020
1 parent 124c6d3 commit ec7f73c
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 55 deletions.
10 changes: 10 additions & 0 deletions src/Uno.UI/UI/LayoutHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ internal static Size NumberOrDefault(this Size value, Size defaultValue)
);
}

#if NETSTANDARD2_0
[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 @@ -7,7 +7,11 @@ namespace Microsoft.UI.Xaml.Controls
partial class FlowLayout : OrientationBasedMeasures
{
private ScrollOrientation ScrollOrientation { get; set; }
ScrollOrientation OrientationBasedMeasures.ScrollOrientation => ScrollOrientation;
ScrollOrientation OrientationBasedMeasures.ScrollOrientation
{
get => ScrollOrientation;
set => ScrollOrientation = value;
}

private protected double Major(Size size) => ((OrientationBasedMeasures) this).Major(size);
private protected double Minor(Size size) => ((OrientationBasedMeasures) this).Minor(size);
Expand Down
25 changes: 15 additions & 10 deletions src/Uno.UI/UI/Xaml/Controls/Primitives/Repeater/FlowLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using static Microsoft.UI.Xaml.Controls._Tracing;
#if NETSTANDARD2_0
using _Float = Uno.UI.LayoutHelper;
#else
using _Float = System.Single;
#endif

namespace Microsoft.UI.Xaml.Controls
{
Expand Down Expand Up @@ -45,7 +50,7 @@ public FlowLayout()
LayoutId = "FlowLayout";
}

#region IVirtualizingLayoutOverrides
#region IVirtualizingLayoutOverrides
protected internal override void InitializeForContextCore(VirtualizingLayoutContext context)
{
var state = context.LayoutState;
Expand Down Expand Up @@ -110,9 +115,9 @@ protected internal override void OnItemsChangedCore(VirtualizingLayoutContext co
InvalidateLayout();
}

#endregion
#endregion

#region IFlowLayoutOverrides
#region IFlowLayoutOverrides

protected virtual Size GetMeasureSize(int index, Size availableSize)
{
Expand Down Expand Up @@ -222,7 +227,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, double.IsFinite(availableSizeMinor) ? availableSizeMinor : Math.Max(0.0f, MinorEnd(lastRealizedLayoutBounds)));
SetMinorSize(ref extent, _Float.IsFinite(availableSizeMinor) ? availableSizeMinor : Math.Max(0.0f, MinorEnd(lastRealizedLayoutBounds)));
}
else
{
Expand All @@ -231,7 +236,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)
_Float.IsFinite(availableSizeMinor)
? MinorMajorRect(0, 0, availableSizeMinor, Math.Max(0.0f, (float)(numLines * averageLineSize - lineSpacing)))
: MinorMajorRect(
0,
Expand Down Expand Up @@ -282,9 +287,9 @@ protected virtual void OnLineArranged(
flowState.OnLineArranged(startIndex, countInLine, lineSize, context);
}

#endregion
#endregion

#region IFlowLayoutAlgorithmDelegates
#region IFlowLayoutAlgorithmDelegates

Size IFlowLayoutAlgorithmDelegates.Algorithm_GetMeasureSize(int index, Size availableSize, VirtualizingLayoutContext context)
{
Expand Down Expand Up @@ -370,7 +375,7 @@ void IFlowLayoutAlgorithmDelegates.Algorithm_OnLineArranged(
context);
}

#endregion
#endregion

void OnPropertyChanged(DependencyPropertyChangedEventArgs args)
{
Expand Down Expand Up @@ -400,7 +405,7 @@ void OnPropertyChanged(DependencyPropertyChangedEventArgs args)
InvalidateLayout();
}

#region private helpers
#region private helpers

double GetAverageLineInfo(

Expand Down Expand Up @@ -431,6 +436,6 @@ double GetAverageLineInfo(
return avgLineSize;
}

#endregion
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ namespace Microsoft.UI.Xaml.Controls
partial class FlowLayoutAlgorithm : OrientationBasedMeasures
{
private ScrollOrientation ScrollOrientation { get; set; }
ScrollOrientation OrientationBasedMeasures.ScrollOrientation => ScrollOrientation;
ScrollOrientation OrientationBasedMeasures.ScrollOrientation
{
get => ScrollOrientation;
set => ScrollOrientation = value;
}

private protected double Major(Size size) => ((OrientationBasedMeasures)this).Major(size);
private protected double Minor(Size size) => ((OrientationBasedMeasures)this).Minor(size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
using Windows.Foundation;
using Windows.UI.Xaml;
using static Microsoft.UI.Xaml.Controls._Tracing;
#if NETSTANDARD2_0
using _Double = Uno.UI.LayoutHelper;
#else
using _Double = System.Double;
#endif

namespace Microsoft.UI.Xaml.Controls
{
Expand Down Expand Up @@ -386,7 +391,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, _Double.IsFinite(availableSizeMinor) ? availableSizeMinor - Minor(desiredSize) : 0.0f);
SetMajorStart(ref currentBounds, lineOffset - Major(desiredSize) - (float)(lineSpacing));

if (lineNeedsReposition)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ namespace Microsoft.UI.Xaml.Controls
{
public partial class ItemsRepeater : FrameworkElement, IPanel
{
private readonly UIElementCollection _children;
UIElementCollection IPanel.Children => _children;
internal IList<UIElement> Children => _children.AsUIElementList();
private readonly UIElementCollection _repeaterChildren;
UIElementCollection IPanel.Children => _repeaterChildren;
internal IList<UIElement> Children => _repeaterChildren.AsUIElementList();


// Change to 'true' to turn on debugging outputs in Output window
Expand Down Expand Up @@ -92,7 +92,7 @@ public ItemsRepeater()
{
//__RP_Marker_ClassById(RuntimeProfiler.ProfId_ItemsRepeater);

_children = new UIElementCollection(this);
_repeaterChildren = new UIElementCollection(this);
m_animationManager = new AnimationManager(this);
m_viewManager = new ViewManager(this);
//if (SharedHelpers.IsRS5OrHigher())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using Windows.Foundation;
using Android.Bluetooth;

namespace Microsoft.UI.Xaml.Controls
{
Expand All @@ -10,20 +9,23 @@ namespace Microsoft.UI.Xaml.Controls
// but it's actually **NOT** an interface!
internal interface OrientationBasedMeasures
{
private protected ScrollOrientation ScrollOrientation { get; } // = ScrollOrientation.Vertical; we cannot init properties of interfaces, however Vertical is already the default value!
ScrollOrientation ScrollOrientation { get; set; } // = ScrollOrientation.Vertical; we cannot init properties of interfaces, however Vertical is already the default value!
}

double Major(Size size)
=> ScrollOrientation == ScrollOrientation.Vertical ? size.Height : size.Width;
internal static class OrientationBasedMeasuresExtensions
{
public static double Major(this OrientationBasedMeasures obm, Size size)
=> obm.ScrollOrientation == ScrollOrientation.Vertical ? size.Height : size.Width;

double Minor(Size size)
=> ScrollOrientation == ScrollOrientation.Vertical ? size.Width : size.Height;
public static double Minor(this OrientationBasedMeasures obm, Size size)
=> obm.ScrollOrientation == ScrollOrientation.Vertical ? size.Width : size.Height;

double MajorSize(Rect rect)
=> ScrollOrientation == ScrollOrientation.Vertical ? rect.Height : rect.Width;
public static double MajorSize(this OrientationBasedMeasures obm, Rect rect)
=> obm.ScrollOrientation == ScrollOrientation.Vertical ? rect.Height : rect.Width;

public void SetMajorSize(ref Rect rect, double value)
public static void SetMajorSize(this OrientationBasedMeasures obm, ref Rect rect, double value)
{
if (ScrollOrientation == ScrollOrientation.Vertical)
if (obm.ScrollOrientation == ScrollOrientation.Vertical)
{
rect.Height = value;
}
Expand All @@ -33,12 +35,12 @@ public void SetMajorSize(ref Rect rect, double value)
}
}

double MinorSize(Rect rect)
=> ScrollOrientation == ScrollOrientation.Vertical ? rect.Width : rect.Height;
public static double MinorSize(this OrientationBasedMeasures obm, Rect rect)
=> obm.ScrollOrientation == ScrollOrientation.Vertical ? rect.Width : rect.Height;

public void SetMinorSize(ref Rect rect, double value)
public static void SetMinorSize(this OrientationBasedMeasures obm, ref Rect rect, double value)
{
if (ScrollOrientation == ScrollOrientation.Vertical)
if (obm.ScrollOrientation == ScrollOrientation.Vertical)
{
rect.Width = value;
}
Expand All @@ -48,30 +50,30 @@ public void SetMinorSize(ref Rect rect, double value)
}
}

public double MajorStart(Rect rect)
=> ScrollOrientation == ScrollOrientation.Vertical ? rect.Y : rect.X;
public static double MajorStart(this OrientationBasedMeasures obm, Rect rect)
=> obm.ScrollOrientation == ScrollOrientation.Vertical ? rect.Y : rect.X;

public void SetMajorStart(ref Rect rect, double value)
public static void SetMajorStart(this OrientationBasedMeasures obm, ref Rect rect, double value)
{
if (ScrollOrientation == ScrollOrientation.Vertical)
if (obm.ScrollOrientation == ScrollOrientation.Vertical)
{
rect.Y = value;
}
else
{
rect.X = value;
}
}
}

public double MajorEnd(Rect rect)
=> ScrollOrientation == ScrollOrientation.Vertical ? rect.Y + rect.Height : rect.X + rect.Width;
public static double MajorEnd(this OrientationBasedMeasures obm, Rect rect)
=> obm.ScrollOrientation == ScrollOrientation.Vertical ? rect.Y + rect.Height : rect.X + rect.Width;

double MinorStart(Rect rect)
=> ScrollOrientation == ScrollOrientation.Vertical ? rect.X : rect.Y;
public static double MinorStart(this OrientationBasedMeasures obm, Rect rect)
=> obm.ScrollOrientation == ScrollOrientation.Vertical ? rect.X : rect.Y;

public void SetMinorStart(ref Rect rect, double value)
public static void SetMinorStart(this OrientationBasedMeasures obm, ref Rect rect, double value)
{
if (ScrollOrientation == ScrollOrientation.Vertical)
if (obm.ScrollOrientation == ScrollOrientation.Vertical)
{
rect.X = value;
}
Expand All @@ -81,9 +83,9 @@ public void SetMinorStart(ref Rect rect, double value)
}
}

public void AddMinorStart(ref Rect rect, double increment)
public static void AddMinorStart(this OrientationBasedMeasures obm, ref Rect rect, double increment)
{
if (ScrollOrientation == ScrollOrientation.Vertical)
if (obm.ScrollOrientation == ScrollOrientation.Vertical)
{
rect.X += increment;
}
Expand All @@ -93,18 +95,18 @@ public void AddMinorStart(ref Rect rect, double increment)
}
}

double MinorEnd(Rect rect)
=> ScrollOrientation == ScrollOrientation.Vertical ? rect.X + rect.Width : rect.Y + rect.Height;
public static double MinorEnd(this OrientationBasedMeasures obm, Rect rect)
=> obm.ScrollOrientation == ScrollOrientation.Vertical ? rect.X + rect.Width : rect.Y + rect.Height;

Rect MinorMajorRect(float minor, float major, float minorSize, float majorSize)
=> ScrollOrientation == ScrollOrientation.Vertical
public static Rect MinorMajorRect(this OrientationBasedMeasures obm, float minor, float major, float minorSize, float majorSize)
=> obm.ScrollOrientation == ScrollOrientation.Vertical
? new Rect(minor, major, minorSize, majorSize)
: new Rect(major, minor, majorSize, minorSize);

Point MinorMajorPoint(float minor, float major)
=> ScrollOrientation == ScrollOrientation.Vertical ? new Point(minor, major) : new Point(major, minor);
public static Point MinorMajorPoint(this OrientationBasedMeasures obm, float minor, float major)
=> obm.ScrollOrientation == ScrollOrientation.Vertical ? new Point(minor, major) : new Point(major, minor);

Size MinorMajorSize(float minor, float major)
=> ScrollOrientation == ScrollOrientation.Vertical ? new Size(minor, major) : new Size(major, minor);
public static Size MinorMajorSize(this OrientationBasedMeasures obm, float minor, float major)
=> obm.ScrollOrientation == ScrollOrientation.Vertical ? new Size(minor, major) : new Size(major, minor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ namespace Microsoft.UI.Xaml.Controls
partial class StackLayout : OrientationBasedMeasures
{
private ScrollOrientation ScrollOrientation { get; set; }
ScrollOrientation OrientationBasedMeasures.ScrollOrientation => ScrollOrientation;
ScrollOrientation OrientationBasedMeasures.ScrollOrientation
{
get => ScrollOrientation;
set => ScrollOrientation = value;
}

private protected double Major(Size size) => ((OrientationBasedMeasures)this).Major(size);
private protected double Minor(Size size) => ((OrientationBasedMeasures)this).Minor(size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using static Microsoft.UI.Xaml.Controls._Tracing;
#if NETSTANDARD2_0
using _Double = Uno.UI.LayoutHelper;
#else
using _Double = System.Double;
#endif

namespace Microsoft.UI.Xaml.Controls
{
Expand Down Expand Up @@ -222,7 +227,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) (_Double.IsFinite(measureSizeMinor) ? Math.Max(measureSizeMinor, Minor(desiredSize)) : Minor(desiredSize)),
(float) Major(desiredSize));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using Windows.Foundation;
using Windows.UI.Xaml.Markup;
using Android.OS;
using Uno.Extensions;

namespace Microsoft.UI.Xaml.Controls
Expand Down

0 comments on commit ec7f73c

Please sign in to comment.