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
|
using System;
using System.Collections;
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) {
underlyingType = Nullable.GetUnderlyingType (t);
t = 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.DoConstraints ( new[] {
NumericEditor.ConstraintTo (this, (n, c) => n.Top == c.Top + 1),
NumericEditor.ConstraintTo (this, (n, c) => n.Left == c.Left + 4),
NumericEditor.ConstraintTo (this, (n, c) => n.Width == c.Width - 33),
});
}
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.DecrementButton;
protected NSNumberFormatterStyle NumberStyle {
get {
return NumericEditor.NumberStyle; }
set {
NumericEditor.NumberStyle = value;
}
}
private Type underlyingType;
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;
}
protected virtual void OnValueChanged (object sender, EventArgs e)
{
var t = typeof (T);
if (underlyingType != null)
t = underlyingType;
ViewModel.Value = (T)Convert.ChangeType (NumericEditor.Value, t);
}
protected override void UpdateValue()
{
if (underlyingType != null) {
NumericEditor.StringValue = ViewModel.Value == null ? string.Empty : ViewModel.Value.ToString ();
} else {
NumericEditor.Value = (double)Convert.ChangeType (ViewModel.Value, typeof (double));
}
}
protected override void UpdateAccessibilityValues ()
{
NumericEditor.AccessibilityEnabled = NumericEditor.Enabled;
NumericEditor.AccessibilityTitle = string.Format (LocalizationResources.AccessibilityNumeric, ViewModel.Property.Name);
}
}
}
|