mirror of
https://github.com/DarthAffe/RGB.NET.git
synced 2025-12-12 17:48:31 +00:00
67 lines
2.7 KiB
C#
67 lines
2.7 KiB
C#
using System.ComponentModel;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
namespace RGB.NET.Core
|
|
{
|
|
/// <summary>
|
|
/// Represents a basic bindable class which notifies when a property value changes.
|
|
/// </summary>
|
|
public abstract class AbstractBindable : IBindable
|
|
{
|
|
#region Events
|
|
|
|
/// <summary>
|
|
/// Occurs when a property value changes.
|
|
/// </summary>
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
/// <summary>
|
|
/// Checks if the property already matches the desirec value or needs to be updated.
|
|
/// </summary>
|
|
/// <typeparam name="T">Type of the property.</typeparam>
|
|
/// <param name="storage">Reference to the backing-filed.</param>
|
|
/// <param name="value">Value to apply.</param>
|
|
/// <returns></returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
protected virtual bool RequiresUpdate<T>(ref T storage, T value)
|
|
{
|
|
return !Equals(storage, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks if the property already matches the desired value and updates it if not.
|
|
/// </summary>
|
|
/// <typeparam name="T">Type of the property.</typeparam>
|
|
/// <param name="storage">Reference to the backing-filed.</param>
|
|
/// <param name="value">Value to apply.</param>
|
|
/// <param name="propertyName">Name of the property used to notify listeners. This value is optional
|
|
/// and can be provided automatically when invoked from compilers that support <see cref="CallerMemberNameAttribute"/>.</param>
|
|
/// <returns><c>true</c> if the value was changed, <c>false</c> if the existing value matched the desired value.</returns>
|
|
protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
|
|
{
|
|
if (!this.RequiresUpdate(ref storage, value)) return false;
|
|
|
|
storage = value;
|
|
// ReSharper disable once ExplicitCallerInfoArgument
|
|
this.OnPropertyChanged(propertyName);
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Triggers the <see cref="PropertyChanged"/>-event when a a property value has changed.
|
|
/// </summary>
|
|
/// <param name="propertyName">Name of the property used to notify listeners. This value is optional
|
|
/// and can be provided automatically when invoked from compilers that support <see cref="CallerMemberNameAttribute"/>.</param>
|
|
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
|
{
|
|
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|