Merge remote-tracking branch 'origin/master' into tizen
[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), null, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
40             {
41                 var bindableObject = (BindableObject)bindable;
42                 if (newValue != null)
43                 {
44                     bindableObject.bindingContext = newValue;
45                     bindableObject.FlushBinding();
46
47                     if (newValue is BindableObject targetBindableObject)
48                     {
49                         targetBindableObject.IsCreateByXaml = true;
50                     }
51                 }
52             }),
53             defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
54             {
55                 if (null != bindable.bindingContext)
56                 {
57                     return bindable.bindingContext;
58                 }
59
60                 if (bindable is Container container)
61                 {
62                     return container.Parent?.BindingContext;
63                 }
64                 else
65                 {
66                     return null;
67                 }
68             }));
69
70         readonly List<BindablePropertyContext> properties = new List<BindablePropertyContext>(4);
71
72         bool applying;
73         object inheritedContext;
74
75         private object bindingContext;
76
77         /// <summary>
78         /// Gets or sets object that contains the properties that will be targeted by the bound properties that belong to this BindableObject.
79         /// </summary>
80         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
81         [EditorBrowsable(EditorBrowsableState.Never)]
82         public object BindingContext
83         {
84             get { return inheritedContext ?? GetValue(BindingContextProperty); }
85             set { SetValue(BindingContextProperty, value); }
86         }
87
88         void IDynamicResourceHandler.SetDynamicResource(BindableProperty property, string key)
89         {
90             SetDynamicResource(property, key, false);
91         }
92
93         /// <summary>
94         /// Raised when a property has changed.
95         /// </summary>
96         /// <since_tizen> 9 </since_tizen>
97         public event PropertyChangedEventHandler PropertyChanged;
98
99         /// <summary>Copy properties of other ViewStyle to this.</summary>
100         /// <param name="other">The other BindableProperty merge to this.</param>
101         [EditorBrowsable(EditorBrowsableState.Never)]
102         public virtual void CopyFrom(BindableObject other)
103         {
104             if (null == other) return;
105
106             Type type1 = this.GetType();
107             BindableProperty.GetBindablePropertysOfType(type1, out var nameToBindableProperty1);
108
109             Type type2 = other.GetType();
110             BindableProperty.GetBindablePropertysOfType(type2, out var nameToBindableProperty2);
111
112             if (null != nameToBindableProperty1)
113             {
114                 foreach (KeyValuePair<string, BindableProperty> keyValuePair in nameToBindableProperty1)
115                 {
116                     nameToBindableProperty2.TryGetValue(keyValuePair.Key, out var bindableProperty);
117
118                     if (null != bindableProperty)
119                     {
120                         object value = other.GetValue(bindableProperty);
121
122                         if (null != value)
123                         {
124                             SetValue(keyValuePair.Value, value);
125                         }
126                     }
127                 }
128             }
129         }
130
131         /// <summary>
132         /// Raised whenever the BindingContext property changes.
133         /// </summary>
134         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
135         [EditorBrowsable(EditorBrowsableState.Never)]
136         public event EventHandler BindingContextChanged;
137
138         internal void ClearValue(BindableProperty property, bool fromStyle)
139         {
140             ClearValue(property, fromStyle: fromStyle, checkAccess: true);
141         }
142
143         /// <summary>
144         /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue.
145         /// </summary>
146         /// <param name="property">The BindableProperty to clear</param>
147         internal void ClearValue(BindableProperty property)
148         {
149             ClearValue(property, fromStyle: false, checkAccess: true);
150         }
151
152         /// <summary>
153         /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue for the property that is identified by propertyKey.
154         /// </summary>
155         /// <param name="propertyKey">The BindablePropertyKey that identifies the BindableProperty to clear.</param>
156         internal void ClearValue(BindablePropertyKey propertyKey)
157         {
158             if (propertyKey == null)
159                 throw new ArgumentNullException(nameof(propertyKey));
160
161             ClearValue(propertyKey.BindableProperty, fromStyle: false, checkAccess: false);
162         }
163
164         /// <summary>
165         /// Return true if the target property exists and has been set.
166         /// </summary>
167         /// <param name="targetProperty">The target property</param>
168         /// <returns>return true if the target property exists and has been set</returns>
169         internal bool IsSet(BindableProperty targetProperty)
170         {
171             if (targetProperty == null)
172                 throw new ArgumentNullException(nameof(targetProperty));
173
174             var bpcontext = GetContext(targetProperty);
175             return bpcontext != null
176                 && (bpcontext.Attributes & BindableContextAttributes.IsDefaultValue) == 0;
177         }
178
179         /// <summary>
180         /// Returns the value that is contained the BindableProperty.
181         /// </summary>
182         /// <param name="property">The BindableProperty for which to get the value.</param>
183         /// <returns>The value that is contained the BindableProperty</returns>
184         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
185         [EditorBrowsable(EditorBrowsableState.Never)]
186         public object GetValue(BindableProperty property)
187         {
188             if (property == null)
189                 throw new ArgumentNullException(nameof(property));
190
191             BindablePropertyContext context = property.DefaultValueCreator != null ? GetOrCreateContext(property) : GetContext(property);
192
193             if (context == null)
194                 return property.DefaultValue;
195
196             return context.Value;
197         }
198
199         /// <summary>
200         /// Raised when a property is about to change.
201         /// </summary>
202         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
203         [EditorBrowsable(EditorBrowsableState.Never)]
204         public event PropertyChangingEventHandler PropertyChanging;
205
206         /// <summary>
207         /// Removes a previously set binding.
208         /// </summary>
209         /// <param name="property">The BindableProperty from which to remove bindings.</param>
210         internal void RemoveBinding(BindableProperty property)
211         {
212             if (property == null)
213                 throw new ArgumentNullException(nameof(property));
214
215             BindablePropertyContext context = GetContext(property);
216             if (context == null || context.Binding == null)
217                 return;
218
219             RemoveBinding(property, context);
220         }
221
222         /// <summary>
223         /// Assigns a binding to a property.
224         /// </summary>
225         /// <param name="targetProperty">The BindableProperty on which to set a binding.</param>
226         /// <param name="binding">The binding to set.</param>
227         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
228         [EditorBrowsable(EditorBrowsableState.Never)]
229         public void SetBinding(BindableProperty targetProperty, BindingBase binding)
230         {
231             SetBinding(targetProperty, binding, false);
232         }
233
234         private bool isCreateByXaml = false;
235         /// Only used by the IL of xaml, will never changed to not hidden.
236         [EditorBrowsable(EditorBrowsableState.Never)]
237         public virtual bool IsCreateByXaml
238         {
239             get
240             {
241                 return isCreateByXaml;
242             }
243             set
244             {
245                 isCreateByXaml = value;
246             }
247         }
248
249         /// <summary>
250         /// Sets the value of the specified property.
251         /// </summary>
252         /// <param name="property">The BindableProperty on which to assign a value.</param>
253         /// <param name="value">The value to set.</param>
254         /// <exception cref="ArgumentNullException"> Thrown when property is null. </exception>
255         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
256         [EditorBrowsable(EditorBrowsableState.Never)]
257         public void SetValue(BindableProperty property, object value)
258         {
259             if (true == isCreateByXaml)
260             {
261                 SetValue(property, value, false, true);
262             }
263             else
264             {
265                 if (null == property)
266                 {
267                     throw new ArgumentNullException(nameof(property));
268                 }
269
270                 if (null == property.DefaultValueCreator)
271                 {
272                     BindablePropertyContext context = GetOrCreateContext(property);
273                     if (null != context)
274                     {
275                         context.Value = value;
276                     }
277                 }
278
279                 property.PropertyChanged?.Invoke(this, null, value);
280
281                 OnPropertyChanged(property.PropertyName);
282                 OnPropertyChangedWithData(property);
283             }
284         }
285
286         internal void SetValueAndForceSendChangeSignal(BindableProperty property, object value)
287         {
288             if (property == null)
289                 throw new ArgumentNullException(nameof(property));
290
291             if (true == isCreateByXaml)
292             {
293                 if (property.IsReadOnly)
294                     throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
295
296                 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
297                     SetValuePrivateFlags.ManuallySet | SetValuePrivateFlags.CheckAccess, true);
298             }
299             else
300             {
301                 property.PropertyChanged?.Invoke(this, null, value);
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             IsCreateByXaml = 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         static void BindingContextPropertyBindingChanging(BindableObject bindable, BindingBase oldBindingBase, BindingBase newBindingBase)
756         {
757             object context = bindable.inheritedContext;
758             var oldBinding = oldBindingBase as Binding;
759             var newBinding = newBindingBase as Binding;
760
761             if (context == null && oldBinding != null)
762                 context = oldBinding.Context;
763             if (context != null && newBinding != null)
764                 newBinding.Context = context;
765         }
766
767         void ClearValue(BindableProperty property, bool fromStyle, bool checkAccess)
768         {
769             if (property == null)
770                 throw new ArgumentNullException(nameof(property));
771
772             if (checkAccess && property.IsReadOnly)
773                 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
774
775             BindablePropertyContext bpcontext = GetContext(property);
776             if (bpcontext == null)
777                 return;
778
779             if (fromStyle && !CanBeSetFromStyle(property))
780                 return;
781
782             object original = bpcontext.Value;
783
784             object newValue = property.GetDefaultValue(this);
785
786             bool same = Equals(original, newValue);
787             if (!same)
788             {
789                 property.PropertyChanging?.Invoke(this, original, newValue);
790
791                 OnPropertyChanging(property.PropertyName);
792             }
793
794             bpcontext.Attributes &= ~BindableContextAttributes.IsManuallySet;
795             bpcontext.Value = newValue;
796             if (property.DefaultValueCreator == null)
797                 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValue;
798             else
799                 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValueCreated;
800
801             if (!same)
802             {
803                 OnPropertyChanged(property.PropertyName);
804                 OnPropertyChangedWithData(property);
805                 property.PropertyChanged?.Invoke(this, original, newValue);
806             }
807         }
808
809         [MethodImpl(MethodImplOptions.AggressiveInlining)]
810         BindablePropertyContext CreateAndAddContext(BindableProperty property)
811         {
812             var context = new BindablePropertyContext { Property = property, Value = property.DefaultValueCreator != null ? property.DefaultValueCreator(this) : property.DefaultValue };
813
814             if (property.DefaultValueCreator == null)
815                 context.Attributes = BindableContextAttributes.IsDefaultValue;
816             else
817                 context.Attributes = BindableContextAttributes.IsDefaultValueCreated;
818
819             properties.Add(context);
820             return context;
821         }
822
823         [MethodImpl(MethodImplOptions.AggressiveInlining)]
824         BindablePropertyContext GetContext(BindableProperty property)
825         {
826             List<BindablePropertyContext> propertyList = properties;
827
828             for (var i = 0; i < propertyList.Count; i++)
829             {
830                 BindablePropertyContext context = propertyList[i];
831                 if (ReferenceEquals(context.Property, property))
832                     return context;
833             }
834
835             return null;
836         }
837
838         [MethodImpl(MethodImplOptions.AggressiveInlining)]
839         BindablePropertyContext GetOrCreateContext(BindableProperty property)
840         {
841             BindablePropertyContext context = GetContext(property);
842             if (context == null)
843             {
844                 context = CreateAndAddContext(property);
845             }
846             else if (property.DefaultValueCreator != null)
847             {
848                 context.Value = property.DefaultValueCreator(this); //Update Value from dali
849             }//added by xb.teng
850
851             return context;
852         }
853
854         void RemoveBinding(BindableProperty property, BindablePropertyContext context)
855         {
856             context.Binding.Unapply();
857
858             property.BindingChanging?.Invoke(this, context.Binding, null);
859
860             context.Binding = null;
861         }
862
863         internal void SetValue(BindableProperty property, object value, bool fromStyle, bool checkAccess)
864         {
865             if (property == null)
866                 throw new ArgumentNullException(nameof(property));
867
868             if (checkAccess && property.IsReadOnly)
869                 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
870
871             if (fromStyle && !CanBeSetFromStyle(property))
872                 return;
873
874             SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
875                 (fromStyle ? SetValuePrivateFlags.FromStyle : SetValuePrivateFlags.ManuallySet) | (checkAccess ? SetValuePrivateFlags.CheckAccess : 0),
876                 false);
877         }
878
879         void SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, bool forceSendChangeSignal, SetValueFlags attributes, bool silent = false)
880         {
881             object original = context.Value;
882             bool raiseOnEqual = (attributes & SetValueFlags.RaiseOnEqual) != 0;
883             bool clearDynamicResources = (attributes & SetValueFlags.ClearDynamicResource) != 0;
884             bool clearOneWayBindings = (attributes & SetValueFlags.ClearOneWayBindings) != 0;
885             bool clearTwoWayBindings = (attributes & SetValueFlags.ClearTwoWayBindings) != 0;
886
887             bool same = ReferenceEquals(context.Property, BindingContextProperty) ? ReferenceEquals(value, original) : Equals(value, original);
888             if (!silent && (!same || raiseOnEqual))
889             {
890                 property.PropertyChanging?.Invoke(this, original, value);
891
892                 OnPropertyChanging(property.PropertyName);
893             }
894
895             if (!same || raiseOnEqual)
896             {
897                 context.Value = value;
898             }
899
900             context.Attributes &= ~BindableContextAttributes.IsDefaultValue;
901             context.Attributes &= ~BindableContextAttributes.IsDefaultValueCreated;
902
903             if ((context.Attributes & BindableContextAttributes.IsDynamicResource) != 0 && clearDynamicResources)
904                 RemoveDynamicResource(property);
905
906             BindingBase binding = context.Binding;
907             if (binding != null)
908             {
909                 if (clearOneWayBindings && binding.GetRealizedMode(property) == BindingMode.OneWay || clearTwoWayBindings && binding.GetRealizedMode(property) == BindingMode.TwoWay)
910                 {
911                     RemoveBinding(property, context);
912                     binding = null;
913                 }
914             }
915
916             PropertyToGroup.TryGetValue(property, out HashSet<BindableProperty> propertyGroup);
917
918             if (!silent)
919             {
920                 if ((!same || raiseOnEqual))
921                 {
922                     property.PropertyChanged?.Invoke(this, original, value);
923
924                     if (binding != null && !currentlyApplying)
925                     {
926                         applying = true;
927                         binding.Apply(true);
928                         applying = false;
929                     }
930
931                     OnPropertyChanged(property.PropertyName);
932
933                     if (null != propertyGroup)
934                     {
935                         foreach (var relativeProperty in propertyGroup)
936                         {
937                             if (relativeProperty != property)
938                             {
939                                 var relativeContext = GetOrCreateContext(relativeProperty);
940                                 var relativeBinding = relativeContext.Binding;
941
942                                 if (null != relativeBinding)
943                                 {
944                                     applying = true;
945                                     relativeBinding.Apply(true);
946                                     applying = false;
947                                 }
948
949                                 OnPropertyChanged(relativeProperty.PropertyName);
950                             }
951                         }
952                     }
953                 }
954                 else if (true == same && true == forceSendChangeSignal)
955                 {
956                     if (binding != null && !currentlyApplying)
957                     {
958                         applying = true;
959                         binding.Apply(true);
960                         applying = false;
961                     }
962
963                     OnPropertyChanged(property.PropertyName);
964
965                     if (null != propertyGroup)
966                     {
967                         foreach (var relativeProperty in propertyGroup)
968                         {
969                             if (relativeProperty != property)
970                             {
971                                 var relativeContext = GetOrCreateContext(relativeProperty);
972                                 var relativeBinding = relativeContext.Binding;
973
974                                 if (null != relativeBinding)
975                                 {
976                                     applying = true;
977                                     relativeBinding.Apply(true);
978                                     applying = false;
979                                 }
980
981                                 OnPropertyChanged(relativeProperty.PropertyName);
982                             }
983                         }
984                     }
985                 }
986
987                 OnPropertyChangedWithData(property);
988             }
989         }
990
991         private static Dictionary<BindableProperty, HashSet<BindableProperty>> PropertyToGroup { get; }
992             = new Dictionary<BindableProperty, HashSet<BindableProperty>>();
993
994         [Flags]
995         enum BindableContextAttributes
996         {
997             IsManuallySet = 1 << 0,
998             IsBeingSet = 1 << 1,
999             IsDynamicResource = 1 << 2,
1000             IsSetFromStyle = 1 << 3,
1001             IsDefaultValue = 1 << 4,
1002             IsDefaultValueCreated = 1 << 5,
1003         }
1004
1005         class BindablePropertyContext
1006         {
1007             public BindableContextAttributes Attributes;
1008             public BindingBase Binding;
1009             public Queue<SetValueArgs> DelayedSetters;
1010             public BindableProperty Property;
1011             public object Value;
1012         }
1013
1014         [Flags]
1015         internal enum SetValuePrivateFlags
1016         {
1017             None = 0,
1018             CheckAccess = 1 << 0,
1019             Silent = 1 << 1,
1020             ManuallySet = 1 << 2,
1021             FromStyle = 1 << 3,
1022             Converted = 1 << 4,
1023             Default = CheckAccess
1024         }
1025
1026         class SetValueArgs
1027         {
1028             public readonly SetValueFlags Attributes;
1029             public readonly BindablePropertyContext Context;
1030             public readonly bool CurrentlyApplying;
1031             public readonly BindableProperty Property;
1032             public readonly object Value;
1033
1034             public SetValueArgs(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes)
1035             {
1036                 Property = property;
1037                 Context = context;
1038                 Value = value;
1039                 CurrentlyApplying = currentlyApplying;
1040                 Attributes = attributes;
1041             }
1042         }
1043
1044         internal void AddChildBindableObject(BindableObject child)
1045         {
1046             if (null != child)
1047             {
1048                 children.Add(child);
1049                 child.FlushBinding();
1050             }
1051         }
1052
1053         internal void RemoveChildBindableObject(BindableObject child)
1054         {
1055             children.Remove(child);
1056         }
1057
1058         private List<BindableObject> children = new List<BindableObject>();
1059
1060         private void FlushBinding()
1061         {
1062             ApplyBindings(skipBindingContext: true, fromBindingContextChanged: true);
1063             OnBindingContextChanged();
1064
1065             foreach (var child in children)
1066             {
1067                 child.FlushBinding();
1068             }
1069         }
1070     }
1071 }
1072
1073 namespace Tizen.NUI.Binding.Internals
1074 {
1075     /// <summary>
1076     /// SetValueFlags. For internal use.
1077     /// </summary>
1078     [Flags]
1079     [EditorBrowsable(EditorBrowsableState.Never)]
1080     public enum SetValueFlags
1081     {
1082         /// <summary>
1083         /// None.
1084         /// </summary>
1085         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1086         [EditorBrowsable(EditorBrowsableState.Never)]
1087         None = 0,
1088
1089         /// <summary>
1090         /// Clear OneWay bindings.
1091         /// </summary>
1092         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1093         [EditorBrowsable(EditorBrowsableState.Never)]
1094         ClearOneWayBindings = 1 << 0,
1095
1096         /// <summary>
1097         /// Clear TwoWay bindings.
1098         /// </summary>
1099         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1100         [EditorBrowsable(EditorBrowsableState.Never)]
1101         ClearTwoWayBindings = 1 << 1,
1102
1103         /// <summary>
1104         /// Clear dynamic resource.
1105         /// </summary>
1106         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1107         [EditorBrowsable(EditorBrowsableState.Never)]
1108         ClearDynamicResource = 1 << 2,
1109
1110         /// <summary>
1111         /// Raise or equal.
1112         /// </summary>
1113         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1114         [EditorBrowsable(EditorBrowsableState.Never)]
1115         RaiseOnEqual = 1 << 3
1116     }
1117 }