diff --git a/Ooui/EventTarget.cs b/Ooui/EventTarget.cs index 2b4cfbc..ae366c7 100644 --- a/Ooui/EventTarget.cs +++ b/Ooui/EventTarget.cs @@ -85,12 +85,11 @@ namespace Ooui protected bool SetProperty (ref T backingStore, T newValue, string attributeName, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "") { - if (!backingStore.Equals (newValue)) { - backingStore = newValue; - SendSet (attributeName, newValue); - return true; - } - return false; + if (EqualityComparer.Default.Equals (backingStore, newValue)) + return false; + backingStore = newValue; + SendSet (attributeName, newValue); + return true; } public const char IdPrefix = '\u2999'; diff --git a/Ooui/Style.cs b/Ooui/Style.cs new file mode 100644 index 0000000..e793829 --- /dev/null +++ b/Ooui/Style.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using Value = System.Object; + +namespace Ooui +{ + public class Style : INotifyPropertyChanged + { + public event PropertyChangedEventHandler PropertyChanged; + + readonly Dictionary properties = + new Dictionary (); + + public Value BackgroundColor { + get => GetProperty ("background-color"); + set => SetProperty ("background-color", value); + } + + private Value GetProperty (string propertyName) + { + lock (properties) { + Value p; + if (!properties.TryGetValue (propertyName, out p)) { + p = new Value (); + properties[propertyName] = p; + } + return p; + } + } + + private void SetProperty (string propertyName, Value value) + { + lock (properties) { + Value old; + if (properties.TryGetValue (propertyName, out old)) { + if (EqualityComparer.Default.Equals (old, value)) + return; + } + properties[propertyName] = value; + } + PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName)); + } + } +} diff --git a/Tests/StyleTests.cs b/Tests/StyleTests.cs new file mode 100644 index 0000000..2915d29 --- /dev/null +++ b/Tests/StyleTests.cs @@ -0,0 +1,25 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using Ooui; + +namespace Tests +{ + [TestClass] + public class StyleTests + { + [TestMethod] + public void ChangedWhen () + { + var s = new Style (); + var changeCount = 0; + s.PropertyChanged += (_, e) => changeCount++; + s.BackgroundColor = "red"; + Assert.AreEqual (1, changeCount); + s.BackgroundColor = "blue"; + Assert.AreEqual (2, changeCount); + s.BackgroundColor = "blue"; + Assert.AreEqual (2, changeCount); + } + } +}