-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[iOS] Fix wrong gray color using transparent in iOS gradients #17696
Changes from all commits
7546f35
94d4ac3
b35ed3c
185a6e4
63d803d
a06bcf8
f7969b8
5337c7b
65f30bb
5fb6d92
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,28 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
x:Class="Maui.Controls.Sample.Issues.Issue17366"> | ||
<StackLayout | ||
Padding="12"> | ||
<Label | ||
AutomationId="WaitForStubControl" | ||
Text="Background (Gradient using a transparent color)"/> | ||
<Frame | ||
HeightRequest="200"> | ||
<Frame.Background> | ||
<LinearGradientBrush | ||
StartPoint="0,0" | ||
EndPoint="0,1"> | ||
<GradientStop | ||
Color="Transparent" | ||
Offset="0.0"/> | ||
<GradientStop | ||
Color="Red" | ||
Offset="1.00"/> | ||
</LinearGradientBrush> | ||
</Frame.Background> | ||
<Label | ||
Text="Issue 17366" /> | ||
</Frame> | ||
</StackLayout> | ||
</ContentPage> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Microsoft.Maui.Controls; | ||
using Microsoft.Maui.Controls.Xaml; | ||
using Microsoft.Maui.Platform; | ||
|
||
namespace Maui.Controls.Sample.Issues | ||
{ | ||
[XamlCompilation(XamlCompilationOptions.Compile)] | ||
[Issue(IssueTracker.Github, 17366, "Wrong gray color using transparent in iOS gradients", PlatformAffected.iOS)] | ||
public partial class Issue17366 : ContentPage | ||
{ | ||
public Issue17366() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using NUnit.Framework; | ||
using UITest.Appium; | ||
using UITest.Core; | ||
|
||
namespace Microsoft.Maui.AppiumTests.Issues | ||
{ | ||
public class Issue17366 : _IssuesUITest | ||
{ | ||
public Issue17366(TestDevice device) : base(device) | ||
{ | ||
} | ||
|
||
public override string Issue => "Wrong gray color using transparent in iOS gradients"; | ||
|
||
[Test] | ||
public void Issue17366Test() | ||
{ | ||
this.IgnoreIfPlatforms(new TestDevice[] { TestDevice.Android, TestDevice.Mac, TestDevice.Windows }, | ||
"The bug only happens on iOS; see https://github.com/dotnet/maui/pull/17789"); | ||
|
||
App.WaitForElement("WaitForStubControl"); | ||
VerifyScreenshot(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,7 +69,7 @@ public static partial class PaintExtensions | |
if (linearGradientPaint.GradientStops != null && linearGradientPaint.GradientStops.Length > 0) | ||
{ | ||
var orderedStops = linearGradientPaint.GradientStops.OrderBy(x => x.Offset).ToList(); | ||
linearGradientLayer.Colors = orderedStops.Select(x => x.Color.ToCGColor()).ToArray(); | ||
linearGradientLayer.Colors = GetCAGradientLayerColors(orderedStops); | ||
linearGradientLayer.Locations = GetCAGradientLayerLocations(orderedStops); | ||
} | ||
|
||
|
@@ -96,7 +96,7 @@ public static partial class PaintExtensions | |
if (radialGradientPaint.GradientStops != null && radialGradientPaint.GradientStops.Length > 0) | ||
{ | ||
var orderedStops = radialGradientPaint.GradientStops.OrderBy(x => x.Offset).ToList(); | ||
radialGradientLayer.Colors = orderedStops.Select(x => x.Color.ToCGColor()).ToArray(); | ||
radialGradientLayer.Colors = GetCAGradientLayerColors(orderedStops); | ||
radialGradientLayer.Locations = GetCAGradientLayerLocations(orderedStops); | ||
} | ||
|
||
|
@@ -165,5 +165,30 @@ static NSNumber[] GetCAGradientLayerLocations(List<PaintGradientStop> gradientSt | |
return locations; | ||
} | ||
} | ||
|
||
static CGColor[] GetCAGradientLayerColors(List<PaintGradientStop> gradientStops) | ||
{ | ||
if (gradientStops == null || gradientStops.Count == 0) | ||
return Array.Empty<CGColor>(); | ||
|
||
CGColor[] colors = new CGColor[gradientStops.Count]; | ||
|
||
int index = 0; | ||
foreach (var gradientStop in gradientStops) | ||
{ | ||
if (gradientStop.Color == Colors.Transparent) | ||
{ | ||
var color = gradientStops[index == 0 ? index + 1 : index - 1].Color; | ||
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. What happens if there are multiple transparent together? trans at 0, trans at 0.3 and then red at 1? Also, what happens if the transparent is between two colors? red, trans, green. nit: this feels like a for loop instead of foreach. I personally don't like the rando index variable outside the loop. But I am also just weird. 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. |
||
CGColor nativeColor = color.ToPlatform().ColorWithAlpha(0.0f).CGColor; | ||
colors[index] = nativeColor; | ||
} | ||
else | ||
colors[index] = gradientStop.Color.ToCGColor(); | ||
|
||
index++; | ||
} | ||
|
||
return colors; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are mostly pending changes from xamarin/Xamarin.Forms#13401
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this rather be a reference to the code in Core? Instead of a duplicate? Might be possible if the method was internal.