2 using System.Collections.Generic;
3 using System.Reflection;
4 using Tizen.NUI.StyleSheets;
5 using System.ComponentModel;
7 namespace Tizen.NUI.Binding
9 [EditorBrowsable(EditorBrowsableState.Never)]
10 [ContentProperty("Setters")]
11 public sealed class XamlStyle : IStyle
13 internal const string StyleClassPrefix = "Tizen.NUI.Binding.StyleClass.";
15 readonly BindableProperty basedOnResourceProperty = BindableProperty.CreateAttached("BasedOnResource", typeof(XamlStyle), typeof(XamlStyle), default(XamlStyle),
16 propertyChanged: OnBasedOnResourceChanged);
18 readonly List<WeakReference<BindableObject>> targets = new List<WeakReference<BindableObject>>(4);
20 XamlStyle basedOnStyle;
22 string baseResourceKey;
24 IList<Behavior> behaviors;
26 IList<TriggerBase> triggers;
28 [EditorBrowsable(EditorBrowsableState.Never)]
29 public XamlStyle([TypeConverter(typeof(TypeTypeConverter))][Parameter("TargetType")] Type targetType)
31 if (targetType == null)
32 throw new ArgumentNullException(nameof(targetType));
34 TargetType = targetType;
35 Setters = new List<Setter>();
38 [EditorBrowsable(EditorBrowsableState.Never)]
39 public bool ApplyToDerivedTypes { get; set; }
41 [EditorBrowsable(EditorBrowsableState.Never)]
42 public XamlStyle BasedOn
44 get { return basedOnStyle; }
47 if (basedOnStyle == value)
49 if (!ValidateBasedOn(value))
50 throw new ArgumentException("BasedOn.TargetType is not compatible with TargetType");
51 XamlStyle oldValue = basedOnStyle;
53 BasedOnChanged(oldValue, value);
55 BaseResourceKey = null;
59 [EditorBrowsable(EditorBrowsableState.Never)]
60 public string BaseResourceKey
62 get { return baseResourceKey; }
65 if (baseResourceKey == value)
67 baseResourceKey = value;
68 //update all DynamicResources
69 foreach (WeakReference<BindableObject> bindableWr in targets)
71 BindableObject target;
72 if (!bindableWr.TryGetTarget(out target))
74 target.RemoveDynamicResource(basedOnResourceProperty);
76 target.SetDynamicResource(basedOnResourceProperty, value);
83 internal IList<Behavior> Behaviors
85 get { return behaviors ?? (behaviors = new AttachedCollection<Behavior>()); }
88 [EditorBrowsable(EditorBrowsableState.Never)]
89 public bool CanCascade { get; set; }
91 [EditorBrowsable(EditorBrowsableState.Never)]
92 public string Class { get; set; }
94 [EditorBrowsable(EditorBrowsableState.Never)]
95 public IList<Setter> Setters { get; }
97 [EditorBrowsable(EditorBrowsableState.Never)]
98 public IList<TriggerBase> Triggers
100 get { return triggers ?? (triggers = new AttachedCollection<TriggerBase>()); }
103 void IStyle.Apply(BindableObject bindable)
105 targets.Add(new WeakReference<BindableObject>(bindable));
106 if (BaseResourceKey != null)
107 bindable.SetDynamicResource(basedOnResourceProperty, BaseResourceKey);
108 ApplyCore(bindable, BasedOn ?? GetBasedOnResource(bindable));
111 [EditorBrowsable(EditorBrowsableState.Never)]
112 public Type TargetType { get; }
114 void IStyle.UnApply(BindableObject bindable)
116 UnApplyCore(bindable, BasedOn ?? GetBasedOnResource(bindable));
117 bindable.RemoveDynamicResource(basedOnResourceProperty);
118 targets.RemoveAll(wr =>
120 BindableObject target;
121 return wr.TryGetTarget(out target) && target == bindable;
125 internal bool CanBeAppliedTo(Type targetType)
127 if (TargetType == targetType)
129 if (!ApplyToDerivedTypes)
133 targetType = targetType.GetTypeInfo().BaseType;
134 if (TargetType == targetType)
136 } while (targetType != typeof(Element));
140 void ApplyCore(BindableObject bindable, XamlStyle basedOn)
143 ((IStyle)basedOn).Apply(bindable);
145 foreach (Setter setter in Setters)
146 setter.Apply(bindable, true);
147 ((AttachedCollection<Behavior>)Behaviors).AttachTo(bindable);
148 ((AttachedCollection<TriggerBase>)Triggers).AttachTo(bindable);
151 void BasedOnChanged(XamlStyle oldValue, XamlStyle newValue)
153 foreach (WeakReference<BindableObject> bindableRef in targets)
155 BindableObject bindable;
156 if (!bindableRef.TryGetTarget(out bindable))
159 UnApplyCore(bindable, oldValue);
160 ApplyCore(bindable, newValue);
164 XamlStyle GetBasedOnResource(BindableObject bindable)
166 return (XamlStyle)bindable.GetValue(basedOnResourceProperty);
169 static void OnBasedOnResourceChanged(BindableObject bindable, object oldValue, object newValue)
171 // Style style = (bindable as BaseHandle).Style;
172 // if (style == null)
174 // style.UnApplyCore(bindable, (Style)oldValue);
175 // style.ApplyCore(bindable, (Style)newValue);
178 void UnApplyCore(BindableObject bindable, XamlStyle basedOn)
180 ((AttachedCollection<TriggerBase>)Triggers).DetachFrom(bindable);
181 ((AttachedCollection<Behavior>)Behaviors).DetachFrom(bindable);
182 foreach (Setter setter in Setters)
183 setter.UnApply(bindable, true);
186 ((IStyle)basedOn).UnApply(bindable);
189 bool ValidateBasedOn(XamlStyle value)
193 return value.TargetType.IsAssignableFrom(TargetType);