Add UI.Styles

This commit is contained in:
Frank A. Krueger 2017-06-26 18:48:57 -07:00
parent f8c5e75070
commit 3b83e49680
2 changed files with 113 additions and 1 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -16,6 +17,12 @@ namespace Ooui
static readonly Dictionary<string, Func<Element>> publishedPaths = static readonly Dictionary<string, Func<Element>> publishedPaths =
new Dictionary<string, Func<Element>> (); new Dictionary<string, Func<Element>> ();
static readonly Dictionary<string, Style> styles =
new Dictionary<string, Style> ();
static readonly StyleSelectors rules = new StyleSelectors ();
public static StyleSelectors Styles => rules;
static readonly byte[] clientJsBytes; static readonly byte[] clientJsBytes;
public static string Template { get; set; } = $@"<!DOCTYPE html> public static string Template { get; set; } = $@"<!DOCTYPE html>
@ -24,6 +31,7 @@ namespace Ooui
<title>@ElementPath</title> <title>@ElementPath</title>
<meta name=""viewport"" content=""width=device-width, initial-scale=1"" /> <meta name=""viewport"" content=""width=device-width, initial-scale=1"" />
<link rel=""stylesheet"" href=""https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"" integrity=""sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"" crossorigin=""anonymous""> <link rel=""stylesheet"" href=""https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"" integrity=""sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"" crossorigin=""anonymous"">
<style>@Styles</style>
</head> </head>
<body> <body>
<div id=""ooui-body"" class=""container-fluid""></div> <div id=""ooui-body"" class=""container-fluid""></div>
@ -177,7 +185,7 @@ namespace Ooui
static string RenderTemplate (string elementPath) static string RenderTemplate (string elementPath)
{ {
return Template.Replace ("@ElementPath", elementPath); return Template.Replace ("@ElementPath", elementPath).Replace ("@Styles", rules.ToString ());
} }
static void WriteElementHtml (string elementPath, HttpListenerResponse response) static void WriteElementHtml (string elementPath, HttpListenerResponse response)
@ -444,5 +452,52 @@ namespace Ooui
} }
} }
} }
public class StyleSelectors
{
public Style this[string selector] {
get {
Style r;
var key = selector ?? "";
lock (styles) {
if (!styles.TryGetValue (key, out r)) {
r = new Style ();
styles.Add (key, r);
}
return r;
}
}
set {
var key = selector ?? "";
lock (styles) {
if (value == null) {
styles.Remove (key);
}
else {
styles[key] = value;
}
}
}
}
public void Clear ()
{
lock (styles) {
styles.Clear ();
}
}
public override string ToString()
{
lock (styles) {
var q =
from s in styles
let v = s.Value.ToString ()
where v.Length > 0
select s.Key + " {" + s.Value.ToString () + "}";
return String.Join ("\n", q);
}
}
}
} }
} }

57
Tests/UITests.cs Normal file
View File

@ -0,0 +1,57 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ooui;
namespace Tests
{
[TestClass]
public class UITests
{
[TestMethod]
public void UndefinedStylePropertyIsInherit ()
{
Assert.AreEqual ("inherit", UI.Styles["something random and made up"].BackgroundColor);
}
[TestMethod]
public void SetStyleProperty ()
{
UI.Styles.Clear ();
UI.Styles[".t1"].BackgroundColor = "red";
Assert.AreEqual ("red", UI.Styles[".t1"].BackgroundColor);
}
[TestMethod]
public void ClearWorks ()
{
UI.Styles[".t1"].BackgroundColor = "red";
UI.Styles.Clear ();
Assert.AreEqual ("inherit", UI.Styles[".t1"].BackgroundColor);
Assert.AreEqual ("", UI.Styles.ToString ());
}
[TestMethod]
public void SetStyle ()
{
UI.Styles.Clear ();
UI.Styles[".t2"] = new Style {
BackgroundColor = "red",
};
Assert.AreEqual ("red", UI.Styles[".t2"].BackgroundColor);
Assert.AreEqual (".t2 {background-color:red}", UI.Styles.ToString ());
}
[TestMethod]
public void SetNullStyle ()
{
UI.Styles.Clear ();
UI.Styles[".t3"] = new Style {
BackgroundColor = "red",
};
UI.Styles[".t3"] = null;
Assert.AreEqual ("inherit", UI.Styles[".t3"].BackgroundColor);
Assert.AreEqual ("", UI.Styles.ToString ());
}
}
}