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