From 74780a4df496ff8c1f1cdac013eacebad85483aa Mon Sep 17 00:00:00 2001 From: Vladislav Antonyuk Date: Tue, 12 Oct 2021 17:40:20 +0300 Subject: [PATCH 1/6] 122 UniformGrid --- .../Views/UniformGrid.shared.cs | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/CommunityToolkit.Maui/Views/UniformGrid.shared.cs diff --git a/src/CommunityToolkit.Maui/Views/UniformGrid.shared.cs b/src/CommunityToolkit.Maui/Views/UniformGrid.shared.cs new file mode 100644 index 000000000..ea9380b30 --- /dev/null +++ b/src/CommunityToolkit.Maui/Views/UniformGrid.shared.cs @@ -0,0 +1,72 @@ +using System; +using Microsoft.Maui; +using Microsoft.Maui.Controls; +using Microsoft.Maui.Controls.Compatibility; +using Microsoft.Maui.Graphics; + +namespace CommunityToolkit.Maui.Views +{ + /// + /// The UniformGrid is just like the Grid, with the possibility of multiple rows and columns, but with one important difference: + /// All rows and columns will have the same size. + /// Use this when you need the Grid behavior without the need to specify different sizes for the rows and columns. + /// + public class UniformGrid : Layout + { + double childWidth; + + double childHeight; + + protected override void LayoutChildren(double x, double y, double width, double height) + { + Measure(width, height, 0); + var columns = GetColumnsCount(Children.Count, width, childWidth); + var rows = GetRowsCount(Children.Count, columns); + var boundsWidth = width / columns; + var boundsHeight = childHeight; + var bounds = new Rectangle(0, 0, boundsWidth, boundsHeight); + var count = 0; + + for (var i = 0; i < rows; i++) + { + for (var j = 0; j < columns && count < Children.Count; j++) + { + var item = Children[count]; + bounds.X = j * boundsWidth; + bounds.Y = i * boundsHeight; + item.Layout(bounds); + count++; + } + } + } + + protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint) + { + foreach (var child in Children) + { + if (!child.IsVisible) + continue; + + var sizeRequest = child.Measure(double.PositiveInfinity, double.PositiveInfinity, 0); + var minimum = sizeRequest.Minimum; + var request = sizeRequest.Request; + + childHeight = Math.Max(minimum.Height, request.Height); + childWidth = Math.Max(minimum.Width, request.Width); + } + + var columns = GetColumnsCount(Children.Count, widthConstraint, childWidth); + var rows = GetRowsCount(Children.Count, columns); + var size = new Size(columns * childWidth, rows * childHeight); + return new SizeRequest(size, size); + } + + int GetColumnsCount(int visibleChildrenCount, double widthConstraint, double maxChildWidth) + => double.IsPositiveInfinity(widthConstraint) + ? visibleChildrenCount + : Math.Min((int)(widthConstraint / maxChildWidth), visibleChildrenCount); + + int GetRowsCount(int visibleChildrenCount, int columnsCount) + => (int)Math.Ceiling((double)visibleChildrenCount / columnsCount); + } +} \ No newline at end of file From 5857e8f4a27964fcd4720068855cc26fb1f7bd0e Mon Sep 17 00:00:00 2001 From: Vladislav Antonyuk Date: Tue, 12 Oct 2021 23:13:58 +0300 Subject: [PATCH 2/6] Fix for rc2. remove compatibility usage --- .../CommunityToolkit.Maui.Sample.csproj | 2 +- .../MauiProgram.cs | 1 + .../Platforms/Android/MainApplication.cs | 1 + .../Platforms/MacCatalyst/AppDelegate.cs | 1 + .../Views/UniformGrid.shared.cs | 41 ++++++++++--------- 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/samples/CommunityToolkit.Maui.Sample/CommunityToolkit.Maui.Sample.csproj b/samples/CommunityToolkit.Maui.Sample/CommunityToolkit.Maui.Sample.csproj index 2ea67effa..df2d7cfce 100644 --- a/samples/CommunityToolkit.Maui.Sample/CommunityToolkit.Maui.Sample.csproj +++ b/samples/CommunityToolkit.Maui.Sample/CommunityToolkit.Maui.Sample.csproj @@ -15,7 +15,7 @@ com.microsoft.CommunityToolkit.Maui.Sample - 1.0 + 1 1 diff --git a/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs b/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs index f6c19f6f0..41eaef564 100644 --- a/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs +++ b/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs @@ -1,6 +1,7 @@ using Microsoft.Maui; using Microsoft.Maui.Controls.Hosting; using Microsoft.Maui.Controls.Xaml; +using Microsoft.Maui.Hosting; [assembly: XamlCompilationAttribute(XamlCompilationOptions.Compile)] diff --git a/samples/CommunityToolkit.Maui.Sample/Platforms/Android/MainApplication.cs b/samples/CommunityToolkit.Maui.Sample/Platforms/Android/MainApplication.cs index 2b9fb5123..7e0f55e2f 100644 --- a/samples/CommunityToolkit.Maui.Sample/Platforms/Android/MainApplication.cs +++ b/samples/CommunityToolkit.Maui.Sample/Platforms/Android/MainApplication.cs @@ -2,6 +2,7 @@ using Android.App; using Android.Runtime; using Microsoft.Maui; +using Microsoft.Maui.Hosting; namespace CommunityToolkit.Maui.Sample { diff --git a/samples/CommunityToolkit.Maui.Sample/Platforms/MacCatalyst/AppDelegate.cs b/samples/CommunityToolkit.Maui.Sample/Platforms/MacCatalyst/AppDelegate.cs index 1c90735eb..b53e3b1ec 100644 --- a/samples/CommunityToolkit.Maui.Sample/Platforms/MacCatalyst/AppDelegate.cs +++ b/samples/CommunityToolkit.Maui.Sample/Platforms/MacCatalyst/AppDelegate.cs @@ -1,5 +1,6 @@ using Foundation; using Microsoft.Maui; +using Microsoft.Maui.Hosting; namespace CommunityToolkit.Maui.Sample { diff --git a/src/CommunityToolkit.Maui/Views/UniformGrid.shared.cs b/src/CommunityToolkit.Maui/Views/UniformGrid.shared.cs index ea9380b30..ac583819b 100644 --- a/src/CommunityToolkit.Maui/Views/UniformGrid.shared.cs +++ b/src/CommunityToolkit.Maui/Views/UniformGrid.shared.cs @@ -1,8 +1,8 @@ using System; using Microsoft.Maui; using Microsoft.Maui.Controls; -using Microsoft.Maui.Controls.Compatibility; using Microsoft.Maui.Graphics; +using Microsoft.Maui.Layouts; namespace CommunityToolkit.Maui.Views { @@ -11,18 +11,23 @@ namespace CommunityToolkit.Maui.Views /// All rows and columns will have the same size. /// Use this when you need the Grid behavior without the need to specify different sizes for the rows and columns. /// - public class UniformGrid : Layout + public class UniformGrid : Grid, ILayoutManager { double childWidth; double childHeight; - protected override void LayoutChildren(double x, double y, double width, double height) - { - Measure(width, height, 0); - var columns = GetColumnsCount(Children.Count, width, childWidth); + protected override ILayoutManager CreateLayoutManager() + { + return this; + } + + public Size ArrangeChildren(Rectangle rectangle) + { + Measure(rectangle.Width, rectangle.Height, 0); + var columns = GetColumnsCount(Children.Count, rectangle.Width, childWidth); var rows = GetRowsCount(Children.Count, columns); - var boundsWidth = width / columns; + var boundsWidth = rectangle.Width / columns; var boundsHeight = childHeight; var bounds = new Rectangle(0, 0, boundsWidth, boundsHeight); var count = 0; @@ -34,31 +39,29 @@ protected override void LayoutChildren(double x, double y, double width, double var item = Children[count]; bounds.X = j * boundsWidth; bounds.Y = i * boundsHeight; - item.Layout(bounds); + item.Arrange(bounds); count++; } } + + return new Size(boundsWidth, boundsHeight); } - protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint) - { + public Size Measure(double widthConstraint, double heightConstraint) + { foreach (var child in Children) { - if (!child.IsVisible) + if (child.Visibility != Visibility.Visible) continue; - var sizeRequest = child.Measure(double.PositiveInfinity, double.PositiveInfinity, 0); - var minimum = sizeRequest.Minimum; - var request = sizeRequest.Request; - - childHeight = Math.Max(minimum.Height, request.Height); - childWidth = Math.Max(minimum.Width, request.Width); + var sizeRequest = child.Measure(double.PositiveInfinity, double.PositiveInfinity); + childHeight = sizeRequest.Height; + childWidth = sizeRequest.Width; } var columns = GetColumnsCount(Children.Count, widthConstraint, childWidth); var rows = GetRowsCount(Children.Count, columns); - var size = new Size(columns * childWidth, rows * childHeight); - return new SizeRequest(size, size); + return new Size(columns * childWidth, rows * childHeight); } int GetColumnsCount(int visibleChildrenCount, double widthConstraint, double maxChildWidth) From a74a84471b6b704e054340f8e4f204ad351eee01 Mon Sep 17 00:00:00 2001 From: Vladislav Antonyuk Date: Wed, 20 Oct 2021 23:54:08 +0300 Subject: [PATCH 3/6] Add comments, add tests --- .../BaseTest.cs | 55 ++++---- .../Views/UniformGrid_Tests.cs | 46 +++++++ .../Views/UniformGrid.shared.cs | 122 ++++++++++-------- 3 files changed, 141 insertions(+), 82 deletions(-) create mode 100644 src/CommunityToolkit.Maui.UnitTests/Views/UniformGrid_Tests.cs diff --git a/src/CommunityToolkit.Maui.UnitTests/BaseTest.cs b/src/CommunityToolkit.Maui.UnitTests/BaseTest.cs index 9ab8cbdfd..1b1bdd427 100644 --- a/src/CommunityToolkit.Maui.UnitTests/BaseTest.cs +++ b/src/CommunityToolkit.Maui.UnitTests/BaseTest.cs @@ -3,40 +3,39 @@ using CommunityToolkit.Maui.UnitTests.Mocks; using Microsoft.Maui.Controls; -namespace CommunityToolkit.Maui.UnitTests +namespace CommunityToolkit.Maui.UnitTests; + +public class BaseTest : IDisposable { - public class BaseTest : IDisposable - { - readonly CultureInfo? defaultCulture, defaultUICulture; + readonly CultureInfo? defaultCulture, defaultUICulture; - bool _isDisposed; + bool _isDisposed; - protected BaseTest() - { - defaultCulture = System.Threading.Thread.CurrentThread.CurrentCulture; - defaultUICulture = System.Threading.Thread.CurrentThread.CurrentUICulture; - Device.PlatformServices = new MockPlatformServices(); - } + protected BaseTest() + { + defaultCulture = System.Threading.Thread.CurrentThread.CurrentCulture; + defaultUICulture = System.Threading.Thread.CurrentThread.CurrentUICulture; + Device.PlatformServices = new MockPlatformServices(); + } - ~BaseTest() => Dispose(false); + ~BaseTest() => Dispose(false); - protected virtual void Dispose(bool isDisposing) - { - if (_isDisposed) - return; + protected virtual void Dispose(bool isDisposing) + { + if (_isDisposed) + return; - Device.PlatformServices = null; + Device.PlatformServices = null; - System.Threading.Thread.CurrentThread.CurrentCulture = defaultCulture ?? throw new NullReferenceException(); - System.Threading.Thread.CurrentThread.CurrentUICulture = defaultUICulture ?? throw new NullReferenceException(); + System.Threading.Thread.CurrentThread.CurrentCulture = defaultCulture ?? throw new NullReferenceException(); + System.Threading.Thread.CurrentThread.CurrentUICulture = defaultUICulture ?? throw new NullReferenceException(); - _isDisposed = true; - } + _isDisposed = true; + } - void IDisposable.Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - } -} \ No newline at end of file + void IDisposable.Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } +} diff --git a/src/CommunityToolkit.Maui.UnitTests/Views/UniformGrid_Tests.cs b/src/CommunityToolkit.Maui.UnitTests/Views/UniformGrid_Tests.cs new file mode 100644 index 000000000..fa95039ec --- /dev/null +++ b/src/CommunityToolkit.Maui.UnitTests/Views/UniformGrid_Tests.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using CommunityToolkit.Maui.Views; +using Microsoft.Maui.Controls; +using Microsoft.Maui.Graphics; +using Xunit; + +namespace CommunityToolkit.Maui.UnitTests.Views; + +public class UniformGridTests : BaseTest +{ + UniformGrid uniformGrid; + public UniformGridTests() + { + uniformGrid = new UniformGrid() + { + Children = + { + new BoxView { BackgroundColor = Colors.Yellow, WidthRequest = 25, HeightRequest = 25 }, + new BoxView { BackgroundColor = Colors.Orange, WidthRequest = 25, HeightRequest = 25 }, + new BoxView { BackgroundColor = Colors.Purple, WidthRequest = 25, HeightRequest = 25 }, + }, + WidthRequest = 75, + HeightRequest = 75 + }; + } + + [Fact] + public void MeasureUniformGrid() + { + var expectedSize = new Size(75, 75); + var actualSize = uniformGrid.Measure(75, 75); + Assert.Equal(expectedSize, actualSize); + } + + [Fact] + public void ArrangeChildrenUniformGrid() + { + var expectedSize = new Size(25, 25); + var actualSize = uniformGrid.ArrangeChildren(new Rectangle(0, 0, 75, 75)); + Assert.Equal(expectedSize, actualSize); + } +} diff --git a/src/CommunityToolkit.Maui/Views/UniformGrid.shared.cs b/src/CommunityToolkit.Maui/Views/UniformGrid.shared.cs index ac583819b..c915ac3be 100644 --- a/src/CommunityToolkit.Maui/Views/UniformGrid.shared.cs +++ b/src/CommunityToolkit.Maui/Views/UniformGrid.shared.cs @@ -4,72 +4,86 @@ using Microsoft.Maui.Graphics; using Microsoft.Maui.Layouts; -namespace CommunityToolkit.Maui.Views +namespace CommunityToolkit.Maui.Views; + +/// +/// The UniformGrid is just like the Grid, with the possibility of multiple rows and columns, but with one important difference: +/// All rows and columns will have the same size. +/// Use this when you need the Grid behavior without the need to specify different sizes for the rows and columns. +/// +public class UniformGrid : Grid, ILayoutManager { + double childWidth; + + double childHeight; + /// - /// The UniformGrid is just like the Grid, with the possibility of multiple rows and columns, but with one important difference: - /// All rows and columns will have the same size. - /// Use this when you need the Grid behavior without the need to specify different sizes for the rows and columns. + /// Assign this as a LayoutManager /// - public class UniformGrid : Grid, ILayoutManager + /// + protected override ILayoutManager CreateLayoutManager() { - double childWidth; - - double childHeight; - - protected override ILayoutManager CreateLayoutManager() - { - return this; - } + return this; + } - public Size ArrangeChildren(Rectangle rectangle) - { - Measure(rectangle.Width, rectangle.Height, 0); - var columns = GetColumnsCount(Children.Count, rectangle.Width, childWidth); - var rows = GetRowsCount(Children.Count, columns); - var boundsWidth = rectangle.Width / columns; - var boundsHeight = childHeight; - var bounds = new Rectangle(0, 0, boundsWidth, boundsHeight); - var count = 0; + /// + /// Arrange children + /// + /// Grid rectangle + /// Child size + public Size ArrangeChildren(Rectangle rectangle) + { + Measure(rectangle.Width, rectangle.Height, MeasureFlags.None); + var columns = GetColumnsCount(Children.Count, rectangle.Width, childWidth); + var rows = GetRowsCount(Children.Count, columns); + var boundsWidth = rectangle.Width / columns; + var boundsHeight = childHeight; + var bounds = new Rectangle(0, 0, boundsWidth, boundsHeight); + var count = 0; - for (var i = 0; i < rows; i++) + for (var i = 0; i < rows; i++) + { + for (var j = 0; j < columns && count < Children.Count; j++) { - for (var j = 0; j < columns && count < Children.Count; j++) - { - var item = Children[count]; - bounds.X = j * boundsWidth; - bounds.Y = i * boundsHeight; - item.Arrange(bounds); - count++; - } + var item = Children[count]; + bounds.X = j * boundsWidth; + bounds.Y = i * boundsHeight; + item.Arrange(bounds); + count++; } - - return new Size(boundsWidth, boundsHeight); } - public Size Measure(double widthConstraint, double heightConstraint) - { - foreach (var child in Children) - { - if (child.Visibility != Visibility.Visible) - continue; + return new Size(boundsWidth, boundsHeight); + } - var sizeRequest = child.Measure(double.PositiveInfinity, double.PositiveInfinity); - childHeight = sizeRequest.Height; - childWidth = sizeRequest.Width; - } + /// + /// Measure grid size + /// + /// Width constraint + /// Height constraint + /// Grid size + public Size Measure(double widthConstraint, double heightConstraint) + { + foreach (var child in Children) + { + if (child.Visibility != Visibility.Visible) + continue; - var columns = GetColumnsCount(Children.Count, widthConstraint, childWidth); - var rows = GetRowsCount(Children.Count, columns); - return new Size(columns * childWidth, rows * childHeight); + var sizeRequest = child.Measure(double.PositiveInfinity, double.PositiveInfinity); + childHeight = sizeRequest.Height; + childWidth = sizeRequest.Width; } - int GetColumnsCount(int visibleChildrenCount, double widthConstraint, double maxChildWidth) - => double.IsPositiveInfinity(widthConstraint) - ? visibleChildrenCount - : Math.Min((int)(widthConstraint / maxChildWidth), visibleChildrenCount); - - int GetRowsCount(int visibleChildrenCount, int columnsCount) - => (int)Math.Ceiling((double)visibleChildrenCount / columnsCount); + var columns = GetColumnsCount(Children.Count, widthConstraint, childWidth); + var rows = GetRowsCount(Children.Count, columns); + return new Size(columns * childWidth, rows * childHeight); } -} \ No newline at end of file + + int GetColumnsCount(int visibleChildrenCount, double widthConstraint, double maxChildWidth) + => double.IsPositiveInfinity(widthConstraint) + ? visibleChildrenCount + : Math.Min((int)(widthConstraint / maxChildWidth), visibleChildrenCount); + + int GetRowsCount(int visibleChildrenCount, int columnsCount) + => (int)Math.Ceiling((double)visibleChildrenCount / columnsCount); +} From fc289920d415d1c9fbc8d1385f30b50b43ffcc6d Mon Sep 17 00:00:00 2001 From: Vladislav Antonyuk Date: Sat, 13 Nov 2021 17:08:51 +0200 Subject: [PATCH 4/6] Fix uniform tests --- .../CommunityToolkit.Maui.UnitTests.csproj | 1 + .../Views/UniformGrid_Tests.cs | 47 ++++++++++++++----- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/CommunityToolkit.Maui.UnitTests/CommunityToolkit.Maui.UnitTests.csproj b/src/CommunityToolkit.Maui.UnitTests/CommunityToolkit.Maui.UnitTests.csproj index 9aa756a01..08320d20c 100644 --- a/src/CommunityToolkit.Maui.UnitTests/CommunityToolkit.Maui.UnitTests.csproj +++ b/src/CommunityToolkit.Maui.UnitTests/CommunityToolkit.Maui.UnitTests.csproj @@ -9,6 +9,7 @@ + diff --git a/src/CommunityToolkit.Maui.UnitTests/Views/UniformGrid_Tests.cs b/src/CommunityToolkit.Maui.UnitTests/Views/UniformGrid_Tests.cs index fa95039ec..5aea6d889 100644 --- a/src/CommunityToolkit.Maui.UnitTests/Views/UniformGrid_Tests.cs +++ b/src/CommunityToolkit.Maui.UnitTests/Views/UniformGrid_Tests.cs @@ -3,9 +3,12 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using CommunityToolkit.Maui.UnitTests.Mocks; using CommunityToolkit.Maui.Views; +using Microsoft.Maui; using Microsoft.Maui.Controls; using Microsoft.Maui.Graphics; +using NSubstitute; using Xunit; namespace CommunityToolkit.Maui.UnitTests.Views; @@ -13,34 +16,56 @@ namespace CommunityToolkit.Maui.UnitTests.Views; public class UniformGridTests : BaseTest { UniformGrid uniformGrid; + IView uniformChild; + const double childCount = 3; + const double childWidth = 20; + const double childHeight = 10; public UniformGridTests() { + uniformChild = Substitute.For(); uniformGrid = new UniformGrid() { Children = { - new BoxView { BackgroundColor = Colors.Yellow, WidthRequest = 25, HeightRequest = 25 }, - new BoxView { BackgroundColor = Colors.Orange, WidthRequest = 25, HeightRequest = 25 }, - new BoxView { BackgroundColor = Colors.Purple, WidthRequest = 25, HeightRequest = 25 }, - }, - WidthRequest = 75, - HeightRequest = 75 + uniformChild, + uniformChild, + uniformChild + } }; } [Fact] - public void MeasureUniformGrid() + public void MeasureUniformGrid_NoWrap() { - var expectedSize = new Size(75, 75); - var actualSize = uniformGrid.Measure(75, 75); + var expectedSize = new Size(childWidth * childCount, childHeight); + var childSize = new Size(childWidth, childHeight); + SetupChildrenSize(childSize); + var actualSize = uniformGrid.Measure(childWidth * childCount, childHeight * childCount); + Assert.Equal(expectedSize, actualSize); + } + + [Fact] + public void MeasureUniformGrid_WrapOnNextRow() + { + var expectedSize = new Size(childWidth, childHeight * childCount); + var childSize = new Size(childWidth, childHeight); + SetupChildrenSize(childSize); + var actualSize = uniformGrid.Measure(childWidth, childHeight * childCount); Assert.Equal(expectedSize, actualSize); } [Fact] public void ArrangeChildrenUniformGrid() { - var expectedSize = new Size(25, 25); - var actualSize = uniformGrid.ArrangeChildren(new Rectangle(0, 0, 75, 75)); + var expectedSize = new Size(childWidth, childHeight); + SetupChildrenSize(expectedSize); + var actualSize = uniformGrid.ArrangeChildren(new Rectangle(0, 0, childWidth * childCount, childHeight * childCount)); Assert.Equal(expectedSize, actualSize); } + + void SetupChildrenSize(Size size) + { + uniformChild.Measure(double.PositiveInfinity, double.PositiveInfinity).ReturnsForAnyArgs(size); + uniformGrid.Measure(childWidth * childCount, childHeight * childCount); + } } From 18c09feb116de703e4f1b98b4ab56924f9d54330 Mon Sep 17 00:00:00 2001 From: Vladislav Antonyuk Date: Sat, 13 Nov 2021 20:40:54 +0200 Subject: [PATCH 5/6] MaxRows, MaxColumns --- .../Views/UniformGrid_Tests.cs | 11 ++++++ .../Views/UniformGrid.shared.cs | 36 ++++++++++++++++--- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/CommunityToolkit.Maui.UnitTests/Views/UniformGrid_Tests.cs b/src/CommunityToolkit.Maui.UnitTests/Views/UniformGrid_Tests.cs index 5aea6d889..073d1dac1 100644 --- a/src/CommunityToolkit.Maui.UnitTests/Views/UniformGrid_Tests.cs +++ b/src/CommunityToolkit.Maui.UnitTests/Views/UniformGrid_Tests.cs @@ -63,6 +63,17 @@ public void ArrangeChildrenUniformGrid() Assert.Equal(expectedSize, actualSize); } + [Fact] + public void MaxRowsArrangeChildrenUniformGrid() + { + var expectedSize = new Size(childWidth, childHeight); + SetupChildrenSize(expectedSize); + uniformGrid.MaxColumns = 1; + uniformGrid.MaxRows = 1; + var actualSize = uniformGrid.Measure(double.PositiveInfinity, double.PositiveInfinity); + Assert.Equal(expectedSize, actualSize); + } + void SetupChildrenSize(Size size) { uniformChild.Measure(double.PositiveInfinity, double.PositiveInfinity).ReturnsForAnyArgs(size); diff --git a/src/CommunityToolkit.Maui/Views/UniformGrid.shared.cs b/src/CommunityToolkit.Maui/Views/UniformGrid.shared.cs index c915ac3be..891935c9f 100644 --- a/src/CommunityToolkit.Maui/Views/UniformGrid.shared.cs +++ b/src/CommunityToolkit.Maui/Views/UniformGrid.shared.cs @@ -26,6 +26,34 @@ protected override ILayoutManager CreateLayoutManager() return this; } + /// + /// Backing BindableProperty for the property. + /// + public static readonly BindableProperty MaxRowsProperty = BindableProperty.Create(nameof(MaxRows), typeof(int), typeof(UniformGrid), int.MaxValue); + + /// + /// Backing BindableProperty for the property. + /// + public static readonly BindableProperty MaxColumnsProperty = BindableProperty.Create(nameof(MaxColumns), typeof(int), typeof(UniformGrid), int.MaxValue); + + /// + /// Max rows + /// + public int MaxRows + { + get { return (int)GetValue(MaxRowsProperty); } + set { SetValue(MaxRowsProperty, value); } + } + + /// + /// Max columns + /// + public int MaxColumns + { + get { return (int)GetValue(MaxColumnsProperty); } + set { SetValue(MaxColumnsProperty, value); } + } + /// /// Arrange children /// @@ -80,10 +108,10 @@ public Size Measure(double widthConstraint, double heightConstraint) } int GetColumnsCount(int visibleChildrenCount, double widthConstraint, double maxChildWidth) - => double.IsPositiveInfinity(widthConstraint) - ? visibleChildrenCount - : Math.Min((int)(widthConstraint / maxChildWidth), visibleChildrenCount); + => Math.Min(double.IsPositiveInfinity(widthConstraint) + ? visibleChildrenCount + : Math.Min((int)(widthConstraint / maxChildWidth), visibleChildrenCount), MaxColumns); int GetRowsCount(int visibleChildrenCount, int columnsCount) - => (int)Math.Ceiling((double)visibleChildrenCount / columnsCount); + => Math.Min((int)Math.Ceiling((double)visibleChildrenCount / columnsCount), MaxRows); } From 0cfd38f2f2ddc757c5ff9d9ad107a11f53e707e8 Mon Sep 17 00:00:00 2001 From: Vladislav Antonyuk Date: Tue, 16 Nov 2021 11:57:50 +0200 Subject: [PATCH 6/6] Add samples --- .../Pages/Views/UniformGridPage.xaml | 40 +++++++++++++++++++ .../Pages/Views/UniformGridPage.xaml.cs | 9 +++++ .../Pages/Views/ViewsGalleryPage.cs | 11 +++++ .../ViewModels/MainViewModel.cs | 4 ++ .../ViewModels/Views/ViewsGalleryViewModel.cs | 17 ++++++++ 5 files changed, 81 insertions(+) create mode 100644 samples/CommunityToolkit.Maui.Sample/Pages/Views/UniformGridPage.xaml create mode 100644 samples/CommunityToolkit.Maui.Sample/Pages/Views/UniformGridPage.xaml.cs create mode 100644 samples/CommunityToolkit.Maui.Sample/Pages/Views/ViewsGalleryPage.cs create mode 100644 samples/CommunityToolkit.Maui.Sample/ViewModels/Views/ViewsGalleryViewModel.cs diff --git a/samples/CommunityToolkit.Maui.Sample/Pages/Views/UniformGridPage.xaml b/samples/CommunityToolkit.Maui.Sample/Pages/Views/UniformGridPage.xaml new file mode 100644 index 000000000..ec58f4fb7 --- /dev/null +++ b/samples/CommunityToolkit.Maui.Sample/Pages/Views/UniformGridPage.xaml @@ -0,0 +1,40 @@ + + + + + + + + \ No newline at end of file diff --git a/samples/CommunityToolkit.Maui.Sample/Pages/Views/UniformGridPage.xaml.cs b/samples/CommunityToolkit.Maui.Sample/Pages/Views/UniformGridPage.xaml.cs new file mode 100644 index 000000000..c3f1b5b18 --- /dev/null +++ b/samples/CommunityToolkit.Maui.Sample/Pages/Views/UniformGridPage.xaml.cs @@ -0,0 +1,9 @@ +namespace CommunityToolkit.Maui.Sample.Pages.Views; + +public partial class UniformGridPage : BasePage +{ + public UniformGridPage() + { + InitializeComponent(); + } +} diff --git a/samples/CommunityToolkit.Maui.Sample/Pages/Views/ViewsGalleryPage.cs b/samples/CommunityToolkit.Maui.Sample/Pages/Views/ViewsGalleryPage.cs new file mode 100644 index 000000000..5aac09bec --- /dev/null +++ b/samples/CommunityToolkit.Maui.Sample/Pages/Views/ViewsGalleryPage.cs @@ -0,0 +1,11 @@ +using System; +using CommunityToolkit.Maui.Sample.ViewModels.Views; + +namespace CommunityToolkit.Maui.Sample.Pages.Views; + +public class ViewsGalleryPage : BaseGalleryPage +{ + public ViewsGalleryPage() : base("Views") + { + } +} diff --git a/samples/CommunityToolkit.Maui.Sample/ViewModels/MainViewModel.cs b/samples/CommunityToolkit.Maui.Sample/ViewModels/MainViewModel.cs index c5c629b6a..ad5e7fea5 100644 --- a/samples/CommunityToolkit.Maui.Sample/ViewModels/MainViewModel.cs +++ b/samples/CommunityToolkit.Maui.Sample/ViewModels/MainViewModel.cs @@ -2,6 +2,7 @@ using CommunityToolkit.Maui.Sample.Models; using CommunityToolkit.Maui.Sample.Pages.Behaviors; using CommunityToolkit.Maui.Sample.Pages.Converters; +using CommunityToolkit.Maui.Sample.Pages.Views; using Microsoft.Maui.Graphics; namespace CommunityToolkit.Maui.Sample.ViewModels; @@ -15,5 +16,8 @@ protected override IEnumerable CreateItems() => new[] new SectionModel(typeof(ConvertersGalleryPage), "Converters", Color.FromArgb("#EA005E"), "Converters let you convert bindings of a certain type to a different value, based on custom logic"), + + new SectionModel(typeof(ViewsGalleryPage), "Views", Color.FromArgb("#EA005E"), + "Views"), }; } \ No newline at end of file diff --git a/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/ViewsGalleryViewModel.cs b/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/ViewsGalleryViewModel.cs new file mode 100644 index 000000000..38fd87e3d --- /dev/null +++ b/samples/CommunityToolkit.Maui.Sample/ViewModels/Views/ViewsGalleryViewModel.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; +using CommunityToolkit.Maui.Sample.Models; +using CommunityToolkit.Maui.Sample.Pages.Views; +using CommunityToolkit.Maui.Views; + +namespace CommunityToolkit.Maui.Sample.ViewModels.Views; + +public class ViewsGalleryViewModel : BaseGalleryViewModel +{ + protected override IEnumerable CreateItems() => new[] + { + new SectionModel( + typeof(UniformGridPage), + nameof(UniformGrid), + "UniformGrid") + }; +}