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

GroupEditorControl.cs « Controls « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c5288667cb0318ee1db385e41305da4935240125 (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
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.InteropServices;

using AppKit;
using CoreGraphics;
using Foundation;

using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
{
	internal class GroupEditorControl
		: NSView, IEditorView
	{
		public GroupEditorControl (IHostResourceProvider hostResources)
		{
			if (hostResources == null)
				throw new ArgumentNullException (nameof (hostResources));

			this.hostResources = hostResources;

			this.container = new NSStackView (Bounds) {
				Alignment = NSLayoutAttribute.CenterX,
				AutoresizingMask = NSViewResizingMask.HeightSizable | NSViewResizingMask.WidthSizable
			};

			this.table = new GroupedTableView {
				RowHeight = 24,
				IntercellSpacing = new CGSize (0, 0)
			};
			this.table.AddColumn (new NSTableColumn ());
			this.container.AddView (this.table, NSStackViewGravity.Top);

			this.host = new NSBox {
				BoxType = NSBoxType.NSBoxCustom,
				BorderWidth = 0,
				TranslatesAutoresizingMaskIntoConstraints = false,
				ContentViewMargins = new CGSize (0, 0)
			};

			this.container.AddView (this.host, NSStackViewGravity.Top);
			this.container.AddConstraint (NSLayoutConstraint.Create (this.host, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this.container, NSLayoutAttribute.Width, 1, 0));

			AddSubview (this.container);

			AppearanceChanged ();
		}

		NSView INativeContainer.NativeView => this;

		EditorViewModel IEditorView.ViewModel
		{
			get { return ViewModel; }
			set { ViewModel = (PropertyGroupViewModel)value; }
		}

		public bool IsDynamicallySized => true;

		public bool NeedsPropertyButton => false;

		public nint GetHeight (EditorViewModel viewModel)
		{
			if (!(viewModel is PropertyGroupViewModel gvm))
				throw new ArgumentException ("Invalid viewmodel type");

			Type propertyVmType = gvm.Properties[0].GetType ();
			IEditorView view;
			if (propertyVmType == ViewModel?.Properties[0].GetType()) {
				if (this.hostedEditor == null)
					UpdateHosted ();

				view = this.hostedEditor;
			} else {
				view = this.selector.GetEditor (this.hostResources, gvm.Properties[0]);
			}

			nint editorHeight = view.GetHeight (gvm.Properties[0]);
			return ((nint)this.table.RowHeight * gvm.Properties.Count) + editorHeight;
		}

		public sealed override void ViewDidChangeEffectiveAppearance ()
		{
			base.ViewDidChangeEffectiveAppearance ();

			AppearanceChanged ();
		}

		private void AppearanceChanged ()
		{
			this.table.BackgroundColor = this.hostResources.GetNamedColor (NamedResources.PadBackgroundColor);
			this.host.FillColor = this.hostResources.GetNamedColor (NamedResources.ValueBlockBackgroundColor);
		}

		internal PropertyGroupViewModel ViewModel
		{
			get { return this.source?.ViewModel; }
			set {
				if (this.incc != null)
					this.incc.CollectionChanged -= OnCollectionChanged;

				this.source = (value != null) ? new GroupedDataSource (value, this) : null;
				this.table.Source = this.source;
				this.incc = ViewModel as INotifyCollectionChanged;
				if (this.incc != null)
					this.incc.CollectionChanged += OnCollectionChanged;

				UpdateHosted ();
			}
		}

		private readonly NSTableView table;
		private readonly NSStackView container;
		private readonly NSBox host;
		private readonly PropertyGroupedEditorSelector selector = new PropertyGroupedEditorSelector ();
		private readonly IHostResourceProvider hostResources;

		private IEditorView hostedEditor;
		private GroupedDataSource source;
		private INotifyCollectionChanged incc;

		private class GroupedTableView
			: NSTableView
		{
			public GroupedTableView()
			{
				FocusRingType = NSFocusRingType.None;
			}

			// ValidateProposedFirstResponder is implemented as an extension method so we have to override the hard way (see xamarin-macios/4837)
			[DllImport ("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSendSuper")]
			public extern static bool bool_objc_msgSendSuper_IntPtr_IntPtr (IntPtr receiver, IntPtr selector, IntPtr arg1, IntPtr arg2);

			static readonly IntPtr selValidateProposedFirstResponder_ForEvent_Handle = ObjCRuntime.Selector.GetHandle ("validateProposedFirstResponder:forEvent:");


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

				bool baseRet = bool_objc_msgSendSuper_IntPtr_IntPtr (this.SuperHandle, selValidateProposedFirstResponder_ForEvent_Handle, responder.Handle, forEvent == null ? IntPtr.Zero : forEvent.Handle);
				return true;
			}
		}

		private class GroupedDataSource
			: NSTableViewSource
		{
			public GroupedDataSource (PropertyGroupViewModel vm, GroupEditorControl host)
			{
				if (vm == null)
					throw new ArgumentNullException (nameof (vm));

				ViewModel = vm;
				this.host = host;
			}

			public PropertyGroupViewModel ViewModel
			{
				get;
			}

			public override nint GetRowCount (NSTableView tableView)
			{
				return ViewModel.Properties.Count;
			}

			public override NSView GetViewForItem (NSTableView tableView, NSTableColumn tableColumn, nint row)
			{
				PropertyViewModel pvm = ViewModel.Properties[(int)row];

				string identifier = pvm.GetType ().FullName;
				var view = tableView.MakeView (identifier, tableView) as PreviewView;
				if (view == null) {
					IValueView valueView = this.selector.CreateView (this.host.hostResources, pvm.Property.Type);
					if (valueView == null)
						return new NSView ();

					view = new PreviewView (this.host.hostResources, valueView) {
						Identifier = identifier
					};
				}

				view.Label = pvm.Name;
				view.ViewModel = pvm;
				return view;
			}

			public override void SelectionDidChange (NSNotification notification)
			{
				this.host.UpdateHosted ();
			}

			private readonly GroupEditorControl host;
			private readonly PropertyInlinePreviewSelector selector = new PropertyInlinePreviewSelector ();
		}

		private class PreviewView
			: PropertyContainer
		{
			public PreviewView (IHostResourceProvider hostResources, IValueView valueView)
				: base (hostResources, valueView, includePropertyButton: true, vertInset: -6f)
			{
				this.view = valueView;
			}

			public PropertyViewModel ViewModel
			{
				get { return this.vm; }
				set
				{
					if (this.vm == value)
						return;

					if (this.vm != null)
						this.vm.PropertyChanged -= OnPropertyChanged;

					this.vm = value;
					if (this.vm != null) {
						this.view.SetValue (((IPropertyValue)this.vm).Value);
						this.vm.PropertyChanged += OnPropertyChanged;
					}

					PropertyButton.ViewModel = value;
				}
			}

			private PropertyViewModel vm;
			private IValueView view;

			private void OnPropertyChanged (object sender, PropertyChangedEventArgs e)
			{
				if (e.PropertyName == nameof (PropertyViewModel<string>.Value)) {
					this.view.SetValue (((IPropertyValue)this.vm).Value);
				}
			}
		}

		private void UpdateHosted()
		{
			nint index = this.table.SelectedRow;
			if (index < 0) {
				index = 0;
				this.table.SelectRow (0, false);
			}

			PropertyViewModel pvm = ViewModel.Properties[(int)index];
			if (this.hostedEditor == null) {
				this.hostedEditor = this.selector.GetEditor (this.hostResources, pvm);
				this.host.ContentView = this.hostedEditor.NativeView;
			}

			this.hostedEditor.ViewModel = pvm;
		}

		private void OnCollectionChanged (object sender, NotifyCollectionChangedEventArgs e)
		{
			switch (e.Action) {
			case NotifyCollectionChangedAction.Add:
				if (e.NewStartingIndex < 0 || e.NewItems == null || e.NewItems.Count == 0)
					goto default;

				this.table.InsertRows (NSIndexSet.FromNSRange (new NSRange (e.NewStartingIndex, e.NewItems.Count)), NSTableViewAnimation.SlideDown);
				break;
			case NotifyCollectionChangedAction.Remove:
				if (e.OldStartingIndex < 0 || e.OldItems == null || e.OldItems.Count == 0)
					goto default;

				this.table.RemoveRows (NSIndexSet.FromNSRange (new NSRange (e.OldStartingIndex, e.OldItems.Count)), NSTableViewAnimation.SlideDown);
				break;
			default:
				this.table.ReloadData ();
				break;
			}
		}
	}
}