Skip to content

Commit

Permalink
feat: Added CompositionLinearGradientBrush implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
VitezslavImrysek committed Jul 30, 2021
1 parent 38db8d6 commit ab93de0
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 16 deletions.
20 changes: 7 additions & 13 deletions src/Uno.UI/UI/Xaml/Shapes/Shape.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Uno.Disposables;
using System.IO.Compression;
using SkiaSharp;
using Windows.Devices.Usb;
using System.Numerics;

namespace Windows.UI.Xaml.Shapes
Expand Down Expand Up @@ -81,15 +80,8 @@ private void UpdateFill()

_pathSpriteShape.FillBrush = null;

switch(Fill)
{
case SolidColorBrush scb:
_fillSubscription.Disposable =
Brush.AssignAndObserveBrush(scb, c => _pathSpriteShape.FillBrush = Visual.Compositor.CreateColorBrush(c));
break;
case GradientBrush gb:
break;
}
_fillSubscription.Disposable =
Brush.AssignAndObserveBrush(Fill, Visual.Compositor, compositionBrush => _pathSpriteShape.FillBrush = compositionBrush);
}
}

Expand All @@ -103,12 +95,14 @@ private void UpdateStrokeThickness()

private void UpdateStroke()
{
var brush = Stroke as SolidColorBrush ?? SolidColorBrushHelper.Transparent;

if (_pathSpriteShape != null)
{
_strokeSubscription.Disposable = null;

_pathSpriteShape.StrokeBrush = null;

_strokeSubscription.Disposable =
Brush.AssignAndObserveBrush(brush, c => _pathSpriteShape.StrokeBrush = Visual.Compositor.CreateColorBrush(c));
Brush.AssignAndObserveBrush(Stroke, Visual.Compositor, compositionBrush => _pathSpriteShape.StrokeBrush = compositionBrush);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#pragma warning disable 114 // new keyword hiding
namespace Windows.UI.Composition
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false
[global::Uno.NotImplemented]
#endif
public partial class CompositionLinearGradientBrush : global::Windows.UI.Composition.CompositionGradientBrush
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public global::System.Numerics.Vector2 StartPoint
{
Expand All @@ -21,7 +21,7 @@ public partial class CompositionLinearGradientBrush : global::Windows.UI.Compos
}
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public global::System.Numerics.Vector2 EndPoint
{
Expand Down
30 changes: 30 additions & 0 deletions src/Uno.UWP/UI/Composition/CompositionLinearGradientBrush.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#nullable enable

using System.Numerics;

namespace Windows.UI.Composition
{
public partial class CompositionLinearGradientBrush : CompositionGradientBrush
{
private Vector2 _startPoint = Vector2.Zero;
private Vector2 _endPoint = new Vector2(1, 0);

internal CompositionLinearGradientBrush(Compositor compositor)
: base(compositor)
{

}

public Vector2 StartPoint
{
get => _startPoint;
set => SetProperty(ref _startPoint, value);
}

public Vector2 EndPoint
{
get => _endPoint;
set => SetProperty(ref _endPoint, value);
}
}
}
50 changes: 50 additions & 0 deletions src/Uno.UWP/UI/Composition/CompositionLinearGradientBrush.skia.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#nullable enable

using SkiaSharp;

namespace Windows.UI.Composition
{
public partial class CompositionLinearGradientBrush
{
private protected override void UpdatePaintCore(SKPaint paint, SKRect bounds)
{
var startPoint = StartPoint.ToSKPoint();
var endPoint = EndPoint.ToSKPoint();
var transform = CreateTransformMatrix(bounds);

// Transform the points into absolute coordinates.
if (MappingMode == CompositionMappingMode.Relative)
{
// If mapping is relative to bounding box, multiply points by bounds.
startPoint.X *= (float)bounds.Width;
startPoint.Y *= (float)bounds.Height;

endPoint.X *= (float)bounds.Width;
endPoint.Y *= (float)bounds.Height;
}

// Translate gradient points by bounds offset.
startPoint.X += bounds.Left;
startPoint.Y += bounds.Top;

endPoint.X += bounds.Left;
endPoint.Y += bounds.Top;
//

// Create linear gradient shader.
var shader = SKShader.CreateLinearGradient(
startPoint, endPoint,
Colors, ColorPositions,
TileMode, transform);

// Clean up old shader
if (paint.Shader != null)
{
paint.Shader.Dispose();
paint.Shader = null;
}

paint.Shader = shader;
}
}
}

0 comments on commit ab93de0

Please sign in to comment.