Repository navigation
[iOS, MacCatalyst] Fix Image and ImageButton BackgroundColor does not reset when set to null #36427
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
ccc33da
1d15dca
6f151ae
ad1cc26
340c8f5
5c5c04a
c8e637e
fcf122f
a6d7c5c
845ba0d
478e807
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 |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| namespace Maui.Controls.Sample.Issues; | ||
|
|
||
|
|
||
| [Issue(IssueTracker.Github, 36302, "Image and ImageButton backgrounds not cleared on iOS/MacCatalyst when set to null", | ||
| PlatformAffected.iOS | PlatformAffected.macOS)] | ||
| public class Issue36302 : ContentPage | ||
| { | ||
| public Issue36302() | ||
| { | ||
| var image = new Image | ||
| { | ||
| Source = "dotnet_bot.png", | ||
| HeightRequest = 80, | ||
| WidthRequest = 80, | ||
| BackgroundColor = Colors.Blue, | ||
| AutomationId = "BackgroundImage", | ||
| }; | ||
|
|
||
| var imageButton = new ImageButton | ||
| { | ||
| Source = "dotnet_bot.png", | ||
| HeightRequest = 80, | ||
| WidthRequest = 80, | ||
| BackgroundColor = Colors.Blue, | ||
| AutomationId = "BackgroundImageButton", | ||
| }; | ||
|
|
||
| var clearButton = new Button | ||
| { | ||
| Text = "Clear Backgrounds", | ||
| AutomationId = "ClearBackgroundsButton", | ||
| }; | ||
| clearButton.Clicked += (s, e) => | ||
| { | ||
| image.BackgroundColor = null; | ||
|
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.
[moderate] Regression Prevention and Test Coverage — This test (and the added handler code) only exercises the solid- |
||
| imageButton.BackgroundColor = null; | ||
| }; | ||
|
|
||
| Content = new VerticalStackLayout | ||
| { | ||
| Spacing = 20, | ||
| Margin = new Thickness(20), | ||
| Children = | ||
| { | ||
| image, | ||
| imageButton, | ||
| clearButton, | ||
| } | ||
| }; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| using NUnit.Framework; | ||
| using UITest.Appium; | ||
| using UITest.Core; | ||
|
|
||
| namespace Microsoft.Maui.TestCases.Tests.Issues; | ||
| public class Issue36302 : _IssuesUITest | ||
| { | ||
| public Issue36302(TestDevice testDevice) : base(testDevice) { } | ||
|
|
||
| public override string Issue => "Image and ImageButton backgrounds not cleared on iOS/MacCatalyst when set to null"; | ||
|
|
||
| [Test] | ||
| [Category(UITestCategories.Image)] | ||
|
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
|
||
| public void ImageAndImageButtonBackgroundClearsWhenSetToNull() | ||
| { | ||
| App.WaitForElement("ClearBackgroundsButton"); | ||
| App.Tap("ClearBackgroundsButton"); | ||
| VerifyScreenshot(); | ||
|
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
|
||
| } | ||
|
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
Comment on lines
+12
to
+19
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,15 +18,15 @@ public partial class ImageHandler : IImageHandler | |
| { | ||
| public static IPropertyMapper<IImage, IImageHandler> Mapper = new PropertyMapper<IImage, IImageHandler>(ViewHandler.ViewMapper) | ||
| { | ||
| #if __ANDROID__ || WINDOWS || TIZEN | ||
| [nameof(IImage.Background)] = MapBackground, | ||
| #endif | ||
| #if WINDOWS | ||
| [nameof(IImage.Height)] = MapHeight, | ||
| [nameof(IImage.Width)] = MapWidth, | ||
| #endif | ||
| [nameof(IImage.Aspect)] = MapAspect, | ||
| [nameof(IImage.IsAnimationPlaying)] = MapIsAnimationPlaying, | ||
| #if __ANDROID__ || __IOS__ || MACCATALYST || WINDOWS || TIZEN | ||
|
Contributor
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. do we need #if here? |
||
| [nameof(IImage.Background)] = MapBackground, | ||
| #endif | ||
| [nameof(IImage.Source)] = MapSource, | ||
| }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Maui.Graphics; | ||
| using Microsoft.Maui.Primitives; | ||
| using UIKit; | ||
|
|
||
|
|
@@ -23,7 +24,29 @@ public static void MapBackground(IImageHandler handler, IImage image) | |
| { | ||
| handler.UpdateValue(nameof(IViewHandler.ContainerView)); | ||
|
|
||
| handler.ToPlatform().UpdateBackground(image); | ||
| var platformView = handler.ToPlatform(); | ||
|
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
|
||
|
|
||
| if (image.Background is ImageSourcePaint imagePaint) | ||
|
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.
[minor] Handler Mapper and Property Patterns — Same |
||
| { | ||
| var provider = handler.GetRequiredService<IImageSourceServiceProvider>(); | ||
| platformView.UpdateBackgroundImageSourceAsync(imagePaint.ImageSource, provider) | ||
|
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] Async and Threading Safety — |
||
| .FireAndForget(handler); | ||
| } | ||
| else if (image.Background.IsNullOrEmpty()) | ||
| { | ||
| platformView.RemoveBackgroundLayer(); | ||
| platformView.BackgroundColor = null; | ||
| } | ||
| else | ||
| { | ||
| platformView.UpdateBackground(image); | ||
|
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| // After the container transition (wrapper added or removed), re-run the full | ||
| // opacity mapper so the container receives view.Opacity and the inner UIImageView | ||
| // is reset to alpha 1. Using UpdateOpacity(image) directly would only set the | ||
| // container alpha, leaving the inner view at its previous alpha and compounding it. | ||
| handler.UpdateValue(nameof(IView.Opacity)); | ||
| } | ||
|
|
||
| public static void MapAspect(IImageHandler handler, IImage image) => | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| using System; | ||
| using Microsoft.Maui.Graphics; | ||
| using UIKit; | ||
|
|
||
| namespace Microsoft.Maui.Handlers | ||
|
|
@@ -46,6 +47,31 @@ protected override void DisconnectHandler(UIButton platformView) | |
| SourceLoader.Reset(); | ||
| } | ||
|
|
||
| //TODO : Make it public in NET 11. | ||
| internal static void MapBackground(IImageButtonHandler handler, IImageButton imageButton) | ||
|
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.
[moderate] Public API Surface / Handler Mapper Patterns — This |
||
| { | ||
| var platformView = handler.PlatformView; | ||
|
|
||
| if (platformView is null) | ||
| return; | ||
|
|
||
| if (imageButton.Background is ImageSourcePaint imagePaint) | ||
|
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.
[moderate] Handler Mapper and Property Patterns / Complexity Reduction — This re-implements the |
||
| { | ||
| var provider = handler.GetRequiredService<IImageSourceServiceProvider>(); | ||
| platformView.UpdateBackgroundImageSourceAsync(imagePaint.ImageSource, provider) | ||
|
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
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] Async and Threading Safety — This fire-and-forget image-background load is not ordered against later |
||
| .FireAndForget(handler); | ||
| } | ||
| else if (imageButton.Background.IsNullOrEmpty()) | ||
| { | ||
| platformView.RemoveBackgroundLayer(); | ||
| platformView.BackgroundColor = UIColor.Clear; | ||
|
Comment on lines
+66
to
+67
|
||
| } | ||
| else | ||
| { | ||
| platformView.UpdateBackground(imageButton); | ||
|
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
|
|
||
| public static void MapStrokeColor(IImageButtonHandler handler, IButtonStroke buttonStroke) | ||
| { | ||
| handler.PlatformView?.UpdateStrokeColor(buttonStroke); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.