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

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

using Foundation;
using AppKit;

using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
{
	internal class PredefinedValuesEditor<T>
		: PropertyEditorControl<PredefinedValuesViewModel<T>>
	{
		public PredefinedValuesEditor (IHostResourceProvider hostResources)
			: base (hostResources)
		{
			base.TranslatesAutoresizingMaskIntoConstraints = false;
		}

		public override NSView FirstKeyView => this.firstKeyView;
		public override NSView LastKeyView => this.lastKeyView;

		protected override void SetEnabled ()
		{
			if (ViewModel.IsConstrainedToPredefined) {
				this.popupButton.Enabled = ViewModel.Property.CanWrite;
			} else {
				this.comboBox.Enabled = ViewModel.Property.CanWrite;
			}
		}

		protected override void OnViewModelChanged (PropertyViewModel oldModel)
		{
			if (ViewModel == null)
				return;

			if (ViewModel.IsConstrainedToPredefined) {
				RequirePopup ();

				this.popupButtonList.RemoveAllItems ();
				foreach (var item in ViewModel.PossibleValues) {
					this.popupButtonList.AddItem (new NSMenuItem (item));
				}
			} else {
				RequireComboBox ();

				UnhookSelectionChangeAndClearItems ();
				foreach (var item in ViewModel.PossibleValues) {
					this.comboBox.Add (new NSString (item));
				}
				this.comboBox.SelectionChanged += ComboBox_SelectionChanged;
			}

			base.OnViewModelChanged (oldModel);
		}

		protected override void UpdateValue ()
		{
			if (ViewModel.IsConstrainedToPredefined) {
				this.popupButton.Title = ViewModel.ValueName ?? String.Empty;
			} else {
				this.comboBox.StringValue = ViewModel.ValueName ?? String.Empty;
			}
		}

		protected override void UpdateAccessibilityValues ()
		{
			if (ViewModel.IsConstrainedToPredefined) {
				this.popupButton.AccessibilityEnabled = this.popupButton.Enabled;
				this.popupButton.AccessibilityTitle = string.Format (Properties.Resources.AccessibilityPopUp, ViewModel.Property.Name);
			} else {
				this.comboBox.AccessibilityEnabled = this.comboBox.Enabled;
				this.comboBox.AccessibilityTitle = string.Format (Properties.Resources.AccessibilityCombobox, ViewModel.Property.Name);
			}
		}

		private NSComboBox comboBox;
		private NSPopUpButton popupButton;
		private NSMenu popupButtonList;

		private NSView firstKeyView;
		private NSView lastKeyView;

		private void RemovePopup()
		{
			if (this.popupButton == null)
				return;

			this.popupButton.Activated -= PopupButton_Activated;
			this.popupButton.RemoveFromSuperview ();
			this.popupButton.Dispose ();
			this.popupButton = null;

			this.popupButtonList.RemoveAllItems ();
			this.popupButtonList.Dispose ();
			this.popupButtonList = null;
		}

		private void RequirePopup()
		{
			if (this.popupButton != null)
				return;

			RemoveComboBox ();

			this.popupButton = new FocusablePopUpButton {
				AllowsExpansionToolTips = true,
				Cell = {
					LineBreakMode = NSLineBreakMode.TruncatingTail,
					UsesSingleLineMode = true,
				},
				ControlSize = NSControlSize.Small,
				Font = NSFont.SystemFontOfSize (NSFont.SystemFontSizeForControlSize (NSControlSize.Small)),
				TranslatesAutoresizingMaskIntoConstraints = false,
				StringValue = String.Empty,
				ResponderProxy = new ProxyRowResponder(this, ProxyRowType.SingleView)
			};

			this.popupButtonList = new NSMenu ();
			this.popupButton.Menu = this.popupButtonList;
			this.popupButton.Activated += PopupButton_Activated; 

			AddSubview (this.popupButton);

			AddConstraints (new[] {
				NSLayoutConstraint.Create (this.popupButton, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this,  NSLayoutAttribute.CenterY, 1f, 0f),
				NSLayoutConstraint.Create (this.popupButton, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 0f),
				NSLayoutConstraint.Create (this.popupButton, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this,  NSLayoutAttribute.Width, 1f, 0),
			});

			this.firstKeyView = this.popupButton;
			this.lastKeyView = this.popupButton;
		}

		private void RemoveComboBox()
		{
			if (this.comboBox == null)
				return;
			UnhookSelectionChangeAndClearItems ();
			this.comboBox.RemoveFromSuperview ();
			this.comboBox.Dispose ();
			this.comboBox = null;
		}

		private void RequireComboBox()
		{
			if (this.comboBox != null)
				return;

			RemovePopup ();

			this.comboBox = new FocusableComboBox {
				AllowsExpansionToolTips = true,
				BackgroundColor = NSColor.Clear,
				Cell = {
					LineBreakMode = NSLineBreakMode.TruncatingTail,
					UsesSingleLineMode = true,
				},
				ResponderProxy = new ProxyRowResponder (this, ProxyRowType.SingleView),
				ControlSize = NSControlSize.Small,
				Font = NSFont.SystemFontOfSize (NSFont.SystemFontSizeForControlSize (NSControlSize.Small)),
				TranslatesAutoresizingMaskIntoConstraints = false,
				StringValue = String.Empty,
			};

			this.comboBox.SelectionChanged += ComboBox_SelectionChanged;

			AddSubview (this.comboBox);

			AddConstraints (new[] {
				NSLayoutConstraint.Create (this.comboBox, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0f),
				NSLayoutConstraint.Create (this.comboBox, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 0f),
				NSLayoutConstraint.Create (this.comboBox, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this, NSLayoutAttribute.Width, 1f, 0),
			});

			this.firstKeyView = this.comboBox;
			this.lastKeyView = this.comboBox;
		}

		private void PopupButton_Activated (object sender, EventArgs e)
		{
			if (ViewModel != null && sender is NSPopUpButton popUpButton) {
				ViewModel.ValueName = popUpButton.Title;
			}
		}

		private void ComboBox_SelectionChanged (object sender, EventArgs e)
		{
			if (ViewModel != null
				&& this.comboBox != null
				&& this.comboBox.SelectedValue != null) {
				ViewModel.ValueName = this.comboBox.SelectedValue.ToString ();
			}
		}

		private void UnhookSelectionChangeAndClearItems ()
		{
			if (this.comboBox != null) {
				this.comboBox.SelectionChanged -= ComboBox_SelectionChanged;
				this.comboBox.RemoveAll ();
			}
		}
	}
}