Skip to content

Commit

Permalink
Clamp Grid coordinates for child views (#1947)
Browse files Browse the repository at this point in the history
* Clamp Grid coordinates

* Use correct values

* bad coments

* Add span tests
  • Loading branch information
mattleibow committed Aug 5, 2021
1 parent a075136 commit 1854f03
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/Core/src/Layouts/GridLayoutManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ void InitializeCells()
continue;
}

var column = _grid.GetColumn(view);
var columnSpan = _grid.GetColumnSpan(view);
var column = _grid.GetColumn(view).Clamp(0, _columns.Length - 1);
var columnSpan = _grid.GetColumnSpan(view).Clamp(1, _columns.Length - column);

var columnGridLengthType = GridLengthType.None;

Expand All @@ -169,8 +169,8 @@ void InitializeCells()
columnGridLengthType |= ToGridLengthType(_columns[columnIndex].ColumnDefinition.Width.GridUnitType);
}

var row = _grid.GetRow(view);
var rowSpan = _grid.GetRowSpan(view);
var row = _grid.GetRow(view).Clamp(0, _rows.Length - 1);
var rowSpan = _grid.GetRowSpan(view).Clamp(1, _rows.Length - row);

var rowGridLengthType = GridLengthType.None;

Expand All @@ -189,11 +189,11 @@ void InitializeCells()

public Rectangle GetCellBoundsFor(IView view)
{
var firstColumn = _grid.GetColumn(view);
var lastColumn = firstColumn + _grid.GetColumnSpan(view);
var firstColumn = _grid.GetColumn(view).Clamp(0, _columns.Length - 1);
var lastColumn = firstColumn + _grid.GetColumnSpan(view).Clamp(1, _columns.Length - firstColumn);

var firstRow = _grid.GetRow(view);
var lastRow = firstRow + _grid.GetRowSpan(view);
var firstRow = _grid.GetRow(view).Clamp(0, _rows.Length - 1);
var lastRow = firstRow + _grid.GetRowSpan(view).Clamp(1, _rows.Length - firstRow);

double top = TopEdgeOfRow(firstRow);
double left = LeftEdgeOfColumn(firstColumn);
Expand Down
87 changes: 87 additions & 0 deletions src/Core/tests/UnitTests/Layouts/GridLayoutManagerTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Layouts;
Expand Down Expand Up @@ -1240,5 +1241,91 @@ public void GridMeasureShouldUseExplicitWidth()

Assert.Equal(50, measure.Width);
}

[Theory]
// at 0, 0
[InlineData(1, 1, 0, 0, 0, 0)]
[InlineData(1, 2, 0, 0, 0, 0)]
[InlineData(2, 1, 0, 0, 0, 0)]
[InlineData(2, 2, 0, 0, 0, 0)]
// at 1, 0
[InlineData(1, 1, 1, 0, 0, 0)]
[InlineData(1, 2, 1, 0, 0, 0)]
[InlineData(2, 1, 1, 0, 1, 0)]
[InlineData(2, 2, 1, 0, 1, 0)]
// at 0, 1
[InlineData(1, 1, 0, 1, 0, 0)]
[InlineData(1, 2, 0, 1, 0, 1)]
[InlineData(2, 1, 0, 1, 0, 0)]
[InlineData(2, 2, 0, 1, 0, 1)]
// at 1, 1
[InlineData(1, 1, 1, 1, 0, 0)]
[InlineData(1, 2, 1, 1, 0, 1)]
[InlineData(2, 1, 1, 1, 1, 0)]
[InlineData(2, 2, 1, 1, 1, 1)]
public void ViewOutsideRowsAndColsClampsToGrid(int rows, int cols, int row, int col, int actualRow, int actualCol)
{
var r = string.Join(",", Enumerable.Repeat("100", rows));
var c = string.Join(",", Enumerable.Repeat("100", cols));

var grid = CreateGridLayout(rows: r, columns: c);
var view0 = CreateTestView(new Size(10, 10));
SubstituteChildren(grid, view0);
SetLocation(grid, view0, row, col);

MeasureAndArrange(grid, 100 * cols, 100 * rows);

AssertArranged(view0, 100 * actualCol, 100 * actualRow, 100, 100);
}

[Theory]
// normal
[InlineData(0, 0, 1, 1, 0, 0, 1, 1)]
[InlineData(1, 1, 1, 1, 1, 1, 1, 1)]
[InlineData(1, 1, 2, 1, 1, 1, 2, 1)]
// negative origin
[InlineData(-1, 0, 1, 1, 0, 0, 1, 1)]
[InlineData(0, -1, 1, 1, 0, 0, 1, 1)]
[InlineData(-1, -1, 1, 1, 0, 0, 1, 1)]
// negative span
[InlineData(1, 1, -1, 0, 1, 1, 1, 1)]
[InlineData(1, 1, 0, -1, 1, 1, 1, 1)]
[InlineData(1, 1, -1, -1, 1, 1, 1, 1)]
// positive origin
[InlineData(5, 0, 1, 1, 3, 0, 1, 1)]
[InlineData(0, 5, 1, 1, 0, 3, 1, 1)]
[InlineData(5, 5, 1, 1, 3, 3, 1, 1)]
// positive span
[InlineData(0, 0, 1, 5, 0, 0, 1, 4)]
[InlineData(0, 0, 5, 1, 0, 0, 4, 1)]
[InlineData(0, 0, 5, 5, 0, 0, 4, 4)]
// normal origin + positive span
[InlineData(1, 1, 1, 5, 1, 1, 1, 3)]
[InlineData(1, 1, 5, 1, 1, 1, 3, 1)]
[InlineData(1, 1, 5, 5, 1, 1, 3, 3)]
// positive origin + positive span
[InlineData(5, 5, 1, 5, 3, 3, 1, 1)]
[InlineData(5, 5, 5, 1, 3, 3, 1, 1)]
[InlineData(5, 5, 5, 5, 3, 3, 1, 1)]
public void SpansOutsideRowsAndColsClampsToGrid(int row, int col, int rowSpan, int colSpan, int actualRow, int actualCol, int actualRowSpan, int actualColSpan)
{
const int GridSize = 4;
var r = string.Join(",", Enumerable.Repeat("100", GridSize));
var c = string.Join(",", Enumerable.Repeat("100", GridSize));

var grid = CreateGridLayout(rows: r, columns: c);
var view0 = CreateTestView(new Size(10, 10));
SubstituteChildren(grid, view0);
SetLocation(grid, view0, row, col, rowSpan, colSpan);

MeasureAndArrange(grid, 100 * GridSize, 100 * GridSize);

AssertArranged(
view0,
100 * actualCol,
100 * actualRow,
100 * actualColSpan,
100 * actualRowSpan);
}
}
}

0 comments on commit 1854f03

Please sign in to comment.