diff --git a/src/Controls/tests/Core.UnitTests/Layouts/FlexLayoutTests.cs b/src/Controls/tests/Core.UnitTests/Layouts/FlexLayoutTests.cs index 15934ee07889..558ebb7b9222 100644 --- a/src/Controls/tests/Core.UnitTests/Layouts/FlexLayoutTests.cs +++ b/src/Controls/tests/Core.UnitTests/Layouts/FlexLayoutTests.cs @@ -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; @@ -91,10 +93,14 @@ public void FlexLayoutRecognizesVisibilityChange() * depending on the target platform. */ - (IFlexLayout, IView) SetUpUnconstrainedTest() + (IFlexLayout, IView) SetUpUnconstrainedTest(Action 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(); var size = new Size(100, 100); @@ -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); + } } } diff --git a/src/Core/src/Layouts/Flex.cs b/src/Core/src/Layouts/Flex.cs index 09cd596483fa..381462e1c5fc 100644 --- a/src/Core/src/Layouts/Flex.cs +++ b/src/Core/src/Layouts/Flex.cs @@ -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) =>