Browse Source

Implement DashStyle equality.

pull/2744/head
Steven Kirk 7 years ago
parent
commit
e235efe388
  1. 56
      src/Avalonia.Visuals/Media/DashStyle.cs
  2. 12
      src/Avalonia.Visuals/Media/Immutable/ImmutableDashStyle.cs
  3. 22
      tests/Avalonia.Visuals.UnitTests/Media/PenTests.cs

56
src/Avalonia.Visuals/Media/DashStyle.cs

@ -9,7 +9,7 @@ namespace Avalonia.Media
/// <summary>
/// Represents the sequence of dashes and gaps that will be applied by a <see cref="Pen"/>.
/// </summary>
public class DashStyle : Animatable, IDashStyle, IAffectsRender
public class DashStyle : Animatable, IDashStyle, IAffectsRender, IEquatable<IDashStyle>
{
/// <summary>
/// Defines the <see cref="Dashes"/> property.
@ -105,10 +105,64 @@ namespace Avalonia.Media
/// </summary>
public event EventHandler Invalidated;
/// <inheritdoc/>
public override bool Equals(object obj) => DashEquals(this, obj as IDashStyle);
/// <inheritdoc/>
public bool Equals(IDashStyle other) => DashEquals(this, other);
/// <inheritdoc/>
public override int GetHashCode() => GetHashCode(this);
/// <summary>
/// Returns an immutable clone of the <see cref="DashStyle"/>.
/// </summary>
/// <returns></returns>
public ImmutableDashStyle ToImmutable() => new ImmutableDashStyle(Dashes, Offset);
internal static bool DashEquals(IDashStyle a, IDashStyle b)
{
if (ReferenceEquals(a, b))
{
return true;
}
else if ((a is null && !(b is null)) || (b is null && !(a is null)))
{
return false;
}
if (a.Offset != b.Offset)
{
return false;
}
if (ReferenceEquals(a.Dashes, b.Dashes))
{
return true;
}
if ((a.Dashes is null && !(b.Dashes is null)) || (b.Dashes is null && !(a.Dashes is null)))
{
return false;
}
return a.Dashes.SequenceEqual(b.Dashes);
}
internal static int GetHashCode(IDashStyle style)
{
var hashCode = 717868523;
hashCode = hashCode * -1521134295 + style.Offset.GetHashCode();
if (style.Dashes != null)
{
foreach (var i in style.Dashes)
{
hashCode = hashCode * -1521134295 + i.GetHashCode();
}
}
return hashCode;
}
}
}

12
src/Avalonia.Visuals/Media/Immutable/ImmutableDashStyle.cs

@ -8,7 +8,7 @@ namespace Avalonia.Media.Immutable
/// Represents the sequence of dashes and gaps that will be applied by an
/// <see cref="ImmutablePen"/>.
/// </summary>
public class ImmutableDashStyle : IDashStyle
public class ImmutableDashStyle : IDashStyle, IEquatable<IDashStyle>
{
/// <summary>
/// Initializes a new instance of the <see cref="ImmutableDashStyle"/> class.
@ -26,5 +26,15 @@ namespace Avalonia.Media.Immutable
/// <inheritdoc/>
public double Offset { get; }
/// <inheritdoc/>
public override bool Equals(object obj) => DashStyle.DashEquals(this, obj as IDashStyle);
/// <inheritdoc/>
public bool Equals(IDashStyle other) => DashStyle.DashEquals(this, other);
/// <inheritdoc/>
public override int GetHashCode() => DashStyle.GetHashCode(this);
}
}

22
tests/Avalonia.Visuals.UnitTests/Media/PenTests.cs

@ -65,5 +65,27 @@ namespace Avalonia.Visuals.UnitTests.Media
Assert.True(Equals(target1, target2));
}
[Fact]
public void Equality_Is_Implemented_Between_Mutable_And_Immutable_DashStyles()
{
var brush = new SolidColorBrush(Colors.Red);
var target1 = new Pen(
brush: brush,
thickness: 2,
dashStyle: new DashStyle(new[] { 0.1, 0.2 }, 5),
lineCap: PenLineCap.Round,
lineJoin: PenLineJoin.Round,
miterLimit: 21);
var target2 = new ImmutablePen(
brush: brush,
thickness: 2,
dashStyle: new ImmutableDashStyle(new[] { 0.1, 0.2 }, 5),
lineCap: PenLineCap.Round,
lineJoin: PenLineJoin.Round,
miterLimit: 21);
Assert.True(Equals(target1, target2));
}
}
}

Loading…
Cancel
Save