Ooui-tws-port/Ooui/Style.cs

52 lines
1.6 KiB
C#
Raw Normal View History

2017-06-18 19:52:51 +00:00
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);
}
2017-06-18 20:00:07 +00:00
public Value BackgroundImage {
get => GetProperty ("background-image");
set => SetProperty ("background-image", value);
}
2017-06-18 19:52:51 +00:00
private Value GetProperty (string propertyName)
{
lock (properties) {
Value p;
if (!properties.TryGetValue (propertyName, out p)) {
2017-06-18 20:00:07 +00:00
p = "inherit";
2017-06-18 19:52:51 +00:00
properties[propertyName] = p;
}
return p;
}
}
private void SetProperty (string propertyName, Value value)
{
2017-06-18 20:00:07 +00:00
var safeValue = value ?? "inherit";
2017-06-18 19:52:51 +00:00
lock (properties) {
Value old;
if (properties.TryGetValue (propertyName, out old)) {
2017-06-18 20:00:07 +00:00
if (EqualityComparer<Value>.Default.Equals (old, safeValue))
2017-06-18 19:52:51 +00:00
return;
}
2017-06-18 20:00:07 +00:00
properties[propertyName] = safeValue;
2017-06-18 19:52:51 +00:00
}
PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName));
}
}
}