Ooui-tws-port/Ooui/Style.cs

434 lines
12 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> ();
static readonly private char[] numberChars = new char[] {
'0','1','2','3','4','5','6','7','8','9',
'.','-','+',
};
2017-11-09 21:03:56 +00:00
2017-06-18 21:11:09 +00:00
public Value AlignSelf {
get => this["align-self"];
set => this["align-self"] = value;
}
2017-06-18 19:52:51 +00:00
public Value BackgroundColor {
2017-06-18 20:30:22 +00:00
get => this["background-color"];
set => this["background-color"] = value;
2017-06-18 19:52:51 +00:00
}
2017-06-18 20:00:07 +00:00
public Value BackgroundImage {
2017-06-18 20:30:22 +00:00
get => this["background-image"];
set => this["background-image"] = value;
2017-06-18 20:00:07 +00:00
}
2017-06-18 20:23:51 +00:00
public Value BorderTopColor {
2017-06-18 20:30:22 +00:00
get => this["border-top-color"];
set => this["border-top-color"] = value;
2017-06-18 20:23:51 +00:00
}
public Value BorderRightColor {
2017-06-18 20:30:22 +00:00
get => this["border-right-color"];
set => this["border-right-color"] = value;
2017-06-18 20:23:51 +00:00
}
public Value BorderBottomColor {
2017-06-18 20:30:22 +00:00
get => this["border-bottom-color"];
set => this["border-bottom-color"] = value;
2017-06-18 20:23:51 +00:00
}
public Value BorderLeftColor {
2017-06-18 20:30:22 +00:00
get => this["border-left-color"];
set => this["border-left-color"] = value;
2017-06-18 20:23:51 +00:00
}
public Value BorderColor {
2017-06-18 20:30:22 +00:00
get => this["border-top-color"];
2017-06-18 20:23:51 +00:00
set {
2017-06-18 20:30:22 +00:00
this["border-top-color"] = value;
this["border-right-color"] = value;
this["border-bottom-color"] = value;
this["border-left-color"] = value;
2017-06-18 20:23:51 +00:00
}
}
public Value BorderTopStyle {
2017-06-18 20:30:22 +00:00
get => this["border-top-style"];
set => this["border-top-style"] = value;
2017-06-18 20:23:51 +00:00
}
public Value BorderRightStyle {
2017-06-18 20:30:22 +00:00
get => this["border-right-style"];
set => this["border-right-style"] = value;
2017-06-18 20:23:51 +00:00
}
public Value BorderBottomStyle {
2017-06-18 20:30:22 +00:00
get => this["border-bottom-style"];
set => this["border-bottom-style"] = value;
2017-06-18 20:23:51 +00:00
}
public Value BorderLeftStyle {
2017-06-18 20:30:22 +00:00
get => this["border-left-style"];
set => this["border-left-style"] = value;
2017-06-18 20:23:51 +00:00
}
public Value BorderStyle {
2017-06-18 20:30:22 +00:00
get => this["border-top-style"];
2017-06-18 20:23:51 +00:00
set {
2017-06-18 20:30:22 +00:00
this["border-top-style"] = value;
this["border-right-style"] = value;
this["border-bottom-style"] = value;
this["border-left-style"] = value;
2017-06-18 20:23:51 +00:00
}
}
public Value BorderTopWidth {
2017-06-18 20:30:22 +00:00
get => this["border-top-width"];
set => this["border-top-width"] = value;
2017-06-18 20:23:51 +00:00
}
public Value BorderRightWidth {
2017-06-18 20:30:22 +00:00
get => this["border-right-width"];
set => this["border-right-width"] = value;
2017-06-18 20:23:51 +00:00
}
public Value BorderBottomWidth {
2017-06-18 20:30:22 +00:00
get => this["border-bottom-width"];
set => this["border-bottom-width"] = value;
2017-06-18 20:23:51 +00:00
}
public Value BorderLeftWidth {
2017-06-18 20:30:22 +00:00
get => this["border-left-width"];
set => this["border-left-width"] = value;
2017-06-18 20:23:51 +00:00
}
2017-11-10 00:09:48 +00:00
public Value BorderRadius {
get => this["border-radius"];
set {
this["border-radius"] = value;
}
}
2017-06-18 20:23:51 +00:00
public Value BorderWidth {
2017-06-18 20:30:22 +00:00
get => this["border-top-width"];
2017-06-18 20:23:51 +00:00
set {
2017-06-18 20:30:22 +00:00
this["border-top-width"] = value;
this["border-right-width"] = value;
this["border-bottom-width"] = value;
this["border-left-width"] = value;
2017-06-18 20:23:51 +00:00
}
}
public Value Bottom {
2017-06-18 20:30:22 +00:00
get => this["bottom"];
set => this["bottom"] = AddNumberUnits (value, "px");
2017-06-18 20:23:51 +00:00
}
public Value Clear {
2017-06-18 20:30:22 +00:00
get => this["clear"];
set => this["clear"] = value;
2017-06-18 20:23:51 +00:00
}
public Value Color {
2017-06-18 20:30:22 +00:00
get => this["color"];
set => this["color"] = value;
2017-06-18 20:23:51 +00:00
}
public Value Cursor {
2017-06-18 20:30:22 +00:00
get => this["cursor"];
set => this["cursor"] = value;
2017-06-18 20:23:51 +00:00
}
2017-06-18 21:11:09 +00:00
public Value Display {
get => this["display"];
set => this["display"] = value;
}
public Value FlexFlow {
get => this["flex-flow"];
set => this["flex-flow"] = value;
}
public Value FlexGrow {
get => this["flex-grow"];
set => this["flex-grow"] = value;
}
public Value FlexShrink {
get => this["flex-shrink"];
set => this["flex-shrink"] = value;
}
2017-06-18 20:23:51 +00:00
public Value Float {
2017-06-18 20:30:22 +00:00
get => this["float"];
set => this["float"] = value;
2017-06-18 20:23:51 +00:00
}
public Value FontFamily {
2017-06-18 20:30:22 +00:00
get => this["font-family"];
set => this["font-family"] = value;
2017-06-18 20:23:51 +00:00
}
public Value FontSize {
2017-06-18 20:30:22 +00:00
get => this["font-size"];
set => this["font-size"] = value;
2017-06-18 20:23:51 +00:00
}
public Value FontStyle {
2017-06-18 20:30:22 +00:00
get => this["font-style"];
set => this["font-style"] = value;
2017-06-18 20:23:51 +00:00
}
public Value FontVariant {
2017-06-18 20:30:22 +00:00
get => this["font-variant"];
set => this["font-variant"] = value;
2017-06-18 20:23:51 +00:00
}
public Value FontWeight {
2017-06-18 20:30:22 +00:00
get => this["font-weight"];
set => this["font-weight"] = value;
2017-06-18 20:23:51 +00:00
}
public Value Height {
2017-06-18 20:30:22 +00:00
get => this["height"];
set => this["height"] = AddNumberUnits (value, "px");
2017-06-18 20:23:51 +00:00
}
public Value Left {
2017-06-18 20:30:22 +00:00
get => this["left"];
set => this["left"] = AddNumberUnits (value, "px");
2017-06-18 20:23:51 +00:00
}
public Value LineHeight {
2017-06-18 20:30:22 +00:00
get => this["line-height"];
set => this["line-height"] = value;
2017-06-18 20:23:51 +00:00
}
public Value MarginTop {
2017-06-18 20:30:22 +00:00
get => this["margin-top"];
set => this["margin-top"] = value;
2017-06-18 20:23:51 +00:00
}
public Value MarginRight {
2017-06-18 20:30:22 +00:00
get => this["margin-right"];
set => this["margin-right"] = value;
2017-06-18 20:23:51 +00:00
}
public Value MarginBottom {
2017-06-18 20:30:22 +00:00
get => this["margin-bottom"];
set => this["margin-bottom"] = value;
2017-06-18 20:23:51 +00:00
}
public Value MarginLeft {
2017-06-18 20:30:22 +00:00
get => this["margin-left"];
set => this["margin-left"] = value;
2017-06-18 20:23:51 +00:00
}
public Value Margin {
2017-06-18 20:30:22 +00:00
get => this["margin-top"];
2017-06-18 20:23:51 +00:00
set {
2017-06-18 20:30:22 +00:00
this["margin-top"] = value;
this["margin-right"] = value;
this["margin-bottom"] = value;
this["margin-left"] = value;
2017-06-18 20:23:51 +00:00
}
}
2017-11-09 21:03:56 +00:00
public Value Opacity {
get => this["opacity"];
set => this["opacity"] = value;
}
2017-06-18 21:11:09 +00:00
public Value Order {
get => this["order"];
set => this["order"] = value;
}
2017-06-18 20:23:51 +00:00
public Value PaddingTop {
2017-06-18 20:30:22 +00:00
get => this["padding-top"];
set => this["padding-top"] = value;
2017-06-18 20:23:51 +00:00
}
public Value PaddingRight {
2017-06-18 20:30:22 +00:00
get => this["padding-right"];
set => this["padding-right"] = value;
2017-06-18 20:23:51 +00:00
}
public Value PaddingBottom {
2017-06-18 20:30:22 +00:00
get => this["padding-bottom"];
set => this["padding-bottom"] = value;
2017-06-18 20:23:51 +00:00
}
public Value PaddingLeft {
2017-06-18 20:30:22 +00:00
get => this["padding-left"];
set => this["padding-left"] = value;
2017-06-18 20:23:51 +00:00
}
public Value Padding {
2017-06-18 20:30:22 +00:00
get => this["padding-top"];
2017-06-18 20:23:51 +00:00
set {
2017-06-18 20:30:22 +00:00
this["padding-top"] = value;
this["padding-right"] = value;
this["padding-bottom"] = value;
this["padding-left"] = value;
2017-06-18 20:23:51 +00:00
}
}
2017-11-09 21:03:56 +00:00
public Value Position {
get => this["position"];
set => this["position"] = value;
}
2017-06-18 20:23:51 +00:00
public Value Right {
2017-06-18 20:30:22 +00:00
get => this["right"];
set => this["right"] = value;
}
public Value TextAlign {
get => this["text-align"];
set => this["text-align"] = value;
2017-06-18 20:23:51 +00:00
}
2017-06-18 21:00:06 +00:00
public Value TextDecoration {
get => this["text-decoration"];
set => this["text-decoration"] = value;
}
2017-06-18 20:23:51 +00:00
public Value Top {
2017-06-18 20:30:22 +00:00
get => this["top"];
set => this["top"] = AddNumberUnits (value, "px");
2017-06-18 20:30:22 +00:00
}
2017-11-10 21:41:51 +00:00
public Value Transform {
get => this["transform"];
set => this["transform"] = value;
}
public Value TransformOrigin {
get => this["transform-origin"];
set => this["transform-origin"] = value;
}
2017-06-18 20:30:22 +00:00
public Value VerticalAlign {
get => this["vertical-align"];
set => this["vertical-align"] = value;
2017-06-18 20:23:51 +00:00
}
public Value Visibility {
2017-06-18 20:30:22 +00:00
get => this["visibility"];
set => this["visibility"] = value;
2017-06-18 20:23:51 +00:00
}
public Value Width {
2017-06-18 20:30:22 +00:00
get => this["width"];
set => this["width"] = AddNumberUnits (value, "px");
2017-06-18 20:23:51 +00:00
}
2017-11-09 22:17:36 +00:00
public Value ZIndex {
get => this["z-index"];
set => this["z-index"] = value;
}
2017-11-09 21:03:56 +00:00
public Value this[string propertyName] {
2017-06-18 20:30:22 +00:00
get {
lock (properties) {
Value p;
if (!properties.TryGetValue (propertyName, out p)) {
p = "inherit";
}
return p;
2017-06-18 19:52:51 +00:00
}
}
2017-06-18 20:30:22 +00:00
set {
var safeValue = value ?? "inherit";
lock (properties) {
2017-06-18 21:26:32 +00:00
if (value == null) {
2017-11-10 01:34:29 +00:00
if (!properties.Remove (propertyName))
return;
2017-06-18 21:26:32 +00:00
}
else {
Value old;
if (properties.TryGetValue (propertyName, out old)) {
if (EqualityComparer<Value>.Default.Equals (old, safeValue))
return;
}
properties[propertyName] = safeValue;
2017-06-18 20:30:22 +00:00
}
2017-06-18 19:52:51 +00:00
}
2017-06-18 20:30:22 +00:00
PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName));
2017-06-18 19:52:51 +00:00
}
}
2017-06-18 21:26:32 +00:00
2017-06-18 23:50:22 +00:00
public static string GetJsName (string propertyName)
{
var o = new System.Text.StringBuilder ();
var needsCap = false;
for (var i = 0; i < propertyName.Length; i++) {
var c = propertyName[i];
if (c == '-') {
needsCap = true;
}
else {
o.Append (needsCap ? Char.ToUpperInvariant (c) : c);
needsCap = false;
}
}
return o.ToString ();
}
2017-06-18 21:26:32 +00:00
public override string ToString ()
{
var o = new System.Text.StringBuilder ();
var head = "";
lock (properties) {
foreach (var p in properties) {
o.Append (head);
o.Append (p.Key);
o.Append (":");
o.Append (String.Format (System.Globalization.CultureInfo.InvariantCulture, "{0}", p.Value));
head = ";";
}
}
return o.ToString ();
}
static string AddNumberUnits (object val, string units)
{
if (val is string s)
return s;
if (val is IConvertible c)
return c.ToString (System.Globalization.CultureInfo.InvariantCulture) + units;
return val.ToString ();
}
public double GetNumberWithUnits (string key, string units, double baseValue)
{
var v = this[key];
if (v == null)
return 0;
if (v is string s) {
var lastIndex = s.LastIndexOfAny (numberChars);
if (lastIndex < 0)
return 0;
var num = double.Parse (s.Substring (0, lastIndex + 1), System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture);
return num;
}
if (v is IConvertible c)
return c.ToDouble (System.Globalization.CultureInfo.InvariantCulture);
return 0;
}
2017-06-18 19:52:51 +00:00
}
}