Add Events

This commit is contained in:
Frank A. Krueger 2017-06-14 23:10:58 -07:00
parent 44ae8c96f3
commit 76b9d95ddf
4 changed files with 94 additions and 2 deletions

View File

@ -16,6 +16,11 @@ namespace Ooui
set => SetProperty (ref val, value, "value");
}
public event EventHandler Clicked {
add => AddEventListener ("onclick", value);
remove => RemoveEventListener ("onclick", value);
}
public Button ()
{
}

View File

@ -6,12 +6,15 @@ namespace Ooui
{
public abstract class EventTarget
{
readonly List<Message> stateMessages = new List<Message> ();
readonly Dictionary<string, List<EventHandler>> eventListeners =
new Dictionary<string, List<EventHandler>> ();
public string Id { get; private set; } = GenerateId ();
public Mapping Mapping { get; private set; }
readonly List<Message> stateMessages = new List<Message> ();
public event Action<Message> MessageSent;
public IEnumerable<Message> StateMessages => stateMessages;
@ -21,6 +24,36 @@ namespace Ooui
Mapping = Mapping.Get (GetType ());
}
public void AddEventListener (string eventType, EventHandler handler)
{
if (eventType == null) return;
if (handler == null) return;
var sendListen = false;
List<EventHandler> handlers;
if (!eventListeners.TryGetValue (eventType, out handlers)) {
handlers = new List<EventHandler> ();
eventListeners[eventType] = handlers;
sendListen = true;
}
handlers.Add (handler);
if (sendListen)
SendListen (eventType);
}
public void RemoveEventListener (string eventType, EventHandler handler)
{
if (eventType == null) return;
if (handler == null) return;
List<EventHandler> handlers;
if (eventListeners.TryGetValue (eventType, out handlers)) {
handlers.Remove (handler);
}
}
protected bool SetProperty<T> (ref T backingStore, T newValue, string attributeName, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
if (!backingStore.Equals (newValue)) {
@ -73,6 +106,15 @@ namespace Ooui
});
}
protected void SendListen (string eventType)
{
Send (new Message {
MessageType = MessageType.Listen,
TargetId = Id,
Key = eventType,
});
}
public virtual void Receive (Message message)
{
if (message == null)
@ -110,11 +152,21 @@ namespace Ooui
ReplaceStateMessage (old, message);
}
break;
case MessageType.Listen:
SaveStateMessage (message);
break;
}
}
protected virtual void TriggerEventFromMessage (Message message)
{
List<EventHandler> handlers;
if (eventListeners.TryGetValue (message.Key, out handlers)) {
var args = EventArgs.Empty;
foreach (var h in handlers) {
h.Invoke (this, args);
}
}
}
}
}

View File

@ -26,6 +26,18 @@ namespace Ooui
set => v = FixupValue (value);
}
public static Message Listen (string targetId, string eventType) => new Message {
MessageType = MessageType.Listen,
TargetId = targetId,
Key = eventType,
};
public static Message Event (string targetId, string eventType) => new Message {
MessageType = MessageType.Event,
TargetId = targetId,
Key = eventType,
};
static object FixupValue (object v)
{
if (v is Array a) {
@ -60,5 +72,9 @@ namespace Ooui
Set,
[EnumMember(Value = "call")]
Call,
[EnumMember(Value = "listen")]
Listen,
[EnumMember(Value = "event")]
Event,
}
}

View File

@ -21,5 +21,24 @@ namespace Tests
var b = new Button ("Hello World!");
Assert.AreEqual ("Hello World!", b.Text);
}
[TestMethod]
public void Clicked ()
{
var b = new Button ("Hello World!");
var clicked = false;
var listened = false;
b.MessageSent += m => {
listened = listened || (m.MessageType == MessageType.Listen);
};
Assert.IsFalse (listened);
b.Clicked += (s, e) => {
clicked = true;
};
Assert.IsTrue (listened);
Assert.IsFalse (clicked);
b.Receive (Message.Event (b.Id, "onclick"));
Assert.IsTrue (clicked);
}
}
}