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

PropertyEditorPanel.cs « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 644b7dc1dfa3745dceaa1a61f27fbdad1588e0af (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
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;

using CoreGraphics;
using Foundation;
using AppKit;
using ObjCRuntime;

using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
{
	public partial class PropertyEditorPanel : NSView
	{
		public PropertyEditorPanel ()
		{
			this.hostResources = new HostResourceProvider ();
			Initialize ();
		}

		public PropertyEditorPanel (IHostResourceProvider hostResources)
		{
			this.hostResources = hostResources;
			Initialize ();
		}

		// Called when created from unmanaged code
		public PropertyEditorPanel (NativeHandle handle) : base (handle)
		{
			this.hostResources = new HostResourceProvider ();
			Initialize ();
		}

		// Called when created directly from a XIB file
		[Export ("initWithCoder:")]
		public PropertyEditorPanel (NSCoder coder) : base (coder)
		{
			this.hostResources = new HostResourceProvider ();
			Initialize ();
		}

		public bool ShowHeader
		{
			get => this.propertyList.ShowHeader;
			set => this.propertyList.ShowHeader = value;
		}

		public PropertyArrangeMode ArrangeMode
		{
			get => this.viewModel.ArrangeMode;
			set => this.viewModel.ArrangeMode = value;
		}

		public bool IsArrangeEnabled
		{
			get { return this.isArrangeEnabled; }
			set
			{
				if (this.isArrangeEnabled == value)
					return;

				this.isArrangeEnabled = value;
			}
		}

		public IHostResourceProvider HostResourceProvider
		{
			get => this.hostResources;
			set
			{
				if (this.hostResources == value)
					return;
				if (value == null)
					throw new ArgumentNullException (nameof (value), "Cannot set HostResourceProvider to null");

				this.hostResources = value;
				UpdateResourceProvider ();
			}
		}

		public TargetPlatform TargetPlatform
		{
			get { return this.targetPlatform; }
			set
			{
				if (this.viewModel != null) {
					this.viewModel.PropertyChanged -= OnVmPropertyChanged;

					var views = this.tabStack.Views;
					for (int i = 0; i < views.Length; i++) {
						var button = (TabButton)views[i];
						button.Clicked -= OnArrangeModeChanged;
						button.RemoveFromSuperview ();
					}
				}

				this.targetPlatform = value;
				this.viewModel = (value != null) ? new PanelViewModel (value) : null;
				this.propertyList.ViewModel = this.viewModel;

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

					for (int i = 0; i < this.viewModel.ArrangeModes.Count; i++) {
						var item = this.viewModel.ArrangeModes[i];
						string imageName = GetIconName (item.ArrangeMode);
						TabButton arrangeMode = new TabButton (this.hostResources, imageName) {
							Bounds = new CGRect (0, 0, 32, 30),
							Selected = item.IsChecked,
							Tag = i,
							ToolTip = GetTooltip (item.ArrangeMode),
						};

						arrangeMode.Clicked += OnArrangeModeChanged;

						arrangeMode.AccessibilityEnabled = true;
						arrangeMode.AccessibilityTitle = string.Format (Properties.Resources.ArrangeByButtonName, item.ArrangeMode.ToString());

						this.tabStack.AddView (arrangeMode, NSStackViewGravity.Top);
					}
				}
			}
		}

		public ICollection<object> SelectedItems => this.viewModel.SelectedObjects;

		public void Select (IEnumerable<object> selectedItems)
		{
			if (selectedItems == null)
				throw new ArgumentNullException (nameof (selectedItems));

			((ObservableCollectionEx<object>)SelectedItems).Reset (selectedItems);
		}

		private IHostResourceProvider hostResources;
		private bool isArrangeEnabled = true;
		private TargetPlatform targetPlatform;
		private PropertyList propertyList;
		private PanelViewModel viewModel;

		private NSSearchField propertyFilter;
		private NSStackView tabStack;
		private DynamicBox header, border;

		private void Initialize ()
		{
			this.header = new DynamicBox (HostResourceProvider, NamedResources.PanelTabBackground) {
				ContentViewMargins = new CGSize (0, 0),
				ContentView = new NSView ()
			};
			AddSubview (this.header);

			this.border = new DynamicBox (HostResourceProvider, NamedResources.TabBorderColor) {
				Frame = new CGRect (0, 0, 1, 1)
			};
			header.AddSubview (this.border);

			this.propertyFilter = new NSSearchField {
				PlaceholderString = Properties.Resources.PropertyFilterLabel,
				TranslatesAutoresizingMaskIntoConstraints = false,
			};
			((NSView)this.header.ContentView).AddSubview (this.propertyFilter);

			this.propertyFilter.Changed += OnPropertyFilterChanged;
			this.propertyFilter.AccessibilityEnabled = true;
			this.propertyFilter.AccessibilityTitle = Properties.Resources.AccessibilityPropertyFilter;

			this.tabStack = new NSStackView {
				Orientation = NSUserInterfaceLayoutOrientation.Horizontal,
				TranslatesAutoresizingMaskIntoConstraints = false,
				EdgeInsets = new NSEdgeInsets (0, 0, 0, 0)
			};

			((NSView)this.header.ContentView).AddSubview (this.tabStack);

			this.propertyList = new PropertyList {
				HostResourceProvider = this.hostResources,
				TranslatesAutoresizingMaskIntoConstraints = false
			};
			AddSubview (this.propertyList);

			this.AddConstraints (new[] {
				NSLayoutConstraint.Create (this.header, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1, 0),
				NSLayoutConstraint.Create (this.header, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this, NSLayoutAttribute.Width, 1, 0),
				NSLayoutConstraint.Create (this.header, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1, 30),

				NSLayoutConstraint.Create (this.tabStack, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, this.header,  NSLayoutAttribute.Leading, 1, 0),
				NSLayoutConstraint.Create (this.tabStack, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this.header, NSLayoutAttribute.Top, 1, 0),
				NSLayoutConstraint.Create (this.tabStack, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this.header, NSLayoutAttribute.Bottom, 1, 0),
				NSLayoutConstraint.Create (this.tabStack, NSLayoutAttribute.Trailing, NSLayoutRelation.LessThanOrEqual, this.propertyFilter, NSLayoutAttribute.Leading, 1, 0),

				NSLayoutConstraint.Create (this.propertyFilter, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, this.tabStack, NSLayoutAttribute.Trailing, 1, 20),
				NSLayoutConstraint.Create (this.propertyFilter, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, this.header, NSLayoutAttribute.Trailing, 1, -19),
				NSLayoutConstraint.Create (this.propertyFilter, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this.header, NSLayoutAttribute.CenterY, 1, 0),

				NSLayoutConstraint.Create (this.border, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this.header, NSLayoutAttribute.Bottom, 1, 0),
				NSLayoutConstraint.Create (this.border, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this.header, NSLayoutAttribute.Width, 1, 0),

				NSLayoutConstraint.Create (this.propertyList, NSLayoutAttribute.Top, NSLayoutRelation.Equal, border, NSLayoutAttribute.Bottom, 1, 0),
				NSLayoutConstraint.Create (this.propertyList, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this, NSLayoutAttribute.Bottom, 1, 0),
				NSLayoutConstraint.Create (this.propertyList, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this, NSLayoutAttribute.Width, 1, 0),
			});

			UpdateResourceProvider ();
		}

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

			UpdateResourceProvider ();
		}

		private void UpdateResourceProvider ()
		{
			if (this.propertyList != null) {
				this.propertyList.HostResourceProvider = HostResourceProvider;
				this.header.HostResourceProvider = HostResourceProvider;
				this.border.HostResourceProvider = HostResourceProvider;
			}
		}

		private void OnArrangeModeChanged (object sender, EventArgs e)
		{
			this.viewModel.ArrangeMode = this.viewModel.ArrangeModes[(int)((NSView)sender).Tag].ArrangeMode;
		}

		private void OnPropertyFilterChanged (object sender, EventArgs e)
		{
			this.viewModel.FilterText = this.propertyFilter.Cell.Title;
			this.propertyList.UpdateExpansions ();
		}

		private void OnVmPropertyChanged (object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == nameof (PanelViewModel.ArrangeMode) || String.IsNullOrEmpty (e.PropertyName)) {
				if (this.viewModel != null) {
					int selected = this.viewModel.ArrangeModes.Select (vm => vm.ArrangeMode).IndexOf (this.viewModel.ArrangeMode);
					var views = this.tabStack.Views;
					for (int i = 0; i < views.Length; i++) {
						((TabButton)views[i]).Selected = (i == selected);
					}
				}
			}
		}

		private string GetIconName (PropertyArrangeMode mode)
		{
			switch (mode) {
			case PropertyArrangeMode.Name:
				return "pe-sort-alphabetically-16";
			case PropertyArrangeMode.Category:
				return "pe-group-by-category-16";
			default:
				throw new ArgumentException();
			}
		}

		private string GetTooltip (PropertyArrangeMode mode)
		{
			switch (mode) {
				case PropertyArrangeMode.Name:
					return string.Format("{0} {1}", Properties.Resources.ArrangeByLabel, Properties.Resources.ArrangeByName);
				case PropertyArrangeMode.Category:
					return string.Format ("{0} {1}", Properties.Resources.ArrangeByLabel, Properties.Resources.ArrangeByCategory);
				default:
					throw new ArgumentException ();
			}
		}
	}
}