diff options
author | Eric Maupin <ermaup@microsoft.com> | 2018-02-14 03:24:37 +0300 |
---|---|---|
committer | Eric Maupin <ermaup@microsoft.com> | 2018-02-14 03:24:37 +0300 |
commit | 3d6818c4b629b6c85b71f66efc0c0ad823098019 (patch) | |
tree | fdbcc11fe3f53d42d7d5a920bb297807af82ea5c /Xamarin.PropertyEditing.Mac/Controls/NumericEditorControl.cs | |
parent | ebc9baab45bb113d6d9d33ba237e6b9416c82b99 (diff) |
[mac] Generic numeric editor
Diffstat (limited to 'Xamarin.PropertyEditing.Mac/Controls/NumericEditorControl.cs')
-rw-r--r-- | Xamarin.PropertyEditing.Mac/Controls/NumericEditorControl.cs | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/Xamarin.PropertyEditing.Mac/Controls/NumericEditorControl.cs b/Xamarin.PropertyEditing.Mac/Controls/NumericEditorControl.cs new file mode 100644 index 0000000..5f326b5 --- /dev/null +++ b/Xamarin.PropertyEditing.Mac/Controls/NumericEditorControl.cs @@ -0,0 +1,102 @@ +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 + where T : struct + { + public NumericEditorControl () + { + base.TranslatesAutoresizingMaskIntoConstraints = false; + + NumericEditor = new NumericSpinEditor (); + NumericEditor.ValueChanged += OnValueChanged; + + TypeCode code = Type.GetTypeCode (typeof (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 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; + } + } + + 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) + { + ((PropertyViewModel<T>)ViewModel).Value = (T)Convert.ChangeType (NumericEditor.Value, typeof(T)); + } + + protected override void UpdateValue() + { + NumericEditor.Value = (double)Convert.ChangeType (((PropertyViewModel<T>)ViewModel).Value, typeof(double)); + } + + protected override void UpdateAccessibilityValues () + { + NumericEditor.AccessibilityEnabled = NumericEditor.Enabled; + NumericEditor.AccessibilityTitle = string.Format (LocalizationResources.AccessibilityNumeric, ViewModel.Property.Name); + } + } +} |