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

PanelHeaderEditorControl.cs « Controls « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6cdbf15d5708cdf5f69628c3563521433b032b4f (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
using System;
using System.Collections;
using System.ComponentModel;
using System.IO;
using AppKit;
using Xamarin.PropertyEditing.Mac.Resources;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
{
	internal class PanelHeaderLabelControl : NSView
	{
		public const string PanelHeaderLabelIdentifierString = "PanelHeaderLabelIdentifier";

		private NSImageView propertyIcon;

		private PanelViewModel viewModel;

		public PanelHeaderLabelControl (PanelViewModel viewModel)
		{
			if (viewModel == null)
				throw new ArgumentNullException (nameof (viewModel));

			this.viewModel = viewModel;
			this.viewModel.PropertyChanged += ViewModel_PropertyChanged;

			Identifier = PanelHeaderLabelIdentifierString;

			var propertyObjectNameLabel = new UnfocusableTextField {
				Alignment = NSTextAlignment.Right,
				StringValue = Properties.Resources.Name + ":",
				TranslatesAutoresizingMaskIntoConstraints = false,
			};
			AddSubview (propertyObjectNameLabel);

			var propertyTypeNameLabel = new UnfocusableTextField {
				Alignment = NSTextAlignment.Right,
				StringValue = Properties.Resources.Type + ":",
				TranslatesAutoresizingMaskIntoConstraints = false,
			};
			AddSubview (propertyTypeNameLabel);

			this.propertyIcon = new NSImageView {
				ImageScaling = NSImageScale.AxesIndependently,
				TranslatesAutoresizingMaskIntoConstraints = false,
			};
			AddSubview (this.propertyIcon);

			this.DoConstraints (new NSLayoutConstraint[] {
				propertyObjectNameLabel.ConstraintTo(this, (ol, c) => ol.Top == c.Top),
				propertyObjectNameLabel.ConstraintTo(this, (ol, c) => ol.Left == c.Left + 182),
				propertyObjectNameLabel.ConstraintTo(this, (ol, c) => ol.Width == 40),
				propertyObjectNameLabel.ConstraintTo(this, (ol, c) => ol.Height == PropertyEditorControl.DefaultControlHeight),

				propertyTypeNameLabel.ConstraintTo(this, (tl, c) => tl.Top == c.Top + 22),
				propertyTypeNameLabel.ConstraintTo(this, (tl, c) => tl.Left == c.Left + 182),
				propertyTypeNameLabel.ConstraintTo(this, (tl, c) => tl.Width == 40),
				propertyTypeNameLabel.ConstraintTo(this, (tl, c) => tl.Height == PropertyEditorControl.DefaultControlHeight),

				this.propertyIcon.ConstraintTo(this, (ico, c) => ico.Top == c.Top + 5),
				this.propertyIcon.ConstraintTo(this, (ico, c) => ico.Left == c.Left + 32),
				this.propertyIcon.ConstraintTo(this, (ico, c) => ico.Width == 32),
				this.propertyIcon.ConstraintTo(this, (ico, c) => ico.Height == 32),
			});

			UpdateIcon ();
		}

		private async void UpdateIcon ()
		{
			Stream icon = await this.viewModel.GetIconAsync ();

			if (icon != null)
				this.propertyIcon.Image = NSImage.FromStream (icon);

			this.propertyIcon.Hidden = (icon == null);
		}

		void ViewModel_PropertyChanged (object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == nameof (PanelViewModel.TypeName)) {
				UpdateIcon ();
			}
		}
	}

	internal class PanelHeaderEditorControl : PropertyEditorControl
	{
		private NSTextField propertyObjectName;

		private PanelViewModel viewModel;

		public PanelHeaderEditorControl (PanelViewModel viewModel)
		{
			if (viewModel == null)
				throw new ArgumentNullException (nameof (viewModel));

			this.viewModel = viewModel;
			this.viewModel.PropertyChanged += ViewModel_PropertyChanged;

			NSControlSize controlSize = NSControlSize.Small;
			TranslatesAutoresizingMaskIntoConstraints = false;

			this.propertyObjectName = new NSTextField {
				ControlSize = controlSize,
				Font = NSFont.FromFontName (DefaultFontName, DefaultFontSize),
				PlaceholderString = LocalizationResources.ObjectNamePlaceholder,
				TranslatesAutoresizingMaskIntoConstraints = false,
			};

			this.propertyObjectName.Activated += PropertyObjectName_Activated;

			AddSubview (this.propertyObjectName);

			var propertyTypeName = new UnfocusableTextField {
				StringValue = viewModel.TypeName,
				TranslatesAutoresizingMaskIntoConstraints = false,
			};
			AddSubview (propertyTypeName);

			this.DoConstraints (new NSLayoutConstraint[] {
				this.propertyObjectName.ConstraintTo(this, (on, c) => on.Top == c.Top + 2),
				this.propertyObjectName.ConstraintTo(this, (on, c) => on.Left == c.Left + 4),
				this.propertyObjectName.ConstraintTo(this, (on, c) => on.Width == c.Width - 34),
				this.propertyObjectName.ConstraintTo(this, (on, c) => on.Height == DefaultControlHeight - 3),

				propertyTypeName.ConstraintTo(this, (tn, c) => tn.Top == c.Top + 22),
				propertyTypeName.ConstraintTo(this, (tn, c) => tn.Left == c.Left + 4),
				propertyTypeName.ConstraintTo(this, (tn, c) => tn.Width == c.Width - 34),
				propertyTypeName.ConstraintTo(this, (tn, c) => tn.Height == DefaultControlHeight),
			});

			// We won't enable or show the PropertyButton for the header
			this.PropertyButton.Enabled = false;
			this.PropertyButton.Hidden = true;

			UpdateValue ();
		}

		void PropertyObjectName_Activated (object sender, EventArgs e)
		{
			this.viewModel.ObjectName = this.propertyObjectName.StringValue;
		}

		public override NSView FirstKeyView => this.propertyObjectName;

		public override NSView LastKeyView => this.propertyObjectName;

		protected override void HandleErrorsChanged (object sender, DataErrorsChangedEventArgs e)
		{
			UpdateErrorsDisplayed (viewModel.GetErrors (viewModel.GetType ().Name));
		}

		protected override void SetEnabled ()
		{
			this.propertyObjectName.Editable = !this.viewModel.IsObjectNameReadOnly;
		}

		protected override void UpdateAccessibilityValues ()
		{
			this.propertyObjectName.AccessibilityTitle = string.Format (LocalizationResources.AccessibilityObjectName, nameof (viewModel.ObjectName));
		}

		protected override void UpdateErrorsDisplayed (IEnumerable errors)
		{
			if (this.viewModel.HasErrors) {
				SetErrors (errors);
			} else {
				SetErrors (null);
				SetEnabled ();
			}
		}

		protected override void UpdateValue ()
		{
			if (this.propertyObjectName != null) {
				this.propertyObjectName.StringValue = this.viewModel.ObjectName ?? string.Empty;
				this.propertyObjectName.Editable = !this.viewModel.IsObjectNameReadOnly;
			}
		}

		void ViewModel_PropertyChanged (object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == nameof (PanelViewModel.ObjectName)) {
				UpdateValue ();
			}
		}

		public override nint GetHeight (EditorViewModel vm)
		{
			return 44;
		}
	}
}