Skip to content

Commit 1812419

Browse files
committed
Added a support for GradientBrushes on Shape.Stroke (#21983)
1 parent a730258 commit 1812419

File tree

8 files changed

+124
-4
lines changed

8 files changed

+124
-4
lines changed
38.8 KB
Loading
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+
#if !WINDOWS
2+
using NUnit.Framework;
3+
using UITest.Appium;
4+
using UITest.Core;
5+
6+
namespace Microsoft.Maui.AppiumTests.Issues
7+
{
8+
public class Issue21983 : _IssuesUITest
9+
{
10+
public override string Issue => "GradientBrushes are not supported on Shape.Stroke";
11+
12+
public Issue21983(TestDevice device) : base(device)
13+
{
14+
}
15+
16+
[Test]
17+
[Category(UITestCategories.Shape)]
18+
public void GradientShouldBeAppliedToStrokes()
19+
{
20+
_ = App.WaitForElement("path");
21+
22+
VerifyScreenshot();
23+
}
24+
}
25+
}
26+
#endif
65.5 KB
Loading

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
@@ -891,8 +891,21 @@ private CGPath GetPlatformPath(PathF path)
891891
protected override void PlatformDrawPath(PathF path)
892892
{
893893
var platformPath = GetPlatformPath(path);
894-
_context.AddPath(platformPath);
895-
_context.DrawPath(CGPathDrawingMode.Stroke);
894+
895+
if (_gradient != null)
896+
{
897+
FillWithGradient(() =>
898+
{
899+
_context.AddPath(platformPath);
900+
_context.ReplacePathWithStrokedPath();
901+
return true;
902+
});
903+
}
904+
else
905+
{
906+
_context.AddPath(platformPath);
907+
_context.DrawPath(CGPathDrawingMode.Stroke);
908+
}
896909
}
897910

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

0 commit comments

Comments
 (0)