Added change-detection to gradients

This commit is contained in:
DarthAffe committed 2018-02-03 21:02:36 +01:00
1 parent 04735cc37d
commit 9563198419
3 files changed
+62 -2

No files matched your search

+40 -1
View File
@@ -2,7 +2,10 @@
// ReSharper disable MemberCanBePrivate.Global // ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global // ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq; using System.Linq;
using RGB.NET.Core; using RGB.NET.Core;
@@ -36,13 +39,23 @@ namespace RGB.NET.Brushes.Gradients
#endregion #endregion
#region Events
/// <inheritdoc />
public event EventHandler GradientChanged;
#endregion
#region Constructors #region Constructors
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="AbstractGradient"/> class. /// Initializes a new instance of the <see cref="AbstractGradient"/> class.
/// </summary> /// </summary>
protected AbstractGradient() protected AbstractGradient()
{ } {
GradientStops.CollectionChanged += GradientCollectionChanged;
PropertyChanged += (sender, args) => OnGradientChanged();
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="AbstractGradient"/> class. /// Initializes a new instance of the <see cref="AbstractGradient"/> class.
@@ -50,6 +63,9 @@ namespace RGB.NET.Brushes.Gradients
/// <param name="gradientStops">The stops with which the gradient should be initialized.</param> /// <param name="gradientStops">The stops with which the gradient should be initialized.</param>
protected AbstractGradient(params GradientStop[] gradientStops) protected AbstractGradient(params GradientStop[] gradientStops)
{ {
GradientStops.CollectionChanged += GradientCollectionChanged;
PropertyChanged += (sender, args) => OnGradientChanged();
foreach (GradientStop gradientStop in gradientStops) foreach (GradientStop gradientStop in gradientStops)
GradientStops.Add(gradientStop); GradientStops.Add(gradientStop);
} }
@@ -63,6 +79,9 @@ namespace RGB.NET.Brushes.Gradients
{ {
this.WrapGradient = wrapGradient; this.WrapGradient = wrapGradient;
GradientStops.CollectionChanged += GradientCollectionChanged;
PropertyChanged += (sender, args) => OnGradientChanged();
foreach (GradientStop gradientStop in gradientStops) foreach (GradientStop gradientStop in gradientStops)
GradientStops.Add(gradientStop); GradientStops.Add(gradientStop);
} }
@@ -106,6 +125,26 @@ namespace RGB.NET.Brushes.Gradients
gradientStop.Offset += 1; gradientStop.Offset += 1;
} }
/// <summary>
/// Should be called to indicate that the gradient was changed.
/// </summary>
protected void OnGradientChanged() => GradientChanged?.Invoke(this, null);
private void GradientCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.OldItems != null)
foreach (GradientStop gradientStop in e.OldItems)
gradientStop.PropertyChanged -= GradientStopChanged;
if (e.NewItems != null)
foreach (GradientStop gradientStop in e.NewItems)
gradientStop.PropertyChanged += GradientStopChanged;
OnGradientChanged();
}
private void GradientStopChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs) => OnGradientChanged();
#endregion #endregion
} }
} }
+7 -1
View File
@@ -1,4 +1,5 @@
using RGB.NET.Core; using System;
using RGB.NET.Core;
namespace RGB.NET.Brushes.Gradients namespace RGB.NET.Brushes.Gradients
{ {
@@ -8,6 +9,11 @@ namespace RGB.NET.Brushes.Gradients
/// </summary> /// </summary>
public interface IGradient : IDecoratable<IGradientDecorator> public interface IGradient : IDecoratable<IGradientDecorator>
{ {
/// <summary>
/// Occurs if the <see cref="IGradient"/> is changed.
/// </summary>
event EventHandler GradientChanged;
/// <summary> /// <summary>
/// Gets the <see cref="Color"/> of the <see cref="IGradient"/> on the specified offset. /// Gets the <see cref="Color"/> of the <see cref="IGradient"/> on the specified offset.
/// </summary> /// </summary>
@@ -1,6 +1,7 @@
// ReSharper disable MemberCanBePrivate.Global // ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global // ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
using System;
using RGB.NET.Core; using RGB.NET.Core;
namespace RGB.NET.Brushes.Gradients namespace RGB.NET.Brushes.Gradients
@@ -37,6 +38,13 @@ namespace RGB.NET.Brushes.Gradients
#endregion #endregion
#region Events
/// <inheritdoc />
public event EventHandler GradientChanged;
#endregion
#region Constructors #region Constructors
/// <summary> /// <summary>
@@ -48,6 +56,8 @@ namespace RGB.NET.Brushes.Gradients
{ {
this.StartHue = startHue; this.StartHue = startHue;
this.EndHue = endHue; this.EndHue = endHue;
PropertyChanged += (sender, args) => OnGradientChanged();
} }
#endregion #endregion
@@ -90,6 +100,11 @@ namespace RGB.NET.Brushes.Gradients
} }
} }
/// <summary>
/// Should be called to indicate that the gradient was changed.
/// </summary>
protected void OnGradientChanged() => GradientChanged?.Invoke(this, null);
#endregion #endregion
} }
} }