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

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

namespace Xamarin.PropertyEditing.Mac
{
	internal class BindingObjectSelectorControl : NSView
	{
		internal class ObjectOutlineView : NSOutlineView
		{
			private IReadOnlyList<ObjectTreeElement> viewModel;
			public IReadOnlyList<ObjectTreeElement> ViewModel {
				get => this.viewModel;
				set {
					if (this.viewModel != value) {
						this.viewModel = value;
						var dataSource = new ObjectOutlineViewDataSource (this.viewModel);
						Delegate = new ObjectOutlineViewDelegate (dataSource);
						DataSource = dataSource;
					}

					if (this.viewModel != null) {
						ReloadData ();

						ExpandItem (null, true);
					}
				}
			}

			public ObjectOutlineView ()
			{
				Initialize ();
			}

			// Called when created from unmanaged code
			public ObjectOutlineView (IntPtr handle) : base (handle)
			{
				Initialize ();
			}

			// Called when created directly from a XIB file
			[Export ("initWithCoder:")]
			public ObjectOutlineView (NSCoder coder) : base (coder)
			{
				Initialize ();
			}

			[Export ("validateProposedFirstResponder:forEvent:")]
			public bool ValidateProposedFirstResponder (NSResponder responder, NSEvent forEvent)
			{
				return true;
			}

			public void Initialize ()
			{
				AutoresizingMask = NSViewResizingMask.WidthSizable;
				HeaderView = null;
				TranslatesAutoresizingMaskIntoConstraints = false;
			}
		}

		internal class ObjectOutlineViewDelegate : NSOutlineViewDelegate
		{
			private ObjectOutlineViewDataSource dataSource;

			public ObjectOutlineViewDelegate (ObjectOutlineViewDataSource dataSource)
			{
				this.dataSource = dataSource;
			}

			public override nfloat GetRowHeight (NSOutlineView outlineView, NSObject item)
			{
				return PropertyEditorControl.DefaultControlHeight;
			}

			public override NSView GetView (NSOutlineView outlineView, NSTableColumn tableColumn, NSObject item)
			{
				var labelContainer = (UnfocusableTextField)outlineView.MakeView ("type", this);
				if (labelContainer == null) {
					labelContainer = new UnfocusableTextField {
						Identifier = "type",
					};
				}
				var target = (item as NSObjectFacade).Target;

				switch (target) {
				case KeyValuePair<string, SimpleCollectionView> kvp:
					labelContainer.StringValue = kvp.Key;
					break;
				case TypeInfo info:
					labelContainer.StringValue = info.Name;
					break;
				default:
					labelContainer.StringValue = "Type Not Supported";
					break;
				}

				return labelContainer;
			}

			public override bool ShouldSelectItem (NSOutlineView outlineView, NSObject item)
			{
				var target = (item as NSObjectFacade).Target;
				switch (target) {
				case KeyValuePair<string, SimpleCollectionView> kvp:
					return false;
				case TypeInfo info:
					return true;

				default:
					return false;
				}
			}
		}

		internal class ObjectOutlineViewDataSource : NSOutlineViewDataSource
		{
			public IReadOnlyList<ObjectTreeElement> ViewModel { get; }

			internal ObjectOutlineViewDataSource (IReadOnlyList<ObjectTreeElement> viewModel)
			{
				if (viewModel == null)
					throw new ArgumentNullException (nameof (viewModel));

				ViewModel = viewModel;
			}

			public override nint GetChildrenCount (NSOutlineView outlineView, NSObject item)
			{
				var childCount = 0;
				if (item == null) {
					childCount = this.ViewModel != null ? this.ViewModel.Count () : 0;
				} else {
					var target = (item as NSObjectFacade).Target;
					switch (target) {
					case KeyValuePair<string, SimpleCollectionView> kvp:
						childCount = kvp.Value.Count;
						break;
					case TypeInfo info:
						childCount = 0;
						break;
					default:
						childCount = 0;
						break;
					}
				}

				return childCount;
			}

			public override NSObject GetChild (NSOutlineView outlineView, nint childIndex, NSObject item)
			{
				object element;

				if (item == null) {
					element = this.ViewModel.ElementAt ((int)childIndex);
				} else {
					var target = (item as NSObjectFacade).Target;
					switch (target) {
					case KeyValuePair<string, SimpleCollectionView> kvp:
						element = kvp.Value[(int)childIndex];
						break;
					case TypeInfo info:
						element = info;
						break;
					default:
						return null;
					}
				}

				return new NSObjectFacade (element);
			}

			public override bool ItemExpandable (NSOutlineView outlineView, NSObject item)
			{
				var target = (item as NSObjectFacade).Target;
				switch (target) {
				case KeyValuePair<string, SimpleCollectionView> kvp:
					return kvp.Value.Count > 0;
				case TypeInfo info:
					return false;
				default:
					return false;
				}
			}
		}

		internal ObjectOutlineView objectOutlineView;

		internal BindingObjectSelectorControl (CreateBindingViewModel viewModel)
		{
			if (viewModel == null)
				throw new ArgumentNullException (nameof (viewModel));

			this.objectOutlineView = new ObjectOutlineView ();
			TranslatesAutoresizingMaskIntoConstraints = false;

			viewModel.PropertyChanged += (sender, e) => {
				if (e.PropertyName == nameof (CreateBindingViewModel.ShowObjectSelector)) {
					Hidden = !viewModel.ShowObjectSelector;

					if (viewModel.ShowObjectSelector && viewModel.ObjectElementRoots != null) {
						this.objectOutlineView.ViewModel = viewModel.ObjectElementRoots.Value;
					};
				}
			};
		}
	}
}