Skip to content

fix: Workaround a lifecycle bug to allow ContentControl to be measured properly when not in visual tree - #14368

Closed
Youssef1313 wants to merge 5 commits into
unoplatform:masterfrom
Youssef1313:fix-cc-measure
Closed

Youssef1313 wants to merge 5 commits into
unoplatform:masterfrom
Youssef1313:fix-cc-measure

Conversation

@Youssef1313

@Youssef1313 Youssef1313 commented Nov 9, 2023 •

Copy link
Copy Markdown
Member

GitHub Issue (If applicable): Related #7271

PR Type

What kind of change does this PR introduce?

What is the current behavior?

What is the new behavior?

Copilot Summary

🤖 Generated by Copilot at 5b95944

This pull request fixes a unit test failure on Skia by overriding the MeasureOverride method of ContentControl to handle a null first child. This is a temporary workaround for a bug in the ContentControl measurement logic on Skia.

PR Checklist

Please check if your PR fulfills the following requirements:

Other information

Internal Issue (If applicable):

@github-actions github-actions Bot added the area/skia ✏️ Categorizes an issue or PR as relevant to Skia label Nov 9, 2023
@Youssef1313 Youssef1313 mentioned this pull request Nov 9, 2023
11 of 13 tasks
@Youssef1313
Youssef1313 marked this pull request as ready for review November 9, 2023 10:38
protected override Size MeasureOverride(Size availableSize) => base.MeasureOverride(availableSize);
protected override Size MeasureOverride(Size availableSize)
{
// This is the same as Control.MeasureOverride, except that we use Content if FindFirstChild is null.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you create an issue to track this issue specifically? Just so we can include it in lifecycle fixes backlog

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MartinZikmund I guess this is already #190 ?

The issue mentions:

On Windows the template of a control is being applied only when this control is in the visual tree and either we explicitly invoke the ApplyTemplate method, or we wait for the first measure pass.

The case I'm dealing with here is the "wait for the first measure pass". I'll add a link to the issue in code comments.

Comment on lines +37 to +38
var child = this.FindFirstChild() ?? (Content as UIElement);
return child != null ? MeasureElement(child, availableSize) : new Size(0, 0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeromelaban This test also fails in WinUI. However, there is another NaN-measuring test that doesn't fail in WinUI. So I still need to figure that out 👀

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeromelaban The exception on WinUI is a weird "COMException". It happens when Measure is called from C# with NaN. However, if Measure is called with NaN somewhere in the C++ code, it is allowed.

I think it should be acceptable to modify this test and expect no exception be thrown?

To see this behavior on WinUI, use this code

            var grid = new Grid();
            var SUT = new MyControl02();
            SUT.Content = new Grid();
            grid.Children.Add(SUT);
            SUT.BaseAvailableSize = new Size(double.NaN, double.NaN);
            grid.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

and

    public partial class MyControl02 : ContentControl
    {
        public List<Size> MeasureOverrides { get; } = new List<Size>();

        public Size? BaseAvailableSize;

        protected override Size MeasureOverride(Size availableSize)
        {
            MeasureOverrides.Add(availableSize);
            //(Content as UIElement).Measure(BaseAvailableSize ?? availableSize);
            //return (Content as UIElement).DesiredSize;
            return base.MeasureOverride(BaseAvailableSize ?? availableSize);
        }
    }

The above code will work without any crashes/exceptions.
However, base.MeasureOverride will end up calling ContentControl.MeasureOverride which in turn will call Measure with NaN.

Now, if you remove the base.MeasureOverride line and uncomment the two lines above it, it will crash.

image

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was likely present to align with some original behavior. Did you take a look at why this test was originally added?

Otherwise, if adjusting the test matches more with what WinUI does, I'm good with it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Youssef1313
Youssef1313 marked this pull request as draft November 14, 2023 06:09
@Youssef1313
Youssef1313 marked this pull request as ready for review November 20, 2023 18:44
@Youssef1313
Youssef1313 marked this pull request as draft November 28, 2023 14:07
@Youssef1313

Copy link
Copy Markdown
Member Author

Making draft. It seems this can get fixed with #14528.

We'll see after #14528 is completed, then we can either close or merge this.

@Youssef1313

Copy link
Copy Markdown
Member Author

Fixed in lifecycle already :)

@Youssef1313
Youssef1313 deleted the fix-cc-measure branch May 20, 2024 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/skia ✏️ Categorizes an issue or PR as relevant to Skia

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants