diff options
author | Bertrand Le Roy <beleroy@microsoft.com> | 2018-06-12 05:47:09 +0300 |
---|---|---|
committer | Bertrand Le Roy <beleroy@microsoft.com> | 2018-06-20 21:23:50 +0300 |
commit | f0d6d95a08a67d263cdffe65cf6cac67cb243be2 (patch) | |
tree | f41b5a0e18e8614facd224f3a9ed16cd44705c87 | |
parent | 0cec86376f629b4dfc682797557a70c591fc1a84 (diff) |
Allow for properties of type CommonColor.
8 files changed, 78 insertions, 20 deletions
diff --git a/Xamarin.PropertyEditing.Tests/MockObjectEditor.cs b/Xamarin.PropertyEditing.Tests/MockObjectEditor.cs index fdd59ae..8c0f6a5 100644 --- a/Xamarin.PropertyEditing.Tests/MockObjectEditor.cs +++ b/Xamarin.PropertyEditing.Tests/MockObjectEditor.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; +using System.Reflection; using System.Threading.Tasks; using Xamarin.PropertyEditing.Reflection; using Xamarin.PropertyEditing.Tests.MockControls; @@ -188,10 +190,19 @@ namespace Xamarin.PropertyEditing.Tests // Set to resource won't pass values so we will store it on the info since we just pass it back in GetValue if (value.Source == ValueSource.Resource && value.ValueDescriptor is Resource) { - var rt = value.ValueDescriptor.GetType(); - if (rt.IsGenericType && typeof(T).IsAssignableFrom (rt.GetGenericArguments ()[0])) { - var pi = rt.GetProperty ("Value"); - value.Value = (T)pi.GetValue (value.ValueDescriptor); + Type rt = value.ValueDescriptor.GetType(); + if (rt.IsGenericType) { + Type ta = rt.GetGenericArguments ()[0]; + if (typeof (T).IsAssignableFrom (ta)) { + PropertyInfo pi = rt.GetProperty ("Value"); + value.Value = (T)pi.GetValue (value.ValueDescriptor); + } else { + TypeConverter converter = TypeDescriptor.GetConverter (ta); + if (converter != null && converter.CanConvertTo(typeof(T))) { + PropertyInfo pi = rt.GetProperty ("Value"); + value.Value = (T)converter.ConvertTo (pi.GetValue (value.ValueDescriptor), typeof (T)); + } + } } } @@ -259,7 +270,7 @@ namespace Xamarin.PropertyEditing.Tests object newValue; IPropertyConverter converter = property as IPropertyConverter; - if (converter != null && converter.TryConvert (value, typeof(T), out newValue)) { + if (converter != null && converter.TryConvert (value, tType, out newValue)) { return new ValueInfo<T> { Source = source, Value = (T)newValue, diff --git a/Xamarin.PropertyEditing.Windows.Standalone/MockedSampleControlButton.cs b/Xamarin.PropertyEditing.Windows.Standalone/MockedSampleControlButton.cs index ef6ff12..5514b53 100644 --- a/Xamarin.PropertyEditing.Windows.Standalone/MockedSampleControlButton.cs +++ b/Xamarin.PropertyEditing.Windows.Standalone/MockedSampleControlButton.cs @@ -28,6 +28,13 @@ namespace Xamarin.PropertyEditing.Windows.Standalone category: "Windows Only", canWrite: false); MockedControl.AddProperty<CommonBrush> (this.readOnlyBrushPropertyInfo); + + this.colorPropertyInfo = new MockPropertyInfo<CommonColor> ( + name: "ColorNoBrush", + category: "Windows Only", + canWrite: true, + valueSources: ValueSources.Default | ValueSources.Local | ValueSources.Resource); + MockedControl.AddProperty<CommonColor> (this.colorPropertyInfo); } public async Task SetBrushInitialValueAsync (IObjectEditor editor, CommonBrush brush) @@ -51,9 +58,10 @@ namespace Xamarin.PropertyEditing.Windows.Standalone this.readOnlyBrushSet = true; } - private IPropertyInfo brushPropertyInfo; - private IPropertyInfo materialDesignBrushPropertyInfo; - private IPropertyInfo readOnlyBrushPropertyInfo; + private readonly IPropertyInfo brushPropertyInfo; + private readonly IPropertyInfo materialDesignBrushPropertyInfo; + private readonly IPropertyInfo readOnlyBrushPropertyInfo; + private readonly IPropertyInfo colorPropertyInfo; private bool brushSet = false; private bool materialDesignBrushSet = false; private bool readOnlyBrushSet = false; diff --git a/Xamarin.PropertyEditing/Drawing/CommonBrushType.cs b/Xamarin.PropertyEditing/Drawing/CommonBrushType.cs index 8098306..cb1e8e8 100644 --- a/Xamarin.PropertyEditing/Drawing/CommonBrushType.cs +++ b/Xamarin.PropertyEditing/Drawing/CommonBrushType.cs @@ -1,3 +1,5 @@ +using System; + namespace Xamarin.PropertyEditing.Drawing { public enum CommonBrushType diff --git a/Xamarin.PropertyEditing/Drawing/CommonColor.cs b/Xamarin.PropertyEditing/Drawing/CommonColor.cs index 632b033..dabef69 100644 --- a/Xamarin.PropertyEditing/Drawing/CommonColor.cs +++ b/Xamarin.PropertyEditing/Drawing/CommonColor.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; namespace Xamarin.PropertyEditing.Drawing { @@ -6,6 +7,7 @@ namespace Xamarin.PropertyEditing.Drawing /// Describes a color. /// </summary> [Serializable] + [TypeConverter(typeof(CommonColorToCommonBrushConverter))] public struct CommonColor : IEquatable<CommonColor> { public CommonColor (byte r, byte g, byte b, byte a = 255, string label = null) diff --git a/Xamarin.PropertyEditing/Drawing/CommonColorToCommonBrushConverter.cs b/Xamarin.PropertyEditing/Drawing/CommonColorToCommonBrushConverter.cs new file mode 100644 index 0000000..7161334 --- /dev/null +++ b/Xamarin.PropertyEditing/Drawing/CommonColorToCommonBrushConverter.cs @@ -0,0 +1,26 @@ +using System; +using System.ComponentModel; +using System.Globalization; + +namespace Xamarin.PropertyEditing.Drawing +{ + internal class CommonColorToCommonBrushConverter : TypeConverter + { + public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType) + => typeof(CommonBrush) == destinationType ? true : base.CanConvertTo (context, destinationType); + + public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) + { + if (typeof (CommonBrush).IsAssignableFrom (destinationType) && value is CommonColor color) { + return new CommonSolidBrush (color); + } + return base.ConvertTo (context, culture, value, destinationType); + } + + public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType) + => sourceType == typeof (CommonColor) ? true : base.CanConvertFrom (context, sourceType); + + public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value) + => value is CommonColor color ? new CommonSolidBrush(color) : base.ConvertFrom (context, culture, value); + } +}
\ No newline at end of file diff --git a/Xamarin.PropertyEditing/ViewModels/BrushPropertyViewModel.cs b/Xamarin.PropertyEditing/ViewModels/BrushPropertyViewModel.cs index 9ae2791..8b367af 100644 --- a/Xamarin.PropertyEditing/ViewModels/BrushPropertyViewModel.cs +++ b/Xamarin.PropertyEditing/ViewModels/BrushPropertyViewModel.cs @@ -12,10 +12,12 @@ namespace Xamarin.PropertyEditing.ViewModels { internal class BrushPropertyViewModel : PropertyViewModel<CommonBrush> { - public BrushPropertyViewModel (TargetPlatform platform, IPropertyInfo property, IEnumerable<IObjectEditor> editors) + public BrushPropertyViewModel (TargetPlatform platform, IPropertyInfo property, IEnumerable<IObjectEditor> editors, + IEnumerable<CommonBrushType> allowedBrushTypes = null) : base (platform, property, editors) { - if (property.Type.IsAssignableFrom (typeof (CommonSolidBrush))) { + if (property.Type.IsAssignableFrom (typeof (CommonSolidBrush)) + || property.Type.IsAssignableFrom (typeof (CommonColor))) { Solid = new SolidBrushViewModel (this, property is IColorSpaced colorSpacedPropertyInfo ? colorSpacedPropertyInfo.ColorSpaces : null); if (platform.SupportsMaterialDesign) { @@ -23,14 +25,19 @@ namespace Xamarin.PropertyEditing.ViewModels } } - // TODO: we actually need to localize this for platforms really, "brush" doesn't make sense for some - var types = new OrderedDictionary<string, CommonBrushType> { - { Resources.NoBrush, CommonBrushType.NoBrush }, - { Resources.SolidBrush, CommonBrushType.Solid }, - { Resources.ResourceBrush, CommonBrushType.Resource } + allowedBrushTypes = allowedBrushTypes ?? new[] { + CommonBrushType.NoBrush, CommonBrushType.Solid, CommonBrushType.MaterialDesign, + CommonBrushType.Gradient, CommonBrushType.Tile, CommonBrushType.Resource }; - if (platform.SupportsMaterialDesign) { + // TODO: we actually need to localize this for platforms really, "brush" doesn't make sense for some + var types = new OrderedDictionary<string, CommonBrushType> (); + + if (allowedBrushTypes.Contains (CommonBrushType.NoBrush)) types.Add (Resources.NoBrush, CommonBrushType.NoBrush); + if (allowedBrushTypes.Contains (CommonBrushType.Solid)) types.Add (Resources.SolidBrush, CommonBrushType.Solid); + if (allowedBrushTypes.Contains (CommonBrushType.Resource)) types.Add (Resources.ResourceBrush, CommonBrushType.Resource); + + if (platform.SupportsMaterialDesign && allowedBrushTypes.Contains (CommonBrushType.MaterialDesign)) { types.Insert (2, Resources.MaterialDesignColorBrush, CommonBrushType.MaterialDesign); } @@ -171,8 +178,8 @@ namespace Xamarin.PropertyEditing.ViewModels break; case CommonBrushType.Solid: Value = Solid?.PreviousSolidBrush ?? new CommonSolidBrush (CommonColor.Black); - Solid.CommitLastColor (); - Solid.CommitHue (); + Solid?.CommitLastColor (); + Solid?.CommitHue (); break; case CommonBrushType.MaterialDesign: MaterialDesign.SetToClosest (); diff --git a/Xamarin.PropertyEditing/ViewModels/PropertiesViewModel.cs b/Xamarin.PropertyEditing/ViewModels/PropertiesViewModel.cs index 4318b1e..41f31df 100644 --- a/Xamarin.PropertyEditing/ViewModels/PropertiesViewModel.cs +++ b/Xamarin.PropertyEditing/ViewModels/PropertiesViewModel.cs @@ -454,14 +454,15 @@ namespace Xamarin.PropertyEditing.ViewModels private Task busyTask; - private static readonly Dictionary<Type,Func<TargetPlatform,IPropertyInfo,IEnumerable<IObjectEditor>,PropertyViewModel>> ViewModelMap = new Dictionary<Type, Func<TargetPlatform, IPropertyInfo, IEnumerable<IObjectEditor>, PropertyViewModel>> { + private static readonly Dictionary<Type, Func<TargetPlatform, IPropertyInfo, IEnumerable<IObjectEditor>, PropertyViewModel>> ViewModelMap = new Dictionary<Type, Func<TargetPlatform, IPropertyInfo, IEnumerable<IObjectEditor>, PropertyViewModel>> { { typeof(string), (tp,p,e) => new StringPropertyViewModel (tp, p, e) }, { typeof(bool), (tp,p,e) => new PropertyViewModel<bool?> (tp, p, e) }, { typeof(float), (tp,p,e) => new NumericPropertyViewModel<float?> (tp, p, e) }, { typeof(double), (tp,p,e) => new NumericPropertyViewModel<double?> (tp, p, e) }, { typeof(int), (tp,p,e) => new NumericPropertyViewModel<int?> (tp, p, e) }, { typeof(long), (tp,p,e) => new NumericPropertyViewModel<long?> (tp, p, e) }, - { typeof(CommonSolidBrush), (tp,p,e) => new BrushPropertyViewModel(tp, p, e) }, + { typeof(CommonSolidBrush), (tp,p,e) => new BrushPropertyViewModel(tp, p, e, new[] {CommonBrushType.NoBrush, CommonBrushType.Solid, CommonBrushType.MaterialDesign, CommonBrushType.Resource }) }, + { typeof(CommonColor), (tp,p,e) => new BrushPropertyViewModel(tp, p, e, new[] {CommonBrushType.NoBrush, CommonBrushType.Solid, CommonBrushType.MaterialDesign, CommonBrushType.Resource }) }, { typeof(CommonBrush), (tp,p,e) => new BrushPropertyViewModel(tp, p, e) }, { typeof(CommonPoint), (tp,p,e) => new PointPropertyViewModel (tp, p, e) }, { typeof(CommonSize), (tp,p,e) => new SizePropertyViewModel (tp, p, e) }, diff --git a/Xamarin.PropertyEditing/Xamarin.PropertyEditing.csproj b/Xamarin.PropertyEditing/Xamarin.PropertyEditing.csproj index 64622a4..ad11638 100644 --- a/Xamarin.PropertyEditing/Xamarin.PropertyEditing.csproj +++ b/Xamarin.PropertyEditing/Xamarin.PropertyEditing.csproj @@ -58,6 +58,7 @@ <Compile Include="Drawing\CommonBrushType.cs" /> <Compile Include="Drawing\CommonColor.cs" /> <Compile Include="Drawing\CommonColorInterpolationMode.cs" /> + <Compile Include="Drawing\CommonColorToCommonBrushConverter.cs" /> <Compile Include="Drawing\CommonGradientBrush.cs" /> <Compile Include="Drawing\CommonGradientSpreadMethod.cs" /> <Compile Include="Drawing\CommonGradientStop.cs" /> |