Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecated 2D array extensions from Microsoft.Toolkit #3444

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Drawing;
using Microsoft.Toolkit.Diagnostics;

namespace Microsoft.Toolkit.Uwp.UI.Controls
{
Expand All @@ -16,23 +14,81 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
/// <see cref="UniformGrid.GetFreeSpot"/> iterator.
/// This is used so we can better isolate our logic and make it easier to test.
/// </summary>
internal class TakenSpotsReferenceHolder
internal sealed class TakenSpotsReferenceHolder
{
/// <summary>
/// Gets or sets the array to hold taken spots.
/// True value indicates an item in the layout is fixed to that position.
/// False values indicate free openings where an item can be placed.
/// The <see cref="BitArray"/> instance used to efficiently track empty spots.
/// </summary>
public bool[,] SpotsTaken { get; set; }
private readonly BitArray spotsTaken;

/// <summary>
/// Initializes a new instance of the <see cref="TakenSpotsReferenceHolder"/> class.
/// </summary>
/// <param name="rows">The number of rows to track.</param>
/// <param name="columns">The number of columns to track.</param>
public TakenSpotsReferenceHolder(int rows, int columns)
{
SpotsTaken = new bool[rows, columns];
Guard.IsGreaterThanOrEqualTo(rows, 0, nameof(rows));
Guard.IsGreaterThanOrEqualTo(columns, 0, nameof(columns));

Height = rows;
Width = columns;

this.spotsTaken = new BitArray(rows * columns);
}

/// <summary>
/// Gets the height of the grid to monitor.
/// </summary>
public int Height { get; }

/// <summary>
/// Gets the width of the grid to monitor.
/// </summary>
public int Width { get; }

/// <summary>
/// Gets or sets the value of a specified grid cell.
/// </summary>
/// <param name="i">The vertical offset.</param>
/// <param name="j">The horizontal offset.</param>
public bool this[int i, int j]
{
get => this.spotsTaken[(i * Width) + j];
set => this.spotsTaken[(i * Width) + j] = value;
}

/// <summary>
/// Fills the specified area in the current grid with a given value.
/// If invalid coordinates are given, they will simply be ignored and no exception will be thrown.
/// </summary>
/// <param name="value">The value to fill the target area with.</param>
/// <param name="row">The row to start on (inclusive, 0-based index).</param>
/// <param name="column">The column to start on (inclusive, 0-based index).</param>
/// <param name="width">The positive width of area to fill.</param>
/// <param name="height">The positive height of area to fill.</param>
public void Fill(bool value, int row, int column, int width, int height)
{
Rectangle bounds = new Rectangle(0, 0, Width, Height);

// Precompute bounds to skip branching in main loop
bounds.Intersect(new Rectangle(column, row, width, height));

for (int i = bounds.Top; i < bounds.Bottom; i++)
{
for (int j = bounds.Left; j < bounds.Right; j++)
{
this[i, j] = value;
}
}
}

public TakenSpotsReferenceHolder(bool[,] array)
/// <summary>
/// Resets the current reference holder.
/// </summary>
public void Reset()
{
SpotsTaken = array;
this.spotsTaken.SetAll(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ internal static IEnumerable<(int row, int column)> GetFreeSpot(TakenSpotsReferen
{
if (topdown)
{
var rows = arrayref.SpotsTaken.GetLength(0);
var rows = arrayref.Height;

// Layout spots from Top-Bottom, Left-Right (right-left handled automatically by Grid with Flow-Direction).
// Effectively transpose the Grid Layout.
for (int c = 0; c < arrayref.SpotsTaken.GetLength(1); c++)
for (int c = 0; c < arrayref.Width; c++)
{
int start = (c == 0 && firstcolumn > 0 && firstcolumn < rows) ? firstcolumn : 0;
for (int r = start; r < rows; r++)
{
if (!arrayref.SpotsTaken[r, c])
if (!arrayref[r, c])
{
yield return (r, c);
}
Expand All @@ -40,17 +40,17 @@ internal static IEnumerable<(int row, int column)> GetFreeSpot(TakenSpotsReferen
}
else
{
var columns = arrayref.SpotsTaken.GetLength(1);
var columns = arrayref.Width;

// Layout spots as normal from Left-Right.
// (right-left handled automatically by Grid with Flow-Direction
// during its layout, internal model is always left-right).
for (int r = 0; r < arrayref.SpotsTaken.GetLength(0); r++)
for (int r = 0; r < arrayref.Height; r++)
{
int start = (r == 0 && firstcolumn > 0 && firstcolumn < columns) ? firstcolumn : 0;
for (int c = start; c < columns; c++)
{
if (!arrayref.SpotsTaken[r, c])
if (!arrayref[r, c])
{
yield return (r, c);
}
Expand Down
31 changes: 24 additions & 7 deletions Microsoft.Toolkit.Uwp.UI.Controls/UniformGrid/UniformGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Toolkit.Extensions;
using Windows.Foundation;
using Windows.Foundation.Metadata;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace Microsoft.Toolkit.Uwp.UI.Controls
{
Expand All @@ -20,11 +18,16 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
public partial class UniformGrid : Grid
{
// Guard for 15063 as Grid Spacing only works on 16299+.
private static bool _hasGridSpacing = ApiInformation.IsPropertyPresent("Windows.UI.Xaml.Controls.Grid", "ColumnSpacing");
private static readonly bool _hasGridSpacing = ApiInformation.IsPropertyPresent("Windows.UI.Xaml.Controls.Grid", "ColumnSpacing");

// Internal list we use to keep track of items that we don't have space to layout.
private List<UIElement> _overflow = new List<UIElement>();

/// <summary>
/// The <see cref="TakenSpotsReferenceHolder"/> instance in use, if any.
/// </summary>
private TakenSpotsReferenceHolder _spotref;

/// <inheritdoc/>
protected override Size MeasureOverride(Size availableSize)
{
Expand All @@ -41,7 +44,20 @@ protected override Size MeasureOverride(Size availableSize)
SetupRowDefinitions(rows);
SetupColumnDefinitions(columns);

var spotref = new TakenSpotsReferenceHolder(rows, columns);
TakenSpotsReferenceHolder spotref;

// If the last spot holder matches the size currently in use, just reset
// that instance and reuse it to avoid allocating a new bit array.
if (_spotref != null && _spotref.Height == rows && _spotref.Width == columns)
{
spotref = _spotref;

spotref.Reset();
}
else
{
spotref = _spotref = new TakenSpotsReferenceHolder(rows, columns);
}

// Figure out which children we should automatically layout and where available openings are.
foreach (var child in visible)
Expand All @@ -61,7 +77,8 @@ protected override Size MeasureOverride(Size availableSize)
else
{
SetAutoLayout(child, false);
spotref.SpotsTaken.Fill(true, row, col, colspan, rowspan); // row, col, width, height

spotref.Fill(true, row, col, colspan, rowspan);
}
}

Expand Down Expand Up @@ -109,7 +126,7 @@ protected override Size MeasureOverride(Size availableSize)
if (rowspan > 1 || colspan > 1)
{
// TODO: Need to tie this into iterator
spotref.SpotsTaken.Fill(true, row, column, GetColumnSpan(child), GetRowSpan(child)); // row, col, width, height
spotref.Fill(true, row, column, colspan, rowspan);
}
}
else
Expand Down Expand Up @@ -144,7 +161,7 @@ protected override Size MeasureOverride(Size availableSize)
}

// Return our desired size based on the largest child we found, our dimensions, and spacing.
var desiredSize = new Size((maxWidth * (double)columns) + columnSpacingSize, (maxHeight * (double)rows) + rowSpacingSize);
var desiredSize = new Size((maxWidth * columns) + columnSpacingSize, (maxHeight * rows) + rowSpacingSize);

// Required to perform regular grid measurement, but ignore result.
base.MeasureOverride(desiredSize);
Expand Down
Loading