Skip to content
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

Fix for FlexLayout padding issue #24195

Merged
merged 17 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
36 changes: 36 additions & 0 deletions src/Controls/tests/Core.UnitTests/Layouts/FlexLayoutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ protected override Size MeasureOverride(double widthConstraint, double heightCon
}
}

class TestBoxView : BoxView
{
protected override Size MeasureOverride(double widthConstraint, double heightConstraint)
{
return new Size(40, 20);
}
}

[Fact]
public void FlexLayoutMeasuresImagesUnconstrained()
{
Expand Down Expand Up @@ -133,6 +141,34 @@ public void UnconstrainedWidthChildrenHaveWidth()
Assert.Equal(100, flexFrame.Width);
}

[Fact]
public void FlexLayoutPaddingShouldBeAppliedCorrectly()
{
var padding = 16;
var root = new VerticalStackLayout();

var boxView = new TestBoxView
{
Color = Colors.Red
};

var layout = new FlexLayout
{
Direction = FlexDirection.Column,
BackgroundColor = Colors.LightGreen,
Padding = new Thickness(padding),
HeightRequest = 500,
WidthRequest = 500,
Children = { boxView }
};
root.Add(layout);
var measure = layout.CrossPlatformMeasure(500, 500);
layout.CrossPlatformArrange(new Rect(Point.Zero, measure));

var frame = layout.Children[0].Frame;
Assert.Equal(padding, frame.X);
PureWeen marked this conversation as resolved.
Show resolved Hide resolved
}

[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
Loading