[NUI] Adjust directory (#903)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / XamlBinding / Interactivity / BindingCondition.cs
1 using System;
2 using System.ComponentModel;
3 using Tizen.NUI.Xaml;
4
5 namespace Tizen.NUI.Binding
6 {
7     /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
8     [EditorBrowsable(EditorBrowsableState.Never)]
9     [ProvideCompiled("Tizen.NUI.Xaml.Core.XamlC.PassthroughValueProvider")]
10     [AcceptEmptyServiceProvider]
11     public sealed class BindingCondition : Condition, IValueProvider
12     {
13         readonly BindableProperty _boundProperty;
14
15         BindingBase _binding;
16         object _triggerValue;
17
18         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
19         [EditorBrowsable(EditorBrowsableState.Never)]
20         public BindingCondition()
21         {
22             _boundProperty = BindableProperty.CreateAttached("Bound", typeof(object), typeof(BindingCondition), null, propertyChanged: OnBoundPropertyChanged);
23         }
24
25         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
26         [EditorBrowsable(EditorBrowsableState.Never)]
27         public BindingBase Binding
28         {
29             get { return _binding; }
30             set
31             {
32                 if (_binding == value)
33                     return;
34                 if (IsSealed)
35                     throw new InvalidOperationException("Can not change Binding once the Condition has been applied.");
36                 _binding = value;
37             }
38         }
39
40         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
41         [EditorBrowsable(EditorBrowsableState.Never)]
42         public object Value
43         {
44             get { return _triggerValue; }
45             set
46             {
47                 if (_triggerValue == value)
48                     return;
49                 if (IsSealed)
50                     throw new InvalidOperationException("Can not change Value once the Condition has been applied.");
51                 _triggerValue = value;
52             }
53         }
54
55         object IValueProvider.ProvideValue(IServiceProvider serviceProvider)
56         {
57             //This is no longer required
58             return this;
59         }
60
61         internal override bool GetState(BindableObject bindable)
62         {
63             object newValue = bindable.GetValue(_boundProperty);
64             return EqualsToValue(newValue);
65         }
66
67         internal override void SetUp(BindableObject bindable)
68         {
69             if (Binding != null)
70                 bindable.SetBinding(_boundProperty, Binding.Clone());
71         }
72
73         internal override void TearDown(BindableObject bindable)
74         {
75             bindable.RemoveBinding(_boundProperty);
76             bindable.ClearValue(_boundProperty);
77         }
78
79         static IValueConverterProvider s_valueConverter = DependencyService.Get<IValueConverterProvider>();
80
81         bool EqualsToValue(object other)
82         {
83             if ((other == Value) || (other != null && other.Equals(Value)))
84                 return true;
85
86             object converted = null;
87             if (s_valueConverter != null)
88                 converted = s_valueConverter.Convert(Value, other != null ? other.GetType() : typeof(object), null, null);
89             else
90                 return false;
91
92             return (other == converted) || (other != null && other.Equals(converted));
93         }
94
95         void OnBoundPropertyChanged(BindableObject bindable, object oldValue, object newValue)
96         {
97             bool oldState = EqualsToValue(oldValue);
98             bool newState = EqualsToValue(newValue);
99
100             if (newState == oldState)
101                 return;
102
103             if (ConditionChanged != null)
104                 ConditionChanged(bindable, oldState, newState);
105         }
106     }
107 }