2 using System.Collections.Generic;
3 using System.Reflection;
4 using Tizen.NUI.StyleSheets;
6 namespace Tizen.NUI.Binding
8 [ContentProperty("Setters")]
9 internal sealed class Style : IStyle
11 internal const string StyleClassPrefix = "Tizen.NUI.Binding.StyleClass.";
13 readonly BindableProperty _basedOnResourceProperty = BindableProperty.CreateAttached("BasedOnResource", typeof(Style), typeof(Style), default(Style),
14 propertyChanged: OnBasedOnResourceChanged);
16 readonly List<WeakReference<BindableObject>> _targets = new List<WeakReference<BindableObject>>(4);
20 string _baseResourceKey;
22 IList<Behavior> _behaviors;
24 IList<TriggerBase> _triggers;
26 public Style([TypeConverter(typeof(TypeTypeConverter))] [Parameter("TargetType")] Type targetType)
28 if (targetType == null)
29 throw new ArgumentNullException("targetType");
31 TargetType = targetType;
32 Setters = new List<Setter>();
35 public bool ApplyToDerivedTypes { get; set; }
39 get { return _basedOnStyle; }
42 if (_basedOnStyle == value)
44 if (!ValidateBasedOn(value))
45 throw new ArgumentException("BasedOn.TargetType is not compatible with TargetType");
46 Style oldValue = _basedOnStyle;
47 _basedOnStyle = value;
48 BasedOnChanged(oldValue, value);
50 BaseResourceKey = null;
54 public string BaseResourceKey
56 get { return _baseResourceKey; }
59 if (_baseResourceKey == value)
61 _baseResourceKey = value;
62 //update all DynamicResources
63 foreach (WeakReference<BindableObject> bindableWr in _targets)
65 BindableObject target;
66 if (!bindableWr.TryGetTarget(out target))
68 target.RemoveDynamicResource(_basedOnResourceProperty);
70 target.SetDynamicResource(_basedOnResourceProperty, value);
77 internal IList<Behavior> Behaviors
79 get { return _behaviors ?? (_behaviors = new AttachedCollection<Behavior>()); }
82 public bool CanCascade { get; set; }
84 public string Class { get; set; }
86 public IList<Setter> Setters { get; }
88 internal IList<TriggerBase> Triggers
90 get { return _triggers ?? (_triggers = new AttachedCollection<TriggerBase>()); }
93 void IStyle.Apply(BindableObject bindable)
95 _targets.Add(new WeakReference<BindableObject>(bindable));
96 if (BaseResourceKey != null)
97 bindable.SetDynamicResource(_basedOnResourceProperty, BaseResourceKey);
98 ApplyCore(bindable, BasedOn ?? GetBasedOnResource(bindable));
101 public Type TargetType { get; }
103 void IStyle.UnApply(BindableObject bindable)
105 UnApplyCore(bindable, BasedOn ?? GetBasedOnResource(bindable));
106 bindable.RemoveDynamicResource(_basedOnResourceProperty);
107 _targets.RemoveAll(wr =>
109 BindableObject target;
110 return wr.TryGetTarget(out target) && target == bindable;
114 internal bool CanBeAppliedTo(Type targetType)
116 if (TargetType == targetType)
118 if (!ApplyToDerivedTypes)
122 targetType = targetType.GetTypeInfo().BaseType;
123 if (TargetType == targetType)
125 } while (targetType != typeof(Element));
129 void ApplyCore(BindableObject bindable, Style basedOn)
132 ((IStyle)basedOn).Apply(bindable);
134 foreach (Setter setter in Setters)
135 setter.Apply(bindable, true);
136 ((AttachedCollection<Behavior>)Behaviors).AttachTo(bindable);
137 ((AttachedCollection<TriggerBase>)Triggers).AttachTo(bindable);
140 void BasedOnChanged(Style oldValue, Style newValue)
142 foreach (WeakReference<BindableObject> bindableRef in _targets)
144 BindableObject bindable;
145 if (!bindableRef.TryGetTarget(out bindable))
148 UnApplyCore(bindable, oldValue);
149 ApplyCore(bindable, newValue);
153 Style GetBasedOnResource(BindableObject bindable)
155 return (Style)bindable.GetValue(_basedOnResourceProperty);
158 static void OnBasedOnResourceChanged(BindableObject bindable, object oldValue, object newValue)
160 // Style style = (bindable as /*VisualElement*/BaseHandle).Style;
161 // if (style == null)
163 // style.UnApplyCore(bindable, (Style)oldValue);
164 // style.ApplyCore(bindable, (Style)newValue);
167 void UnApplyCore(BindableObject bindable, Style basedOn)
169 ((AttachedCollection<TriggerBase>)Triggers).DetachFrom(bindable);
170 ((AttachedCollection<Behavior>)Behaviors).DetachFrom(bindable);
171 foreach (Setter setter in Setters)
172 setter.UnApply(bindable, true);
175 ((IStyle)basedOn).UnApply(bindable);
178 bool ValidateBasedOn(Style value)
182 return value.TargetType.IsAssignableFrom(TargetType);