Skip to content

Add Min/Max Width/Height support on Grid Column/Row Definitions #29582

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions src/Controls/src/Core/ColumnDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ public sealed class ColumnDefinition : BindableObject, IDefinition, IGridColumnD
public static readonly BindableProperty WidthProperty = BindableProperty.Create(nameof(Width), typeof(GridLength), typeof(ColumnDefinition), GridLength.Star,
propertyChanged: (bindable, oldValue, newValue) => ((ColumnDefinition)bindable).OnSizeChanged());

/// <summary>Bindable property for <see cref="MinWidth"/>.</summary>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not default to zero and PositiveInfinity?

public static readonly BindableProperty MinWidthProperty = BindableProperty.Create(
nameof(MinWidth), typeof(double), typeof(ColumnDefinition), -1d,
propertyChanged: (bindable, oldValue, newValue) => ((ColumnDefinition)bindable).OnSizeChanged());

/// <summary>Bindable property for <see cref="MaxWidth"/>.</summary>
public static readonly BindableProperty MaxWidthProperty = BindableProperty.Create(
nameof(MaxWidth), typeof(double), typeof(ColumnDefinition), -1d,
propertyChanged: (bindable, oldValue, newValue) => ((ColumnDefinition)bindable).OnSizeChanged());

/// <include file="../../docs/Microsoft.Maui.Controls/ColumnDefinition.xml" path="//Member[@MemberName='.ctor']/Docs/*" />
public ColumnDefinition()
{
Expand All @@ -26,6 +36,24 @@ public GridLength Width
set { SetValue(WidthProperty, value); }
}

/// <summary>
/// Gets or sets the minimum allowed width of the column.
/// </summary>
public double MinWidth
{
get { return (double)GetValue(MinWidthProperty); }
set { SetValue(MinWidthProperty, value); }
}

/// <summary>
/// Gets or sets the maximum allowed width of the column.
/// </summary>
public double MaxWidth
{
get { return (double)GetValue(MaxWidthProperty); }
set { SetValue(MaxWidthProperty, value); }
}

internal double ActualWidth { get; set; }

internal double MinimumWidth { get; set; } = -1;
Expand Down
28 changes: 28 additions & 0 deletions src/Controls/src/Core/RowDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ public sealed class RowDefinition : BindableObject, IDefinition, IGridRowDefinit
public static readonly BindableProperty HeightProperty = BindableProperty.Create(nameof(Height), typeof(GridLength), typeof(RowDefinition), GridLength.Star,
propertyChanged: (bindable, oldValue, newValue) => ((RowDefinition)bindable).OnSizeChanged());

/// <summary>Bindable property for <see cref="MinHeight"/>.</summary>
public static readonly BindableProperty MinHeightProperty = BindableProperty.Create(
nameof(MinHeight), typeof(double), typeof(RowDefinition), -1d,
propertyChanged: (bindable, oldValue, newValue) => ((RowDefinition)bindable).OnSizeChanged());

/// <summary>Bindable property for <see cref="MaxHeight"/>.</summary>
public static readonly BindableProperty MaxHeightProperty = BindableProperty.Create(
nameof(MaxHeight), typeof(double), typeof(RowDefinition), -1d,
propertyChanged: (bindable, oldValue, newValue) => ((RowDefinition)bindable).OnSizeChanged());

/// <include file="../../docs/Microsoft.Maui.Controls/RowDefinition.xml" path="//Member[@MemberName='.ctor']/Docs/*" />
public RowDefinition()
{
Expand All @@ -28,6 +38,24 @@ public GridLength Height
set { SetValue(HeightProperty, value); }
}

/// <summary>
/// Gets or sets the minimum allowed height of the row.
/// </summary>
public double MinHeight
{
get { return (double)GetValue(MinHeightProperty); }
set { SetValue(MinHeightProperty, value); }
}

