2 using System.Collections.Generic;
4 using System.Reflection;
5 using Tizen.NUI.StyleSheets;
6 using Tizen.NUI.BaseComponents;
8 namespace Tizen.NUI.Binding
10 internal sealed class MergedStyle : IStyle
12 ////If the base type is one of these, stop registering dynamic resources further
13 ////The last one (typeof(Element)) is a safety guard as we might be creating VisualElement directly in internal code
14 static readonly IList<Type> s_stopAtTypes = new List<Type> { typeof(View), typeof(Element) };
16 IList<BindableProperty> _classStyleProperties;
18 readonly List<BindableProperty> _implicitStyles = new List<BindableProperty>();
20 IList<Style> _classStyles;
22 IStyle _implicitStyle;
26 IList<string> _styleClass;
28 public MergedStyle(Type targetType, BindableObject target)
31 TargetType = targetType;
32 RegisterImplicitStyles();
38 get { return _style; }
39 set { SetStyle(ImplicitStyle, ClassStyles, value); }
42 public IList<string> StyleClass
44 get { return _styleClass; }
47 if (_styleClass == value)
50 if (_styleClass != null && _classStyles != null)
51 foreach (var classStyleProperty in _classStyleProperties)
52 Target.RemoveDynamicResource(classStyleProperty);
56 if (_styleClass != null) {
57 _classStyleProperties = new List<BindableProperty> ();
58 foreach (var styleClass in _styleClass) {
59 var classStyleProperty = BindableProperty.Create ("ClassStyle", typeof(IList<Style>), typeof(View), default(IList<Style>),
60 propertyChanged: (bindable, oldvalue, newvalue) => ((View)bindable)._mergedStyle.OnClassStyleChanged());
61 _classStyleProperties.Add (classStyleProperty);
62 Target.OnSetDynamicResource (classStyleProperty, Tizen.NUI.Binding.Style.StyleClassPrefix + styleClass);
68 public BindableObject Target { get; }
70 IList<Style> ClassStyles
72 get { return _classStyles; }
73 set { SetStyle(ImplicitStyle, value, Style); }
78 get { return _implicitStyle; }
79 set { SetStyle(value, ClassStyles, Style); }
82 public void Apply(BindableObject bindable)
84 ImplicitStyle?.Apply(bindable);
85 if (ClassStyles != null)
86 foreach (var classStyle in ClassStyles)
87 ((IStyle)classStyle)?.Apply(bindable);
88 Style?.Apply(bindable);
91 public Type TargetType { get; }
93 public void UnApply(BindableObject bindable)
95 Style?.UnApply(bindable);
96 if (ClassStyles != null)
97 foreach (var classStyle in ClassStyles)
98 ((IStyle)classStyle)?.UnApply(bindable);
99 ImplicitStyle?.UnApply(bindable);
102 void OnClassStyleChanged()
104 ClassStyles = _classStyleProperties.Select (p => (Target.GetValue (p) as IList<Style>)?.FirstOrDefault (s => s.CanBeAppliedTo (TargetType))).ToList ();
107 void OnImplicitStyleChanged()
110 foreach (BindableProperty implicitStyleProperty in _implicitStyles)
112 var implicitStyle = (Style)Target.GetValue(implicitStyleProperty);
113 if (implicitStyle != null)
115 if (first || implicitStyle.ApplyToDerivedTypes)
117 ImplicitStyle = implicitStyle;
125 void RegisterImplicitStyles()
127 Type type = TargetType;
129 BindableProperty implicitStyleProperty = BindableProperty.Create("ImplicitStyle", typeof(Style), typeof(View), default(Style),
130 propertyChanged: (bindable, oldvalue, newvalue) => OnImplicitStyleChanged());
131 _implicitStyles.Add(implicitStyleProperty);
132 Target.SetDynamicResource(implicitStyleProperty, type.FullName);
133 type = type.GetTypeInfo().BaseType;
134 if (s_stopAtTypes.Contains(type))
139 void SetStyle(IStyle implicitStyle, IList<Style> classStyles, IStyle style)
141 bool shouldReApplyStyle = implicitStyle != ImplicitStyle || classStyles != ClassStyles || Style != style;
142 bool shouldReApplyClassStyle = implicitStyle != ImplicitStyle || classStyles != ClassStyles;
143 bool shouldReApplyImplicitStyle = implicitStyle != ImplicitStyle && (Style as Style == null || ((Style)Style).CanCascade);
145 if (shouldReApplyStyle)
146 Style?.UnApply(Target);
147 if (shouldReApplyClassStyle && ClassStyles != null)
148 foreach (var classStyle in ClassStyles)
149 ((IStyle)classStyle)?.UnApply(Target);
150 if (shouldReApplyImplicitStyle)
151 ImplicitStyle?.UnApply(Target);
153 _implicitStyle = implicitStyle;
154 _classStyles = classStyles;
157 if (shouldReApplyImplicitStyle)
158 ImplicitStyle?.Apply(Target);
159 if (shouldReApplyClassStyle && ClassStyles != null)
160 foreach (var classStyle in ClassStyles)
161 ((IStyle)classStyle)?.Apply(Target);
162 if (shouldReApplyStyle)
163 Style?.Apply(Target);