[NUI] Add license, delete unnecessary code(public)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / XamlBinding / Interactivity / EventTrigger.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System;
19 using System.Collections.Generic;
20 using System.ComponentModel;
21 using System.Linq;
22 using System.Reflection;
23
24 namespace Tizen.NUI.Binding
25 {
26     /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
27     [EditorBrowsable(EditorBrowsableState.Never)]
28     [ContentProperty("Actions")]
29     public sealed class EventTrigger : TriggerBase
30     {
31         static readonly MethodInfo s_handlerinfo = typeof(EventTrigger).GetRuntimeMethods().Single(mi => mi.Name == "OnEventTriggered" && mi.IsPublic == false);
32         readonly List<BindableObject> associatedObjects = new List<BindableObject>();
33
34         EventInfo eventinfo;
35
36         string eventname;
37         Delegate handlerdelegate;
38
39         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
40         [EditorBrowsable(EditorBrowsableState.Never)]
41         public EventTrigger() : base(typeof(BindableObject))
42         {
43             Actions = new SealedList<TriggerAction>();
44         }
45
46         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
47         [EditorBrowsable(EditorBrowsableState.Never)]
48         public IList<TriggerAction> Actions { get; }
49
50         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
51         [EditorBrowsable(EditorBrowsableState.Never)]
52         public string Event
53         {
54             get { return eventname; }
55             set
56             {
57                 if (eventname == value)
58                     return;
59                 if (IsSealed)
60                     throw new InvalidOperationException("Event cannot be changed once the Trigger has been applied");
61                 OnPropertyChanging();
62                 eventname = value;
63                 OnPropertyChanged();
64             }
65         }
66
67         internal override void OnAttachedTo(BindableObject bindable)
68         {
69             base.OnAttachedTo(bindable);
70             if (!string.IsNullOrEmpty(Event))
71                 AttachHandlerTo(bindable);
72             associatedObjects.Add(bindable);
73         }
74
75         internal override void OnDetachingFrom(BindableObject bindable)
76         {
77             associatedObjects.Remove(bindable);
78             DetachHandlerFrom(bindable);
79             base.OnDetachingFrom(bindable);
80         }
81
82         internal override void OnSeal()
83         {
84             base.OnSeal();
85             ((SealedList<TriggerAction>)Actions).IsReadOnly = true;
86         }
87
88         void AttachHandlerTo(BindableObject bindable)
89         {
90             try
91             {
92                 eventinfo = bindable.GetType().GetRuntimeEvent(Event);
93                 handlerdelegate = s_handlerinfo.CreateDelegate(eventinfo?.EventHandlerType, this);
94             }
95             catch (Exception)
96             {
97                Console.WriteLine($"EventTrigger", "Can not attach EventTrigger to {binding.GetType()}.{Evnet}. Check if the handler exists and if the signature is right.");
98             }
99             if (eventinfo != null && handlerdelegate != null)
100                 eventinfo.AddEventHandler(bindable, handlerdelegate);
101         }
102
103         void DetachHandlerFrom(BindableObject bindable)
104         {
105             if (eventinfo != null && handlerdelegate != null)
106                 eventinfo.RemoveEventHandler(bindable, handlerdelegate);
107         }
108
109         // [Preserve]
110         void OnEventTriggered(object sender, EventArgs e)
111         {
112             var bindable = (BindableObject)sender;
113             foreach (TriggerAction action in Actions)
114                 action.DoInvoke(bindable);
115         }
116     }
117 }