[NUI] Change GetDefaultWindow() to static func (#900)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / XamlBinding / Interactivity / EventTrigger.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Reflection;
5 using Tizen.NUI.Binding.Internals;
6
7 namespace Tizen.NUI.Binding
8 {
9     [ContentProperty("Actions")]
10     internal sealed class EventTrigger : TriggerBase
11     {
12         static readonly MethodInfo s_handlerinfo = typeof(EventTrigger).GetRuntimeMethods().Single(mi => mi.Name == "OnEventTriggered" && mi.IsPublic == false);
13         readonly List<BindableObject> _associatedObjects = new List<BindableObject>();
14
15         EventInfo _eventinfo;
16
17         string _eventname;
18         Delegate _handlerdelegate;
19
20         public EventTrigger() : base(typeof(BindableObject))
21         {
22             Actions = new SealedList<TriggerAction>();
23         }
24
25         public IList<TriggerAction> Actions { get; }
26
27         public string Event
28         {
29             get { return _eventname; }
30             set
31             {
32                 if (_eventname == value)
33                     return;
34                 if (IsSealed)
35                     throw new InvalidOperationException("Event cannot be changed once the Trigger has been applied");
36                 OnPropertyChanging();
37                 _eventname = value;
38                 OnPropertyChanged();
39             }
40         }
41
42         internal override void OnAttachedTo(BindableObject bindable)
43         {
44             base.OnAttachedTo(bindable);
45             if (!string.IsNullOrEmpty(Event))
46                 AttachHandlerTo(bindable);
47             _associatedObjects.Add(bindable);
48         }
49
50         internal override void OnDetachingFrom(BindableObject bindable)
51         {
52             _associatedObjects.Remove(bindable);
53             DetachHandlerFrom(bindable);
54             base.OnDetachingFrom(bindable);
55         }
56
57         internal override void OnSeal()
58         {
59             base.OnSeal();
60             ((SealedList<TriggerAction>)Actions).IsReadOnly = true;
61         }
62
63         void AttachHandlerTo(BindableObject bindable)
64         {
65             try
66             {
67                 _eventinfo = bindable.GetType().GetRuntimeEvent(Event);
68                 _handlerdelegate = s_handlerinfo.CreateDelegate(_eventinfo?.EventHandlerType, this);
69             }
70             catch (Exception)
71             {
72                 Console.WriteLine("EventTrigger", "Can not attach EventTrigger to {0}.{1}. Check if the handler exists and if the signature is right.", bindable.GetType(), Event);
73             }
74             if (_eventinfo != null && _handlerdelegate != null)
75                 _eventinfo.AddEventHandler(bindable, _handlerdelegate);
76         }
77
78         void DetachHandlerFrom(BindableObject bindable)
79         {
80             if (_eventinfo != null && _handlerdelegate != null)
81                 _eventinfo.RemoveEventHandler(bindable, _handlerdelegate);
82         }
83
84         // [Preserve]
85         void OnEventTriggered(object sender, EventArgs e)
86         {
87             var bindable = (BindableObject)sender;
88             foreach (TriggerAction action in Actions)
89                 action.DoInvoke(bindable);
90         }
91     }
92 }