From 556858ffb9ccb22d1640943c4a613aee5f7e3396 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Tue, 14 Mar 2023 14:17:50 -0600 Subject: [PATCH 1/3] Adjust flex item position to account for reversal when laying out unconstrained Fixes #7756 --- src/Core/src/Layouts/Flex.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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) => From bea5f250f2adb935439f6a53707412a4be132aff Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Mon, 10 Apr 2023 12:40:29 -0600 Subject: [PATCH 2/3] Add automated tests --- .../Core.UnitTests/Layouts/FlexLayoutTests.cs | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Controls/tests/Core.UnitTests/Layouts/FlexLayoutTests.cs b/src/Controls/tests/Core.UnitTests/Layouts/FlexLayoutTests.cs index 15934ee07889..569de708f3c9 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); + } } } From 9dd9632c57ae5ff10d2c21ee559a4fa4f99731a9 Mon Sep 17 00:00:00 2001 From: GitHub Actions Autoformatter Date: Mon, 10 Apr 2023 18:45:15 +0000 Subject: [PATCH 3/3] Auto-format source code --- src/Controls/tests/Core.UnitTests/Layouts/FlexLayoutTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controls/tests/Core.UnitTests/Layouts/FlexLayoutTests.cs b/src/Controls/tests/Core.UnitTests/Layouts/FlexLayoutTests.cs index 569de708f3c9..558ebb7b9222 100644 --- a/src/Controls/tests/Core.UnitTests/Layouts/FlexLayoutTests.cs +++ b/src/Controls/tests/Core.UnitTests/Layouts/FlexLayoutTests.cs @@ -144,7 +144,7 @@ public void UnconstrainedWidthChildrenHaveWidth() public void UnconstrainedMeasureHonorsFlexDirection(double widthConstraint, double heightConstraint, FlexDirection flexDirection) { - (var flexLayout, var view) = SetUpUnconstrainedTest((fl)=> { fl.Direction = flexDirection; }) ; + (var flexLayout, var view) = SetUpUnconstrainedTest((fl) => { fl.Direction = flexDirection; }); _ = flexLayout.CrossPlatformMeasure(widthConstraint, heightConstraint);