Welcome to mirror list, hosted at ThFree Co, Russian Federation.

EditorPropertySelector.cs « Xamarin.PropertyEditing.Windows - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 82eedbd0cacfc08f31f0f35461c0af3ba2d2b901 (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
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Xamarin.PropertyEditing.Common;
using Xamarin.PropertyEditing.Drawing;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Windows
{
	/// <remarks>
	/// While technically it might be reasonable for the template to be relative to the container, its not a scenario
	/// we currently need so it's faster to cache the template in a global capacity. Container relative can be achieved
	/// by putting a new instance of the selector 
	/// </remarks>
	internal class EditorTreeSelector
		: DataTemplateSelector
	{
		public object EditorTemplateKey { get; set; }
		public object ParentTemplateKey { get; set; }

		public override DataTemplate SelectTemplate (object item, DependencyObject container)
		{
			if (item is EditorViewModel) {
				if (this.editorTemplate == null)
					this.editorTemplate = (DataTemplate)((FrameworkElement)container).TryFindResource (EditorTemplateKey);

				return this.editorTemplate;
			}

			if (this.parentTemplate == null)
				this.parentTemplate = (DataTemplate)((FrameworkElement)container).TryFindResource (ParentTemplateKey);

			return this.parentTemplate;
		}

		private DataTemplate editorTemplate, parentTemplate;
	}

	internal class GroupedEditorPropertySelector
		: EditorPropertySelector
	{
		protected override bool TryGetTemplateType (Type type, out Type templateType)
		{
			if (TypeMap.TryGetValue (type, out templateType))
				return true;

			return base.TryGetTemplateType (type, out templateType);
		}

		private static readonly Dictionary<Type, Type> TypeMap = new Dictionary<Type, Type> {
			{ typeof(BrushPropertyViewModel), typeof(BrushTabbedEditorControl) }
		};
	}

	internal class EditorPropertySelector
		: DataTemplateSelector
	{
		public override DataTemplate SelectTemplate (object item, DependencyObject container)
		{
			if (item != null) {
				Type type = item.GetType ();
				if (!TryGetTemplate (type, out DataTemplate template)) {
					if (type.IsConstructedGenericType) {
						type = type.GetGenericTypeDefinition ();
						TryGetTemplate (type, out template);
					}
				}

				if (template != null)
					return template;
			}

			return base.SelectTemplate (item, container);
		}

		protected virtual bool TryGetTemplateType (Type type, out Type templateType)
		{
			return TypeMap.TryGetValue (type, out templateType);
		}

		private readonly Dictionary<Type, DataTemplate> templates = new Dictionary<Type, DataTemplate> ();

		private bool TryGetTemplate (Type type, out DataTemplate template)
		{
			if (this.templates.TryGetValue (type, out template))
				return true;

			if (TryGetTemplateType (type, out Type controlType)) {
				this.templates[type] = template = new DataTemplate (type) {
					VisualTree = new FrameworkElementFactory (controlType)
				};

				return true;
			}

			return false;
		}

		private static readonly Dictionary<Type, Type> TypeMap = new Dictionary<Type, Type> {
			{ typeof(PropertyViewModel<DateTime>), typeof(StringEditorControl) },
			{ typeof(PropertyViewModel<TimeSpan>), typeof(StringEditorControl) },
			{ typeof(StringPropertyViewModel), typeof(StringEditorControl) },
			{ typeof(PropertyViewModel<bool?>), typeof(BoolEditorControl) },
			{ typeof(NumericPropertyViewModel<>), typeof(NumericEditorControl) },
			{ typeof(PointPropertyViewModel), typeof(PointEditorControl) },
			{ typeof(SizePropertyViewModel), typeof(SizeEditorControl) },
			{ typeof(ThicknessPropertyViewModel), typeof(ThicknessEditorControl) },
			{ typeof(PredefinedValuesViewModel<>), typeof(EnumEditorControl) },
			{ typeof(CombinablePropertyViewModel<>), typeof(CombinablePredefinedValuesEditor) },
			{ typeof(BrushPropertyViewModel), typeof(BrushEditorControl) },
			{ typeof(PropertyGroupViewModel), typeof(GroupEditorControl) },
			{ typeof(ObjectPropertyViewModel), typeof(ObjectEditorControl) },
			{ typeof(TypePropertyViewModel), typeof(TypeEditorControl) },
			{ typeof(CollectionPropertyViewModel), typeof(CollectionEditor) },
			{ typeof(RatioViewModel), typeof(RatioEditorControl) },
			{ typeof(AutoResizingPropertyViewModel), typeof(AutoResizingMaskEditorControl) },
			{ typeof(PropertyViewModel<char>), typeof(StringEditorControl) },
			{ typeof(PropertyViewModel<FilePath>), typeof(StringEditorControl) },
			{ typeof(PropertyViewModel<Date>), typeof(DateEditorControl) },
			{ typeof(PropertyViewModel<Time>), typeof(TimeEditorControl) },
		};
	}
}