Skip to content

Commit d7c3906

Browse files
committed
Code refactoring, avoid exceptions for invalid offsets
1 parent 02a46f5 commit d7c3906

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

Microsoft.Toolkit.Uwp.UI.Controls/UniformGrid/TakenSpotsReferenceHolder.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Collections;
6+
using System.Drawing;
7+
using System.Runtime.CompilerServices;
68
using Microsoft.Toolkit.Diagnostics;
79

810
namespace Microsoft.Toolkit.Uwp.UI.Controls
@@ -57,6 +59,31 @@ public TakenSpotsReferenceHolder(int rows, int columns)
5759
set => this.spotsTaken[(i * Width) + j] = value;
5860
}
5961

62+
/// <summary>
63+
/// Fills the specified area in the current grid with a given value.
64+
/// If invalid coordinates are given, they will simply be ignored and no exception will be thrown.
65+
/// </summary>
66+
/// <param name="value">The value to fill the target area with.</param>
67+
/// <param name="row">The row to start on (inclusive, 0-based index).</param>
68+
/// <param name="column">The column to start on (inclusive, 0-based index).</param>
69+
/// <param name="width">The positive width of area to fill.</param>
70+
/// <param name="height">The positive height of area to fill.</param>
71+
public void Fill(bool value, int row, int column, int width, int height)
72+
{
73+
Rectangle bounds = new Rectangle(0, 0, Width, Height);
74+
75+
// Precompute bounds to skip branching in main loop
76+
bounds.Intersect(new Rectangle(column, row, width, height));
77+
78+
for (int i = bounds.Top; i < bounds.Bottom; i++)
79+
{
80+
for (int j = 0; j < bounds.Width; j++)
81+
{
82+
this[i, j] = value;
83+
}
84+
}
85+
}
86+
6087
/// <summary>
6188
/// Resets the current reference holder.
6289
/// </summary>

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

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
1818
public partial class UniformGrid : Grid
1919
{
2020
// Guard for 15063 as Grid Spacing only works on 16299+.
21-
private static bool _hasGridSpacing = ApiInformation.IsPropertyPresent("Windows.UI.Xaml.Controls.Grid", "ColumnSpacing");
21+
private static readonly bool _hasGridSpacing = ApiInformation.IsPropertyPresent("Windows.UI.Xaml.Controls.Grid", "ColumnSpacing");
2222

2323
// Internal list we use to keep track of items that we don't have space to layout.
2424
private List<UIElement> _overflow = new List<UIElement>();
@@ -78,14 +78,7 @@ protected override Size MeasureOverride(Size availableSize)
7878
{
7979
SetAutoLayout(child, false);
8080

81-
// Fill the area covered by the current element
82-
for (int i = 0; i < rowspan; i++)
83-
{
84-
for (int j = 0; j < colspan; j++)
85-
{
86-
spotref[row + i, col + j] = true;
87-
}
88-
}
81+
spotref.Fill(true, row, col, colspan, rowspan);
8982
}
9083
}
9184

@@ -133,15 +126,7 @@ protected override Size MeasureOverride(Size availableSize)
133126
if (rowspan > 1 || colspan > 1)
134127
{
135128
// TODO: Need to tie this into iterator
136-
137-
// Fill the covered area as above
138-
for (int i = 0; i < rowspan; i++)
139-
{
140-
for (int j = 0; j < colspan; j++)
141-
{
142-
spotref[row + i, column + j] = true;
143-
}
144-
}
129+
spotref.Fill(true, row, column, colspan, rowspan);
145130
}
146131
}
147132
else

0 commit comments

Comments
 (0)