summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/MergedStyle.cs
blob: 941ebbd8802ebd8dc42e0ab732b19bc2289f324b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Xamarin.Forms
{
	public partial class VisualElement
	{
		sealed class MergedStyle : IStyle
		{
			////If the base type is one of these, stop registering dynamic resources further
			////The last one (typeof(Element)) is a safety guard as we might be creating VisualElement directly in internal code
			static readonly IList<Type> s_stopAtTypes = new List<Type> { typeof(View), typeof(Layout<>), typeof(VisualElement), typeof(Element) };

			IList<BindableProperty> _classStyleProperties;

			readonly List<BindableProperty> _implicitStyles = new List<BindableProperty>();

			IList<Style> _classStyles;

			IStyle _implicitStyle;

			IStyle _style;

			IList<string> _styleClass;

			public MergedStyle(Type targetType, BindableObject target)
			{
				Target = target;
				TargetType = targetType;
				RegisterImplicitStyles();
				Apply(Target);
			}

			public IStyle Style
			{
				get { return _style; }
				set { SetStyle(ImplicitStyle, ClassStyles, value); }
			}

			public IList<string> StyleClass
			{
				get { return _styleClass; }
				set
				{
					if (_styleClass == value)
						return;

					if (_styleClass != null && _classStyles != null)
						foreach (var classStyleProperty in _classStyleProperties)
							Target.RemoveDynamicResource(classStyleProperty);

					_styleClass = value;

					if (_styleClass != null) {
						_classStyleProperties = new List<BindableProperty> ();
						foreach (var styleClass in _styleClass) {
							var classStyleProperty = BindableProperty.Create ("ClassStyle", typeof(IList<Style>), typeof(VisualElement), default(IList<Style>),
								propertyChanged: (bindable, oldvalue, newvalue) => ((VisualElement)bindable)._mergedStyle.OnClassStyleChanged());
							_classStyleProperties.Add (classStyleProperty);
							Target.OnSetDynamicResource (classStyleProperty, Forms.Style.StyleClassPrefix + styleClass);
						}
					}
				}
			}

			public BindableObject Target { get; }

			IList<Style> ClassStyles
			{
				get { return _classStyles; }
				set { SetStyle(ImplicitStyle, value, Style); }
			}

			IStyle ImplicitStyle
			{
				get { return _implicitStyle; }
				set { SetStyle(value, ClassStyles, Style); }
			}

			public void Apply(BindableObject bindable)
			{
				ImplicitStyle?.Apply(bindable);
				if (ClassStyles != null)
					foreach (var classStyle in ClassStyles)
						((IStyle)classStyle)?.Apply(bindable);
				Style?.Apply(bindable);
			}

			public Type TargetType { get; }

			public void UnApply(BindableObject bindable)
			{
				Style?.UnApply(bindable);
				if (ClassStyles != null)
					foreach (var classStyle in ClassStyles)
						((IStyle)classStyle)?.UnApply(bindable);
				ImplicitStyle?.UnApply(bindable);
			}

			void OnClassStyleChanged()
			{
				ClassStyles = _classStyleProperties.Select (p => (Target.GetValue (p) as IList<Style>)?.FirstOrDefault (s => s.CanBeAppliedTo (TargetType))).ToList ();
			}

			void OnImplicitStyleChanged()
			{
				var first = true;
				foreach (BindableProperty implicitStyleProperty in _implicitStyles)
				{
					var implicitStyle = (Style)Target.GetValue(implicitStyleProperty);
					if (implicitStyle != null)
					{
						if (first || implicitStyle.ApplyToDerivedTypes)
						{
							ImplicitStyle = implicitStyle;
							return;
						}
					}
					first = false;
				}
			}

			void RegisterImplicitStyles()
			{
				Type type = TargetType;
				while (true) {
					BindableProperty implicitStyleProperty = BindableProperty.Create("ImplicitStyle", typeof(Style), typeof(VisualElement), default(Style),
						 propertyChanged: (bindable, oldvalue, newvalue) => OnImplicitStyleChanged());
					_implicitStyles.Add(implicitStyleProperty);
					Target.SetDynamicResource(implicitStyleProperty, type.FullName);
					type = type.GetTypeInfo().BaseType;
					if (s_stopAtTypes.Contains(type))
						return;
				}
			}

			void SetStyle(IStyle implicitStyle, IList<Style> classStyles, IStyle style)
			{
				bool shouldReApplyStyle = implicitStyle != ImplicitStyle || classStyles != ClassStyles || Style != style;
				bool shouldReApplyClassStyle = implicitStyle != ImplicitStyle || classStyles != ClassStyles;
				bool shouldReApplyImplicitStyle = implicitStyle != ImplicitStyle && (Style as Style == null || ((Style)Style).CanCascade);

				if (shouldReApplyStyle)
					Style?.UnApply(Target);
				if (shouldReApplyClassStyle && ClassStyles != null)
					foreach (var classStyle in ClassStyles)
						((IStyle)classStyle)?.UnApply(Target);
				if (shouldReApplyImplicitStyle)
					ImplicitStyle?.UnApply(Target);

				_implicitStyle = implicitStyle;
				_classStyles = classStyles;
				_style = style;

				if (shouldReApplyImplicitStyle)
					ImplicitStyle?.Apply(Target);
				if (shouldReApplyClassStyle && ClassStyles != null)
					foreach (var classStyle in ClassStyles)
						((IStyle)classStyle)?.Apply(Target);
				if (shouldReApplyStyle)
					Style?.Apply(Target);
			}
		}
	}
}