Fix bubbling events through nodes
This commit is contained in:
parent
d4bae780d3
commit
a780f97578
|
@ -33,16 +33,16 @@ namespace Ooui
|
|||
}
|
||||
return context2d;
|
||||
}
|
||||
protected override void SaveStateMessageIfNeeded (Message message)
|
||||
protected override bool SaveStateMessageIfNeeded (Message message)
|
||||
{
|
||||
if (message.TargetId == Id) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
return base.SaveStateMessageIfNeeded (message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
const debug = true;
|
||||
const debug = false;
|
||||
|
||||
const nodes = {};
|
||||
|
||||
|
|
|
@ -134,7 +134,6 @@ namespace Ooui
|
|||
{
|
||||
if (message == null)
|
||||
return;
|
||||
if (message.TargetId == Id)
|
||||
SaveStateMessageIfNeeded (message);
|
||||
TriggerEventFromMessage (message);
|
||||
}
|
||||
|
@ -149,8 +148,11 @@ namespace Ooui
|
|||
lock (stateMessages) updater (stateMessages);
|
||||
}
|
||||
|
||||
protected virtual void SaveStateMessageIfNeeded (Message message)
|
||||
protected virtual bool SaveStateMessageIfNeeded (Message message)
|
||||
{
|
||||
if (message.TargetId != Id)
|
||||
return false;
|
||||
|
||||
switch (message.MessageType) {
|
||||
case MessageType.Create:
|
||||
AddStateMessage (message);
|
||||
|
@ -165,12 +167,15 @@ namespace Ooui
|
|||
AddStateMessage (message);
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected virtual bool TriggerEventFromMessage (Message message)
|
||||
{
|
||||
if (message.TargetId != Id)
|
||||
return false;
|
||||
|
||||
List<EventHandler> handlers = null;
|
||||
lock (eventListeners) {
|
||||
List<EventHandler> hs;
|
||||
|
@ -178,11 +183,12 @@ namespace Ooui
|
|||
handlers = new List<EventHandler> (hs);
|
||||
}
|
||||
}
|
||||
if (handlers == null) return true;
|
||||
if (handlers != null) {
|
||||
var args = EventArgs.Empty;
|
||||
foreach (var h in handlers) {
|
||||
h.Invoke (this, args);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -64,6 +64,8 @@ namespace Ooui
|
|||
public Input ()
|
||||
: base ("input")
|
||||
{
|
||||
// Subscribe to the change event so we always get up-to-date values
|
||||
Changed += (s, e) => {};
|
||||
}
|
||||
|
||||
public Input (InputType type)
|
||||
|
@ -71,6 +73,15 @@ namespace Ooui
|
|||
{
|
||||
Type = type;
|
||||
}
|
||||
|
||||
protected override bool TriggerEventFromMessage (Message message)
|
||||
{
|
||||
if (message.TargetId == Id && message.MessageType == MessageType.Event && message.Key == "change") {
|
||||
Value = message.Value != null ? Convert.ToString (message.Value) : "";
|
||||
return true;
|
||||
}
|
||||
return base.TriggerEventFromMessage (message);
|
||||
}
|
||||
}
|
||||
|
||||
public enum InputType
|
||||
|
|
|
@ -29,10 +29,11 @@ namespace Ooui
|
|||
Value = args,
|
||||
};
|
||||
|
||||
public static Message Event (string targetId, string eventType) => new Message {
|
||||
public static Message Event (string targetId, string eventType, object value = null) => new Message {
|
||||
MessageType = MessageType.Event,
|
||||
TargetId = targetId,
|
||||
Key = eventType,
|
||||
Value = value,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
29
Ooui/Node.cs
29
Ooui/Node.cs
|
@ -100,8 +100,9 @@ namespace Ooui
|
|||
Send (message);
|
||||
}
|
||||
|
||||
protected override void SaveStateMessageIfNeeded (Message message)
|
||||
protected override bool SaveStateMessageIfNeeded (Message message)
|
||||
{
|
||||
if (message.TargetId == Id) {
|
||||
switch (message.MessageType) {
|
||||
case MessageType.Call when message.Key == "insertBefore":
|
||||
AddStateMessage (message);
|
||||
|
@ -132,18 +133,32 @@ namespace Ooui
|
|||
base.SaveStateMessageIfNeeded (message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool TriggerEventFromMessage (Message message)
|
||||
{
|
||||
if (base.TriggerEventFromMessage (message))
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
var ch = Children;
|
||||
for (var i = 0; i < ch.Count; i++) {
|
||||
if (ch[i].TriggerEventFromMessage (message))
|
||||
if (ch[i].SaveStateMessageIfNeeded (message))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool TriggerEventFromMessage (Message message)
|
||||
{
|
||||
if (message.TargetId == Id) {
|
||||
if (base.TriggerEventFromMessage (message))
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
var ch = Children;
|
||||
for (var i = 0; i < ch.Count; i++) {
|
||||
if (ch[i].TriggerEventFromMessage (message))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,6 +35,8 @@ namespace Ooui
|
|||
public TextArea ()
|
||||
: base ("textarea")
|
||||
{
|
||||
// Subscribe to the change event so we always get up-to-date values
|
||||
Changed += (s, e) => {};
|
||||
}
|
||||
|
||||
public TextArea (string text)
|
||||
|
@ -42,5 +44,13 @@ namespace Ooui
|
|||
{
|
||||
Text = text;
|
||||
}
|
||||
|
||||
protected override bool TriggerEventFromMessage (Message message)
|
||||
{
|
||||
if (message.TargetId == Id && message.MessageType == MessageType.Event && message.Key == "change") {
|
||||
Value = message.Value != null ? Convert.ToString (message.Value) : "";
|
||||
}
|
||||
return base.TriggerEventFromMessage (message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,21 +1,27 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Ooui;
|
||||
|
||||
namespace Samples
|
||||
{
|
||||
public class TodoSample
|
||||
{
|
||||
Button MakeTodo ()
|
||||
|
||||
Element MakeTodo ()
|
||||
{
|
||||
var button = new Button ("Click me!");
|
||||
button.Style.FontSize = 100;
|
||||
var count = 0;
|
||||
var items = new List ();
|
||||
var input = new Input ();
|
||||
var button = new Button ("Add the item");
|
||||
button.Clicked += (s, e) => {
|
||||
button.Style.FontSize = (int)button.Style.FontSize + 1;
|
||||
count++;
|
||||
button.Text = $"Clicked {count} times";
|
||||
items.AppendChild (new ListItem {
|
||||
Text = input.Value
|
||||
});
|
||||
};
|
||||
return button;
|
||||
var app = new Div ();
|
||||
app.AppendChild (input);
|
||||
app.AppendChild (button);
|
||||
app.AppendChild (items);
|
||||
return app;
|
||||
}
|
||||
|
||||
public void Publish ()
|
||||
|
|
|
@ -51,5 +51,15 @@ namespace Tests
|
|||
p.Receive (Message.Event (b.Id, "click"));
|
||||
Assert.IsTrue (clicked);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ValueBubblesDown ()
|
||||
{
|
||||
var p = new Div ();
|
||||
var b = new Input ();
|
||||
p.AppendChild (b);
|
||||
p.Receive (Message.Event (b.Id, "change", "please work"));
|
||||
Assert.AreEqual ("please work", b.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue