Skip to content

Commit f800ae9

Browse files
committed
reuse logic between measure and arrange
1 parent 1952e16 commit f800ae9

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

Microsoft.Toolkit.Uwp.UI.Controls/WrapPanel/WrapPanel.Data.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public UvMeasure(Orientation orientation, double width, double height)
4545
public UvMeasure Add(double u, double v)
4646
=> new UvMeasure { U = U + u, V = V + v };
4747

48+
public UvMeasure Add(UvMeasure measure)
49+
=> Add(measure.U, measure.V);
50+
4851
public Size ToSize(Orientation orientation)
4952
=> orientation == Orientation.Horizontal ? new Size(U, V) : new Size(V, U);
5053
}

Microsoft.Toolkit.Uwp.UI.Controls/WrapPanel/WrapPanel.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Collections.Generic;
67
using System.Linq;
78
using Windows.Foundation;
@@ -152,7 +153,7 @@ protected override Size ArrangeOverride(Size finalSize)
152153
if (finalSize != DesiredSize)
153154
{
154155
// We haven't received our desired size. We need to refresh the rows.
155-
finalSize = UpdateRows(finalSize);
156+
UpdateRows(finalSize);
156157
}
157158

158159
if (_rows.Count > 0)
@@ -194,6 +195,7 @@ private Size UpdateRows(Size finalSize)
194195
var position = new UvMeasure(Orientation, Padding.Left, Padding.Top);
195196

196197
var currentRow = new Row(new List<UvRect>(), default);
198+
var finalMeasure = new UvMeasure(Orientation, width: 0.0, height: 0.0);
197199
void Arrange(UIElement child, bool isLast = false)
198200
{
199201
var desiredMeasure = new UvMeasure(Orientation, child.DesiredSize);
@@ -222,6 +224,7 @@ void Arrange(UIElement child, bool isLast = false)
222224

223225
// adjust the location for the next items
224226
position.U += desiredMeasure.U + spacingMeasure.U;
227+
finalMeasure.U = Math.Max(finalMeasure.U, position.U);
225228
}
226229

227230
var lastIndex = Children.Count - 1;
@@ -241,10 +244,11 @@ void Arrange(UIElement child, bool isLast = false)
241244
return Size.Empty;
242245
}
243246

244-
var lastRowRect = _rows.Last().Rect.ToRect(Orientation);
245-
return new Size(
246-
lastRowRect.Bottom + Padding.Bottom,
247-
lastRowRect.Right + Padding.Right);
247+
// Get max V here before computing final rect
248+
var lastRowRect = _rows.Last().Rect;
249+
finalMeasure.V = lastRowRect.Position.V + lastRowRect.Size.V;
250+
var finalRect = finalMeasure.Add(paddingEnd).ToSize(Orientation);
251+
return finalRect;
248252
}
249253
}
250254
}

0 commit comments

Comments
 (0)