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
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using AppKit;
using Foundation;
using Xamarin.PropertyEditing.Mac.Resources;
using Xamarin.PropertyEditing.ViewModels;
namespace Xamarin.PropertyEditing.Mac
{
internal class NumericEditorControl<T>
: PropertyEditorControl<NumericPropertyViewModel<T>>
{
public NumericEditorControl ()
{
base.TranslatesAutoresizingMaskIntoConstraints = false;
NumericEditor = new NumericSpinEditor<T> ();
NumericEditor.ValueChanged += OnValueChanged;
var t = typeof (T);
if (t.Name == PropertyViewModel<T>.NullableName) {
this.underlyingType = Nullable.GetUnderlyingType (t);
t = this.underlyingType;
}
TypeCode code = Type.GetTypeCode (t);
switch (code) {
case TypeCode.Double:
case TypeCode.Single:
case TypeCode.Decimal:
NumberStyle = NSNumberFormatterStyle.Decimal;
Formatter.UsesGroupingSeparator = false;
Formatter.MaximumFractionDigits = 15;
break;
default:
NumberStyle = NSNumberFormatterStyle.None;
break;
}
AddSubview (NumericEditor);
this.numericEditorWidthConstraint = NSLayoutConstraint.Create (NumericEditor, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this, NSLayoutAttribute.Width, 1f, -32f);
this.AddConstraints (new[] {
NSLayoutConstraint.Create (NumericEditor, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1f, 1f),
this.numericEditorWidthConstraint,
});
}
protected NumericSpinEditor<T> NumericEditor { get; set; }
protected NSNumberFormatter Formatter {
get { return NumericEditor.Formatter; }
set { NumericEditor.Formatter = value; }
}
public override NSView FirstKeyView => NumericEditor;
public override NSView LastKeyView => NumericEditor;
protected NSNumberFormatterStyle NumberStyle {
get {
return NumericEditor.NumberStyle; }
set {
NumericEditor.NumberStyle = value;
}
}
private Type underlyingType;
internal NSPopUpButton inputModePopup;
private IReadOnlyList<InputMode> viewModelInputModes;
private NSLayoutConstraint numericEditorWidthConstraint;
protected override void UpdateErrorsDisplayed (IEnumerable errors)
{
if (ViewModel.HasErrors) {
SetErrors (errors);
} else {
SetErrors (null);
SetEnabled ();
}
}
protected override void HandleErrorsChanged (object sender, System.ComponentModel.DataErrorsChangedEventArgs e)
{
UpdateErrorsDisplayed (ViewModel.GetErrors (ViewModel.Property.Name));
}
protected override void SetEnabled ()
{
NumericEditor.Editable = ViewModel.Property.CanWrite;
if (this.inputModePopup != null)
this.inputModePopup.Enabled = ViewModel.Property.CanWrite;
}
protected virtual void OnValueChanged (object sender, EventArgs e)
{
var t = typeof (T);
if (this.underlyingType != null)
t = this.underlyingType;
ViewModel.Value = (T)Convert.ChangeType (NumericEditor.Value, t);
}
protected override void UpdateValue()
{
if (this.underlyingType != null) {
NumericEditor.StringValue = ViewModel.Value == null ? string.Empty : ViewModel.Value.ToString ();
NumericEditor.Enabled = ((ViewModel.InputMode != null) && !ViewModel.InputMode.IsSingleValue) || (this.inputModePopup == null);
if (this.inputModePopup != null)
this.inputModePopup.SelectItem ((ViewModel.InputMode == null) ? string.Empty : ViewModel.InputMode.Identifier);
} else {
NumericEditor.Value = (double)Convert.ChangeType (ViewModel.Value, typeof (double));
if (this.inputModePopup != null)
this.inputModePopup.SelectItem (-1);
}
}
protected override void UpdateAccessibilityValues ()
{
NumericEditor.AccessibilityEnabled = NumericEditor.Enabled;
NumericEditor.AccessibilityTitle = string.Format (LocalizationResources.AccessibilityNumeric, ViewModel.Property.Name);
if (this.inputModePopup != null) {
this.inputModePopup.AccessibilityEnabled = NumericEditor.Enabled;
this.inputModePopup.AccessibilityTitle = string.Format (LocalizationResources.AccessibilityInpueModeEditor, ViewModel.Property.Name);
}
}
protected override void OnViewModelChanged (PropertyViewModel oldModel)
{
if (ViewModel.HasInputModes) {
if (this.inputModePopup == null) {
this.inputModePopup = new NSPopUpButton {
Menu = new NSMenu (),
TranslatesAutoresizingMaskIntoConstraints = false,
};
this.inputModePopup.Activated += (o, e) => {
var popupButton = o as NSPopUpButton;
ViewModel.InputMode = this.viewModelInputModes.FirstOrDefault (im => im.Identifier == popupButton.Title);
};
AddSubview (this.inputModePopup);
this.AddConstraints (new[] {
NSLayoutConstraint.Create (this.inputModePopup, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1f, 1f),
NSLayoutConstraint.Create (this.inputModePopup, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1f, -33f),
NSLayoutConstraint.Create (this.inputModePopup, NSLayoutAttribute.Width, NSLayoutRelation.Equal, 1f, 80f),
NSLayoutConstraint.Create (this.inputModePopup, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, DefaultControlHeight - 3 ),
});
}
this.inputModePopup.Menu.RemoveAllItems ();
this.viewModelInputModes = ViewModel.InputModes;
foreach (InputMode item in this.viewModelInputModes) {
this.inputModePopup.Menu.AddItem (new NSMenuItem (item.Identifier));
}
this.numericEditorWidthConstraint.Constant = -117f; // Shorten the stringEditor if we have Inputmodes Showing.
} else {
this.numericEditorWidthConstraint.Constant = -34f; // Lengthen the stringEditor if we have Inputmodes Hidden.
}
// If we are reusing the control we'll have to hid the inputMode if this doesn't have InputMode.
if (this.inputModePopup != null)
this.inputModePopup.Hidden = !ViewModel.HasInputModes;
base.OnViewModelChanged (oldModel);
}
}
}
|