Implement INotifyPropertyChanged for brushes.

This commit is contained in:
2026-09-08 21:39:20 +03:00
parent 2d312b2a9f
commit e2e60bb480
+92 -8
View File
@@ -1,27 +1,111 @@
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace Dashboard.Drawing
{
public abstract class Brush
public abstract class Brush : INotifyPropertyChanged, ICloneable
{
public event PropertyChangedEventHandler? PropertyChanged;
public virtual object Clone()
{
return MemberwiseClone();
}
protected void SetField<T>(ref T field, T value, [CallerMemberName] string property = "")
{
field = value;
OnPropertyChanged(property);
}
protected virtual void OnPropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
public class SolidColorBrush(Color color) : Brush
public sealed class SolidColorBrush(Color color) : Brush
{
public Color Color { get; } = color;
}
public class ImageBrush(Image image) : Brush
{
public Image Image { get; set; } = image;
public Box2d TextureCoordinates { get; set; } = new Box2d(0, 0, 1, 1);
public Image Image
{
get;
set => SetField(ref field, value);
} = image;
public Box2d TextureCoordinates
{
get;
set => SetField(ref field, value);
} = new Box2d(0, 0, 1, 1);
}
public class NinePatchImageBrush(Image image) : Brush
{
public Image Image { get; set; } = image;
public Box2d CenterCoordinates { get; set; } = new Box2d(0, 0, 1, 1);
public Vector4 Extents { get; set; } = Vector4.Zero;
public Image Image
{
get;
set => SetField(ref field, value);
} = image;
public Box2d CenterCoordinates
{
get;
set => SetField(ref field, value);
} = new Box2d(0, 0, 1, 1);
public Extents Extents
{
get;
set => SetField(ref field, value);
} = Extents.None;
}
public class GradientBrush : Brush
{
public Gradient Gradient
{
get;
set
{
Unsubscribe(field);
Subscribe(value);
SetField(ref field, value);
}
}
public GradientBrush(Gradient gradient)
{
Gradient = gradient;
}
private void Subscribe(Gradient gradient)
{
gradient.PropertyChanged += GradientPropertyChanged;
gradient.CollectionChanged += GradientCollectionChanged;
}
private void Unsubscribe(Gradient? gradient)
{
gradient?.PropertyChanged -= GradientPropertyChanged;
gradient?.CollectionChanged -= GradientCollectionChanged;
}
private void GradientCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
OnPropertyChanged(nameof(Gradient));
}
private void GradientPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
OnPropertyChanged(nameof(Gradient));
}
}
}