[NUI] Change GetDefaultWindow() to static func (#900)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / XamlBinding / TriggerBase.cs
1 using System;
2 using System.Reflection;
3 using System.Collections;
4 using System.Collections.Generic;
5 using System.ComponentModel;
6
7 namespace Tizen.NUI.Binding
8 {
9     /// <since_tizen> 6 </since_tizen>
10     /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
11     [EditorBrowsable(EditorBrowsableState.Never)]
12     public abstract class TriggerBase : BindableObject, IAttachedObject
13     {
14         bool _isSealed;
15
16         internal TriggerBase(Type targetType)
17         {
18             if (targetType == null)
19                 throw new ArgumentNullException("targetType");
20             TargetType = targetType;
21
22             EnterActions = new SealedList<TriggerAction>();
23             ExitActions = new SealedList<TriggerAction>();
24         }
25
26         internal TriggerBase(Condition condition, Type targetType) : this(targetType)
27         {
28             Setters = new SealedList<Setter>();
29             Condition = condition;
30             Condition.ConditionChanged = OnConditionChanged;
31         }
32
33         /// <since_tizen> 6 </since_tizen>
34         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
35         [EditorBrowsable(EditorBrowsableState.Never)]
36         public IList<TriggerAction> EnterActions { get; }
37
38         /// <since_tizen> 6 </since_tizen>
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 IList<TriggerAction> ExitActions { get; }
42
43         /// <since_tizen> 6 </since_tizen>
44         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
45         [EditorBrowsable(EditorBrowsableState.Never)]
46         public bool IsSealed
47         {
48             get { return _isSealed; }
49             private set
50             {
51                 if (_isSealed == value)
52                     return;
53                 if (!value)
54                     throw new InvalidOperationException("What is sealed can not be unsealed.");
55                 _isSealed = value;
56                 OnSeal();
57             }
58         }
59
60         /// <since_tizen> 6 </since_tizen>
61         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
62         [EditorBrowsable(EditorBrowsableState.Never)]
63         public Type TargetType { get; }
64
65         internal Condition Condition { get; }
66
67         //Setters and Condition are used by Trigger, DataTrigger and MultiTrigger
68         internal IList<Setter> Setters { get; }
69
70         void IAttachedObject.AttachTo(BindableObject bindable)
71         {
72             IsSealed = true;
73
74             if (bindable == null)
75                 throw new ArgumentNullException("bindable");
76             if (!TargetType.IsInstanceOfType(bindable))
77                 throw new InvalidOperationException("bindable not an instance of AssociatedType");
78             OnAttachedTo(bindable);
79         }
80
81         void IAttachedObject.DetachFrom(BindableObject bindable)
82         {
83             if (bindable == null)
84                 throw new ArgumentNullException("bindable");
85             OnDetachingFrom(bindable);
86         }
87
88         internal virtual void OnAttachedTo(BindableObject bindable)
89         {
90             if (Condition != null)
91                 Condition.SetUp(bindable);
92         }
93
94         internal virtual void OnDetachingFrom(BindableObject bindable)
95         {
96             if (Condition != null)
97                 Condition.TearDown(bindable);
98         }
99
100         internal virtual void OnSeal()
101         {
102             ((SealedList<TriggerAction>)EnterActions).IsReadOnly = true;
103             ((SealedList<TriggerAction>)ExitActions).IsReadOnly = true;
104             if (Setters != null)
105                 ((SealedList<Setter>)Setters).IsReadOnly = true;
106             if (Condition != null)
107                 Condition.IsSealed = true;
108         }
109
110         void OnConditionChanged(BindableObject bindable, bool oldValue, bool newValue)
111         {
112             if (newValue)
113             {
114                 foreach (TriggerAction action in EnterActions)
115                     action.DoInvoke(bindable);
116                 foreach (Setter setter in Setters)
117                     setter.Apply(bindable);
118             }
119             else
120             {
121                 foreach (Setter setter in Setters)
122                     setter.UnApply(bindable);
123                 foreach (TriggerAction action in ExitActions)
124                     action.DoInvoke(bindable);
125             }
126         }
127
128         internal class SealedList<T> : IList<T>
129         {
130             readonly IList<T> _actual;
131
132             bool _isReadOnly;
133
134             public SealedList()
135             {
136                 _actual = new List<T>();
137             }
138
139             public void Add(T item)
140             {
141                 if (IsReadOnly)
142                     throw new InvalidOperationException("This list is ReadOnly");
143                 _actual.Add(item);
144             }
145
146             public void Clear()
147             {
148                 if (IsReadOnly)
149                     throw new InvalidOperationException("This list is ReadOnly");
150                 _actual.Clear();
151             }
152
153             public bool Contains(T item)
154             {
155                 return _actual.Contains(item);
156             }
157
158             public void CopyTo(T[] array, int arrayIndex)
159             {
160                 _actual.CopyTo(array, arrayIndex);
161             }
162
163             public int Count
164             {
165                 get { return _actual.Count; }
166             }
167
168             public bool IsReadOnly
169             {
170                 get { return _isReadOnly; }
171                 set
172                 {
173                     if (_isReadOnly == value)
174                         return;
175                     if (!value)
176                         throw new InvalidOperationException("Can't change this back to non readonly");
177                     _isReadOnly = value;
178                 }
179             }
180
181             public bool Remove(T item)
182             {
183                 if (IsReadOnly)
184                     throw new InvalidOperationException("This list is ReadOnly");
185                 return _actual.Remove(item);
186             }
187
188             IEnumerator IEnumerable.GetEnumerator()
189             {
190                 return ((IEnumerable)_actual).GetEnumerator();
191             }
192
193             public IEnumerator<T> GetEnumerator()
194             {
195                 return _actual.GetEnumerator();
196             }
197
198             public int IndexOf(T item)
199             {
200                 return _actual.IndexOf(item);
201             }
202
203             public void Insert(int index, T item)
204             {
205                 if (IsReadOnly)
206                     throw new InvalidOperationException("This list is ReadOnly");
207                 _actual.Insert(index, item);
208             }
209
210             public T this[int index]
211             {
212                 get { return _actual[index]; }
213                 set
214                 {
215                     if (IsReadOnly)
216                         throw new InvalidOperationException("This list is ReadOnly");
217                     _actual[index] = value;
218                 }
219             }
220
221             public void RemoveAt(int index)
222             {
223                 if (IsReadOnly)
224                     throw new InvalidOperationException("This list is ReadOnly");
225                 _actual.RemoveAt(index);
226             }
227         }
228     }
229 }