summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/Registrar.cs
blob: 617066af2e49e51cc19238e1c549789590b1e0e0 (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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;


namespace Xamarin.Forms.Internals
{
	public class Registrar<TRegistrable> where TRegistrable : class
	{
		readonly Dictionary<Type, Type> _handlers = new Dictionary<Type, Type>();

		public void Register(Type tview, Type trender)
		{
			_handlers[tview] = trender;
		}

		internal TRegistrable GetHandler(Type type)
		{
			Type handlerType = GetHandlerType(type);
			if (handlerType == null)
				return null;

			object handler = Activator.CreateInstance(handlerType);
			return (TRegistrable)handler;
		}

		[EditorBrowsable(EditorBrowsableState.Never)]
		public TOut GetHandler<TOut>(Type type) where TOut : TRegistrable
		{
			return (TOut)GetHandler(type);
		}

		[EditorBrowsable(EditorBrowsableState.Never)]
		public Type GetHandlerType(Type viewType)
		{
			Type type;
			if (LookupHandlerType(viewType, out type))
				return type;

			// lazy load render-view association with RenderWithAttribute (as opposed to using ExportRenderer)
			var attribute = viewType.GetTypeInfo().GetCustomAttribute<RenderWithAttribute>();
			if (attribute == null)
			{
				Register(viewType, null); // Cache this result so we don't have to do GetCustomAttribute again
				return null;
			}

			type = attribute.Type;

			if (type.Name.StartsWith("_"))
			{
				// TODO: Remove attribute2 once renderer names have been unified across all platforms
				var attribute2 = type.GetTypeInfo().GetCustomAttribute<RenderWithAttribute>();
				if (attribute2 != null)
					type = attribute2.Type;

				if (type.Name.StartsWith("_"))
				{
					Register(viewType, null); // Cache this result so we don't work through this chain again
					return null;
				}
			}

			Register(viewType, type); // Register this so we don't have to look for the RenderWith Attibute again in the future

			return type;
		}

		bool LookupHandlerType(Type viewType, out Type handlerType)
		{
			Type type = viewType;

			while (type != null)
			{
				if (_handlers.ContainsKey(type))
				{
					handlerType = _handlers[type];
					return true;
				}

				type = type.GetTypeInfo().BaseType;
			}

			handlerType = null;
			return false;
		}
	}

	public static class Registrar
	{
		static Registrar()
		{
			Registered = new Registrar<IRegisterable>();
		}

		internal static Dictionary<string, Type> Effects { get; } = new Dictionary<string, Type>();

		[EditorBrowsable(EditorBrowsableState.Never)]
		public static IEnumerable<Assembly> ExtraAssemblies { get; set; }

		[EditorBrowsable(EditorBrowsableState.Never)]
		public static Registrar<IRegisterable> Registered { get; }

		[EditorBrowsable(EditorBrowsableState.Never)]
		public static void RegisterAll(Type[] attrTypes)
		{
			Assembly[] assemblies = Device.GetAssemblies();
			if (ExtraAssemblies != null)
			{
				assemblies = assemblies.Union(ExtraAssemblies).ToArray();
			}

			Assembly defaultRendererAssembly = Device.PlatformServices.GetType().GetTypeInfo().Assembly;
			int indexOfExecuting = Array.IndexOf(assemblies, defaultRendererAssembly);

			if (indexOfExecuting > 0)
			{
				assemblies[indexOfExecuting] = assemblies[0];
				assemblies[0] = defaultRendererAssembly;
			}

			// Don't use LINQ for performance reasons
			// Naive implementation can easily take over a second to run
			foreach (Assembly assembly in assemblies)
			{
				foreach (Type attrType in attrTypes)
				{
					Attribute[] attributes = assembly.GetCustomAttributes(attrType).ToArray();
					if (attributes.Length == 0)
						continue;

					foreach (HandlerAttribute attribute in attributes)
					{
						if (attribute.ShouldRegister())
							Registered.Register(attribute.HandlerType, attribute.TargetType);
					}
				}

				string resolutionName = assembly.FullName;

				Attribute[] effectAttributes = assembly.GetCustomAttributes(typeof(ExportEffectAttribute)).ToArray();
				if (effectAttributes.Length > 0)
				{
					var resolutionNameAttribute = (ResolutionGroupNameAttribute)assembly.GetCustomAttribute(typeof(ResolutionGroupNameAttribute));
					if (resolutionNameAttribute != null)
					{
						resolutionName = resolutionNameAttribute.ShortName;
					}

					foreach (Attribute attribute in effectAttributes)
					{
						var effect = (ExportEffectAttribute)attribute;
						Effects [resolutionName + "." + effect.Id] = effect.Type;
					}
				}
			}

			DependencyService.Initialize(assemblies);
		}
	}
}