KEMBAR78
[Windows] Allow to remove shadows at runtime by MartyIX · Pull Request #31452 · dotnet/maui · GitHub
Skip to content

Conversation

@MartyIX
Copy link
Contributor

@MartyIX MartyIX commented Sep 2, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description of Change

This PR attempts to address https://github.com/dotnet/maui/pull/18337/files#r2314339635. That is, it makes sure that shadow is removed from Border when Border.Shadow = null is assigned.

I took inspiration in Android implementation:

partial void ShadowChanged()
{
if (Shadow?.Paint is { } shadowPaint)
{
var context = Context;
var shadowOpacity = Shadow.Opacity;
float radius = context.ToPixels(Shadow.Radius);
float offsetX = context.ToPixels(Shadow.Offset.X);
float offsetY = context.ToPixels(Shadow.Offset.Y);
int paintType;
int[] colors;
float[] positions;
float[] bounds;
switch (shadowPaint)
{
case LinearGradientPaint linearGradientPaint:
var linearGradientData = linearGradientPaint.GetGradientData(shadowOpacity);
paintType = PlatformPaintType.Linear;
colors = linearGradientData.Colors;
positions = linearGradientData.Offsets;
bounds = [linearGradientData.X1, linearGradientData.Y1, linearGradientData.X2, linearGradientData.Y2];
break;
case RadialGradientPaint radialGradientPaint:
var radialGradientData = radialGradientPaint.GetGradientData(shadowOpacity);
paintType = PlatformPaintType.Radial;
colors = radialGradientData.Colors;
positions = radialGradientData.Offsets;
bounds = [radialGradientData.CenterX, radialGradientData.CenterY, radialGradientData.Radius];
break;
case SolidPaint solidPaint:
paintType = PlatformPaintType.Solid;
// If the alpha is set in the color value, the shadow transparency is applied based on that alpha.
// If the Opacity property is set directly, the shadow transparency is applied based on the Opacity.
// If both values are provided, the color alpha is combined with the Opacity to apply a unified transparency effect to the shadow, ensuring consistent behavior across platforms.
colors = [solidPaint.Color.WithAlpha(solidPaint.Color.Alpha * shadowOpacity).ToPlatform().ToArgb()];
positions = null;
bounds = null;
break;
default:
throw new NotSupportedException("Unsupported shadow paint type.");
}
UpdateShadow(paintType, radius, offsetX, offsetY, colors, positions, bounds);
}
else
{
UpdateShadow(PlatformPaintType.None, 0, 0, 0, null, null, null);
}
}

Test effect

My src\Controls\tests\TestCases.WinUI.Tests\snapshots\windows\Issue24414Test_5.png changes like this:

Before After
image image

@MartyIX MartyIX requested a review from a team as a code owner September 2, 2025 09:40
@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Sep 2, 2025
@dotnet-policy-service
Copy link
Contributor

Hey there @@MartyIX! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@MartyIX MartyIX added platform/windows area-drawing Shapes, Borders, Shadows, Graphics, BoxView, custom drawing labels Sep 2, 2025
@MartyIX MartyIX changed the title [Windows] Allow to remove shadows on runtime [Windows] Allow to remove shadows at runtime Sep 2, 2025
@PureWeen
Copy link
Member

PureWeen commented Sep 2, 2025

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

partial void ShadowChanged()
{
if (HasShadow)
if (Shadow?.Paint is { })
Copy link
Contributor Author

@MartyIX MartyIX Sep 2, 2025

Choose a reason for hiding this comment

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

note: You can compare the new snapshot in this PR with the Android one: https://github.com/dotnet/maui/blob/main/src/Controls/tests/TestCases.Android.Tests/snapshots/android/Issue24414Test_5.png. There should be no shadow visible in Issue24414Test_5.png snapshots.

@jsuarezruiz
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@MartyIX
Copy link
Contributor Author

MartyIX commented Sep 3, 2025

It looks like the tests passed. The failing tests are related to Android.

@jfversluis jfversluis changed the base branch from main to inflight/current September 4, 2025 11:24
@jfversluis jfversluis force-pushed the feature/2025-09-02-Windows-remove-shadows branch from 7583191 to 326ee2b Compare September 4, 2025 11:26
@jfversluis jfversluis added this to the .NET 9 SR11 milestone Sep 4, 2025
@jfversluis jfversluis merged commit cc498be into dotnet:inflight/current Sep 4, 2025
1 check passed
@MartyIX MartyIX deleted the feature/2025-09-02-Windows-remove-shadows branch September 4, 2025 11:27
@github-actions github-actions bot locked and limited conversation to collaborators Oct 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-drawing Shapes, Borders, Shadows, Graphics, BoxView, custom drawing community ✨ Community Contribution platform/windows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants