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

BindingTypeSelectorControl.cs « BindingEditor « Controls « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bbfebc91aa6568fd1f0a763f8b8b4453cb56dd43 (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
305
306
307
308
309
310
311
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using AppKit;
using Foundation;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
{
	internal class BindingTypeSelectorControl : NSView
	{
		internal class TypeOutlineView : NSOutlineView
		{
			private TypeSelectorViewModel viewModel;
			public TypeSelectorViewModel ViewModel {
				get => this.viewModel;
				set {
					if (this.viewModel != null) {
						this.viewModel.PropertyChanged -= OnPropertyChanged;
					}

					if (this.viewModel != value) {
						this.viewModel = value;
						var dataSource = new TypeOutlineViewDataSource (this.viewModel);
						Delegate = new TypeOutlineViewDelegate (dataSource);
						DataSource = dataSource;
					}

					OnPropertyChanged (this.viewModel, new PropertyChangedEventArgs (null));
					if (this.viewModel != null) {
						this.viewModel.PropertyChanged += OnPropertyChanged;
					}
				}
			}

			private void OnPropertyChanged (object sender, PropertyChangedEventArgs e)
			{
				ReloadData ();

				ExpandItem (null, true);
			}

			public TypeOutlineView ()
			{
				Initialize ();
			}

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

			// Called when created directly from a XIB file
			[Export ("initWithCoder:")]
			public TypeOutlineView (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 TypeOutlineViewDelegate : NSOutlineViewDelegate
		{
			private TypeOutlineViewDataSource dataSource;

			public TypeOutlineViewDelegate (TypeOutlineViewDataSource 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 TypeOutlineViewDataSource : NSOutlineViewDataSource
		{
			public TypeSelectorViewModel ViewModel { get; }

			internal TypeOutlineViewDataSource (TypeSelectorViewModel 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.Types != null ? this.ViewModel.Types.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.Types.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 const string TypeSelectorColId = "TypeSelectorColumn";

		internal TypeOutlineView typeOutlineView;
		private NSButton showAllAssembliesCheckBox;

		public CreateBindingViewModel ViewModel { get; set; }

		public BindingTypeSelectorControl (CreateBindingViewModel viewModel)
		{
			ViewModel = viewModel;
			TranslatesAutoresizingMaskIntoConstraints = false;

			this.typeOutlineView = new TypeOutlineView {

			};

			this.typeOutlineView.Activated += (sender, e) => {
				if (sender is TypeOutlineView tov) {
					if (tov.SelectedRow != -1) {
						if (tov.ItemAtRow (tov.SelectedRow) is NSObjectFacade item) {
							if (item.Target is ITypeInfo typeInfo) {
								viewModel.TypeSelector.SelectedType = typeInfo;
							}
						}
					}
				}
			};

			var typeColumn = new NSTableColumn (TypeSelectorColId);
			this.typeOutlineView.AddColumn (typeColumn);

			// Set OutlineTableColumn or the arrows showing children/expansion will not be drawn
			this.typeOutlineView.OutlineTableColumn = typeColumn;

			// create a table view and a scroll view
			var tableContainer = new NSScrollView {
				TranslatesAutoresizingMaskIntoConstraints = false,
			};

			// add the panel to the window
			tableContainer.DocumentView = this.typeOutlineView;
			AddSubview (tableContainer);

			var filterObjects = new NSSearchField {
				ControlSize = NSControlSize.Mini,
				Font = NSFont.FromFontName (PropertyEditorControl.DefaultFontName, PropertyEditorControl.DefaultFontSize),
				PlaceholderString = Properties.Resources.SearchObjectsTitle,
				TranslatesAutoresizingMaskIntoConstraints = false,
			};

			filterObjects.Changed += (sender, e) => {
				viewModel.TypeSelector.FilterText = filterObjects.Cell.Title;
				this.typeOutlineView.ReloadData ();
				this.typeOutlineView.ExpandItem (null, true);
			};

			AddSubview (filterObjects);
			AddConstraints (new[] {
				NSLayoutConstraint.Create (filterObjects, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1f, 45f),
				NSLayoutConstraint.Create (filterObjects, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 5f),
				NSLayoutConstraint.Create (filterObjects, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this, NSLayoutAttribute.Width, 1f, -10f),
				NSLayoutConstraint.Create (filterObjects, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, 24),
			});

			// Position based on filterObjects bottom and All Assemblies Top.
			AddConstraints (new[] {
				NSLayoutConstraint.Create (tableContainer, NSLayoutAttribute.Top, NSLayoutRelation.Equal, filterObjects, NSLayoutAttribute.Bottom, 1f, 5f),
				NSLayoutConstraint.Create (tableContainer, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 5f),
				NSLayoutConstraint.Create (tableContainer, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this, NSLayoutAttribute.Width, 1f, -10f),
				NSLayoutConstraint.Create (tableContainer, NSLayoutAttribute.Height, NSLayoutRelation.Equal, this, NSLayoutAttribute.Height, 1f, -105f),
			});

			this.showAllAssembliesCheckBox = new NSButton {
				ControlSize = NSControlSize.Small,
				Font = NSFont.FromFontName (PropertyEditorControl.DefaultFontName, PropertyEditorControl.DefaultFontSize),
				Title = Properties.Resources.ShowAllAssemblies,
				TranslatesAutoresizingMaskIntoConstraints = false,
			};

			this.showAllAssembliesCheckBox.SetButtonType (NSButtonType.Switch);
			this.showAllAssembliesCheckBox.Activated += SelectionChanged;

			AddSubview (this.showAllAssembliesCheckBox);
			AddConstraints (new[] {
				NSLayoutConstraint.Create (this.showAllAssembliesCheckBox, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Bottom, 1f, -30f),
				NSLayoutConstraint.Create (this.showAllAssembliesCheckBox, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 10f),
				NSLayoutConstraint.Create (this.showAllAssembliesCheckBox, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this, NSLayoutAttribute.Width, 1f, -10f),
				NSLayoutConstraint.Create (this.showAllAssembliesCheckBox, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, 24),
			});

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

					if (viewModel.ShowTypeSelector && viewModel.TypeSelector != null) {
						this.typeOutlineView.ViewModel = viewModel.TypeSelector;

						this.showAllAssembliesCheckBox.State = viewModel.TypeSelector.ShowAllAssemblies ? NSCellStateValue.On : NSCellStateValue.Off;
					}
				}
			};
		}

		private void SelectionChanged (object sender, EventArgs e)
		{
			if (sender is NSButton button) {
				this.typeOutlineView.ViewModel.ShowAllAssemblies = (button.State == NSCellStateValue.On) ? true : false;
			}
		}
	}
}