Skip to content

Commit 455d413

Browse files
committed
Added a support for GradientBrushes on Shape.Stroke (#21983)
1 parent 222ca3c commit 455d413

File tree

6 files changed

+124
-4
lines changed

6 files changed

+124
-4
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="Maui.Controls.Sample.Issues.Issue21983"
5+
Title="Issue21983">
6+
7+
<ContentPage.Resources>
8+
<ResourceDictionary>
9+
<LinearGradientBrush x:Key="LinearGradient">
10+
<LinearGradientBrush.GradientStops>
11+
<GradientStop Offset="0" Color="Red" />
12+
<GradientStop Offset="0.5" Color="Blue" />
13+
<GradientStop Offset="0.9" Color="Purple" />
14+
</LinearGradientBrush.GradientStops>
15+
</LinearGradientBrush>
16+
17+
<RadialGradientBrush x:Key="RadialGradient">
18+
<RadialGradientBrush.GradientStops>
19+
<GradientStop Offset="0" Color="Red" />
20+
<GradientStop Offset="0.5" Color="Blue" />
21+
<GradientStop Offset="0.9" Color="Purple" />
22+
</RadialGradientBrush.GradientStops>
23+
</RadialGradientBrush>
24+
</ResourceDictionary>
25+
</ContentPage.Resources>
26+
27+
<VerticalStackLayout Margin="30,0" Spacing="35">
28+
<Path
29+
AutomationId="path"
30+
Fill="Red"
31+
Stroke="{StaticResource LinearGradient}"
32+
StrokeThickness="20">
33+
<Path.Data>
34+
<PathGeometry>
35+
<PathFigureCollection>
36+
<PathFigure StartPoint="0,0">
37+
<ArcSegment
38+
IsLargeArc="False"
39+
Point="120,140"
40+
Size="100,100"
41+
SweepDirection="Clockwise" />
42+
</PathFigure>
43+
</PathFigureCollection>
44+
</PathGeometry>
45+
</Path.Data>
46+
</Path>
47+
48+
<Ellipse
49+
Fill="Red"
50+
HeightRequest="50"
51+
Stroke="{StaticResource RadialGradient}"
52+
StrokeThickness="10"
53+
WidthRequest="50" />
54+
55+
<Ellipse
56+
Fill="{StaticResource RadialGradient}"
57+
HeightRequest="50"
58+
Stroke="Red"
59+
StrokeThickness="10"
60+
WidthRequest="50" />
61+
</VerticalStackLayout>
62+
</ContentPage>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.Maui.Controls;
2+
using Microsoft.Maui.Controls.Xaml;
3+
4+
namespace Maui.Controls.Sample.Issues;
5+
6+
[XamlCompilation(XamlCompilationOptions.Compile)]
7+
[Issue(IssueTracker.Github, 21983, "GradientBrushes are not supported on Shape.Stroke", PlatformAffected.All)]
8+
9+
public partial class Issue21983 : ContentPage
10+
{
11+
public Issue21983()
12+
{
13+
InitializeComponent();
14+
}
15+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.AppiumTests.Issues
6+
{
7+
public class Issue21983 : _IssuesUITest
8+
{
9+
public override string Issue => "GradientBrushes are not supported on Shape.Stroke";
10+
11+
public Issue21983(TestDevice device) : base(device)
12+
{
13+
}
14+
15+
[Test]
16+
public void GradientShouldBeAppliedToStrokes()
17+
{
18+
//It is not supported on Windows yet
19+
this.IgnoreIfPlatforms(new TestDevice[] { TestDevice.Windows });
20+
21+
_ = App.WaitForElement("path");
22+
23+
VerifyScreenshot();
24+
}
25+
}
26+
}

src/Core/src/Graphics/ShapeDrawable.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ void DrawStrokePath(ICanvas canvas, RectF dirtyRect, PathF path)
8383
// Set Stroke
8484
var stroke = ShapeView.Stroke;
8585

86-
// TODO: Add Paint support for Stroke in Microsoft.Maui.Graphics.
87-
// For now, only support a solid color.
86+
// TODO: Add Paint support for Stroke in Microsoft.Maui.Graphics for Windows
8887
canvas.StrokeColor = stroke.ToColor();
8988

9089
// Set StrokeLineCap
@@ -107,6 +106,7 @@ void DrawStrokePath(ICanvas canvas, RectF dirtyRect, PathF path)
107106
var strokeMiterLimit = ShapeView.StrokeMiterLimit;
108107
canvas.MiterLimit = strokeMiterLimit;
109108

109+
canvas.SetFillPaint(stroke, dirtyRect);
110110
canvas.DrawPath(path);
111111

112112
canvas.RestoreState();

src/Graphics/src/Graphics/Platforms/Android/PlatformCanvas.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,10 @@ public override void SubtractFromClip(float x, float y, float width, float heigh
547547
protected override void PlatformDrawPath(PathF aPath)
548548
{
549549
var platformPath = aPath.AsAndroidPath();
550+
if (_shader != null)
551+
{
552+
CurrentState.StrokePaintWithAlpha.SetShader(_shader);
553+
}
550554
_canvas.DrawPath(platformPath, CurrentState.StrokePaintWithAlpha);
551555
platformPath.Dispose();
552556
}

src/Graphics/src/Graphics/Platforms/MaciOS/PlatformCanvas.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,8 +889,21 @@ private CGPath GetPlatformPath(PathF path)
889889
protected override void PlatformDrawPath(PathF path)
890890
{
891891
var platformPath = GetPlatformPath(path);
892-
_context.AddPath(platformPath);
893-
_context.DrawPath(CGPathDrawingMode.Stroke);
892+
893+
if (_gradient != null)
894+
{
895+
FillWithGradient(() =>
896+
{
897+
_context.AddPath(platformPath);
898+
_context.ReplacePathWithStrokedPath();
899+
return true;
900+
});
901+
}
902+
else
903+
{
904+
_context.AddPath(platformPath);
905+
_context.DrawPath(CGPathDrawingMode.Stroke);
906+
}
894907
}
895908

896909
public override void ClipPath(PathF path, WindingMode windingMode = WindingMode.NonZero)

0 commit comments

Comments
 (0)