Skip to content

Commit

Permalink
Fix RTL in AbsoluteLayout (#7309)
Browse files Browse the repository at this point in the history
* Add AbsoluteLayoutManager RTL tests

* First attempt at fix

* Fix up tests and RTL math

* Remove unnecessary using

* Remove unneeded and wrong code
  • Loading branch information
rachelkang committed May 23, 2022
1 parent cf3b399 commit 347751a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Core/src/Layouts/AbsoluteLayoutManager.cs
Expand Up @@ -60,11 +60,14 @@ 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();

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

destination.X += left;
// Figure out where we're starting from (the left edge of the padded area, or the right edge)
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 should be arranged on the right
var expectedRectangle = new Rect(0, 0, 100, 100);

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

0 comments on commit 347751a

Please sign in to comment.