[NUI] Deprecate BaseHandle.PropertySet and Add BindableProperty.PropertyChanged
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / XamlBinding / BindableObject.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.Diagnostics;
22 using System.Reflection;
23 using System.Runtime.CompilerServices;
24 using Tizen.NUI.Binding.Internals;
25
26 namespace Tizen.NUI.Binding
27 {
28     /// <summary>
29     /// Provides a mechanism by which application developers can propagate changes that are made to data in one object to another.
30     /// </summary>
31     /// <since_tizen> 9 </since_tizen>
32     public abstract class BindableObject : INotifyPropertyChanged, IDynamicResourceHandler
33     {
34         /// <summary>
35         /// Implements the bound property whose interface is provided by the BindingContext property.
36         /// </summary>
37         [EditorBrowsable(EditorBrowsableState.Never)]
38         public static readonly BindableProperty BindingContextProperty =
39             BindableProperty.Create(nameof(BindingContext), typeof(object), typeof(BindableObject), default(object),
40                                     BindingMode.OneWay, null, BindingContextPropertyChanged, null, null, BindingContextPropertyBindingChanging);
41
42         readonly List<BindablePropertyContext> properties = new List<BindablePropertyContext>(4);
43
44         bool applying;
45         object inheritedContext;
46
47         /// <summary>
48         /// Gets or sets object that contains the properties that will be targeted by the bound properties that belong to this BindableObject.
49         /// </summary>
50         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
51         [EditorBrowsable(EditorBrowsableState.Never)]
52         public object BindingContext
53         {
54             get { return inheritedContext ?? GetValue(BindingContextProperty); }
55             set { SetValue(BindingContextProperty, value); }
56         }
57
58         void IDynamicResourceHandler.SetDynamicResource(BindableProperty property, string key)
59         {
60             SetDynamicResource(property, key, false);
61         }
62
63         /// <summary>
64         /// Raised when a property has changed.
65         /// </summary>
66         /// <since_tizen> 9 </since_tizen>
67         public event PropertyChangedEventHandler PropertyChanged;
68
69         /// <summary>Copy properties of other ViewStyle to this.</summary>
70         /// <param name="other">The other BindableProperty merge to this.</param>
71         [EditorBrowsable(EditorBrowsableState.Never)]
72         public virtual void CopyFrom(BindableObject other)
73         {
74             if (null == other) return;
75
76             Type type1 = this.GetType();
77             BindableProperty.GetBindablePropertysOfType(type1, out var nameToBindableProperty1);
78
79             Type type2 = other.GetType();
80             BindableProperty.GetBindablePropertysOfType(type2, out var nameToBindableProperty2);
81
82             if (null != nameToBindableProperty1)
83             {
84                 foreach (KeyValuePair<string, BindableProperty> keyValuePair in nameToBindableProperty1)
85                 {
86                     nameToBindableProperty2.TryGetValue(keyValuePair.Key, out var bindableProperty);
87
88                     if (null != bindableProperty)
89                     {
90                         object value = other.GetValue(bindableProperty);
91
92                         if (null != value)
93                         {
94                             SetValue(keyValuePair.Value, value);
95                         }
96                     }
97                 }
98             }
99         }
100
101         /// <summary>
102         /// Raised whenever the BindingContext property changes.
103         /// </summary>
104         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
105         [EditorBrowsable(EditorBrowsableState.Never)]
106         public event EventHandler BindingContextChanged;
107
108         internal void ClearValue(BindableProperty property, bool fromStyle)
109         {
110             ClearValue(property, fromStyle: fromStyle, checkAccess: true);
111         }
112
113         /// <summary>
114         /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue.
115         /// </summary>
116         /// <param name="property">The BindableProperty to clear</param>
117         internal void ClearValue(BindableProperty property)
118         {
119             ClearValue(property, fromStyle: false, checkAccess: true);
120         }
121
122         /// <summary>
123         /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue for the property that is identified by propertyKey.
124         /// </summary>
125         /// <param name="propertyKey">The BindablePropertyKey that identifies the BindableProperty to clear.</param>
126         internal void ClearValue(BindablePropertyKey propertyKey)
127         {
128             if (propertyKey == null)
129                 throw new ArgumentNullException(nameof(propertyKey));
130
131             ClearValue(propertyKey.BindableProperty, fromStyle: false, checkAccess: false);
132         }
133
134         /// <summary>
135         /// Return true if the target property exists and has been set.
136         /// </summary>
137         /// <param name="targetProperty">The target property</param>
138         /// <returns>return true if the target property exists and has been set</returns>
139         internal bool IsSet(BindableProperty targetProperty)
140         {
141             if (targetProperty == null)
142                 throw new ArgumentNullException(nameof(targetProperty));
143
144             var bpcontext = GetContext(targetProperty);
145             return bpcontext != null
146                 && (bpcontext.Attributes & BindableContextAttributes.IsDefaultValue) == 0;
147         }
148
149         /// <summary>
150         /// Returns the value that is contained the BindableProperty.
151         /// </summary>
152         /// <param name="property">The BindableProperty for which to get the value.</param>
153         /// <returns>The value that is contained the BindableProperty</returns>
154         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
155         [EditorBrowsable(EditorBrowsableState.Never)]
156         public object GetValue(BindableProperty property)
157         {
158             if (property == null)
159                 throw new ArgumentNullException(nameof(property));
160
161             BindablePropertyContext context = property.DefaultValueCreator != null ? GetOrCreateContext(property) : GetContext(property);
162
163             if (context == null)
164                 return property.DefaultValue;
165
166             return context.Value;
167         }
168
169         /// <summary>
170         /// Raised when a property is about to change.
171         /// </summary>
172         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
173         [EditorBrowsable(EditorBrowsableState.Never)]
174         public event PropertyChangingEventHandler PropertyChanging;
175
176         /// <summary>
177         /// Removes a previously set binding.
178         /// </summary>
179         /// <param name="property">The BindableProperty from which to remove bindings.</param>
180         internal void RemoveBinding(BindableProperty property)
181         {
182             if (property == null)
183                 throw new ArgumentNullException(nameof(property));
184
185             BindablePropertyContext context = GetContext(property);
186             if (context == null || context.Binding == null)
187                 return;
188
189             RemoveBinding(property, context);
190         }
191
192         /// <summary>
193         /// Assigns a binding to a property.
194         /// </summary>
195         /// <param name="targetProperty">The BindableProperty on which to set a binding.</param>
196         /// <param name="binding">The binding to set.</param>
197         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
198         [EditorBrowsable(EditorBrowsableState.Never)]
199         public void SetBinding(BindableProperty targetProperty, BindingBase binding)
200         {
201             SetBinding(targetProperty, binding, false);
202         }
203
204         private bool isCreateByXaml = false;
205         /// Only used by the IL of xaml, will never changed to not hidden.
206         [EditorBrowsable(EditorBrowsableState.Never)]
207         public virtual bool IsCreateByXaml
208         {
209             get
210             {
211                 return isCreateByXaml;
212             }
213             set
214             {
215                 isCreateByXaml = value;
216             }
217         }
218
219         /// <summary>
220         /// Sets the value of the specified property.
221         /// </summary>
222         /// <param name="property">The BindableProperty on which to assign a value.</param>
223         /// <param name="value">The value to set.</param>
224         /// <exception cref="ArgumentNullException"> Thrown when property is null. </exception>
225         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
226         [EditorBrowsable(EditorBrowsableState.Never)]
227         public void SetValue(BindableProperty property, object value)
228         {
229             if (true == isCreateByXaml)
230             {
231                 SetValue(property, value, false, true);
232             }
233             else
234             {
235                 if (null == property)
236                 {
237                     throw new ArgumentNullException(nameof(property));
238                 }
239                 property.PropertyChanged?.Invoke(this, null, value);
240
241                 OnPropertyChanged(property.PropertyName);
242                 OnPropertyChangedWithData(property);
243             }
244         }
245
246         internal void SetValueAndForceSendChangeSignal(BindableProperty property, object value)
247         {
248             if (property == null)
249                 throw new ArgumentNullException(nameof(property));
250
251             if (true == isCreateByXaml)
252             {
253                 if (property.IsReadOnly)
254                     throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
255
256                 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
257                     SetValuePrivateFlags.ManuallySet | SetValuePrivateFlags.CheckAccess, true);
258             }
259             else
260             {
261                 property.PropertyChanged?.Invoke(this, null, value);
262             }
263         }
264
265         /// <summary>
266         /// Sets the value of the propertyKey.
267         /// </summary>
268         /// <param name="propertyKey">The BindablePropertyKey on which to assign a value.</param>
269         /// <param name="value">The value to set.</param>
270         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
271         [EditorBrowsable(EditorBrowsableState.Never)]
272         public void SetValue(BindablePropertyKey propertyKey, object value)
273         {
274             if (propertyKey == null)
275                 throw new ArgumentNullException(nameof(propertyKey));
276
277             SetValue(propertyKey.BindableProperty, value, false, false);
278         }
279
280         /// <summary>
281         /// Set the inherited context to a neated element.
282         /// </summary>
283         /// <param name="bindable">The object on which to set the inherited binding context.</param>
284         /// <param name="value">The inherited context to set.</param>
285         /// <exception cref="ArgumentNullException"> Thrown when bindable is null. </exception>
286         [EditorBrowsable(EditorBrowsableState.Never)]
287         public static void SetInheritedBindingContext(BindableObject bindable, object value)
288         {
289             if (null == bindable)
290             {
291                 throw new ArgumentNullException(nameof(bindable));
292             }
293
294             BindablePropertyContext bpContext = bindable.GetContext(BindingContextProperty);
295             if (bpContext != null && ((bpContext.Attributes & BindableContextAttributes.IsManuallySet) != 0))
296                 return;
297
298             object oldContext = bindable.inheritedContext;
299
300             if (ReferenceEquals(oldContext, value))
301                 return;
302
303             if (bpContext != null && oldContext == null)
304                 oldContext = bpContext.Value;
305
306             if (bpContext != null && bpContext.Binding != null)
307             {
308                 bpContext.Binding.Context = value;
309                 bindable.inheritedContext = null;
310             }
311             else
312             {
313                 bindable.inheritedContext = value;
314             }
315
316             bindable.ApplyBindings(skipBindingContext: false, fromBindingContextChanged: true);
317             bindable.OnBindingContextChanged();
318         }
319
320         /// <summary>
321         /// Apply the bindings to BindingContext.
322         /// </summary>
323         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
324         [EditorBrowsable(EditorBrowsableState.Never)]
325         protected void ApplyBindings()
326         {
327             ApplyBindings(skipBindingContext: false, fromBindingContextChanged: false);
328         }
329
330         /// <summary>
331         /// Override this method to execute an action when the BindingContext changes.
332         /// </summary>
333         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
334         [EditorBrowsable(EditorBrowsableState.Never)]
335         protected virtual void OnBindingContextChanged()
336         {
337             BindingContextChanged?.Invoke(this, EventArgs.Empty);
338         }
339
340         /// <summary>
341         /// Call this method from a child class to notify that a change happened on a property.
342         /// </summary>
343         /// <param name="propertyName">The name of the property that changed.</param>
344         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
345         [EditorBrowsable(EditorBrowsableState.Never)]
346         protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
347             => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
348
349         /// <summary>
350         /// Call this method from a child class to notify that a change is going to happen on a property.
351         /// </summary>
352         /// <param name="propertyName">The name of the property that is changing.</param>
353         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
354         [EditorBrowsable(EditorBrowsableState.Never)]
355         protected virtual void OnPropertyChanging([CallerMemberName] string propertyName = null)
356             => PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName));
357         
358         /// <summary>
359         /// Method that is called when a bound property is changed.
360         /// </summary>
361         [EditorBrowsable(EditorBrowsableState.Never)]
362         protected virtual void OnPropertyChangedWithData(BindableProperty property) { }
363
364         /// <summary>
365         /// Unapplies all previously set bindings.
366         /// </summary>
367         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
368         [EditorBrowsable(EditorBrowsableState.Never)]
369         protected void UnapplyBindings()
370         {
371             for (int i = 0, _propertiesCount = properties.Count; i < _propertiesCount; i++)
372             {
373                 BindablePropertyContext context = properties[i];
374                 if (context.Binding == null)
375                     continue;
376
377                 context.Binding.Unapply();
378             }
379         }
380
381         internal bool GetIsBound(BindableProperty targetProperty)
382         {
383             if (targetProperty == null)
384                 throw new ArgumentNullException(nameof(targetProperty));
385
386             BindablePropertyContext bpcontext = GetContext(targetProperty);
387             return bpcontext != null && bpcontext.Binding != null;
388         }
389
390         /// <summary>
391         /// Returns the value that is contained the BindableProperty.
392         /// </summary>
393         /// <param name="property0">The BindableProperty instance.</param>
394         /// <param name="property1">The BindableProperty instance.</param>
395         /// <returns>The value that is contained the BindableProperty</returns>
396         internal object[] GetValues(BindableProperty property0, BindableProperty property1)
397         {
398             var values = new object[2];
399
400             for (var i = 0; i < properties.Count; i++)
401             {
402                 BindablePropertyContext context = properties[i];
403
404                 if (ReferenceEquals(context.Property, property0))
405                 {
406                     values[0] = context.Value;
407                     property0 = null;
408                 }
409                 else if (ReferenceEquals(context.Property, property1))
410                 {
411                     values[1] = context.Value;
412                     property1 = null;
413                 }
414
415                 if (property0 == null && property1 == null)
416                     return values;
417             }
418
419             if (!ReferenceEquals(property0, null))
420                 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
421             if (!ReferenceEquals(property1, null))
422                 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
423
424             return values;
425         }
426
427         /// <summary>
428         /// Returns the value that is contained the BindableProperty.
429         /// </summary>
430         /// <param name="property0">The BindableProperty instance.</param>
431         /// <param name="property1">The BindableProperty instance.</param>
432         /// <param name="property2">The BindableProperty instance.</param>
433         /// <returns>The value that is contained the BindableProperty</returns>
434         internal object[] GetValues(BindableProperty property0, BindableProperty property1, BindableProperty property2)
435         {
436             var values = new object[3];
437
438             for (var i = 0; i < properties.Count; i++)
439             {
440                 BindablePropertyContext context = properties[i];
441
442                 if (ReferenceEquals(context.Property, property0))
443                 {
444                     values[0] = context.Value;
445                     property0 = null;
446                 }
447                 else if (ReferenceEquals(context.Property, property1))
448                 {
449                     values[1] = context.Value;
450                     property1 = null;
451                 }
452                 else if (ReferenceEquals(context.Property, property2))
453                 {
454                     values[2] = context.Value;
455                     property2 = null;
456                 }
457
458                 if (property0 == null && property1 == null && property2 == null)
459                     return values;
460             }
461
462             if (!ReferenceEquals(property0, null))
463                 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
464             if (!ReferenceEquals(property1, null))
465                 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
466             if (!ReferenceEquals(property2, null))
467                 values[2] = property2.DefaultValueCreator == null ? property2.DefaultValue : CreateAndAddContext(property2).Value;
468
469             return values;
470         }
471
472         /// <summary>
473         /// Returns the value that is contained the BindableProperty.
474         /// </summary>
475         /// <param name="properties">The array of the BindableProperty instances</param>
476         /// <returns>The values that is contained the BindableProperty instances.</returns>
477         internal object[] GetValues(params BindableProperty[] properties)
478         {
479             var values = new object[properties.Length];
480             for (var i = 0; i < this.properties.Count; i++)
481             {
482                 var context = this.properties[i];
483                 var index = properties.IndexOf(context.Property);
484                 if (index < 0)
485                     continue;
486                 values[index] = context.Value;
487             }
488             for (var i = 0; i < values.Length; i++)
489             {
490                 if (!ReferenceEquals(values[i], null))
491                     continue;
492                 values[i] = properties[i].DefaultValueCreator == null ? properties[i].DefaultValue : CreateAndAddContext(properties[i]).Value;
493             }
494             return values;
495         }
496
497         internal virtual void OnRemoveDynamicResource(BindableProperty property)
498         {
499         }
500
501         internal virtual void OnSetDynamicResource(BindableProperty property, string key)
502         {
503         }
504
505         internal void RemoveDynamicResource(BindableProperty property)
506         {
507             if (property == null)
508                 throw new ArgumentNullException(nameof(property));
509
510             OnRemoveDynamicResource(property);
511             BindablePropertyContext context = GetOrCreateContext(property);
512             context.Attributes &= ~BindableContextAttributes.IsDynamicResource;
513         }
514
515         internal void SetBinding(BindableProperty targetProperty, BindingBase binding, bool fromStyle)
516         {
517             if (targetProperty == null)
518                 throw new ArgumentNullException(nameof(targetProperty));
519             if (binding == null)
520                 throw new ArgumentNullException(nameof(binding));
521
522             if (fromStyle && !CanBeSetFromStyle(targetProperty))
523                 return;
524
525             var context = GetOrCreateContext(targetProperty);
526             if (fromStyle)
527                 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
528             else
529                 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
530
531             if (context.Binding != null)
532                 context.Binding.Unapply();
533
534             BindingBase oldBinding = context.Binding;
535             context.Binding = binding;
536
537             targetProperty.BindingChanging?.Invoke(this, oldBinding, binding);
538
539             binding.Apply(BindingContext, this, targetProperty);
540         }
541
542         bool CanBeSetFromStyle(BindableProperty property)
543         {
544             var context = GetContext(property);
545             if (context == null)
546                 return true;
547             if ((context.Attributes & BindableContextAttributes.IsSetFromStyle) == BindableContextAttributes.IsSetFromStyle)
548                 return true;
549             if ((context.Attributes & BindableContextAttributes.IsDefaultValue) == BindableContextAttributes.IsDefaultValue)
550                 return true;
551             if ((context.Attributes & BindableContextAttributes.IsDefaultValueCreated) == BindableContextAttributes.IsDefaultValueCreated)
552                 return true;
553             return false;
554         }
555
556         internal void SetDynamicResource(BindableProperty property, string key)
557         {
558             SetDynamicResource(property, key, false);
559         }
560
561         internal void SetDynamicResource(BindableProperty property, string key, bool fromStyle)
562         {
563             if (property == null)
564                 throw new ArgumentNullException(nameof(property));
565             if (string.IsNullOrEmpty(key))
566                 throw new ArgumentNullException(nameof(key));
567             if (fromStyle && !CanBeSetFromStyle(property))
568                 return;
569
570             var context = GetOrCreateContext(property);
571
572             context.Attributes |= BindableContextAttributes.IsDynamicResource;
573             if (fromStyle)
574                 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
575             else
576                 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
577
578             OnSetDynamicResource(property, key);
579         }
580
581         internal void SetValue(BindableProperty property, object value, bool fromStyle)
582         {
583             SetValue(property, value, fromStyle, true);
584         }
585
586         internal void SetValueCore(BindablePropertyKey propertyKey, object value, SetValueFlags attributes = SetValueFlags.None)
587         {
588             SetValueCore(propertyKey.BindableProperty, value, attributes, SetValuePrivateFlags.None, false);
589         }
590
591         /// <summary>
592         /// For internal use.
593         /// </summary>
594         /// <param name="property">The BindableProperty on which to assign a value.</param>
595         /// <param name="value">The value to set</param>
596         /// <param name="attributes">The set value flag</param>
597         [EditorBrowsable(EditorBrowsableState.Never)]
598         internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes = SetValueFlags.None)
599         {
600             SetValueCore(property, value, attributes, SetValuePrivateFlags.Default, false);
601         }
602
603         internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, bool forceSendChangeSignal)
604         {
605             bool checkAccess = (privateAttributes & SetValuePrivateFlags.CheckAccess) != 0;
606             bool manuallySet = (privateAttributes & SetValuePrivateFlags.ManuallySet) != 0;
607             bool silent = (privateAttributes & SetValuePrivateFlags.Silent) != 0;
608             bool fromStyle = (privateAttributes & SetValuePrivateFlags.FromStyle) != 0;
609             bool converted = (privateAttributes & SetValuePrivateFlags.Converted) != 0;
610
611             if (property == null)
612                 throw new ArgumentNullException(nameof(property));
613             if (checkAccess && property.IsReadOnly)
614             {
615                 Debug.WriteLine("Can not set the BindableProperty \"{0}\" because it is readonly.", property.PropertyName);
616                 return;
617             }
618
619             if (!converted && !property.TryConvert(ref value))
620             {
621                 Console.WriteLine($"SetValue : Can not convert {value} to type {property.ReturnType}");
622                 return;
623             }
624
625             if (property.ValidateValue != null && !property.ValidateValue(this, value))
626                 throw new ArgumentException("Value was an invalid value for " + property.PropertyName, nameof(value));
627
628             if (property.CoerceValue != null)
629                 value = property.CoerceValue(this, value);
630
631             BindablePropertyContext context = GetOrCreateContext(property);
632             if (manuallySet)
633             {
634                 context.Attributes |= BindableContextAttributes.IsManuallySet;
635                 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
636             }
637             else
638                 context.Attributes &= ~BindableContextAttributes.IsManuallySet;
639
640             if (fromStyle)
641                 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
642             // else omitted on purpose
643
644             bool currentlyApplying = applying;
645
646             if ((context.Attributes & BindableContextAttributes.IsBeingSet) != 0)
647             {
648                 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
649                 if (delayQueue == null)
650                     context.DelayedSetters = delayQueue = new Queue<SetValueArgs>();
651
652                 delayQueue.Enqueue(new SetValueArgs(property, context, value, currentlyApplying, attributes));
653             }
654             else
655             {
656                 context.Attributes |= BindableContextAttributes.IsBeingSet;
657                 SetValueActual(property, context, value, currentlyApplying, forceSendChangeSignal, attributes, silent);
658
659                 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
660                 if (delayQueue != null)
661                 {
662                     while (delayQueue.Count > 0)
663                     {
664                         SetValueArgs s = delayQueue.Dequeue();
665                         SetValueActual(s.Property, s.Context, s.Value, s.CurrentlyApplying, forceSendChangeSignal, s.Attributes);
666                     }
667
668                     context.DelayedSetters = null;
669                 }
670
671                 context.Attributes &= ~BindableContextAttributes.IsBeingSet;
672             }
673         }
674
675         internal void ApplyBindings(bool skipBindingContext, bool fromBindingContextChanged)
676         {
677             var prop = properties.ToArray();
678             for (int i = 0, propLength = prop.Length; i < propLength; i++)
679             {
680                 BindablePropertyContext context = prop[i];
681                 BindingBase binding = context.Binding;
682                 if (binding == null)
683                     continue;
684
685                 if (skipBindingContext && ReferenceEquals(context.Property, BindingContextProperty))
686                     continue;
687
688                 binding.Unapply(fromBindingContextChanged: fromBindingContextChanged);
689                 binding.Apply(BindingContext, this, context.Property, fromBindingContextChanged: fromBindingContextChanged);
690             }
691         }
692
693         static void BindingContextPropertyBindingChanging(BindableObject bindable, BindingBase oldBindingBase, BindingBase newBindingBase)
694         {
695             object context = bindable.inheritedContext;
696             var oldBinding = oldBindingBase as Binding;
697             var newBinding = newBindingBase as Binding;
698
699             if (context == null && oldBinding != null)
700                 context = oldBinding.Context;
701             if (context != null && newBinding != null)
702                 newBinding.Context = context;
703         }
704
705         static void BindingContextPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
706         {
707             bindable.inheritedContext = null;
708             bindable.ApplyBindings(skipBindingContext: true, fromBindingContextChanged: true);
709             bindable.OnBindingContextChanged();
710         }
711
712         void ClearValue(BindableProperty property, bool fromStyle, bool checkAccess)
713         {
714             if (property == null)
715                 throw new ArgumentNullException(nameof(property));
716
717             if (checkAccess && property.IsReadOnly)
718                 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
719
720             BindablePropertyContext bpcontext = GetContext(property);
721             if (bpcontext == null)
722                 return;
723
724             if (fromStyle && !CanBeSetFromStyle(property))
725                 return;
726
727             object original = bpcontext.Value;
728
729             object newValue = property.GetDefaultValue(this);
730
731             bool same = Equals(original, newValue);
732             if (!same)
733             {
734                 property.PropertyChanging?.Invoke(this, original, newValue);
735
736                 OnPropertyChanging(property.PropertyName);
737             }
738
739             bpcontext.Attributes &= ~BindableContextAttributes.IsManuallySet;
740             bpcontext.Value = newValue;
741             if (property.DefaultValueCreator == null)
742                 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValue;
743             else
744                 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValueCreated;
745
746             if (!same)
747             {
748                 OnPropertyChanged(property.PropertyName);
749                 OnPropertyChangedWithData(property);
750                 property.PropertyChanged?.Invoke(this, original, newValue);
751             }
752         }
753
754         [MethodImpl(MethodImplOptions.AggressiveInlining)]
755         BindablePropertyContext CreateAndAddContext(BindableProperty property)
756         {
757             var context = new BindablePropertyContext { Property = property, Value = property.DefaultValueCreator != null ? property.DefaultValueCreator(this) : property.DefaultValue };
758
759             if (property.DefaultValueCreator == null)
760                 context.Attributes = BindableContextAttributes.IsDefaultValue;
761             else
762                 context.Attributes = BindableContextAttributes.IsDefaultValueCreated;
763
764             properties.Add(context);
765             return context;
766         }
767
768         [MethodImpl(MethodImplOptions.AggressiveInlining)]
769         BindablePropertyContext GetContext(BindableProperty property)
770         {
771             List<BindablePropertyContext> propertyList = properties;
772
773             for (var i = 0; i < propertyList.Count; i++)
774             {
775                 BindablePropertyContext context = propertyList[i];
776                 if (ReferenceEquals(context.Property, property))
777                     return context;
778             }
779
780             return null;
781         }
782
783         [MethodImpl(MethodImplOptions.AggressiveInlining)]
784         BindablePropertyContext GetOrCreateContext(BindableProperty property)
785         {
786             BindablePropertyContext context = GetContext(property);
787             if (context == null)
788             {
789                 context = CreateAndAddContext(property);
790             }
791             else if (property.DefaultValueCreator != null)
792             {
793                 context.Value = property.DefaultValueCreator(this); //Update Value from dali
794             }//added by xb.teng
795
796             return context;
797         }
798
799         void RemoveBinding(BindableProperty property, BindablePropertyContext context)
800         {
801             context.Binding.Unapply();
802
803             property.BindingChanging?.Invoke(this, context.Binding, null);
804
805             context.Binding = null;
806         }
807
808         void SetValue(BindableProperty property, object value, bool fromStyle, bool checkAccess)
809         {
810             if (property == null)
811                 throw new ArgumentNullException(nameof(property));
812
813             if (checkAccess && property.IsReadOnly)
814                 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
815
816             if (fromStyle && !CanBeSetFromStyle(property))
817                 return;
818
819             SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
820                 (fromStyle ? SetValuePrivateFlags.FromStyle : SetValuePrivateFlags.ManuallySet) | (checkAccess ? SetValuePrivateFlags.CheckAccess : 0),
821                 false);
822         }
823
824         void SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, bool forceSendChangeSignal, SetValueFlags attributes, bool silent = false)
825         {
826             object original = context.Value;
827             bool raiseOnEqual = (attributes & SetValueFlags.RaiseOnEqual) != 0;
828             bool clearDynamicResources = (attributes & SetValueFlags.ClearDynamicResource) != 0;
829             bool clearOneWayBindings = (attributes & SetValueFlags.ClearOneWayBindings) != 0;
830             bool clearTwoWayBindings = (attributes & SetValueFlags.ClearTwoWayBindings) != 0;
831
832             bool same = ReferenceEquals(context.Property, BindingContextProperty) ? ReferenceEquals(value, original) : Equals(value, original);
833             if (!silent && (!same || raiseOnEqual))
834             {
835                 property.PropertyChanging?.Invoke(this, original, value);
836
837                 OnPropertyChanging(property.PropertyName);
838             }
839
840             if (!same || raiseOnEqual)
841             {
842                 context.Value = value;
843             }
844
845             context.Attributes &= ~BindableContextAttributes.IsDefaultValue;
846             context.Attributes &= ~BindableContextAttributes.IsDefaultValueCreated;
847
848             if ((context.Attributes & BindableContextAttributes.IsDynamicResource) != 0 && clearDynamicResources)
849                 RemoveDynamicResource(property);
850
851             BindingBase binding = context.Binding;
852             if (binding != null)
853             {
854                 if (clearOneWayBindings && binding.GetRealizedMode(property) == BindingMode.OneWay || clearTwoWayBindings && binding.GetRealizedMode(property) == BindingMode.TwoWay)
855                 {
856                     RemoveBinding(property, context);
857                     binding = null;
858                 }
859             }
860
861             if (!silent)
862             {
863                 if ((!same || raiseOnEqual))
864                 {
865                     property.PropertyChanged?.Invoke(this, original, value);
866
867                     if (binding != null && !currentlyApplying)
868                     {
869                         applying = true;
870                         binding.Apply(true);
871                         applying = false;
872                     }
873
874                     OnPropertyChanged(property.PropertyName);
875                 }
876                 else if (true == same && true == forceSendChangeSignal)
877                 {
878                     if (binding != null && !currentlyApplying)
879                     {
880                         applying = true;
881                         binding.Apply(true);
882                         applying = false;
883                     }
884
885                     OnPropertyChanged(property.PropertyName);
886                 }
887
888                 OnPropertyChangedWithData(property);
889             }
890         }
891
892         [Flags]
893         enum BindableContextAttributes
894         {
895             IsManuallySet = 1 << 0,
896             IsBeingSet = 1 << 1,
897             IsDynamicResource = 1 << 2,
898             IsSetFromStyle = 1 << 3,
899             IsDefaultValue = 1 << 4,
900             IsDefaultValueCreated = 1 << 5,
901         }
902
903         class BindablePropertyContext
904         {
905             public BindableContextAttributes Attributes;
906             public BindingBase Binding;
907             public Queue<SetValueArgs> DelayedSetters;
908             public BindableProperty Property;
909             public object Value;
910         }
911
912         [Flags]
913         internal enum SetValuePrivateFlags
914         {
915             None = 0,
916             CheckAccess = 1 << 0,
917             Silent = 1 << 1,
918             ManuallySet = 1 << 2,
919             FromStyle = 1 << 3,
920             Converted = 1 << 4,
921             Default = CheckAccess
922         }
923
924         class SetValueArgs
925         {
926             public readonly SetValueFlags Attributes;
927             public readonly BindablePropertyContext Context;
928             public readonly bool CurrentlyApplying;
929             public readonly BindableProperty Property;
930             public readonly object Value;
931
932             public SetValueArgs(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes)
933             {
934                 Property = property;
935                 Context = context;
936                 Value = value;
937                 CurrentlyApplying = currentlyApplying;
938                 Attributes = attributes;
939             }
940         }
941     }
942 }
943
944 namespace Tizen.NUI.Binding.Internals
945 {
946     /// <summary>
947     /// SetValueFlags. For internal use.
948     /// </summary>
949     [Flags]
950     [EditorBrowsable(EditorBrowsableState.Never)]
951     public enum SetValueFlags
952     {
953         /// <summary>
954         /// None.
955         /// </summary>
956         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
957         [EditorBrowsable(EditorBrowsableState.Never)]
958         None = 0,
959
960         /// <summary>
961         /// Clear OneWay bindings.
962         /// </summary>
963         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
964         [EditorBrowsable(EditorBrowsableState.Never)]
965         ClearOneWayBindings = 1 << 0,
966
967         /// <summary>
968         /// Clear TwoWay bindings.
969         /// </summary>
970         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
971         [EditorBrowsable(EditorBrowsableState.Never)]
972         ClearTwoWayBindings = 1 << 1,
973
974         /// <summary>
975         /// Clear dynamic resource.
976         /// </summary>
977         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
978         [EditorBrowsable(EditorBrowsableState.Never)]
979         ClearDynamicResource = 1 << 2,
980
981         /// <summary>
982         /// Raise or equal.
983         /// </summary>
984         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
985         [EditorBrowsable(EditorBrowsableState.Never)]
986         RaiseOnEqual = 1 << 3
987     }
988 }