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)
58 _classStyleProperties = new List<BindableProperty>();
59 foreach (var styleClass in _styleClass)
61 var classStyleProperty = BindableProperty.Create("ClassStyle", typeof(IList<Style>), typeof(View), default(IList<Style>),
62 propertyChanged: (bindable, oldvalue, newvalue) => ((View)bindable)._mergedStyle.OnClassStyleChanged());
63 _classStyleProperties.Add(classStyleProperty);
64 Target.OnSetDynamicResource(classStyleProperty, Tizen.NUI.Binding.Style.StyleClassPrefix + styleClass);
70 public BindableObject Target { get; }
72 IList<Style> ClassStyles
74 get { return _classStyles; }
75 set { SetStyle(ImplicitStyle, value, Style); }
80 get { return _implicitStyle; }
81 set { SetStyle(value, ClassStyles, Style); }
84 public void Apply(BindableObject bindable)
86 ImplicitStyle?.Apply(bindable);
87 if (ClassStyles != null)
88 foreach (var classStyle in ClassStyles)
89 ((IStyle)classStyle)?.Apply(bindable);
90 Style?.Apply(bindable);
93 public Type TargetType { get; }
95 public void UnApply(BindableObject bindable)
97 Style?.UnApply(bindable);
98 if (ClassStyles != null)
99 foreach (var classStyle in ClassStyles)
100 ((IStyle)classStyle)?.UnApply(bindable);
101 ImplicitStyle?.UnApply(bindable);
104 void OnClassStyleChanged()
106 ClassStyles = _classStyleProperties.Select(p => (Target.GetValue(p) as IList<Style>)?.FirstOrDefault(s => s.CanBeAppliedTo(TargetType))).ToList();
109 void OnImplicitStyleChanged()
112 foreach (BindableProperty implicitStyleProperty in _implicitStyles)
114 var implicitStyle = (Style)Target.GetValue(implicitStyleProperty);
115 if (implicitStyle != null)
117 if (first || implicitStyle.ApplyToDerivedTypes)
119 ImplicitStyle = implicitStyle;
127 void RegisterImplicitStyles()
129 Type type = TargetType;
136 BindableProperty implicitStyleProperty = BindableProperty.Create(nameof(ImplicitStyle), typeof(Style), typeof(View), default(Style),
137 propertyChanged: (bindable, oldvalue, newvalue) => OnImplicitStyleChanged());
138 _implicitStyles.Add(implicitStyleProperty);
139 Target.SetDynamicResource(implicitStyleProperty, type.FullName);
140 type = type.GetTypeInfo().BaseType;
141 if (s_stopAtTypes.Contains(type))
146 void SetStyle(IStyle implicitStyle, IList<Style> classStyles, IStyle style)
148 bool shouldReApplyStyle = implicitStyle != ImplicitStyle || classStyles != ClassStyles || Style != style;
149 bool shouldReApplyClassStyle = implicitStyle != ImplicitStyle || classStyles != ClassStyles;
150 bool shouldReApplyImplicitStyle = implicitStyle != ImplicitStyle && (Style as Style == null || ((Style)Style).CanCascade);
152 if (shouldReApplyStyle)
153 Style?.UnApply(Target);
154 if (shouldReApplyClassStyle && ClassStyles != null)
155 foreach (var classStyle in ClassStyles)
156 ((IStyle)classStyle)?.UnApply(Target);
157 if (shouldReApplyImplicitStyle)
158 ImplicitStyle?.UnApply(Target);
160 _implicitStyle = implicitStyle;
161 _classStyles = classStyles;
164 if (shouldReApplyImplicitStyle)
165 ImplicitStyle?.Apply(Target);
166 if (shouldReApplyClassStyle && ClassStyles != null)
167 foreach (var classStyle in ClassStyles)
168 ((IStyle)classStyle)?.Apply(Target);
169 if (shouldReApplyStyle)
170 Style?.Apply(Target);