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 RTL in AbsoluteLayout #7309

Merged
merged 8 commits into from May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Core/src/Layouts/AbsoluteLayoutManager.cs
Expand Up @@ -60,11 +60,17 @@ public override Size ArrangeChildren(Rect bounds)
{
var padding = AbsoluteLayout.Padding;

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

bool leftToRight = AbsoluteLayout.ShouldArrangeLeftToRight();

// Figure out where we're starting from (the left edge of the padded area, or the right edge)
double xPosition = leftToRight ? padding.Left + bounds.Left : bounds.Right - padding.Right;
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to reuse left and right here, since they seem to match?

Copy link
Member Author

Choose a reason for hiding this comment

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

definitely, thanks for catching that!


for (int n = 0; n < AbsoluteLayout.Count; n++)
{
var child = AbsoluteLayout[n];
Expand Down Expand Up @@ -93,7 +99,11 @@ public override Size ArrangeChildren(Rect bounds)
destination.Y = (availableHeight - destination.Height) * destination.Y;
}

destination.X += left;
if (leftToRight)
destination.X += left;
else
destination.X = right - destination.X - destination.Width;

destination.Y += top;

child.Arrange(destination);
Expand Down
42 changes: 42 additions & 0 deletions src/Core/tests/UnitTests/Layouts/AbsoluteLayoutManagerTests.cs
Expand Up @@ -445,5 +445,47 @@ public void ChildMeasureRespectsProportionalBounds()

child.Received().Measure(Arg.Is(expectedWidth * widthConstraint), Arg.Is(expectedHeight * heightConstraint));
}

[Fact(DisplayName = "First View in LTR Absolute Layout is on the left")]
public void LtrShouldHaveFirstItemOnTheLeft()
{
var abs = CreateTestLayout();
var child = CreateTestView();
SubstituteChildren(abs, child);
var childBounds = new Rect(10, 0, 100, 100);
SetLayoutBounds(abs, child, childBounds);

abs.FlowDirection.Returns(FlowDirection.LeftToRight);

var manager = new AbsoluteLayoutManager(abs);
var measuredSize = manager.Measure(double.PositiveInfinity, 100);
manager.ArrangeChildren(new Rect(Point.Zero, measuredSize));

// We expect that the view should be arranged on the left
var expectedRectangle = new Rect(10, 0, 100, 100);

abs[0].Received().Arrange(Arg.Is(expectedRectangle));
}

[Fact(DisplayName = "First View in RTL Absolute Layout is on the right")]
public void RtlShouldHaveFirstItemOnTheRight()
{
var abs = CreateTestLayout();
var child = CreateTestView();
SubstituteChildren(abs, child);
var childBounds = new Rect(10, 0, 100, 100);
SetLayoutBounds(abs, child, childBounds);

abs.FlowDirection.Returns(FlowDirection.RightToLeft);

var manager = new AbsoluteLayoutManager(abs);
var measuredSize = manager.Measure(double.PositiveInfinity, 100);
manager.ArrangeChildren(new Rect(Point.Zero, measuredSize));

// We expect that the view hould be arranged on the right
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// We expect that the view hould be arranged on the right
// We expect that the view should be arranged on the right

Copy link
Member

Choose a reason for hiding this comment

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

phew, glad I was here.
TheOfficeMichaelScottGIF

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed, thank you! 😂

var expectedRectangle = new Rect(0, 0, 100, 100);

abs[0].Received().Arrange(Arg.Is(expectedRectangle));
}
}
}