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

[Windows] Fix for setting IsClippedToBounds="True" inside a Border control will crash on Windows #25506

Merged
merged 8 commits into from
Dec 5, 2024

Conversation

Tamilarasan-Paranthaman
Copy link
Contributor

@Tamilarasan-Paranthaman Tamilarasan-Paranthaman commented Oct 24, 2024

Root Cause

  1. The Clip property is applied to the LayoutPanel, and then Composition.Visual.Clip is applied to the content (LayoutPanel) within the ContentPanel class. As a result, setting both the Clip property and Composition.Visual.Clip on a panel causes an exception.

Description of Change

  • Fixed the issue in the LayoutPanel by skipping the clipping when the parent is a Border. In such cases, ContentPanel will now handle clipping.

Issues Fixed

Fixes #18937

Screenshot

Before Issue Fix After Issue Fix
ClippedToBoundsIssue.mp4
ClippedToBoundsFixed.mp4

@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@Tamilarasan-Paranthaman Tamilarasan-Paranthaman marked this pull request as ready for review November 1, 2024 09:24
@Tamilarasan-Paranthaman Tamilarasan-Paranthaman requested a review from a team as a code owner November 1, 2024 09:24
@PureWeen PureWeen added this to the .NET 9 SR2 milestone Nov 1, 2024
@@ -162,7 +163,7 @@ void UpdateBorder(IShape? strokeShape)
UpdateClip(strokeShape, width, height);
}

void UpdateClip(IShape? borderShape, double width, double height)
async void UpdateClip(IShape? borderShape, double width, double height)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could avoid async void? Because error propagation and also the call stack may not accurately represent the execution flow at the time the exception was thrown, complicating the process of identifying and fixing bugs.

Return a Task and rename the method to UpdateClipAsync.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jsuarezruiz, I have renamed the method to UpdateClipAsync and changed its return type to Task. Could you please check once and let me know if you have any concerns?

@jsuarezruiz
Copy link
Contributor

jsuarezruiz commented Nov 12, 2024

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

jsuarezruiz
jsuarezruiz previously approved these changes Nov 18, 2024
}

void UpdateClip(IShape? borderShape, double width, double height)
async Task UpdateClipAsync(IShape? borderShape, double width, double height)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is the right way to approach this problem. Let me try and ping some WinAppSDK folks and see if they have any suggestions.

In the meantime: the Loaded event should be fired after layout is complete on a Visual -- maybe you can check if that has fired first before assigning the clip?

Copy link
Member

Choose a reason for hiding this comment

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

WinAppSDK folks also suggested to try switching SizeChanged to LayoutUpdated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think this is the right way to approach this problem. Let me try and ping some WinAppSDK folks and see if they have any suggestions.

In the meantime: the Loaded event should be fired after layout is complete on a Visual -- maybe you can check if that has fired first before assigning the clip?

With our PR fix, the Loaded event is fired before assigning the clip, ensuring that the layout is completed on the visual before applying the clip.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

WinAppSDK folks also suggested to try switching SizeChanged to LayoutUpdated

We ensured the functionality by calling the UpdateClip method on the LayoutUpdated event, as LayoutUpdated triggers frequently whenever the layout is updated. However, we only need to update the clip during the initial load and when the size changes. To address this, we added flags to limit updates to these specific scenarios. Despite this, after switching from the SizeChanged event to the LayoutUpdated event, the issue persists.

Copy link
Member

Choose a reason for hiding this comment

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

I've narrowed down the root cause of the issue, and reported it here: https://github.com/microsoft/WindowsAppSDK/issues/4914

We can remove all use of async. To fix this:

In LayoutPanel.cs change ArrangeOverride to use Composition.Visual.Clip:

protected override WSize ArrangeOverride(WSize finalSize)
{
	var actual = base.ArrangeOverride(finalSize);
	var visual = ElementCompositionPreview.GetElementVisual(this);
	visual.Clip = ClipsToBounds ? visual.Compositor.CreateInsetClip() : null;

	return actual;
}

Then, add a check in ContentPanel.cs to ensure the clip isn't overridden:

void UpdateClip(IShape? borderShape, double width, double height)
{
	if (Content is null)
	{
		return;
	}

	if (height <= 0 && width <= 0)
	{
		return;
	}

        // Ensure clip isn't overridden on LayoutPanel
	if (Content is LayoutPanel panel && panel.ClipsToBounds)
	{
		return;
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've narrowed down the root cause of the issue, and reported it here: https://github.com/microsoft/WindowsAppSDK/issues/4914

We can remove all use of async. To fix this:

In LayoutPanel.cs change ArrangeOverride to use Composition.Visual.Clip:

protected override WSize ArrangeOverride(WSize finalSize)
{
	var actual = base.ArrangeOverride(finalSize);
	var visual = ElementCompositionPreview.GetElementVisual(this);
	visual.Clip = ClipsToBounds ? visual.Compositor.CreateInsetClip() : null;

	return actual;
}

Then, add a check in ContentPanel.cs to ensure the clip isn't overridden:

void UpdateClip(IShape? borderShape, double width, double height)
{
	if (Content is null)
	{
		return;
	}

	if (height <= 0 && width <= 0)
	{
		return;
	}

        // Ensure clip isn't overridden on LayoutPanel
	if (Content is LayoutPanel panel && panel.ClipsToBounds)
	{
		return;
	}

As per the suggested fix, I tested it using other shapes in the border, such as Ellipse and RoundRectangle. I observed that clipping was not properly applied due to the clipping logic already being handled in the LayoutPanel and returned in the UpdateClip method of the ContentPanel class.

As a result, the additional clipping code in the UpdateClip based on the border shape was not executed, leading to improper clipping in the UI. To address this, I fixed the issue in the LayoutPanel by skipping the clipping when the parent is a Border. In such cases, clipping will now be handled by the ContentPanel.

Could you please review the changes and let me know if you have any concerns?

@PureWeen PureWeen requested a review from Foda November 22, 2024 16:17
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@sheiksyedm sheiksyedm added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Dec 5, 2024
@jfversluis jfversluis enabled auto-merge (squash) December 5, 2024 13:30
@jfversluis jfversluis merged commit a1cd4bc into dotnet:main Dec 5, 2024
105 checks passed
@samhouts samhouts added fixed-in-9.0.21 fixed-in-net8.0-nightly This may be available in a nightly release! labels Dec 16, 2024
@JohnHDev
Copy link

Is this only fixed in .net 9.0? Most of us are on .NET 8 which is LTS (L means Long btw).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-border Border community ✨ Community Contribution fixed-in-9.0.21 fixed-in-net8.0-nightly This may be available in a nightly release! partner/syncfusion Issues / PR's with Syncfusion collaboration platform/windows 🪟
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[Windows] Setting IsClippedToBounds="True" inside a Border control will crash on Windows
8 participants