Repository navigation
Fix FlexLayout measuring items wider than their arranged size #36416
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
Changes from all commits
f3a5325
6006e25
a55888a
906d852
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -528,6 +528,45 @@ void AddFlexItem(int index, IView child) | |
| sizeConstraints.Width = (inMeasureMode && sizeConstraints.Width == 0) ? double.PositiveInfinity : sizeConstraints.Width; | ||
| sizeConstraints.Height = (inMeasureMode && sizeConstraints.Height == 0) ? double.PositiveInfinity : sizeConstraints.Height; | ||
|
|
||
| // Fix for #27520: GetConstraints() returns the whole container size, but an | ||
| // item can never be arranged larger than that container minus its own | ||
| // margins — grow/shrink only redistribute space within those bounds. | ||
| // Measuring with the wider constraint lets wrapping content (e.g. Label | ||
| // text) wrap against a width it will never receive at arrange time, and | ||
| // the overflowing line ends up cut off. Tighten each axis by the item's | ||
| // margins first... | ||
| // (Note: IView.Measure subtracts the margin from this constraint once more | ||
| // before the platform measures. That mirrors the arrange path, where the | ||
| // engine frame is already reduced by the margin and ComputeFrame subtracts | ||
| // it again from the bounds. What matters for wrapping content is that the | ||
| // two paths agree on the final size.) | ||
| if (!double.IsPositiveInfinity(sizeConstraints.Width)) | ||
| { | ||
| sizeConstraints.Width = Math.Max(0, sizeConstraints.Width - (it.MarginLeft + it.MarginRight)); | ||
| } | ||
|
|
||
| if (!double.IsPositiveInfinity(sizeConstraints.Height)) | ||
| { | ||
| sizeConstraints.Height = Math.Max(0, sizeConstraints.Height - (it.MarginTop + it.MarginBottom)); | ||
| } | ||
|
|
||
| // ...and when the engine has already fixed the final cross-axis size (the | ||
| // incoming w/h, e.g. a stretched item or an explicit size request), measure | ||
| // with exactly that. Only the cross axis is safe to clamp from w/h: the | ||
| // main-axis value at this point is the pre-flex basis (explicit size or 0), | ||
| // and grow/shrink can still change it before the item is arranged. | ||
| bool mainAxisIsHorizontal = Direction == FlexDirection.Row || Direction == FlexDirection.RowReverse; | ||
|
|
||
| if (!mainAxisIsHorizontal && !float.IsNaN(w) && w > 0 && w < sizeConstraints.Width) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This still leaves a Row/main-axis shrink case where wrapping content can be measured wider than the frame it is arranged into. In A focused regression test for this would fail on the current PR with class GreedyWidthConstraintRecordingView : View
{
public double MeasuredWidthConstraint { get; private set; } = double.NaN;
protected override Size MeasureOverride(double widthConstraint, double heightConstraint)
{
MeasuredWidthConstraint = widthConstraint;
return new Size(double.IsPositiveInfinity(widthConstraint) ? 1000 : widthConstraint, Math.Min(50, heightConstraint));
}
}
[Fact]
public void ShrunkRowItemWithMarginIsMeasuredAtArrangedWidth_Issue27520()
{
var root = new Grid();
var controlsFlexLayout = new FlexLayout { Direction = FlexDirection.Row };
var flexLayout = controlsFlexLayout as IFlexLayout;
var view = new GreedyWidthConstraintRecordingView { Margin = new Thickness(10, 0, 10, 0) };
root.Add(controlsFlexLayout);
flexLayout.Add(view as IView);
_ = flexLayout.CrossPlatformMeasure(400, 200);
flexLayout.CrossPlatformArrange(new Rect(0, 0, 400, 200));
var flexFrame = flexLayout.GetFlexFrame(view as IView);
Assert.Equal(380, flexFrame.Width);
Assert.Equal(flexFrame.Width, view.MeasuredWidthConstraint);
}Can we extend the fix to cover this Row shrink path as well?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, extended the fix to cover this. Rather than special-casing the shrink path, the constraint derivation now applies the general bound: an item can never be arranged larger than its container minus its own margins, and grow/shrink only redistribute space within that bound. So both container-derived constraints are tightened by the item's margins before the cross-axis clamp is applied. This keeps the grow case from the previous round correct (GrowingItemMainAxisMeasureConstraintIsNotClampedToBasis_Issue27520 still passes, since the constraint is container − margins, not the pre-flex basis), and your Row shrink scenario now measures at the width it's arranged at. I added your regression test as-is, thanks for writing it. Confirmed it failed on the previous revision with exactly Expected: 380, Actual: 400, and passes now. All Flex unit tests and the full Controls.Core.UnitTests suite pass. Tested in the Android emulator and no text cut off or other visual issues after over 50 regenerations of random text.
|
||
| { | ||
| sizeConstraints.Width = w; | ||
| } | ||
|
|
||
| if (mainAxisIsHorizontal && !float.IsNaN(h) && h > 0 && h < sizeConstraints.Height) | ||
| { | ||
| sizeConstraints.Height = h; | ||
| } | ||
|
|
||
| if (child is Image) | ||
| { | ||
| // This is a hack to get FlexLayout to behave like it did in Forms | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -399,5 +399,179 @@ public void GrowItemsPreserveNaturalSizeAndDistributeFreeSpaceEqually_Issue34464 | |
| Assert.Equal(125, frame1.Width); | ||
| Assert.Equal(175, frame2.Width); | ||
| } | ||
|
|
||
| // Records the constraints passed to MeasureOverride so tests can compare them | ||
| // with the size the view is ultimately arranged at. | ||
| class ConstraintRecordingView : View | ||
| { | ||
| public double MeasuredWidthConstraint { get; private set; } = double.NaN; | ||
| public double MeasuredHeightConstraint { get; private set; } = double.NaN; | ||
|
|
||
| protected override Size MeasureOverride(double widthConstraint, double heightConstraint) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[major] Regression Prevention and Test Coverage — This test double overrides
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point — added. HandlerBackedItemWithMarginIsMeasuredAtArrangedWidth_Issue27520 is a [Theory] over Column and Row using a plain View with a mocked IViewHandler (Substitute.For), so measurement flows through the production VisualElement.MeasureOverride → LayoutExtensions.ComputeDesiredSize path, including its margin subtraction. It asserts the width the handler is asked to measure at equals the Frame.Width the view is arranged into (360 for a 400 container with Margin=10: 400 − flex item margin 20 − ComputeDesiredSize margin 20, matching arrange's engine frame 380 − ComputeFrame margin 20). There's no double-subtraction discrepancy for handler-backed controls — measure and arrange agree, which this test now locks in. |
||
| { | ||
| MeasuredWidthConstraint = widthConstraint; | ||
| MeasuredHeightConstraint = heightConstraint; | ||
| return new Size(Math.Min(100, widthConstraint), Math.Min(50, heightConstraint)); | ||
| } | ||
| } | ||
|
|
||
| // Regression test for https://github.com/dotnet/maui/issues/27520 | ||
| // A stretched item with a margin must be measured at the width it will be arranged at | ||
| // (the container width minus its margins), not at the full container width. Otherwise | ||
| // wrapping content (e.g. Label text) wraps against a width the item never receives, | ||
| // and the overflow is cut off at arrange time. | ||
| [Fact] | ||
| public void StretchedItemWithMarginIsMeasuredAtArrangedWidth_Issue27520() | ||
| { | ||
| var root = new Grid(); | ||
| var controlsFlexLayout = new FlexLayout { Direction = FlexDirection.Column }; | ||
| var flexLayout = controlsFlexLayout as IFlexLayout; | ||
| var view = new ConstraintRecordingView { Margin = new Thickness(10) }; | ||
|
|
||
| root.Add(controlsFlexLayout); | ||
| flexLayout.Add(view as IView); | ||
|
|
||
| _ = flexLayout.CrossPlatformMeasure(400, 600); | ||
| flexLayout.CrossPlatformArrange(new Rect(0, 0, 400, 600)); | ||
|
|
||
| // The bounds the child is arranged at (its flex frame) must be the same width | ||
| // the child was measured with; otherwise wrapping content gets cut off. | ||
| // Before the fix the child was measured at the full container width (400), | ||
| // ignoring its own margins, but arranged 20 narrower. | ||
| var flexFrame = flexLayout.GetFlexFrame(view as IView); | ||
| Assert.Equal(380, view.MeasuredWidthConstraint); | ||
| Assert.Equal(flexFrame.Width, view.MeasuredWidthConstraint); | ||
| } | ||
|
|
||
| // Regression test for https://github.com/dotnet/maui/issues/27520 (Row direction: | ||
| // the cross axis is vertical, so the height constraint is the one at risk). | ||
| [Fact] | ||
| public void StretchedItemWithMarginIsMeasuredAtArrangedHeight_Issue27520() | ||
| { | ||
| var root = new Grid(); | ||
| var controlsFlexLayout = new FlexLayout { Direction = FlexDirection.Row }; | ||
| var flexLayout = controlsFlexLayout as IFlexLayout; | ||
| var view = new ConstraintRecordingView { Margin = new Thickness(10) }; | ||
|
|
||
| root.Add(controlsFlexLayout); | ||
| flexLayout.Add(view as IView); | ||
|
|
||
| _ = flexLayout.CrossPlatformMeasure(600, 400); | ||
| flexLayout.CrossPlatformArrange(new Rect(0, 0, 600, 400)); | ||
|
|
||
| // The bounds the child is arranged at (its flex frame) must be the same height | ||
| // the child was measured with; otherwise wrapping content gets cut off. | ||
| // Before the fix the child was measured at the full container height (400), | ||
| // ignoring its own margins, but arranged 20 shorter. | ||
| var flexFrame = flexLayout.GetFlexFrame(view as IView); | ||
| Assert.Equal(380, view.MeasuredHeightConstraint); | ||
| Assert.Equal(flexFrame.Height, view.MeasuredHeightConstraint); | ||
| } | ||
|
|
||
| // Companion to the #27520 fix: the main-axis value passed to the measure callback | ||
| // is the pre-flex basis (explicit size or 0), and grow/shrink can still change it | ||
| // before the item is arranged. It must NOT be used to clamp the measure constraint; | ||
| // a growing item would otherwise be measured at its basis (100) even though it is | ||
| // arranged at the grown size (300). | ||
| [Fact] | ||
| public void GrowingItemMainAxisMeasureConstraintIsNotClampedToBasis_Issue27520() | ||
| { | ||
| var root = new Grid(); | ||
| var controlsFlexLayout = new FlexLayout { Direction = FlexDirection.Row }; | ||
| var flexLayout = controlsFlexLayout as IFlexLayout; | ||
| var view = new ConstraintRecordingView { WidthRequest = 100 }; | ||
|
|
||
| FlexLayout.SetGrow(view, 1); | ||
|
|
||
| root.Add(controlsFlexLayout); | ||
| flexLayout.Add(view as IView); | ||
|
|
||
| _ = flexLayout.CrossPlatformMeasure(300, 200); | ||
| flexLayout.CrossPlatformArrange(new Rect(0, 0, 300, 200)); | ||
|
|
||
| // The item grows to fill the 300px main axis... | ||
| var flexFrame = flexLayout.GetFlexFrame(view as IView); | ||
| Assert.Equal(300, flexFrame.Width); | ||
|
|
||
| // ...so its measure constraint must not have been clamped to the 100px basis. | ||
| Assert.Equal(300, view.MeasuredWidthConstraint); | ||
| } | ||
|
|
||
| // Simulates content that greedily fills whatever width it is given (like wrapping text). | ||
| class GreedyWidthConstraintRecordingView : View | ||
| { | ||
| public double MeasuredWidthConstraint { get; private set; } = double.NaN; | ||
|
|
||
| protected override Size MeasureOverride(double widthConstraint, double heightConstraint) | ||
| { | ||
| MeasuredWidthConstraint = widthConstraint; | ||
| return new Size(double.IsPositiveInfinity(widthConstraint) ? 1000 : widthConstraint, Math.Min(50, heightConstraint)); | ||
| } | ||
| } | ||
|
|
||
| // Regression test for https://github.com/dotnet/maui/issues/27520 (Row main axis: | ||
| // an item with horizontal margins must not be measured at the full container width | ||
| // and then shrunk below it at arrange time; its margins bound the space it can get). | ||
| [Fact] | ||
| public void ShrunkRowItemWithMarginIsMeasuredAtArrangedWidth_Issue27520() | ||
| { | ||
| var root = new Grid(); | ||
| var controlsFlexLayout = new FlexLayout { Direction = FlexDirection.Row }; | ||
| var flexLayout = controlsFlexLayout as IFlexLayout; | ||
| var view = new GreedyWidthConstraintRecordingView { Margin = new Thickness(10, 0, 10, 0) }; | ||
|
|
||
| root.Add(controlsFlexLayout); | ||
| flexLayout.Add(view as IView); | ||
|
|
||
| _ = flexLayout.CrossPlatformMeasure(400, 200); | ||
| flexLayout.CrossPlatformArrange(new Rect(0, 0, 400, 200)); | ||
|
|
||
| var flexFrame = flexLayout.GetFlexFrame(view as IView); | ||
|
|
||
| Assert.Equal(380, flexFrame.Width); | ||
| Assert.Equal(flexFrame.Width, view.MeasuredWidthConstraint); | ||
| } | ||
|
|
||
| // Regression test for https://github.com/dotnet/maui/issues/27520, exercising the | ||
| // real production measure path: a plain View with a (mocked) handler goes through | ||
| // VisualElement.MeasureOverride -> LayoutExtensions.ComputeDesiredSize, which | ||
| // subtracts the margin from the constraint before the handler measures — and | ||
| // ComputeFrame subtracts the margin from the arrange bounds symmetrically. The | ||
| // width the platform (handler) measures at must equal the width of the frame the | ||
| // view ends up arranged into, or wrapping platform content gets cut off. | ||
| [Theory] | ||
| [InlineData(FlexDirection.Column)] // width is the cross axis (stretch path) | ||
| [InlineData(FlexDirection.Row)] // width is the main axis (margin/shrink path) | ||
| public void HandlerBackedItemWithMarginIsMeasuredAtArrangedWidth_Issue27520(FlexDirection direction) | ||
| { | ||
| var root = new Grid(); | ||
| var controlsFlexLayout = new FlexLayout { Direction = direction }; | ||
| var flexLayout = controlsFlexLayout as IFlexLayout; | ||
|
|
||
| double handlerMeasuredWidth = double.NaN; | ||
| var handler = Substitute.For<IViewHandler>(); | ||
| handler.GetDesiredSize(Arg.Any<double>(), Arg.Any<double>()) | ||
| .Returns(args => | ||
| { | ||
| // Greedy, like wrapping text: consume whatever width is offered | ||
| handlerMeasuredWidth = args.ArgAt<double>(0); | ||
| return new Size(handlerMeasuredWidth, 50); | ||
| }); | ||
|
|
||
| var view = new View { Margin = new Thickness(10), Handler = handler }; | ||
|
|
||
| root.Add(controlsFlexLayout); | ||
| flexLayout.Add(view as IView); | ||
|
|
||
| _ = flexLayout.CrossPlatformMeasure(400, 600); | ||
| flexLayout.CrossPlatformArrange(new Rect(0, 0, 400, 600)); | ||
|
|
||
| // The width the platform control is measured at must equal the width of the | ||
| // content frame it is arranged into. Both subtractions happen on both paths: | ||
| // measure = 400 - flex item margin (20) - ComputeDesiredSize margin (20) = 360; | ||
| // arrange = engine frame (380) - ComputeFrame margin (20) = 360. | ||
| Assert.Equal(view.Frame.Width, handlerMeasuredWidth); | ||
| Assert.Equal(360, handlerMeasuredWidth); | ||
| } | ||
| } | ||
| } | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[major] Layout Measure-Arrange Correctness —
child.Measure(...)already subtracts the child margin for normal controls viaVisualElement.MeasureOverride→LayoutExtensions.ComputeDesiredSizebefore it calls the handler. Pre-subtractingit.MarginLeft + it.MarginRighthere means a realLabel/ButtonwithMargin=10in a 400-wide FlexLayout is measured by its handler at 360 instead of the 380 width it will occupy after FlexLayout applies margins. That makes wrapping content wrap too early/leave blank space. The fix should clamp FlexLayout against the space the child will occupy without also pre-subtracting the margin before callingMeasure, or otherwise avoid double-applying the same margin for handler-backed controls.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The premise here isn't quite right: with Margin=10 in a 400-wide FlexLayout, the child does not occupy 380 — it occupies 360. FlexLayout passes the engine-computed frame (already reduced to 380 by the engine's margin handling) directly to child.Arrange, and Arrange bounds are margin-inclusive by convention, so ComputeFrame subtracts the margin again — the content frame comes out at 360. That double application at arrange is long-standing FlexLayout behavior (the engine also offsets the position by the margin, which is why flex children have always sat at 2× their margin offset), and changing it would visibly shift every existing FlexLayout layout, so this PR doesn't touch it.
Given that, the measure path in this PR is symmetric with arrange, not double-subtracting: Measure(400 − 20 = 380) → ComputeDesiredSize subtracts the margin → the handler measures at 360, which is exactly the content width it is arranged into. Without the pre-subtraction the handler would measure at 380 but be arranged into 360 — which is the bug this PR fixes.
I've added a handler-backed regression test (HandlerBackedItemWithMarginIsMeasuredAtArrangedWidth_Issue27520, Column + Row) that exercises the real ComputeDesiredSize path with a mocked IViewHandler and asserts the handler's measure width equals the arranged Frame.Width (360 == 360), and a comment at the fix site documenting the measure/arrange symmetry.