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