From ec19a0876e925a98083a15e8ae5009e22875d789 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Thu, 20 Apr 2023 21:22:36 -0400 Subject: [PATCH] Cleanup Points collection usage, make it use IList --- src/Avalonia.Base/Media/PolyLineSegment.cs | 10 +++++----- src/Avalonia.Base/Media/PolylineGeometry.cs | 18 +++++++++--------- src/Avalonia.Base/Points.cs | 14 +++++++++++++- src/Avalonia.Controls/Shapes/Polygon.cs | 16 +++++++++++----- src/Avalonia.Controls/Shapes/Polyline.cs | 15 ++++++++++----- 5 files changed, 48 insertions(+), 25 deletions(-) diff --git a/src/Avalonia.Base/Media/PolyLineSegment.cs b/src/Avalonia.Base/Media/PolyLineSegment.cs index 5c48c11e19..d17a621348 100644 --- a/src/Avalonia.Base/Media/PolyLineSegment.cs +++ b/src/Avalonia.Base/Media/PolyLineSegment.cs @@ -10,8 +10,8 @@ namespace Avalonia.Media /// /// Defines the property. /// - public static readonly StyledProperty PointsProperty - = AvaloniaProperty.Register(nameof(Points)); + public static readonly StyledProperty> PointsProperty + = AvaloniaProperty.Register>(nameof(Points)); /// /// Gets or sets the points. @@ -19,7 +19,7 @@ namespace Avalonia.Media /// /// The points. /// - public Points Points + public IList Points { get => GetValue(PointsProperty); set => SetValue(PointsProperty, value); @@ -37,9 +37,9 @@ namespace Avalonia.Media /// Initializes a new instance of the class. /// /// The points. - public PolyLineSegment(IEnumerable points) : this() + public PolyLineSegment(IEnumerable points) { - Points.AddRange(points); + Points = new Points(points); } protected internal override void ApplyTo(StreamGeometryContext ctx) diff --git a/src/Avalonia.Base/Media/PolylineGeometry.cs b/src/Avalonia.Base/Media/PolylineGeometry.cs index dd3c298b5b..b0229b6455 100644 --- a/src/Avalonia.Base/Media/PolylineGeometry.cs +++ b/src/Avalonia.Base/Media/PolylineGeometry.cs @@ -14,8 +14,8 @@ namespace Avalonia.Media /// /// Defines the property. /// - public static readonly DirectProperty PointsProperty = - AvaloniaProperty.RegisterDirect(nameof(Points), g => g.Points, (g, f) => g.Points = f); + public static readonly DirectProperty> PointsProperty = + AvaloniaProperty.RegisterDirect>(nameof(Points), g => g.Points, (g, f) => g.Points = f); /// /// Defines the property. @@ -23,13 +23,13 @@ namespace Avalonia.Media public static readonly StyledProperty IsFilledProperty = AvaloniaProperty.Register(nameof(IsFilled)); - private Points _points; + private IList _points; private IDisposable? _pointsObserver; static PolylineGeometry() { AffectsGeometry(IsFilledProperty); - PointsProperty.Changed.AddClassHandler((s, e) => s.OnPointsChanged(e.NewValue as Points)); + PointsProperty.Changed.AddClassHandler((s, e) => s.OnPointsChanged(e.NewValue as IList)); } /// @@ -43,9 +43,9 @@ namespace Avalonia.Media /// /// Initializes a new instance of the class. /// - public PolylineGeometry(IEnumerable points, bool isFilled) : this() + public PolylineGeometry(IEnumerable points, bool isFilled) { - Points.AddRange(points); + _points = new Points(points); IsFilled = isFilled; } @@ -56,7 +56,7 @@ namespace Avalonia.Media /// The points. /// [Content] - public Points Points + public IList Points { get => _points; set => SetAndRaise(PointsProperty, ref _points, value); @@ -97,10 +97,10 @@ namespace Avalonia.Media return geometry; } - private void OnPointsChanged(Points? newValue) + private void OnPointsChanged(IList? newValue) { _pointsObserver?.Dispose(); - _pointsObserver = newValue?.ForEachItem( + _pointsObserver = (newValue as IAvaloniaList)?.ForEachItem( _ => InvalidateGeometry(), _ => InvalidateGeometry(), InvalidateGeometry); diff --git a/src/Avalonia.Base/Points.cs b/src/Avalonia.Base/Points.cs index b655dbcb38..2f88ecd80f 100644 --- a/src/Avalonia.Base/Points.cs +++ b/src/Avalonia.Base/Points.cs @@ -1,6 +1,18 @@ +using System.Collections.Generic; using Avalonia.Collections; namespace Avalonia { - public sealed class Points : AvaloniaList { } + public sealed class Points : AvaloniaList + { + public Points() + { + + } + + public Points(IEnumerable points) : base(points) + { + + } + } } diff --git a/src/Avalonia.Controls/Shapes/Polygon.cs b/src/Avalonia.Controls/Shapes/Polygon.cs index 1a9dc42490..78def84448 100644 --- a/src/Avalonia.Controls/Shapes/Polygon.cs +++ b/src/Avalonia.Controls/Shapes/Polygon.cs @@ -1,21 +1,27 @@ +using System.Collections.Generic; using Avalonia.Media; namespace Avalonia.Controls.Shapes { public class Polygon : Shape { - public static readonly StyledProperty PointsProperty = - AvaloniaProperty.Register("Points"); + public static readonly StyledProperty> PointsProperty = + AvaloniaProperty.Register>("Points"); static Polygon() { AffectsGeometry(PointsProperty); } - public Points? Points + public Polygon() { - get { return GetValue(PointsProperty); } - set { SetValue(PointsProperty, value); } + Points = new Points(); + } + + public IList Points + { + get => GetValue(PointsProperty); + set => SetValue(PointsProperty, value); } protected override Geometry CreateDefiningGeometry() diff --git a/src/Avalonia.Controls/Shapes/Polyline.cs b/src/Avalonia.Controls/Shapes/Polyline.cs index 95ab880142..2533794f89 100644 --- a/src/Avalonia.Controls/Shapes/Polyline.cs +++ b/src/Avalonia.Controls/Shapes/Polyline.cs @@ -5,8 +5,8 @@ namespace Avalonia.Controls.Shapes { public class Polyline: Shape { - public static readonly StyledProperty PointsProperty = - AvaloniaProperty.Register("Points"); + public static readonly StyledProperty> PointsProperty = + AvaloniaProperty.Register>("Points"); static Polyline() { @@ -14,10 +14,15 @@ namespace Avalonia.Controls.Shapes AffectsGeometry(PointsProperty); } - public Points? Points + public Polyline() { - get { return GetValue(PointsProperty); } - set { SetValue(PointsProperty, value); } + Points = new Points(); + } + + public IList Points + { + get => GetValue(PointsProperty); + set => SetValue(PointsProperty, value); } protected override Geometry CreateDefiningGeometry()