Add Canvas

This commit is contained in:
Frank A. Krueger 2017-06-19 00:08:33 -07:00
parent 8f998da8a8
commit d60194c504
5 changed files with 76 additions and 1 deletions

36
Ooui/Canvas.cs Normal file
View File

@ -0,0 +1,36 @@
using System;
namespace Ooui
{
public class Canvas : Element
{
Context2d context2d = new Context2d ();
int gotContext2d = 0;
public Canvas ()
: base ("canvas")
{
}
public Context2d GetContext2d ()
{
if (System.Threading.Interlocked.CompareExchange (ref gotContext2d, 1, 0) == 0) {
var mcall = Message.Call (Id, "getContext", "2d");
mcall.ResultId = context2d.Id;
Send (mcall);
}
return context2d;
}
protected override void SaveStateMessageIfNeeded (Message message)
{
switch (message.MessageType) {
case MessageType.Call when message.Key == "getContext" && message.Value is Array a && a.Length == 1 && "2d".Equals (a.GetValue (0)):
AddStateMessage (message);
break;
default:
base.SaveStateMessageIfNeeded (message);
break;
}
}
}
}

View File

@ -75,7 +75,10 @@ function msgCall (m) {
}
const f = node[m.k];
if (debug) console.log ("Call", node, f, m.v);
f.apply (node, m.v);
const r = f.apply (node, m.v);
if (typeof m.rid === 'string' || m.rid instanceof String) {
nodes[m.rid] = r;
}
}
function msgListen (m) {

10
Ooui/Context2d.cs Normal file
View File

@ -0,0 +1,10 @@
namespace Ooui
{
public class Context2d : EventTarget
{
public Context2d()
: base ("context2d")
{
}
}
}

View File

@ -19,6 +19,9 @@ namespace Ooui
[JsonProperty("v", NullValueHandling = NullValueHandling.Ignore)]
public object Value = null;
[JsonProperty("rid", NullValueHandling = NullValueHandling.Ignore)]
public string ResultId = null;
public static Message Call (string targetId, string method, params object[] args) => new Message {
MessageType = MessageType.Call,
TargetId = targetId,

23
Tests/CanvasTests.cs Normal file
View File

@ -0,0 +1,23 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ooui;
namespace Tests
{
[TestClass]
public class CanvasTests
{
[TestMethod]
public void Context2dState ()
{
var c = new Canvas ();
Assert.AreEqual (1, c.StateMessages.Count);
var c2d = c.GetContext2d ();
Assert.AreEqual (2, c.StateMessages.Count);
var c2d2 = c.GetContext2d ();
Assert.AreEqual (2, c.StateMessages.Count);
Assert.AreEqual (c2d, c2d2);
}
}
}