/// <summary>
/// Gets or sets the maximum allowed height of the row.
/// </summary>
public double MaxHeight
{
get { return (double)GetValue(MaxHeightProperty); }
set { SetValue(MaxHeightProperty, value); }
}

internal double ActualHeight { get; set; }

internal double MinimumHeight { get; set; } = -1;
Expand Down
124 changes: 124 additions & 0 deletions src/Controls/tests/Core.UnitTests/ColumnDefinitionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using Microsoft.Maui.Controls;
using Xunit;

namespace Microsoft.Maui.Controls.Core.UnitTests
{
public class ColumnDefinitionTests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make src/Controls/tests/Core.UnitTests/Layouts/GridLayoutTests.cs partial and this file shoulkd be called GridLayoutTests.ColumnDefinition.cs

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created GridLayoutTests.ColumnDefinition.cs as part of the partial GridLayoutTests class with the column definition tests from the original file in commit 2ef0dc9.

{
[Fact]
public void DefaultMinWidthIsNegativeOne()
{
var column = new ColumnDefinition();
Assert.Equal(-1d, column.MinWidth);
}

[Fact]
public void DefaultMaxWidthIsNegativeOne()
{
var column = new ColumnDefinition();
Assert.Equal(-1d, column.MaxWidth);
}

[Theory]
[InlineData(0)]
[InlineData(50)]
[InlineData(100)]
[InlineData(double.PositiveInfinity)]
public void CanSetMinWidth(double value)
{
var column = new ColumnDefinition();
column.MinWidth = value;
Assert.Equal(value, column.MinWidth);
}

[Theory]
[InlineData(0)]
[InlineData(50)]
[InlineData(100)]
[InlineData(double.PositiveInfinity)]
public void CanSetMaxWidth(double value)
{
var column = new ColumnDefinition();
column.MaxWidth = value;
Assert.Equal(value, column.MaxWidth);
}

[Fact]
public void MinWidthChangeInvalidatesLayout()
{
bool invalidated = false;
var column = new ColumnDefinition();
column.SizeChanged += (sender, args) => invalidated = true;

column.MinWidth = 100;
Assert.True(invalidated);
}

[Fact]
public void MaxWidthChangeInvalidatesLayout()
{
bool invalidated = false;
var column = new ColumnDefinition();
column.SizeChanged += (sender, args) => invalidated = true;

column.MaxWidth = 200;
Assert.True(invalidated);
}

[Fact]
public void WidthChangeInvalidatesLayoutWithMinMaxConstraints()
{
bool invalidated = false;
var column = new ColumnDefinition();
column.MinWidth = 50;
column.MaxWidth = 150;
column.SizeChanged += (sender, args) => invalidated = true;

column.Width = new GridLength(100);
Assert.True(invalidated);
}

[Fact]
public void MinMaxWidthAreBindableProperties()
{
var column = new ColumnDefinition();

bool minWidthChanged = false;
bool maxWidthChanged = false;

column.PropertyChanged += (sender, args) =>
{
if (args.PropertyName == nameof(ColumnDefinition.MinWidth))
minWidthChanged = true;
if (args.PropertyName == nameof(ColumnDefinition.MaxWidth))
maxWidthChanged = true;
};

column.MinWidth = 100;
column.MaxWidth = 200;

Assert.True(minWidthChanged);
Assert.True(maxWidthChanged);
}

[Theory]
[InlineData(-10)] // Negative values other than -1 should be coerced to -1
[InlineData(-2)]
public void NegativeMinWidthIsCoerced(double value)
{
var column = new ColumnDefinition();
column.MinWidth = value;
Assert.Equal(-1d, column.MinWidth);
}

[Theory]
[InlineData(-10)] // Negative values other than -1 should be coerced to -1
[InlineData(-2)]
public void NegativeMaxWidthIsCoerced(double value)
{
var column = new ColumnDefinition();
column.MaxWidth = value;
Assert.Equal(-1d, column.MaxWidth);
}
}
}
Loading