[NUI] Change GetDefaultWindow() to static func (#900)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / XamlBinding / Interactivity / XamlPropertyCondition.cs
1 using System;
2 using System.ComponentModel;
3 using System.Reflection;
4 using Tizen.NUI.Xaml;
5
6 namespace Tizen.NUI.Binding
7 {
8     [ProvideCompiled("Tizen.NUI.Core.XamlC.PassthroughValueProvider")]
9     [AcceptEmptyServiceProvider]
10     internal sealed class XamlPropertyCondition : Condition, IValueProvider
11     {
12         readonly BindableProperty _stateProperty;
13
14         BindableProperty _property;
15         object _triggerValue;
16
17         public XamlPropertyCondition()
18         {
19             _stateProperty = BindableProperty.CreateAttached("State", typeof(bool), typeof(XamlPropertyCondition), false, propertyChanged: OnStatePropertyChanged);
20         }
21
22         public BindableProperty Property
23         {
24             get { return _property; }
25             set
26             {
27                 if (_property == value)
28                     return;
29                 if (IsSealed)
30                     throw new InvalidOperationException("Can not change Property once the Trigger has been applied.");
31                 _property = value;
32
33                 //convert the value
34                 if (_property != null && s_valueConverter != null)
35                 {
36                     Func<MemberInfo> minforetriever = () => _property.DeclaringType.GetRuntimeProperty(_property.PropertyName);
37                     Value = s_valueConverter.Convert(Value, _property.ReturnType, minforetriever, null);
38                 }
39             }
40         }
41
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 Trigger has been applied.");
51
52                 //convert the value
53                 if (_property != null && s_valueConverter != null)
54                 {
55                     Func<MemberInfo> minforetriever = () => _property.DeclaringType.GetRuntimeProperty(_property.PropertyName);
56                     _triggerValue = s_valueConverter.Convert(value, _property.ReturnType, minforetriever, null);
57                 }
58                 else
59                 {
60                     _triggerValue = value;
61                 }
62                 
63             }
64         }
65
66         object IValueProvider.ProvideValue(IServiceProvider serviceProvider)
67         {
68             //This is no longer required
69             return this;
70         }
71
72         internal override bool GetState(BindableObject bindable)
73         {
74             return (bool)bindable.GetValue(_stateProperty);
75         }
76
77         static IValueConverterProvider s_valueConverter = DependencyService.Get<IValueConverterProvider>();
78
79         internal override void SetUp(BindableObject bindable)
80         {
81             object newvalue = bindable.GetValue(Property);
82             bool newState = (newvalue == Value) || (newvalue != null && newvalue.Equals(Value));
83             bindable.SetValue(_stateProperty, newState);
84             bindable.PropertyChanged += OnAttachedObjectPropertyChanged;
85         }
86
87         internal override void TearDown(BindableObject bindable)
88         {
89             bindable.ClearValue(_stateProperty);
90             bindable.PropertyChanged -= OnAttachedObjectPropertyChanged;
91         }
92
93         void OnAttachedObjectPropertyChanged(object sender, PropertyChangedEventArgs e)
94         {
95             var bindable = (BindableObject)sender;
96             var oldState = (bool)bindable.GetValue(_stateProperty);
97
98             if (Property == null)
99                 return;
100             if (e.PropertyName != Property.PropertyName)
101                 return;
102             object newvalue = bindable.GetValue(Property);
103             bool newstate = (newvalue == Value) || (newvalue != null && newvalue.Equals(Value));
104             if (oldState != newstate)
105                 bindable.SetValue(_stateProperty, newstate);
106         }
107
108         void OnStatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
109         {
110             if ((bool)oldValue == (bool)newValue)
111                 return;
112
113             ConditionChanged?.Invoke(bindable, (bool)oldValue, (bool)newValue);
114         }
115     }
116 }