2 * Copyright(c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.Collections.Generic;
20 using System.ComponentModel;
22 using System.Reflection;
24 namespace Tizen.NUI.Binding
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
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>();
37 Delegate handlerdelegate;
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))
43 Actions = new SealedList<TriggerAction>();
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; }
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)]
54 get { return eventname; }
57 if (eventname == value)
60 throw new InvalidOperationException("Event cannot be changed once the Trigger has been applied");
67 internal override void OnAttachedTo(BindableObject bindable)
69 base.OnAttachedTo(bindable);
70 if (!string.IsNullOrEmpty(Event))
71 AttachHandlerTo(bindable);
72 associatedObjects.Add(bindable);
75 internal override void OnDetachingFrom(BindableObject bindable)
77 associatedObjects.Remove(bindable);
78 DetachHandlerFrom(bindable);
79 base.OnDetachingFrom(bindable);
82 internal override void OnSeal()
85 ((SealedList<TriggerAction>)Actions).IsReadOnly = true;
88 void AttachHandlerTo(BindableObject bindable)
92 eventinfo = bindable.GetType().GetRuntimeEvent(Event);
93 handlerdelegate = s_handlerinfo.CreateDelegate(eventinfo?.EventHandlerType, this);
97 Console.WriteLine($"EventTrigger", "Can not attach EventTrigger to {binding.GetType()}.{Evnet}. Check if the handler exists and if the signature is right.");
99 if (eventinfo != null && handlerdelegate != null)
100 eventinfo.AddEventHandler(bindable, handlerdelegate);
103 void DetachHandlerFrom(BindableObject bindable)
105 if (eventinfo != null && handlerdelegate != null)
106 eventinfo.RemoveEventHandler(bindable, handlerdelegate);
110 void OnEventTriggered(object sender, EventArgs e)
112 var bindable = (BindableObject)sender;
113 foreach (TriggerAction action in Actions)
114 action.DoInvoke(bindable);