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

Adjust flex item position to account for reversal when laying out unconstrained #13936

Merged
merged 3 commits into from
May 15, 2023
Merged
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
30 changes: 27 additions & 3 deletions src/Controls/tests/Core.UnitTests/Layouts/FlexLayoutTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.Maui.Graphics;
using System;
using System.Transactions;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Layouts;
using NSubstitute;
using Xunit;
Expand Down Expand Up @@ -91,10 +93,14 @@ public void FlexLayoutRecognizesVisibilityChange()
* depending on the target platform.
*/

(IFlexLayout, IView) SetUpUnconstrainedTest()
(IFlexLayout, IView) SetUpUnconstrainedTest(Action<FlexLayout> configure = null)
{
var root = new Grid(); // FlexLayout requires a parent, at least for now
var flexLayout = new FlexLayout() as IFlexLayout;

var controlsFlexLayout = new FlexLayout();
configure?.Invoke(controlsFlexLayout);

var flexLayout = controlsFlexLayout as IFlexLayout;

var view = Substitute.For<IView>();
var size = new Size(100, 100);
Expand Down Expand Up @@ -129,5 +135,23 @@ public void UnconstrainedWidthChildrenHaveWidth()

Assert.Equal(100, flexFrame.Width);
}

[Theory]
[InlineData(double.PositiveInfinity, 400, FlexDirection.RowReverse)]
[InlineData(double.PositiveInfinity, 400, FlexDirection.Row)]
[InlineData(400, double.PositiveInfinity, FlexDirection.ColumnReverse)]
[InlineData(400, double.PositiveInfinity, FlexDirection.Column)]
public void UnconstrainedMeasureHonorsFlexDirection(double widthConstraint, double heightConstraint,
FlexDirection flexDirection)
{
(var flexLayout, var view) = SetUpUnconstrainedTest((fl) => { fl.Direction = flexDirection; });

_ = flexLayout.CrossPlatformMeasure(widthConstraint, heightConstraint);

var flexFrame = flexLayout.GetFlexFrame(view);

Assert.Equal(0, flexFrame.X);
Assert.Equal(0, flexFrame.Y);
}
}
}
22 changes: 22 additions & 0 deletions src/Core/src/Layouts/Flex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,28 @@ static void layout_items(Item item, int child_begin, int child_end, int children

layout.lines_sizes += line.size;
}

if (layout.reverse && layout.size_dim == 0)
{
// Handle reversed layouts when there was no fixed size in the first place. All of the positions will be flipped
// across the axis. Luckily the pos variable is already tracking how far negative the values were in this situation,
// so we can just offset the distance by that amount and get the desired value

for (int i = child_begin; i < child_end; i++)
{
Item child = layout.child_at(item, i);
if (!child.IsVisible)
continue;

if (child.Position == Position.Absolute)
{
// Not helpful for this
continue;
}

child.Frame[layout.frame_pos_i] = child.Frame[layout.frame_pos_i] - pos;
}
}
}

static float absolute_size(float val, float pos1, float pos2, float dim) =>
Expand Down