summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/Style.cs
blob: 8d27f01007071d68c73003f551113d6d72b8871a (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Xamarin.Forms
{
	[ContentProperty("Setters")]
	public sealed class Style : IStyle
	{
		internal const string StyleClassPrefix = "Xamarin.Forms.StyleClass.";

		readonly BindableProperty _basedOnResourceProperty = BindableProperty.CreateAttached("BasedOnResource", typeof(Style), typeof(Style), default(Style),
			propertyChanged: OnBasedOnResourceChanged);

		readonly List<WeakReference<BindableObject>> _targets = new List<WeakReference<BindableObject>>(4);

		Style _basedOnStyle;

		string _baseResourceKey;

		IList<Behavior> _behaviors;

		IList<TriggerBase> _triggers;

		public Style([TypeConverter(typeof(TypeTypeConverter))] [Parameter("TargetType")] Type targetType)
		{
			if (targetType == null)
				throw new ArgumentNullException("targetType");

			TargetType = targetType;
			Setters = new List<Setter>();
		}

		public bool ApplyToDerivedTypes { get; set; }

		public Style BasedOn
		{
			get { return _basedOnStyle; }
			set
			{
				if (_basedOnStyle == value)
					return;
				if (!ValidateBasedOn(value))
					throw new ArgumentException("BasedOn.TargetType is not compatible with TargetType");
				Style oldValue = _basedOnStyle;
				_basedOnStyle = value;
				BasedOnChanged(oldValue, value);
				if (value != null)
					BaseResourceKey = null;
			}
		}

		public string BaseResourceKey
		{
			get { return _baseResourceKey; }
			set
			{
				if (_baseResourceKey == value)
					return;
				_baseResourceKey = value;
				//update all DynamicResources
				foreach (WeakReference<BindableObject> bindableWr in _targets)
				{
					BindableObject target;
					if (!bindableWr.TryGetTarget(out target))
						continue;
					target.RemoveDynamicResource(_basedOnResourceProperty);
					if (value != null)
						target.SetDynamicResource(_basedOnResourceProperty, value);
				}
				if (value != null)
					BasedOn = null;
			}
		}

		public IList<Behavior> Behaviors
		{
			get { return _behaviors ?? (_behaviors = new AttachedCollection<Behavior>()); }
		}

		public bool CanCascade { get; set; }

		public string Class { get; set; }

		public IList<Setter> Setters { get; }

		public IList<TriggerBase> Triggers
		{
			get { return _triggers ?? (_triggers = new AttachedCollection<TriggerBase>()); }
		}

		void IStyle.Apply(BindableObject bindable)
		{
			_targets.Add(new WeakReference<BindableObject>(bindable));
			if (BaseResourceKey != null)
				bindable.SetDynamicResource(_basedOnResourceProperty, BaseResourceKey);
			ApplyCore(bindable, BasedOn ?? GetBasedOnResource(bindable));
		}

		public Type TargetType { get; }

		void IStyle.UnApply(BindableObject bindable)
		{
			UnApplyCore(bindable, BasedOn ?? GetBasedOnResource(bindable));
			bindable.RemoveDynamicResource(_basedOnResourceProperty);
			_targets.RemoveAll(wr =>
			{
				BindableObject target;
				return wr.TryGetTarget(out target) && target == bindable;
			});
		}

		internal bool CanBeAppliedTo(Type targetType)
		{
			if (TargetType == targetType)
				return true;
			if (!ApplyToDerivedTypes)
				return false;
			do
			{
				targetType = targetType.GetTypeInfo().BaseType;
				if (TargetType == targetType)
					return true;
			} while (targetType != typeof(Element));
			return false;
		}

		void ApplyCore(BindableObject bindable, Style basedOn)
		{
			if (basedOn != null)
				((IStyle)basedOn).Apply(bindable);

			foreach (Setter setter in Setters)
				setter.Apply(bindable, true);
			((AttachedCollection<Behavior>)Behaviors).AttachTo(bindable);
			((AttachedCollection<TriggerBase>)Triggers).AttachTo(bindable);
		}

		void BasedOnChanged(Style oldValue, Style newValue)
		{
			foreach (WeakReference<BindableObject> bindableRef in _targets)
			{
				BindableObject bindable;
				if (!bindableRef.TryGetTarget(out bindable))
					continue;

				UnApplyCore(bindable, oldValue);
				ApplyCore(bindable, newValue);
			}
		}

		Style GetBasedOnResource(BindableObject bindable)
		{
			return (Style)bindable.GetValue(_basedOnResourceProperty);
		}

		static void OnBasedOnResourceChanged(BindableObject bindable, object oldValue, object newValue)
		{
			Style style = (bindable as VisualElement).Style;
			if (style == null)
				return;
			style.UnApplyCore(bindable, (Style)oldValue);
			style.ApplyCore(bindable, (Style)newValue);
		}

		void UnApplyCore(BindableObject bindable, Style basedOn)
		{
			((AttachedCollection<TriggerBase>)Triggers).DetachFrom(bindable);
			((AttachedCollection<Behavior>)Behaviors).DetachFrom(bindable);
			foreach (Setter setter in Setters)
				setter.UnApply(bindable, true);

			if (basedOn != null)
				((IStyle)basedOn).UnApply(bindable);
		}

		bool ValidateBasedOn(Style value)
		{
			if (value == null)
				return true;
			return value.TargetType.IsAssignableFrom(TargetType);
		}
	}
}