diff options
author | kingces95 <kingces95@users.noreply.github.com> | 2017-06-22 18:36:52 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-22 18:36:52 -0400 |
commit | 9940fc261e4325196eaf8947a2fd2bf7001160c0 (patch) | |
tree | 03cc7097c8cc9417aacd6e2bfe82f6a4ac11f3a8 | |
parent | 075a6b370d1dce8f211264422723276411f98b85 (diff) | |
download | xamarin-forms-9940fc261e4325196eaf8947a2fd2bf7001160c0.tar.gz xamarin-forms-9940fc261e4325196eaf8947a2fd2bf7001160c0.tar.bz2 xamarin-forms-9940fc261e4325196eaf8947a2fd2bf7001160c0.zip |
Breaking; Make Grid.AddHz/Vt span ortho dim (#984)
-rw-r--r-- | Xamarin.Forms.Core.UnitTests/GridTests.cs | 158 | ||||
-rw-r--r-- | Xamarin.Forms.Core/Grid.cs | 34 |
2 files changed, 182 insertions, 10 deletions
diff --git a/Xamarin.Forms.Core.UnitTests/GridTests.cs b/Xamarin.Forms.Core.UnitTests/GridTests.cs index fa92b566..38105e78 100644 --- a/Xamarin.Forms.Core.UnitTests/GridTests.cs +++ b/Xamarin.Forms.Core.UnitTests/GridTests.cs @@ -39,6 +39,164 @@ namespace Xamarin.Forms.Core.UnitTests Assert.Throws<ArgumentNullException> (() => layout.Children.Remove (null)); } + [TestFixture] + public class AddDimension : GridTests + { + [Datapoints] + public static IEnumerable<string> Operations = new[] + { + "HHH", + "HHV", + "HVH", + "HVV", + "VHH", + "VHV", + "VVH", + "VVV", + + "RCRHVHVHVHVHV", + + "HHHV", + "VVVH", + + "RV", + "RH", + "CV", + "CH", + + "RVRRV", + "CHCCH", + }; + + Grid _grid; + + int _id = 0; + int _rowDef = 0; + int _colDef = 0; + int _totalWidth = 0; + int _totalHeight = 0; + + public void AddHoizontal() + { + // new block gets new id + var id = _id++; + + // adding column only increases height if no rows exist + if (_totalHeight == 0) + _totalHeight = 1; + + // adding column always increased width by 1 + _totalWidth++; + + // column spans rows 0 to the last row + var row = 0; + var height = _totalHeight; + + // column is always added at the end with a width of 1 + var column = _totalWidth - 1; + var width = 1; + + _grid.Children.AddHorizontal( + new Label() + { + Text = $"{id}: {column}x{row} {width}x{height}" + } + ); + } + public void AddVertical() + { + // new block gets new id + var id = _id++; + + // adding row only increases width if no columns exist + if (_totalWidth == 0) + _totalWidth = 1; + + // adding row always increased height by 1 + _totalHeight++; + + // row spans columns 0 to the last column + var column = 0; + var width = _totalWidth; + + // row is always added at the end with a height of 1 + var row = _totalHeight - 1; + var height = 1; + + _grid.Children.AddVertical( + new Label() + { + Text = $"{id}: {column}x{row} {width}x{height}" + } + ); + } + public void AddRowDef() + { + _rowDef++; + _totalHeight = Math.Max(_rowDef, _totalHeight); + + _grid.RowDefinitions.Add(new RowDefinition()); + } + public void AddColumnDef() + { + _colDef++; + _totalWidth = Math.Max(_colDef, _totalWidth); + + _grid.ColumnDefinitions.Add(new ColumnDefinition()); + } + + [TearDown] + public override void TearDown() + { + _grid = null; + + _id = 0; + _rowDef = 0; + _colDef = 0; + _totalWidth = 0; + _totalHeight = 0; + } + + [Theory] + public void AddDimensionTheory(string operations) + { + _grid = new Grid(); + _grid.Platform = new UnitPlatform(); + + foreach (var op in operations) + { + if (op == 'H') + AddHoizontal(); + + if (op == 'V') + AddVertical(); + + if (op == 'R') + AddRowDef(); + + if (op == 'C') + AddColumnDef(); + + _grid.Layout(new Rectangle(0, 0, 912, 912)); + } + + Console.WriteLine($"Operations: {string.Join(string.Empty, operations)}"); + + var id = 0; + foreach (var view in _grid.Children.Cast<Label>().OrderBy(o => o.Text)) + { + var expected = $"{id++}: " + + $"{Grid.GetColumn(view)}x{Grid.GetRow(view)} " + + $"{Grid.GetColumnSpan(view)}x{Grid.GetRowSpan(view)}"; + + var actual = view.Text; + + Console.WriteLine($" {expected} == {actual}"); + Assert.That(expected == actual); + } + } + } + [Test] public void TestBasicVerticalLayout () { diff --git a/Xamarin.Forms.Core/Grid.cs b/Xamarin.Forms.Core/Grid.cs index adc239e7..4d4094cc 100644 --- a/Xamarin.Forms.Core/Grid.cs +++ b/Xamarin.Forms.Core/Grid.cs @@ -332,12 +332,14 @@ namespace Xamarin.Forms if (view == null) throw new ArgumentNullException("view"); - int lastRow = this.Any() ? this.Max(w => GetRow(w) + GetRowSpan(w) - 1) : -1; - lastRow = Math.Max(lastRow, Parent.RowDefinitions.Count - 1); - int lastCol = this.Any() ? this.Max(w => GetColumn(w) + GetColumnSpan(w) - 1) : -1; - lastCol = Math.Max(lastCol, Parent.ColumnDefinitions.Count - 1); + var rows = RowCount(); + var columns = ColumnCount(); - Add(view, lastCol + 1, lastCol + 2, 0, Math.Max(1, lastRow)); + // if no rows, create a row + if (rows == 0) + rows++; + + Add(view, columns, columns + 1, 0, rows); } public void AddVertical(IEnumerable<View> views) @@ -353,13 +355,25 @@ namespace Xamarin.Forms if (view == null) throw new ArgumentNullException("view"); - int lastRow = this.Any() ? this.Max(w => GetRow(w) + GetRowSpan(w) - 1) : -1; - lastRow = Math.Max(lastRow, Parent.RowDefinitions.Count - 1); - int lastCol = this.Any() ? this.Max(w => GetColumn(w) + GetColumnSpan(w) - 1) : -1; - lastCol = Math.Max(lastCol, Parent.ColumnDefinitions.Count - 1); + var rows = RowCount(); + var columns = ColumnCount(); + + // if no columns, create a column + if (columns == 0) + columns++; - Add(view, 0, Math.Max(1, lastCol), lastRow + 1, lastRow + 2); + Add(view, 0, columns, rows, rows + 1); } + + private int RowCount() => Math.Max( + this.Max<View, int?>(w => GetRow(w) + GetRowSpan(w)) ?? 0, + Parent.RowDefinitions.Count + ); + + private int ColumnCount() => Math.Max( + this.Max<View, int?>(w => GetColumn(w) + GetColumnSpan(w)) ?? 0, + Parent.ColumnDefinitions.Count + ); } } }
\ No newline at end of file |