Add Style

This commit is contained in:
Frank A. Krueger 2017-06-18 12:52:51 -07:00
parent 692187e0cb
commit 3f1b44d855
3 changed files with 75 additions and 6 deletions

View File

@ -85,12 +85,11 @@ namespace Ooui
protected bool SetProperty<T> (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<T>.Default.Equals (backingStore, newValue))
return false;
backingStore = newValue;
SendSet (attributeName, newValue);
return true;
}
public const char IdPrefix = '\u2999';

45
Ooui/Style.cs Normal file
View File

@ -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<string, Value> properties =
new Dictionary<string, Value> ();
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<Value>.Default.Equals (old, value))
return;
}
properties[propertyName] = value;
}
PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName));
}
}
}

25
Tests/StyleTests.cs Normal file
View File

@ -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);
}
}
}