Browse Source

Cleanup Points collection usage, make it use IList

pull/11073/head
Max Katz 4 years ago
parent
commit
ec19a0876e
  1. 10
      src/Avalonia.Base/Media/PolyLineSegment.cs
  2. 18
      src/Avalonia.Base/Media/PolylineGeometry.cs
  3. 14
      src/Avalonia.Base/Points.cs
  4. 16
      src/Avalonia.Controls/Shapes/Polygon.cs
  5. 15
      src/Avalonia.Controls/Shapes/Polyline.cs

10
src/Avalonia.Base/Media/PolyLineSegment.cs

@ -10,8 +10,8 @@ namespace Avalonia.Media
/// <summary> /// <summary>
/// Defines the <see cref="Points"/> property. /// Defines the <see cref="Points"/> property.
/// </summary> /// </summary>
public static readonly StyledProperty<Points> PointsProperty public static readonly StyledProperty<IList<Point>> PointsProperty
= AvaloniaProperty.Register<PolyLineSegment, Points>(nameof(Points)); = AvaloniaProperty.Register<PolyLineSegment, IList<Point>>(nameof(Points));
/// <summary> /// <summary>
/// Gets or sets the points. /// Gets or sets the points.
@ -19,7 +19,7 @@ namespace Avalonia.Media
/// <value> /// <value>
/// The points. /// The points.
/// </value> /// </value>
public Points Points public IList<Point> Points
{ {
get => GetValue(PointsProperty); get => GetValue(PointsProperty);
set => SetValue(PointsProperty, value); set => SetValue(PointsProperty, value);
@ -37,9 +37,9 @@ namespace Avalonia.Media
/// Initializes a new instance of the <see cref="PolyLineSegment"/> class. /// Initializes a new instance of the <see cref="PolyLineSegment"/> class.
/// </summary> /// </summary>
/// <param name="points">The points.</param> /// <param name="points">The points.</param>
public PolyLineSegment(IEnumerable<Point> points) : this() public PolyLineSegment(IEnumerable<Point> points)
{ {
Points.AddRange(points); Points = new Points(points);
} }
protected internal override void ApplyTo(StreamGeometryContext ctx) protected internal override void ApplyTo(StreamGeometryContext ctx)

18
src/Avalonia.Base/Media/PolylineGeometry.cs

@ -14,8 +14,8 @@ namespace Avalonia.Media
/// <summary> /// <summary>
/// Defines the <see cref="Points"/> property. /// Defines the <see cref="Points"/> property.
/// </summary> /// </summary>
public static readonly DirectProperty<PolylineGeometry, Points> PointsProperty = public static readonly DirectProperty<PolylineGeometry, IList<Point>> PointsProperty =
AvaloniaProperty.RegisterDirect<PolylineGeometry, Points>(nameof(Points), g => g.Points, (g, f) => g.Points = f); AvaloniaProperty.RegisterDirect<PolylineGeometry, IList<Point>>(nameof(Points), g => g.Points, (g, f) => g.Points = f);
/// <summary> /// <summary>
/// Defines the <see cref="IsFilled"/> property. /// Defines the <see cref="IsFilled"/> property.
@ -23,13 +23,13 @@ namespace Avalonia.Media
public static readonly StyledProperty<bool> IsFilledProperty = public static readonly StyledProperty<bool> IsFilledProperty =
AvaloniaProperty.Register<PolylineGeometry, bool>(nameof(IsFilled)); AvaloniaProperty.Register<PolylineGeometry, bool>(nameof(IsFilled));
private Points _points; private IList<Point> _points;
private IDisposable? _pointsObserver; private IDisposable? _pointsObserver;
static PolylineGeometry() static PolylineGeometry()
{ {
AffectsGeometry(IsFilledProperty); AffectsGeometry(IsFilledProperty);
PointsProperty.Changed.AddClassHandler<PolylineGeometry>((s, e) => s.OnPointsChanged(e.NewValue as Points)); PointsProperty.Changed.AddClassHandler<PolylineGeometry>((s, e) => s.OnPointsChanged(e.NewValue as IList<Point>));
} }
/// <summary> /// <summary>
@ -43,9 +43,9 @@ namespace Avalonia.Media
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="PolylineGeometry"/> class. /// Initializes a new instance of the <see cref="PolylineGeometry"/> class.
/// </summary> /// </summary>
public PolylineGeometry(IEnumerable<Point> points, bool isFilled) : this() public PolylineGeometry(IEnumerable<Point> points, bool isFilled)
{ {
Points.AddRange(points); _points = new Points(points);
IsFilled = isFilled; IsFilled = isFilled;
} }
@ -56,7 +56,7 @@ namespace Avalonia.Media
/// The points. /// The points.
/// </value> /// </value>
[Content] [Content]
public Points Points public IList<Point> Points
{ {
get => _points; get => _points;
set => SetAndRaise(PointsProperty, ref _points, value); set => SetAndRaise(PointsProperty, ref _points, value);
@ -97,10 +97,10 @@ namespace Avalonia.Media
return geometry; return geometry;
} }
private void OnPointsChanged(Points? newValue) private void OnPointsChanged(IList<Point>? newValue)
{ {
_pointsObserver?.Dispose(); _pointsObserver?.Dispose();
_pointsObserver = newValue?.ForEachItem( _pointsObserver = (newValue as IAvaloniaList<Point>)?.ForEachItem(
_ => InvalidateGeometry(), _ => InvalidateGeometry(),
_ => InvalidateGeometry(), _ => InvalidateGeometry(),
InvalidateGeometry); InvalidateGeometry);

14
src/Avalonia.Base/Points.cs

@ -1,6 +1,18 @@
using System.Collections.Generic;
using Avalonia.Collections; using Avalonia.Collections;
namespace Avalonia namespace Avalonia
{ {
public sealed class Points : AvaloniaList<Point> { } public sealed class Points : AvaloniaList<Point>
{
public Points()
{
}
public Points(IEnumerable<Point> points) : base(points)
{
}
}
} }

16
src/Avalonia.Controls/Shapes/Polygon.cs

@ -1,21 +1,27 @@
using System.Collections.Generic;
using Avalonia.Media; using Avalonia.Media;
namespace Avalonia.Controls.Shapes namespace Avalonia.Controls.Shapes
{ {
public class Polygon : Shape public class Polygon : Shape
{ {
public static readonly StyledProperty<Points?> PointsProperty = public static readonly StyledProperty<IList<Point>> PointsProperty =
AvaloniaProperty.Register<Polygon, Points?>("Points"); AvaloniaProperty.Register<Polygon, IList<Point>>("Points");
static Polygon() static Polygon()
{ {
AffectsGeometry<Polygon>(PointsProperty); AffectsGeometry<Polygon>(PointsProperty);
} }
public Points? Points public Polygon()
{ {
get { return GetValue(PointsProperty); } Points = new Points();
set { SetValue(PointsProperty, value); } }
public IList<Point> Points
{
get => GetValue(PointsProperty);
set => SetValue(PointsProperty, value);
} }
protected override Geometry CreateDefiningGeometry() protected override Geometry CreateDefiningGeometry()

15
src/Avalonia.Controls/Shapes/Polyline.cs

@ -5,8 +5,8 @@ namespace Avalonia.Controls.Shapes
{ {
public class Polyline: Shape public class Polyline: Shape
{ {
public static readonly StyledProperty<Points?> PointsProperty = public static readonly StyledProperty<IList<Point>> PointsProperty =
AvaloniaProperty.Register<Polyline, Points?>("Points"); AvaloniaProperty.Register<Polyline, IList<Point>>("Points");
static Polyline() static Polyline()
{ {
@ -14,10 +14,15 @@ namespace Avalonia.Controls.Shapes
AffectsGeometry<Polyline>(PointsProperty); AffectsGeometry<Polyline>(PointsProperty);
} }
public Points? Points public Polyline()
{ {
get { return GetValue(PointsProperty); } Points = new Points();
set { SetValue(PointsProperty, value); } }
public IList<Point> Points
{
get => GetValue(PointsProperty);
set => SetValue(PointsProperty, value);
} }
protected override Geometry CreateDefiningGeometry() protected override Geometry CreateDefiningGeometry()

Loading…
Cancel
Save