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> stopAtTypes = new List<Type> { typeof(View), typeof(Element) };
16 IList<BindableProperty> classStyleProperties;
18 readonly List<BindableProperty> implicitStyles = new List<BindableProperty>();
20 IList<XamlStyle> classStyles;
26 IList<string> styleClass;
28 public MergedStyle(Type targetType, BindableObject target)
31 TargetType = targetType;
32 RegisterImplicitStyles();
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<XamlStyle>), typeof(View), default(IList<XamlStyle>),
62 propertyChanged: (bindable, oldvalue, newvalue) => ((View)bindable).MergedStyle.OnClassStyleChanged());
63 classStyleProperties.Add(classStyleProperty);
64 Target.OnSetDynamicResource(classStyleProperty, Tizen.NUI.Binding.XamlStyle.StyleClassPrefix + styleClass);
70 public BindableObject Target { get; }
72 IList<XamlStyle> 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<XamlStyle>)?.FirstOrDefault(s => s.CanBeAppliedTo(TargetType))).ToList();
109 void OnImplicitStyleChanged()
112 foreach (BindableProperty implicitStyleProperty in implicitStyles)
114 var implicitStyle = (XamlStyle)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(XamlStyle), typeof(View), default(XamlStyle),
137 propertyChanged: (bindable, oldvalue, newvalue) => OnImplicitStyleChanged());
138 implicitStyles.Add(implicitStyleProperty);
139 Target.SetDynamicResource(implicitStyleProperty, type.FullName);
140 type = type.GetTypeInfo().BaseType;
141 if (stopAtTypes.Contains(type))
146 void SetStyle(IStyle implicitStyle, IList<XamlStyle> 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 XamlStyle == null || ((XamlStyle)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 this.implicitStyle = implicitStyle;
161 this.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);