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 | |
parent | ebc9baab45bb113d6d9d33ba237e6b9416c82b99 (diff) |
[mac] Generic numeric editor
-rw-r--r-- | Xamarin.PropertyEditing.Mac/Controls/DecimalNumericEditorControl.cs | 31 | ||||
-rw-r--r-- | Xamarin.PropertyEditing.Mac/Controls/IntegerNumericEditorControl.cs | 30 | ||||
-rw-r--r-- | Xamarin.PropertyEditing.Mac/Controls/NumericEditorControl.cs (renamed from Xamarin.PropertyEditing.Mac/Controls/BaseNumericEditorControl.cs) | 34 | ||||
-rw-r--r-- | Xamarin.PropertyEditing.Mac/PropertyTableDelegate.cs | 9 | ||||
-rw-r--r-- | Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj | 4 |
5 files changed, 38 insertions, 70 deletions
diff --git a/Xamarin.PropertyEditing.Mac/Controls/DecimalNumericEditorControl.cs b/Xamarin.PropertyEditing.Mac/Controls/DecimalNumericEditorControl.cs deleted file mode 100644 index b9c1468..0000000 --- a/Xamarin.PropertyEditing.Mac/Controls/DecimalNumericEditorControl.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using Foundation; -using Xamarin.PropertyEditing.ViewModels; - -namespace Xamarin.PropertyEditing.Mac -{ - internal class DecimalNumericEditorControl : BaseNumericEditorControl - { - public DecimalNumericEditorControl () - { - NumberStyle = NSNumberFormatterStyle.Decimal; - Formatter.UsesGroupingSeparator = false; - Formatter.MaximumFractionDigits = 15; - - // update the VM value - NumericEditor.ValueChanged += (sender, e) => { - ViewModel.Value = NumericEditor.Value; - }; - } - - internal new FloatingPropertyViewModel ViewModel { - get { return (FloatingPropertyViewModel)base.ViewModel; } - set { base.ViewModel = value; } - } - - protected override void UpdateValue () - { - NumericEditor.Value = ViewModel.Value; - } - } -} diff --git a/Xamarin.PropertyEditing.Mac/Controls/IntegerNumericEditorControl.cs b/Xamarin.PropertyEditing.Mac/Controls/IntegerNumericEditorControl.cs deleted file mode 100644 index 76f440d..0000000 --- a/Xamarin.PropertyEditing.Mac/Controls/IntegerNumericEditorControl.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using AppKit; -using Foundation; -using Xamarin.PropertyEditing.ViewModels; - -namespace Xamarin.PropertyEditing.Mac -{ - internal class IntegerNumericEditorControl : BaseNumericEditorControl - { - public IntegerNumericEditorControl () - { - NumberStyle = NSNumberFormatterStyle.None; - - // update the VM value - NumericEditor.ValueChanged += (sender, e) => { - ViewModel.Value = (long)NumericEditor.Value; - }; - } - - internal new IntegerPropertyViewModel ViewModel { - get { return (IntegerPropertyViewModel)base.ViewModel; } - set { base.ViewModel = value; } - } - - protected override void UpdateValue () - { - NumericEditor.Value = ViewModel.Value; - } - } -} diff --git a/Xamarin.PropertyEditing.Mac/Controls/BaseNumericEditorControl.cs b/Xamarin.PropertyEditing.Mac/Controls/NumericEditorControl.cs index 0736e25..5f326b5 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/BaseNumericEditorControl.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/NumericEditorControl.cs @@ -1,21 +1,37 @@ using System; using System.Collections; -using System.Diagnostics; using AppKit; -using CoreGraphics; using Foundation; using Xamarin.PropertyEditing.Mac.Resources; using Xamarin.PropertyEditing.ViewModels; namespace Xamarin.PropertyEditing.Mac { - internal abstract class BaseNumericEditorControl : PropertyEditorControl + internal class NumericEditorControl<T> + : PropertyEditorControl + where T : struct { - public BaseNumericEditorControl () + 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[] { @@ -67,6 +83,16 @@ namespace Xamarin.PropertyEditing.Mac 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; diff --git a/Xamarin.PropertyEditing.Mac/PropertyTableDelegate.cs b/Xamarin.PropertyEditing.Mac/PropertyTableDelegate.cs index 8c062f6..aabd5db 100644 --- a/Xamarin.PropertyEditing.Mac/PropertyTableDelegate.cs +++ b/Xamarin.PropertyEditing.Mac/PropertyTableDelegate.cs @@ -119,6 +119,9 @@ namespace Xamarin.PropertyEditing.Mac return null; if (controlType.IsGenericTypeDefinition) { + if (genericArgs == null) + genericArgs = propertyType.GetGenericArguments (); + controlType = controlType.MakeGenericType (genericArgs); } @@ -183,8 +186,10 @@ namespace Xamarin.PropertyEditing.Mac private static readonly Dictionary<Type, Type> ViewModelTypes = new Dictionary<Type, Type> { {typeof (StringPropertyViewModel), typeof (StringEditorControl)}, - {typeof (IntegerPropertyViewModel), typeof (IntegerNumericEditorControl)}, - {typeof (FloatingPropertyViewModel), typeof (DecimalNumericEditorControl)}, + {typeof (NumericPropertyViewModel<int>), typeof (NumericEditorControl<>)}, + {typeof (NumericPropertyViewModel<long>), typeof (NumericEditorControl<>)}, + {typeof (NumericPropertyViewModel<float>), typeof (NumericEditorControl<>)}, + {typeof (NumericPropertyViewModel<double>), typeof (NumericEditorControl<>)}, {typeof (PropertyViewModel<bool>), typeof (BooleanEditorControl)}, {typeof (PropertyViewModel<CoreGraphics.CGPoint>), typeof (CGPointEditorControl)}, {typeof (PropertyViewModel<CoreGraphics.CGRect>), typeof (CGRectEditorControl)}, diff --git a/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj b/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj index 5e9a76f..115d3d3 100644 --- a/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj +++ b/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj @@ -59,10 +59,8 @@ <Compile Include="Controls\PropertyEditorControl.cs" />
<Compile Include="PropertyTableDelegate.cs" />
<Compile Include="PropertyTableDataSource.cs" />
- <Compile Include="Controls\BaseNumericEditorControl.cs" />
+ <Compile Include="Controls\NumericEditorControl.cs" />
<Compile Include="Controls\BooleanEditorControl.cs" />
- <Compile Include="Controls\IntegerNumericEditorControl.cs" />
- <Compile Include="Controls\DecimalNumericEditorControl.cs" />
<Compile Include="Controls\ConstraintExtensions.cs" />
<Compile Include="Controls\BaseRectangleEditorControl.cs" />
<Compile Include="Controls\CGRectEditorControl.cs" />
|