From 17c2b20b9fb331749e8bfb84a7646a4c556b7f33 Mon Sep 17 00:00:00 2001 From: Peter Spada Date: Tue, 10 Sep 2024 16:37:48 -0700 Subject: [PATCH] Clear platform view's background when MAUI's background is null --- .../DeviceTests/Elements/Label/LabelTests.cs | 29 ++++++++++++ .../src/Platform/Android/ViewExtensions.cs | 47 ++++++++----------- src/Core/src/Platform/iOS/ViewExtensions.cs | 11 +---- 3 files changed, 51 insertions(+), 36 deletions(-) diff --git a/src/Controls/tests/DeviceTests/Elements/Label/LabelTests.cs b/src/Controls/tests/DeviceTests/Elements/Label/LabelTests.cs index 922f7b0eb5..c4fe493840 100644 --- a/src/Controls/tests/DeviceTests/Elements/Label/LabelTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/Label/LabelTests.cs @@ -335,6 +335,35 @@ namespace Microsoft.Maui.DeviceTests }); } + [Fact] + public async Task SetBackgroundToNullAffectsRendering() + { + SetupBuilder(); + + Label label; + var layout = new VerticalStackLayout + { + (label = new Label + { + WidthRequest = 200, + HeightRequest = 100, + Text = "Label", + TextColor = Colors.Red, + Background = new SolidColorBrush(Colors.Blue), + }) + }; + + await AttachAndRun(layout, async (handler) => + { + var platformView = handler.ToPlatform(); + await platformView.AssertContainsColor(Colors.Blue, MauiContext); + + label.Background = null; + + await platformView.AssertDoesNotContainColor(Colors.Blue, MauiContext); + }); + } + [Theory( #if WINDOWS Skip = "Fails on Windows" diff --git a/src/Core/src/Platform/Android/ViewExtensions.cs b/src/Core/src/Platform/Android/ViewExtensions.cs index df7b0e3009..4fe354cab4 100644 --- a/src/Core/src/Platform/Android/ViewExtensions.cs +++ b/src/Core/src/Platform/Android/ViewExtensions.cs @@ -271,37 +271,30 @@ namespace Microsoft.Maui.Platform { var paint = background; - if (!paint.IsNullOrEmpty()) - { - // Remove previous background gradient if any - if (platformView.Background is MauiDrawable mauiDrawable) - { - platformView.Background = null; - mauiDrawable.Dispose(); - } + // Remove previous background gradient if any + (platformView.Background as MauiDrawable)?.Dispose(); - if (treatTransparentAsNull && paint.IsTransparent()) - { - // For controls where android treats transparent as null it's more - // performant to just set the background to null instead of - // giving it a transparent color/drawable - platformView.Background = null; - } - else if (paint is SolidPaint solidPaint) - { - if (solidPaint.Color is Color backgroundColor) - platformView.SetBackgroundColor(backgroundColor.ToPlatform()); - } - else - { - if (paint!.ToDrawable(platformView.Context) is Drawable drawable) - platformView.Background = drawable; - } - } - else if (platformView is LayoutViewGroup) + if (paint.IsNullOrEmpty()) { platformView.Background = null; } + else if (treatTransparentAsNull && paint.IsTransparent()) + { + // For controls where android treats transparent as null it's more + // performant to just set the background to null instead of + // giving it a transparent color/drawable + platformView.Background = null; + } + else if (paint is SolidPaint solidPaint) + { + if (solidPaint.Color is Color backgroundColor) + platformView.SetBackgroundColor(backgroundColor.ToPlatform()); + } + else + { + if (paint!.ToDrawable(platformView.Context) is Drawable drawable) + platformView.Background = drawable; + } } public static void UpdateOpacity(this AView platformView, IView view) => platformView.UpdateOpacity(view.Opacity); diff --git a/src/Core/src/Platform/iOS/ViewExtensions.cs b/src/Core/src/Platform/iOS/ViewExtensions.cs index fbb27d7846..6690cf4cfe 100644 --- a/src/Core/src/Platform/iOS/ViewExtensions.cs +++ b/src/Core/src/Platform/iOS/ViewExtensions.cs @@ -77,14 +77,9 @@ namespace Microsoft.Maui.Platform if (paint.IsNullOrEmpty()) { - if (platformView is LayoutView) - platformView.BackgroundColor = null; - else - return; + platformView.BackgroundColor = null; } - - - if (paint is SolidPaint solidPaint) + else if (paint is SolidPaint solidPaint) { Color backgroundColor = solidPaint.Color; @@ -92,8 +87,6 @@ namespace Microsoft.Maui.Platform platformView.BackgroundColor = ColorExtensions.BackgroundColor; else platformView.BackgroundColor = backgroundColor.ToPlatform(); - - return; } else if (paint is GradientPaint gradientPaint) {