Skip to content

Commit

Permalink
Fix for FlexLayout padding issue (#24195)
Browse files Browse the repository at this point in the history
* Fix-15991- Fixed the padding issue on FlexLayout by add the padding calculation in the Measure and ArrangeChildren method().

* added UITest - 15991

* Fix-15991- Output images added for android and iOS.

* Fix-15991-Updated test method name.

* Fix-15991-Updated the test image based on the Test method name.

* Fix-15991-Updated test method with scrollView automationID.

* Fix-15991-Output image added for windows.

* Fix-15991-Added Unit test case and removed UI testcase.

* Fix-15991-Modified Unit TestCase FlexLayout.

* Fix-15991-Modified Unit TestCase for FlexLayout.
  • Loading branch information
BagavathiPerumal authored Aug 30, 2024
1 parent e4ac658 commit ddab6b3
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 5 deletions.
68 changes: 68 additions & 0 deletions src/Controls/tests/Core.UnitTests/Layouts/FlexLayoutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,74 @@ public void UnconstrainedWidthChildrenHaveWidth()
Assert.Equal(100, flexFrame.Width);
}

[Fact]
public void FlexLayoutPaddingShouldBeAppliedCorrectly_RowDirection()
{
// Arrange
var padding = 16;
var root = new Grid();
var flexLayout = new FlexLayout { Padding = padding };
var view1 = new TestLabel();
var view2 = new TestLabel();

root.Add(flexLayout);
flexLayout.Add(view1 as IView);
flexLayout.Add(view2 as IView);

// Act
var measure = flexLayout.CrossPlatformMeasure(1000, 1000);
flexLayout.CrossPlatformArrange(new Rect(Point.Zero, measure));

var view1Frame = flexLayout.Children[0].Frame;
var view2Frame = flexLayout.Children[1].Frame;

var leftPadding = view1Frame.X;
var topPadding = view1Frame.Y;
var rightPadding = measure.Width - view2Frame.Right;
var expectedView1Width = measure.Width - (leftPadding + rightPadding + view2.Width);
var expectedView2Width = measure.Width - (leftPadding + rightPadding + view1.Width);

// Assert
Assert.Equal(padding, leftPadding);
Assert.Equal(padding, rightPadding);
Assert.Equal(padding, topPadding);
Assert.Equal(expectedView1Width, view1Frame.Width);
Assert.Equal(expectedView2Width, view2Frame.Width);
}

[Fact]
public void FlexLayoutPaddingShouldBeAppliedCorrectly_ColumnDirection()
{
// Arrange
var padding = 16;
var root = new Grid();
var flexLayout = new FlexLayout { Padding = padding, Direction = FlexDirection.Column };
var view1 = new TestLabel();
var view2 = new TestLabel();

root.Add(flexLayout);
flexLayout.Add(view1 as IView);
flexLayout.Add(view2 as IView);

// Act
var measure = flexLayout.CrossPlatformMeasure(1000, 1000);
flexLayout.CrossPlatformArrange(new Rect(Point.Zero, measure));

var view1Frame = flexLayout.Children[0].Frame;
var view2Frame = flexLayout.Children[1].Frame;

var bottomPadding = measure.Height - view2Frame.Bottom;
var topPadding = view1Frame.Y;
var expectedView1Height = measure.Height - (topPadding + bottomPadding + view2.Height);
var expectedView2Height = measure.Height - (topPadding + bottomPadding + view1.Height);

// Assert
Assert.Equal(padding, bottomPadding);
Assert.Equal(view2.Height, view2Frame.Height);
Assert.Equal(expectedView1Height, view1Frame.Height);
Assert.Equal(expectedView2Height, view2Frame.Height);
}

[Theory]
[InlineData(double.PositiveInfinity, 400, FlexDirection.RowReverse)]
[InlineData(double.PositiveInfinity, 400, FlexDirection.Row)]
Expand Down
26 changes: 21 additions & 5 deletions src/Core/src/Layouts/FlexLayoutManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ public FlexLayoutManager(IFlexLayout flexLayout)

public Size ArrangeChildren(Rect bounds)
{
FlexLayout.Layout(bounds.Width, bounds.Height);
var padding = FlexLayout.Padding;

double top = padding.Top + bounds.Top;
double left = padding.Left + bounds.Left;
double availableWidth = bounds.Width - padding.HorizontalThickness;
double availableHeight = bounds.Height - padding.VerticalThickness;

FlexLayout.Layout(availableWidth, availableHeight);

foreach (var child in FlexLayout)
{
Expand All @@ -25,7 +32,8 @@ public Size ArrangeChildren(Rect bounds)
|| double.IsNaN(frame.Width)
|| double.IsNaN(frame.Height))
throw new Exception("something is deeply wrong");
frame = frame.Offset(bounds.Left, bounds.Top);

frame = frame.Offset(left, top);
child.Arrange(frame);
}

Expand All @@ -34,10 +42,15 @@ public Size ArrangeChildren(Rect bounds)

public Size Measure(double widthConstraint, double heightConstraint)
{
var padding = FlexLayout.Padding;

var availableWidth = widthConstraint - padding.HorizontalThickness;
var availableHeight = heightConstraint - padding.VerticalThickness;

double measuredHeight = 0;
double measuredWidth = 0;

FlexLayout.Layout(widthConstraint, heightConstraint);
FlexLayout.Layout(availableWidth, availableHeight);

foreach (var child in FlexLayout)
{
Expand All @@ -51,8 +64,11 @@ public Size Measure(double widthConstraint, double heightConstraint)
measuredWidth = Math.Max(measuredWidth, frame.Right);
}

var finalHeight = LayoutManager.ResolveConstraints(heightConstraint, FlexLayout.Height, measuredHeight, FlexLayout.MinimumHeight, FlexLayout.MaximumHeight);
var finalWidth = LayoutManager.ResolveConstraints(widthConstraint, FlexLayout.Width, measuredWidth, FlexLayout.MinimumWidth, FlexLayout.MaximumWidth);
var finalHeight = LayoutManager.ResolveConstraints(heightConstraint, FlexLayout.Height, measuredHeight + padding.VerticalThickness,
FlexLayout.MinimumHeight, FlexLayout.MaximumHeight);

var finalWidth = LayoutManager.ResolveConstraints(widthConstraint, FlexLayout.Width, measuredWidth + padding.HorizontalThickness,
FlexLayout.MinimumWidth, FlexLayout.MaximumWidth);

return new Size(finalWidth, finalHeight);
}
Expand Down

0 comments on commit ddab6b3

Please sign in to comment.