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: 9410bc562b1f15e2f6a0c0e1c10f3ae67a41c2ec (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using AppKit;
using Foundation;

using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
{
	internal class PropertyTableDelegate
		: NSOutlineViewDelegate
	{
		public PropertyTableDelegate (IHostResourceProvider hostResources, PropertyTableDataSource dataSource)
		{
			if (hostResources == null)
				throw new ArgumentNullException (nameof (hostResources));
			if (dataSource == null)
				throw new ArgumentNullException (nameof (dataSource));

			this.hostResources = hostResources;
			this.dataSource = dataSource;
		}

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

			if (!String.IsNullOrWhiteSpace (this.dataSource.DataContext.FilterText)) {
				outlineView.ExpandItem (null, true);
			} else {
				foreach (PanelGroupViewModel g in this.dataSource.DataContext.ArrangedEditors) {
					NSObjectFacade item;
					if (!this.dataSource.TryGetFacade (g, out item))
						continue;

					if (this.dataSource.DataContext.GetIsExpanded (g.Category))
						EnsureOpenOrClose (outlineView, item, open: true);
					else
						EnsureOpenOrClose (outlineView, item, open: false);
				}
			}
			this.isUpdatingExpansions = false;
		}

		public override NSView GetView (NSOutlineView outlineView, NSTableColumn tableColumn, NSObject item)
		{
			GetVMGroupCellItendifiterFromFacade (item, out EditorViewModel evm, out PanelGroupViewModel group, out var cellIdentifier);

			if (group != null) {
				var categoryContainer = (CategoryContainerControl)outlineView.MakeView (CategoryIdentifier, this);
				if (categoryContainer == null) {
					categoryContainer = new CategoryContainerControl (this.hostResources, outlineView) {
						Identifier = CategoryIdentifier,
						TableView = outlineView,
					};
				}

				((UnfocusableTextField)categoryContainer.Subviews[1]).StringValue = group.Category;

				if (this.dataSource.DataContext.GetIsExpanded (group.Category)) {
					SynchronizationContext.Current.Post (s => {
						outlineView.ExpandItem (item);
					}, null);
				}

				return categoryContainer;
			}

			NSView editorOrContainer = null;
			if (this.firstCache.TryGetValue (cellIdentifier, out IEditorView editor)) {
				this.firstCache.Remove (cellIdentifier);
				editorOrContainer = (editor.NativeView is PropertyEditorControl) ? new EditorContainer (this.hostResources, editor) { Identifier = cellIdentifier } : editor.NativeView;
			} else {
				editorOrContainer = GetEditor (cellIdentifier, evm, outlineView);
				editor = ((editorOrContainer as EditorContainer)?.EditorView) ?? editorOrContainer as IEditorView;
			}

			if (editorOrContainer is EditorContainer ec) {
				ec.ViewModel = evm;
				ec.Label = evm.Name;

#if DEBUG // Currently only used to highlight which controls haven't been implemented
				if (editor == null)
					ec.LabelTextColor = NSColor.Red;
#endif
			}

			if (editor != null) {
				var ovm = evm as ObjectPropertyViewModel;
				if (ovm != null && editorOrContainer is EditorContainer container) {
					if (container.LeftEdgeView == null) {
						if (ovm.CanDelve)
							container.LeftEdgeView = outlineView.MakeView ("NSOutlineViewDisclosureButtonKey", outlineView);
					} else if (!ovm.CanDelve) {
						container.LeftEdgeView = null;
					}
				} else if (!(editorOrContainer is EditorContainer)) {
					editor.ViewModel = evm;
				}

				bool openObjectRow = ovm != null && outlineView.IsItemExpanded (item);
				if (!openObjectRow) {
					var parent = outlineView.GetParent (item);
					openObjectRow = (parent != null && ((NSObjectFacade)parent).Target is ObjectPropertyViewModel);
				}

				SetRowValueBackground (editorOrContainer, openObjectRow);

				// Force a row update due to new height, but only when we are non-default
				if (editor.IsDynamicallySized) {
					nint index = outlineView.RowForItem (item);
					outlineView.NoteHeightOfRowsWithIndexesChanged (new NSIndexSet (index));
				}
			} else if (editorOrContainer is PanelHeaderEditorControl header) {
				header.ViewModel = this.dataSource.DataContext;
			}

			return editorOrContainer;
		}

		public override bool ShouldSelectItem (NSOutlineView outlineView, NSObject item)
		{
			return (!(item is NSObjectFacade) || !(((NSObjectFacade)item).Target is PanelGroupViewModel));
		}

		public override void ItemDidExpand (NSNotification notification)
		{
			NSObjectFacade facade = notification.UserInfo.Values[0] as NSObjectFacade;
			var outline = (NSOutlineView)notification.Object;
			nint row = outline.RowForItem (facade);

			if (this.isUpdatingExpansions) {
				NSView view = outline.GetView (0, row, makeIfNecessary: true);
				if (view.Subviews[0] is NSButton expander)
					expander.State = NSCellStateValue.On;

				return;
			}

			if (facade.Target is PanelGroupViewModel group)
				this.dataSource.DataContext.SetIsExpanded (group.Category, isExpanded: true);
			else if (facade.Target is ObjectPropertyViewModel ovm) {
				NSView view = outline.GetView (0, row, makeIfNecessary: false);
				SetRowValueBackground (view, valueBackground: true);
			}
		}

		public override void ItemDidCollapse (NSNotification notification)
		{
			NSObjectFacade facade = notification.UserInfo.Values[0] as NSObjectFacade;
			var outline = (NSOutlineView)notification.Object;
			nint row = outline.RowForItem (facade);

			if (this.isUpdatingExpansions) {
				NSView view = outline.GetView (0, row, makeIfNecessary: true);
				if (view.Subviews[0] is NSButton expander)
					expander.State = NSCellStateValue.Off;

				return;
			}

			if (facade.Target is PanelGroupViewModel group)
				this.dataSource.DataContext.SetIsExpanded (group.Category, isExpanded: false);
			else if (facade.Target is ObjectPropertyViewModel ovm) {
				NSView view = outline.GetView (0, row, makeIfNecessary: false);
				SetRowValueBackground (view, valueBackground: false);
			}
		}

		public override void DidRemoveRowView (NSOutlineView outlineView, NSTableRowView rowView, nint row)
		{
			if (rowView.Subviews[0] is EditorContainer ec) {
				ec.ViewModel = null;
			}
		}

		public override nfloat GetRowHeight (NSOutlineView outlineView, NSObject item)
		{
			EditorViewModel vm;
			PanelGroupViewModel group;
			string cellIdentifier;
			GetVMGroupCellItendifiterFromFacade (item, out vm, out group, out cellIdentifier);

			if (group != null)
				return 24;

			if (!this.registrations.TryGetValue (cellIdentifier, out EditorRegistration registration)) {
				registration = new EditorRegistration ();

				if (cellIdentifier == nameof (PanelHeaderEditorControl)) {
					registration.RowSize = 54;
				} else {
					NSView editorOrContainer = GetEditor (cellIdentifier, vm, outlineView);
					IEditorView view = ((editorOrContainer as EditorContainer)?.EditorView) ?? editorOrContainer as IEditorView;

					if (view == null) {
						registration.RowSize = 24;
					} else if (view.IsDynamicallySized) {
						registration.SizingInstance = view;
					} else {
						this.registrations[cellIdentifier] = registration = new EditorRegistration {
							RowSize = view.GetHeight (vm)
						};

						this.firstCache[cellIdentifier] = view;
					}
				}

				this.registrations[cellIdentifier] = registration;
			}

			return registration.GetHeight (vm);
		}

		private class EditorRegistration
		{
			public nint RowSize;
			public IEditorView SizingInstance;

			public nint GetHeight (EditorViewModel vm)
			{
				if (SizingInstance != null)
					return SizingInstance.GetHeight (vm);
				else
					return RowSize;
			}
		}

		public const string CategoryIdentifier = "label";

		private PropertyTableDataSource dataSource;
		private bool isUpdatingExpansions;
		private readonly PropertyEditorSelector editorSelector = new PropertyEditorSelector ();

		private readonly IHostResourceProvider hostResources;
		private readonly Dictionary<string, EditorRegistration> registrations = new Dictionary<string, EditorRegistration> ();
		private readonly Dictionary<string, IEditorView> firstCache = new Dictionary<string, IEditorView> ();

		private void EnsureOpenOrClose (NSOutlineView outline, NSObjectFacade item, bool open)
		{
			if (open)
				outline.ExpandItem (item);
			else
				outline.CollapseItem (item);

			NSView view = GetViewForItem (outline, item, makeIfNeccessary: true);
			if (view.Subviews[0] is NSButton expander)
				expander.State = (open) ? NSCellStateValue.On : NSCellStateValue.Off;
		}

		private NSView GetViewForItem (NSOutlineView outline, NSObjectFacade facade, bool makeIfNeccessary = false)
		{
			nint row = outline.RowForItem (facade);
			return outline.GetView (0, row, makeIfNeccessary);
		}

		private void SetRowValueBackground (NSView view, bool valueBackground)
		{
			if (view == null)
				return;

			if (valueBackground) {
				var c = this.hostResources.GetNamedColor (NamedResources.ValueBlockBackgroundColor);
				view.SetValueForKey (c, new NSString ("backgroundColor"));
			} else {
				view.SetValueForKey (NSColor.Clear, new NSString ("backgroundColor"));
			}
		}

		private NSView GetEditor (string identifier, EditorViewModel vm, NSOutlineView outlineView)
		{
			var view = outlineView.MakeView (identifier, this);
			if (view != null)
				return view;

			if (vm != null) {
				IEditorView editor = this.editorSelector.GetEditor (this.hostResources, vm);

				var editorControl = editor?.NativeView as PropertyEditorControl;
				if (editorControl != null) {
					editorControl.TableView = outlineView;
				} else if (editor?.NativeView != null) {
					editor.NativeView.Identifier = identifier;
					return editor.NativeView;
				}

				return new EditorContainer (this.hostResources, editor) { Identifier = identifier };
			} else
				return new PanelHeaderEditorControl (this.hostResources);
		}

		private void GetVMGroupCellItendifiterFromFacade (NSObject item, out EditorViewModel vm, out PanelGroupViewModel group, out string cellIdentifier)
		{
			var facade = (NSObjectFacade)item;
			vm = facade.Target as EditorViewModel;
			group = facade.Target as PanelGroupViewModel;
			cellIdentifier = facade.Target == null
								   ? nameof (PanelHeaderEditorControl)
								   : (group == null) ? vm.GetType ().FullName : group.Category;
		}
	}
}