From e2e60bb480a746dc19dc0cabec47fb22e75df9d3 Mon Sep 17 00:00:00 2001 From: "H. Utku Maden" Date: Tue, 8 Sep 2026 21:39:20 +0300 Subject: [PATCH] Implement INotifyPropertyChanged for brushes. --- Dashboard.Common/Drawing/Brush.cs | 100 +++++++++++++++++++++++++++--- 1 file changed, 92 insertions(+), 8 deletions(-) diff --git a/Dashboard.Common/Drawing/Brush.cs b/Dashboard.Common/Drawing/Brush.cs index 37a80fd..0d147f5 100644 --- a/Dashboard.Common/Drawing/Brush.cs +++ b/Dashboard.Common/Drawing/Brush.cs @@ -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(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)); + } } }