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

PropertyTableDelegate.cs « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 389a95f5781adb36d6110418da9afaddb9966f94 (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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using AppKit;
using Foundation;
using Xamarin.PropertyEditing.Drawing;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
{
	internal class PropertyTableDelegate
		: NSOutlineViewDelegate
	{
		public PropertyTableDelegate (PropertyTableDataSource datasource)
		{
			this.dataSource = datasource;
		}

		public void UpdateExpansions (NSOutlineView outlineView)
		{
			this.isExpanding = true;

			if (!String.IsNullOrWhiteSpace (this.dataSource.DataContext.FilterText)) {
				outlineView.ExpandItem (null, true);
			} else {
				foreach (IGrouping<string, EditorViewModel> g in this.dataSource.DataContext.ArrangedEditors) {
					NSObject item;
					if (!this.dataSource.TryGetFacade (g, out item))
						continue;

					if (this.dataSource.DataContext.GetIsExpanded (g.Key))
						outlineView.ExpandItem (item);
					else
						outlineView.CollapseItem (item);
				}
			}
			this.isExpanding = false;
		}

		// the table is looking for this method, picks it up automagically
		public override NSView GetView (NSOutlineView outlineView, NSTableColumn tableColumn, NSObject item)
		{
			var facade = (NSObjectFacade)item;
			var vm = facade.Target as PropertyViewModel;
			var group = facade.Target as IGroupingList<string, EditorViewModel>;
			string cellIdentifier = (group == null) ? vm.GetType ().Name : group.Key;

			// Setup view based on the column
			switch (tableColumn.Identifier) {
				case PropertyEditorPanel.PropertyListColId:
					var view = (UnfocusableTextField)outlineView.MakeView ("label", this);
					if (view == null) {
						view = new UnfocusableTextField {
							Identifier = "label",
							Alignment = NSTextAlignment.Right,
						};
					}

					view.StringValue = ((group == null) ? vm.Property.Name + ":" : group.Key) ?? String.Empty;
					return view;

				case PropertyEditorPanel.PropertyEditorColId:
					if (vm == null)
						return null;

					var editor = (PropertyEditorControl)outlineView.MakeView (cellIdentifier + "edits", this);
					if (editor == null) {
						editor = GetEditor (vm, outlineView);
					}

					// we must reset these every time, as the view may have been reused
					editor.ViewModel = vm;
					editor.TableRow = outlineView.RowForItem (item);

					// Force a row update due to new height, but only when we are non-default
					if (editor.TriggerRowChange)
						outlineView.NoteHeightOfRowsWithIndexesChanged (new NSIndexSet (editor.TableRow));

					return editor;
			}

			throw new Exception ("Unknown column identifier: " + tableColumn.Identifier);
		}

		PropertyEditorControl GetEditor (EditorViewModel vm, NSOutlineView outlineView)
		{
			Type[] genericArgs = null;
			Type controlType;
			Type propertyType = vm.GetType ();
			if (!ViewModelTypes.TryGetValue (propertyType, out controlType)) {
				if (propertyType.IsConstructedGenericType) {
					genericArgs = propertyType.GetGenericArguments ();
					propertyType = propertyType.GetGenericTypeDefinition ();
					ViewModelTypes.TryGetValue (propertyType, out controlType);
				}
			}

			if (controlType == null)
				return null;

			if (controlType.IsGenericTypeDefinition) {
				controlType = controlType.MakeGenericType (genericArgs);
			}

			return SetUpEditor (controlType, vm, outlineView);
		}

		public override bool ShouldSelectItem (NSOutlineView outlineView, NSObject item)
		{
			return (!(item is NSObjectFacade) || !(((NSObjectFacade)item).Target is IGroupingList<string, EditorViewModel>));
		}

		public override void ItemDidExpand (NSNotification notification)
		{
			if (this.isExpanding)
				return;

			NSObjectFacade facade = notification.UserInfo.Values[0] as NSObjectFacade;
			var group = facade.Target as IGroupingList<string, EditorViewModel>;
			if (group != null)
				this.dataSource.DataContext.SetIsExpanded (group.Key, isExpanded: true);
		}

		public override void ItemDidCollapse (NSNotification notification)
		{
			if (this.isExpanding)
				return;

			NSObjectFacade facade = notification.UserInfo.Values[0] as NSObjectFacade;
			var group = facade.Target as IGroupingList<string, EditorViewModel>;
			if (group != null)
				this.dataSource.DataContext.SetIsExpanded (group.Key, isExpanded: false);
		}

		public override nfloat GetRowHeight (NSOutlineView outlineView, NSObject item)
		{
			var facade = (NSObjectFacade)item;
			var group = facade.Target as IGroupingList<string, EditorViewModel>;
			if (group != null) {
				return 30;
			}

			var vm = (EditorViewModel)facade.Target;
			var editor = (PropertyEditorControl)outlineView.MakeView (vm.GetType ().Name + "edits", this);
			if (editor == null) {
				editor = GetEditor (vm, outlineView);
			}
			return editor.RowHeight;
		}

		private PropertyTableDataSource dataSource;
		private bool isExpanding;

		// set up the editor based on the type of view model
		private PropertyEditorControl SetUpEditor (Type controlType, EditorViewModel property, NSOutlineView outline)
		{
			var view = (PropertyEditorControl)Activator.CreateInstance (controlType);
			view.Identifier = property.GetType ().Name;
			view.TableView = outline;

			return view;
		}

		private static readonly Dictionary<Type, Type> ViewModelTypes = new Dictionary<Type, Type> {
			{typeof (StringPropertyViewModel), typeof (StringEditorControl)},
			{typeof (IntegerPropertyViewModel), typeof (IntegerNumericEditorControl)},
			{typeof (FloatingPropertyViewModel), typeof (DecimalNumericEditorControl)},
			{typeof (PropertyViewModel<bool>), typeof (BooleanEditorControl)},
			{typeof (PropertyViewModel<CoreGraphics.CGPoint>), typeof (CGPointEditorControl)},
			{typeof (PropertyViewModel<CoreGraphics.CGRect>), typeof (CGRectEditorControl)},
			{typeof (PredefinedValuesViewModel<>), typeof(PredefinedValuesEditor<>)},
			{typeof (PropertyViewModel<CoreGraphics.CGSize>), typeof (CGSizeEditorControl)},
			{typeof (PropertyViewModel<Point>), typeof (SystemPointEditorControl)},
			{typeof (PointPropertyViewModel), typeof (CommonPointEditorControl) },
			{typeof (PropertyViewModel<Size>), typeof (SystemSizeEditorControl)},
			{typeof (SizePropertyViewModel), typeof (CommonSizeEditorControl) },
			{typeof (RectanglePropertyViewModel), typeof (CommonRectangleEditorControl) },
			{typeof (PropertyViewModel<Rectangle>), typeof (SystemRectangleEditorControl)}
		};
	}
}