Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CommunityToolkit.Maui.Core;
static class DockLayoutDefaults
{
public const bool ShouldExpandLastChild = true;
public const double HorizontalSpacing = 0.0d;
public const double VerticalSpacing = 0.0d;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CommunityToolkit.Maui.Core;

static class UniformItemLayoutDefaults
{
public const int MaxRows = int.MaxValue;
public const int MaxColumns = int.MaxValue;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public DockLayoutTests()
DockLayout.SetDockPosition(childBottomView, DockPosition.Bottom);
}

[Fact]
public void EnsureDefaults()
{
var layout = new DockLayout();
Assert.Equal(DockLayoutDefaults.ShouldExpandLastChild, layout.ShouldExpandLastChild);
Assert.Equal(DockLayoutDefaults.HorizontalSpacing, layout.HorizontalSpacing);
Assert.Equal(DockLayoutDefaults.VerticalSpacing, layout.VerticalSpacing);
}

#region Measure

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CommunityToolkit.Maui.Layouts;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Layouts;
using FluentAssertions;
using Xunit;

Expand Down Expand Up @@ -109,6 +110,14 @@ public void ArrangeChildrenUniformItemsLayout()
Assert.Equal(childSize * childCount, actualSize);
}

[Fact]
public void EnsureDefaults()
{
var layout = new UniformItemsLayout();
Assert.Equal(UniformItemLayoutDefaults.MaxRows, layout.MaxRows);
Assert.Equal(UniformItemLayoutDefaults.MaxColumns, layout.MaxColumns);
}

class TestView : View
{
readonly Size size;
Expand Down
46 changes: 9 additions & 37 deletions src/CommunityToolkit.Maui/Layouts/DockLayout.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,27 @@ public partial class DockLayout : Layout, IDockLayout
typeof(DockPosition),
typeof(DockLayout),
DockPosition.None,
propertyChanged: OnDockPositionPropertyChanged);
propertyChanged: OnDockPositionPropertyChanged);

/// <summary>
/// If true, the last child is expanded to fill the remaining space (default: true).
/// </summary>
public static readonly BindableProperty ShouldExpandLastChildProperty = BindableProperty.Create(nameof(ShouldExpandLastChild),
typeof(bool),
typeof(DockLayout),
true,
propertyChanged: OnShouldExpandLastChildPropertyChanged);
[BindableProperty(PropertyChangedMethodName = nameof(OnShouldExpandLastChildPropertyChanged))]
public partial bool ShouldExpandLastChild { get; set; } = DockLayoutDefaults.ShouldExpandLastChild;

/// <summary>
/// Horizontal spacing between docked views.
/// Gets or sets the horizontal spacing between child elements.
/// </summary>
public static readonly BindableProperty HorizontalSpacingProperty = BindableProperty.Create(nameof(HorizontalSpacing),
typeof(double),
typeof(DockLayout),
0.0d,
propertyChanged: OnHorizontalSpacingPropertyChanged);
[BindableProperty(PropertyChangedMethodName = nameof(OnHorizontalSpacingPropertyChanged))]
public partial double HorizontalSpacing { get; set; } = DockLayoutDefaults.HorizontalSpacing;

/// <summary>
/// Vertical spacing between docked views.
/// Gets or sets the vertical spacing between elements.
/// </summary>
public static readonly BindableProperty VerticalSpacingProperty = BindableProperty.Create(nameof(VerticalSpacing),
typeof(double),
typeof(DockLayout),
0.0d,
propertyChanged: OnVerticalSpacingPropertyChanged);

/// <inheritdoc/>
public bool ShouldExpandLastChild
{
get => (bool)GetValue(ShouldExpandLastChildProperty);
set => SetValue(ShouldExpandLastChildProperty, value);
}
[BindableProperty(PropertyChangedMethodName = nameof(OnVerticalSpacingPropertyChanged))]
public partial double VerticalSpacing { get; set; } = DockLayoutDefaults.VerticalSpacing;

/// <inheritdoc/>
public double HorizontalSpacing
{
get => (double)GetValue(HorizontalSpacingProperty);
set => SetValue(HorizontalSpacingProperty, value);
}

/// <inheritdoc/>
public double VerticalSpacing
{
get => (double)GetValue(VerticalSpacingProperty);
set => SetValue(VerticalSpacingProperty, value);
}

/// <summary>
/// Gets the docking position for a view.
Expand Down
47 changes: 18 additions & 29 deletions src/CommunityToolkit.Maui/Layouts/UniformItemsLayout.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,38 @@ namespace CommunityToolkit.Maui.Layouts;
public partial class UniformItemsLayout : Layout, IUniformItemsLayout
{
/// <summary>
/// Backing BindableProperty for the <see cref="MaxRows"/> property.
/// Gets or sets the maximum number of rows to display or process.
/// </summary>
public static readonly BindableProperty MaxRowsProperty = BindableProperty.Create(nameof(MaxRows), typeof(int), typeof(UniformItemsLayout), int.MaxValue);
[BindableProperty(PropertyChangedMethodName = nameof(OnMaxRowsProperChanged))]
public partial int MaxRows { get; set; } = UniformItemLayoutDefaults.MaxRows;

/// <summary>
/// Backing BindableProperty for the <see cref="MaxColumns"/> property.
/// Gets or sets the maximum number of columns to display in the layout.
/// </summary>
public static readonly BindableProperty MaxColumnsProperty = BindableProperty.Create(nameof(MaxColumns), typeof(int), typeof(UniformItemsLayout), int.MaxValue);
/// <remarks>Set this property to limit the number of columns arranged by the layout. The value must be greater
/// than or equal to 1. The default value is <see cref="int.MaxValue"/>, which allows an unlimited number of
/// columns.</remarks>
[BindableProperty(PropertyChangedMethodName = nameof(OnMaxColumnsProperChanged))]
public partial int MaxColumns { get; set; } = UniformItemLayoutDefaults.MaxColumns;

/// <summary>
/// Max rows
/// </summary>
public int MaxRows
static void OnMaxRowsProperChanged(BindableObject bindable, object oldValue, object newValue)
{
get => (int)GetValue(MaxRowsProperty);
set
var maxRows = (int)newValue;
if (maxRows < 1)
{
if (value < 1)
{
throw new ArgumentOutOfRangeException(nameof(value), value, $"{nameof(MaxRows)} must be greater or equal to 1.");
}

SetValue(MaxRowsProperty, value);
throw new ArgumentOutOfRangeException(nameof(newValue), newValue, $"{nameof(MaxRows)} must be greater or equal to 1.");
}
}

/// <summary>
/// Max columns
/// </summary>
public int MaxColumns
static void OnMaxColumnsProperChanged(BindableObject bindable, object oldValue, object newValue)
{
get => (int)GetValue(MaxColumnsProperty);
set
var maxColumns = (int)newValue;
if (maxColumns < 1)
{
if (value < 1)
{
throw new ArgumentOutOfRangeException(nameof(value), value, $"{nameof(MaxColumns)} must be greater or equal to 1.");
}

SetValue(MaxColumnsProperty, value);
throw new ArgumentOutOfRangeException(nameof(newValue), newValue, $"{nameof(MaxColumns)} must be greater or equal to 1.");
}
}

/// <inheritdoc />
protected override ILayoutManager CreateLayoutManager() => new UniformItemsLayoutManager(this);
}
}
Loading