[Core] Share BP across IFontElement implementors (#783)
authorStephane Delcroix <stephane@delcroix.org>
Fri, 3 Mar 2017 08:16:04 +0000 (09:16 +0100)
committerGitHub <noreply@github.com>
Fri, 3 Mar 2017 08:16:04 +0000 (09:16 +0100)
* [Core] Share BP accross IFontElement implementors

* make sure the converter is used

Xamarin.Forms.Core/Button.cs
Xamarin.Forms.Core/Editor.cs
Xamarin.Forms.Core/Entry.cs
Xamarin.Forms.Core/FontElement.cs [new file with mode: 0644]
Xamarin.Forms.Core/IFontElement.cs
Xamarin.Forms.Core/Label.cs
Xamarin.Forms.Core/SearchBar.cs
Xamarin.Forms.Core/Span.cs
Xamarin.Forms.Core/Xamarin.Forms.Core.csproj

index bcd593e..7d8d659 100644 (file)
@@ -25,13 +25,11 @@ namespace Xamarin.Forms
 
                public static readonly BindableProperty FontProperty = BindableProperty.Create("Font", typeof(Font), typeof(Button), default(Font), propertyChanged: FontStructPropertyChanged);
 
-               public static readonly BindableProperty FontFamilyProperty = BindableProperty.Create("FontFamily", typeof(string), typeof(Button), default(string), propertyChanged: SpecificFontPropertyChanged);
+               public static readonly BindableProperty FontFamilyProperty = FontElement.FontFamilyProperty;
 
-               public static readonly BindableProperty FontSizeProperty = BindableProperty.Create("FontSize", typeof(double), typeof(Button), -1.0, propertyChanged: SpecificFontPropertyChanged,
-                       defaultValueCreator: bindable => Device.GetNamedSize(NamedSize.Default, (Button)bindable));
+               public static readonly BindableProperty FontSizeProperty = FontElement.FontSizeProperty;
 
-               public static readonly BindableProperty FontAttributesProperty = BindableProperty.Create("FontAttributes", typeof(FontAttributes), typeof(Button), FontAttributes.None,
-                       propertyChanged: SpecificFontPropertyChanged);
+               public static readonly BindableProperty FontAttributesProperty = FontElement.FontAttributesProperty;
 
                public static readonly BindableProperty BorderWidthProperty = BindableProperty.Create("BorderWidth", typeof(double), typeof(Button), -1d);
 
@@ -214,19 +212,27 @@ namespace Xamarin.Forms
                        {
                                button.FontFamily = button.Font.FontFamily;
                                if (button.Font.UseNamedSize)
-                               {
                                        button.FontSize = Device.GetNamedSize(button.Font.NamedSize, button.GetType(), true);
-                               }
                                else
-                               {
                                        button.FontSize = button.Font.FontSize;
-                               }
                                button.FontAttributes = button.Font.FontAttributes;
                        }
 
                        button._cancelEvents = false;
                }
 
+               void IFontElement.OnFontFamilyChanged(string oldValue, string newValue) =>
+                       SpecificFontPropertyChanged();
+
+               void IFontElement.OnFontSizeChanged(double oldValue, double newValue) =>
+                       SpecificFontPropertyChanged();
+
+               double IFontElement.FontSizeDefaultValueCreator() =>
+                       Device.GetNamedSize(NamedSize.Default, (Button)this);
+
+               void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) =>
+                       SpecificFontPropertyChanged();
+
                void OnCommandChanged()
                {
                        if (Command != null)
@@ -260,27 +266,21 @@ namespace Xamarin.Forms
                                oldvalue.SourceChanged -= OnSourceChanged;
                }
 
-               static void SpecificFontPropertyChanged(BindableObject bindable, object oldValue, object newValue)
+               void SpecificFontPropertyChanged()
                {
-                       var button = (Button)bindable;
-
-                       if (button._cancelEvents)
+                       if (_cancelEvents)
                                return;
 
-                       button.InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
+                       InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
 
-                       button._cancelEvents = true;
+                       _cancelEvents = true;
 
-                       if (button.FontFamily != null)
-                       {
-                               button.Font = Font.OfSize(button.FontFamily, button.FontSize).WithAttributes(button.FontAttributes);
-                       }
+                       if (FontFamily != null)
+                               Font = Font.OfSize(FontFamily, FontSize).WithAttributes(FontAttributes);
                        else
-                       {
-                               button.Font = Font.SystemFontOfSize(button.FontSize, button.FontAttributes);
-                       }
+                               Font = Font.SystemFontOfSize(FontSize, FontAttributes);
 
-                       button._cancelEvents = false;
+                       _cancelEvents = false;
                }
 
                [DebuggerDisplay("Image Position = {Position}, Spacing = {Spacing}")]
index 92a6b9d..66c5caa 100644 (file)
@@ -13,12 +13,11 @@ namespace Xamarin.Forms
                                editor.TextChanged(editor, new TextChangedEventArgs((string)oldValue, (string)newValue));
                });
 
-               public static readonly BindableProperty FontFamilyProperty = BindableProperty.Create("FontFamily", typeof(string), typeof(Editor), default(string));
+               public static readonly BindableProperty FontFamilyProperty = FontElement.FontFamilyProperty;
 
-               public static readonly BindableProperty FontSizeProperty = BindableProperty.Create("FontSize", typeof(double), typeof(Editor), -1.0,
-                       defaultValueCreator: bindable => Device.GetNamedSize(NamedSize.Default, (Editor)bindable));
+               public static readonly BindableProperty FontSizeProperty = FontElement.FontSizeProperty;
 
-               public static readonly BindableProperty FontAttributesProperty = BindableProperty.Create("FontAttributes", typeof(FontAttributes), typeof(Editor), FontAttributes.None);
+               public static readonly BindableProperty FontAttributesProperty = FontElement.FontAttributesProperty;
 
                public static readonly BindableProperty TextColorProperty = BindableProperty.Create("TextColor", typeof(Color), typeof(Editor), Color.Default);
                readonly Lazy<PlatformConfigurationRegistry<Editor>> _platformConfigurationRegistry;
@@ -54,6 +53,21 @@ namespace Xamarin.Forms
                        set { SetValue(FontSizeProperty, value); }
                }
 
+               void IFontElement.OnFontFamilyChanged(string oldValue, string newValue)
+               {
+               }
+
+               void IFontElement.OnFontSizeChanged(double oldValue, double newValue)
+               {
+               }
+
+               double IFontElement.FontSizeDefaultValueCreator() =>
+                       Device.GetNamedSize(NamedSize.Default, (Editor)this);
+
+               void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue)
+               {
+               }
+
                public event EventHandler Completed;
 
                public event EventHandler<TextChangedEventArgs> TextChanged;
index ac16dc5..d499d34 100644 (file)
@@ -18,12 +18,11 @@ namespace Xamarin.Forms
 
                public static readonly BindableProperty PlaceholderColorProperty = BindableProperty.Create("PlaceholderColor", typeof(Color), typeof(Entry), Color.Default);
 
-               public static readonly BindableProperty FontFamilyProperty = BindableProperty.Create("FontFamily", typeof(string), typeof(Entry), default(string));
+               public static readonly BindableProperty FontFamilyProperty = FontElement.FontFamilyProperty;
 
-               public static readonly BindableProperty FontSizeProperty = BindableProperty.Create("FontSize", typeof(double), typeof(Entry), -1.0,
-                       defaultValueCreator: bindable => Device.GetNamedSize(NamedSize.Default, (Entry)bindable));
+               public static readonly BindableProperty FontSizeProperty = FontElement.FontSizeProperty;
 
-               public static readonly BindableProperty FontAttributesProperty = BindableProperty.Create("FontAttributes", typeof(FontAttributes), typeof(Entry), FontAttributes.None);
+               public static readonly BindableProperty FontAttributesProperty = FontElement.FontAttributesProperty;
 
                readonly Lazy<PlatformConfigurationRegistry<Entry>> _platformConfigurationRegistry;
 
@@ -87,6 +86,21 @@ namespace Xamarin.Forms
                        set { SetValue(FontSizeProperty, value); }
                }
 
+               void IFontElement.OnFontFamilyChanged(string oldValue, string newValue)
+               {
+               }
+
+               void IFontElement.OnFontSizeChanged(double oldValue, double newValue)
+               {
+               }
+
+               double IFontElement.FontSizeDefaultValueCreator() =>
+                       Device.GetNamedSize(NamedSize.Default, (Entry)this);
+
+               void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue)
+               {
+               }
+
                public event EventHandler Completed;
 
                public event EventHandler<TextChangedEventArgs> TextChanged;
diff --git a/Xamarin.Forms.Core/FontElement.cs b/Xamarin.Forms.Core/FontElement.cs
new file mode 100644 (file)
index 0000000..66545e0
--- /dev/null
@@ -0,0 +1,38 @@
+namespace Xamarin.Forms
+{
+       static class FontElement
+       {
+               public static readonly BindableProperty FontFamilyProperty =
+                       BindableProperty.Create("FontFamily", typeof(string), typeof(IFontElement), default(string),
+                                                                       propertyChanged: OnFontFamilyChanged);
+
+               public static readonly BindableProperty FontSizeProperty =
+                       BindableProperty.Create("FontSize", typeof(double), typeof(IFontElement), -1.0,
+                                                                       propertyChanged: OnFontSizeChanged,
+                                                                       defaultValueCreator: FontSizeDefaultValueCreator);
+
+               public static readonly BindableProperty FontAttributesProperty =
+                       BindableProperty.Create("FontAttributes", typeof(FontAttributes), typeof(IFontElement), FontAttributes.None,
+                                                                       propertyChanged: OnFontAttributesChanged);
+
+               static void OnFontFamilyChanged(BindableObject bindable, object oldValue, object newValue)
+               {
+                       ((IFontElement)bindable).OnFontFamilyChanged((string)oldValue, (string)newValue);
+               }
+
+               static void OnFontSizeChanged(BindableObject bindable, object oldValue, object newValue)
+               {
+                       ((IFontElement)bindable).OnFontSizeChanged((double)oldValue, (double)newValue);
+               }
+
+               static object FontSizeDefaultValueCreator(BindableObject bindable)
+               {
+                       return ((IFontElement)bindable).FontSizeDefaultValueCreator();
+               }
+
+               static void OnFontAttributesChanged(BindableObject bindable, object oldValue, object newValue)
+               {
+                       ((IFontElement)bindable).OnFontAttributesChanged((FontAttributes)oldValue, (FontAttributes)newValue);
+               }
+       }
+}
\ No newline at end of file
index f1f3a36..469b09f 100644 (file)
@@ -1,11 +1,18 @@
 namespace Xamarin.Forms
 {
-       internal interface IFontElement
+       interface IFontElement
        {
+               //note to implementor: implement the properties publicly
                FontAttributes FontAttributes { get; }
-
                string FontFamily { get; }
 
+               [TypeConverter(typeof(FontSizeConverter))]
                double FontSize { get; }
+
+               //note to implementor: but implement the methods explicitly
+               void OnFontFamilyChanged(string oldValue, string newValue);
+               void OnFontSizeChanged(double oldValue, double newValue);
+               double FontSizeDefaultValueCreator();
+               void OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue);
        }
 }
\ No newline at end of file
index 0634e08..e624932 100644 (file)
@@ -27,13 +27,11 @@ namespace Xamarin.Forms
 
                public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(Label), default(string), propertyChanged: OnTextPropertyChanged);
 
-               public static readonly BindableProperty FontFamilyProperty = BindableProperty.Create("FontFamily", typeof(string), typeof(Label), default(string), propertyChanged: OnFontFamilyChanged);
+               public static readonly BindableProperty FontFamilyProperty = FontElement.FontFamilyProperty;
 
-               public static readonly BindableProperty FontSizeProperty = BindableProperty.Create("FontSize", typeof(double), typeof(Label), -1.0, propertyChanged: OnFontSizeChanged,
-                       defaultValueCreator: bindable => Device.GetNamedSize(NamedSize.Default, (Label)bindable));
+               public static readonly BindableProperty FontSizeProperty = FontElement.FontSizeProperty;
 
-               public static readonly BindableProperty FontAttributesProperty = BindableProperty.Create("FontAttributes", typeof(FontAttributes), typeof(Label), FontAttributes.None,
-                       propertyChanged: OnFontAttributesChanged);
+               public static readonly BindableProperty FontAttributesProperty = FontElement.FontAttributesProperty;
 
                public static readonly BindableProperty FormattedTextProperty = BindableProperty.Create("FormattedText", typeof(FormattedString), typeof(Label), default(FormattedString),
                        propertyChanging: (bindable, oldvalue, newvalue) =>
@@ -171,93 +169,72 @@ namespace Xamarin.Forms
                        label.InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
                }
 
-               static void OnFontAttributesChanged(BindableObject bindable, object oldValue, object newValue)
-               {
-                       var label = (Label)bindable;
+               double IFontElement.FontSizeDefaultValueCreator() =>
+                       Device.GetNamedSize(NamedSize.Default, (Label)this);
 
-                       if (label._cancelEvents)
+               void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue)
+               {
+                       if (_cancelEvents)
                                return;
 
-                       label._cancelEvents = true;
+                       _cancelEvents = true;
 
-                       var attributes = (FontAttributes)newValue;
+                       var attributes = newValue;
 
-                       object[] values = label.GetValues(FontFamilyProperty, FontSizeProperty);
+#pragma warning disable 0618 // retain until Font removed
+                       object[] values = GetValues(FontFamilyProperty, FontSizeProperty);
                        var family = (string)values[0];
                        if (family != null)
-                       {
-#pragma warning disable 0618 // retain until Font removed
-                               label.Font = Font.OfSize(family, (double)values[1]).WithAttributes(attributes);
-#pragma warning restore 0618
-                       }
+                               Font = Font.OfSize(family, (double)values[1]).WithAttributes(attributes);
                        else
-                       {
-#pragma warning disable 0618 // retain until Font removed
-                               label.Font = Font.SystemFontOfSize((double)values[1], attributes);
+                               Font = Font.SystemFontOfSize((double)values[1], attributes);
 #pragma warning restore 0618
-                       }
 
-                       label._cancelEvents = false;
+                       _cancelEvents = false;
 
-                       label.InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
+                       InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
                }
 
-               static void OnFontFamilyChanged(BindableObject bindable, object oldValue, object newValue)
+               void IFontElement.OnFontFamilyChanged(string oldValue, string newValue)
                {
-                       var label = (Label)bindable;
-                       if (label._cancelEvents)
+                       if (_cancelEvents)
                                return;
 
-                       label._cancelEvents = true;
-
-                       object[] values = label.GetValues(FontSizeProperty, FontAttributesProperty);
+                       _cancelEvents = true;
 
-                       var family = (string)newValue;
-                       if (family != null)
-                       {
 #pragma warning disable 0618 // retain until Font removed
-                               label.Font = Font.OfSize(family, (double)values[0]).WithAttributes((FontAttributes)values[1]);
-#pragma warning restore 0618
-                       }
+                       object[] values = GetValues(FontSizeProperty, FontAttributesProperty);
+                       var family = newValue;
+                       if (family != null)
+                               Font = Font.OfSize(family, (double)values[0]).WithAttributes((FontAttributes)values[1]);
                        else
-                       {
-#pragma warning disable 0618 // retain until Font removed
-                               label.Font = Font.SystemFontOfSize((double)values[0], (FontAttributes)values[1]);
+                               Font = Font.SystemFontOfSize((double)values[0], (FontAttributes)values[1]);
 #pragma warning restore 0618
-                       }
 
-                       label._cancelEvents = false;
-                       label.InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
+                       _cancelEvents = false;
+                       InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
                }
 
-               static void OnFontSizeChanged(BindableObject bindable, object oldValue, object newValue)
+               void IFontElement.OnFontSizeChanged(double oldValue, double newValue)
                {
-                       var label = (Label)bindable;
-                       if (label._cancelEvents)
+                       if (_cancelEvents)
                                return;
 
-                       label._cancelEvents = true;
-
-                       object[] values = label.GetValues(FontFamilyProperty, FontAttributesProperty);
+                       _cancelEvents = true;
 
-                       var size = (double)newValue;
+#pragma warning disable 0618 // retain until Font removed
+                       object[] values = GetValues(FontFamilyProperty, FontAttributesProperty);
+                       var size = newValue;
                        var family = (string)values[0];
                        if (family != null)
-                       {
-#pragma warning disable 0618 // retain until Font removed
-                               label.Font = Font.OfSize(family, size).WithAttributes((FontAttributes)values[1]);
-#pragma warning restore 0618
-                       }
+                               Font = Font.OfSize(family, size).WithAttributes((FontAttributes)values[1]);
                        else
-                       {
-#pragma warning disable 0618 // retain until Font removed
-                               label.Font = Font.SystemFontOfSize(size, (FontAttributes)values[1]);
+                               Font = Font.SystemFontOfSize(size, (FontAttributes)values[1]);
 #pragma warning restore 0618
-                       }
 
-                       label._cancelEvents = false;
+                       _cancelEvents = false;
 
-                       label.InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
+                       InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
                }
 
                void OnFormattedTextChanged(object sender, PropertyChangedEventArgs e)
index 6e2a6c6..5e82f84 100644 (file)
@@ -24,12 +24,11 @@ namespace Xamarin.Forms
 
                public static readonly BindableProperty PlaceholderProperty = BindableProperty.Create("Placeholder", typeof(string), typeof(SearchBar), null);
 
-               public static readonly BindableProperty FontFamilyProperty = BindableProperty.Create("FontFamily", typeof(string), typeof(SearchBar), default(string));
+               public static readonly BindableProperty FontFamilyProperty = FontElement.FontFamilyProperty;
 
-               public static readonly BindableProperty FontSizeProperty = BindableProperty.Create("FontSize", typeof(double), typeof(SearchBar), -1.0,
-                       defaultValueCreator: bindable => Device.GetNamedSize(NamedSize.Default, (SearchBar)bindable));
+               public static readonly BindableProperty FontSizeProperty = FontElement.FontSizeProperty;
 
-               public static readonly BindableProperty FontAttributesProperty = BindableProperty.Create("FontAttributes", typeof(FontAttributes), typeof(SearchBar), FontAttributes.None);
+               public static readonly BindableProperty FontAttributesProperty = FontElement.FontAttributesProperty;
 
                public static readonly BindableProperty HorizontalTextAlignmentProperty = BindableProperty.Create("HorizontalTextAlignment", typeof(TextAlignment), typeof(SearchBar), TextAlignment.Start);
 
@@ -111,6 +110,21 @@ namespace Xamarin.Forms
                        set { SetValue(FontSizeProperty, value); }
                }
 
+               void IFontElement.OnFontFamilyChanged(string oldValue, string newValue)
+               {
+               }
+
+               void IFontElement.OnFontSizeChanged(double oldValue, double newValue)
+               {
+               }
+
+               double IFontElement.FontSizeDefaultValueCreator() =>
+                       Device.GetNamedSize(NamedSize.Default, (SearchBar)this);
+
+               void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue)
+               {
+               }
+
                public event EventHandler SearchButtonPressed;
 
                public event EventHandler<TextChangedEventArgs> TextChanged;
index 6ffc3f1..a410c10 100644 (file)
@@ -84,9 +84,11 @@ namespace Xamarin.Forms
                        {
                                if (_fontAttributes == value)
                                        return;
+                               var oldValue = _fontAttributes;
                                _fontAttributes = value;
                                OnPropertyChanged();
-                               UpdateStructFromFontProperties();
+
+                               ((IFontElement)this).OnFontAttributesChanged(oldValue, value);
                        }
                }
 
@@ -97,9 +99,10 @@ namespace Xamarin.Forms
                        {
                                if (_fontFamily == value)
                                        return;
+                               var oldValue = _fontFamily;
                                _fontFamily = value;
                                OnPropertyChanged();
-                               UpdateStructFromFontProperties();
+                               ((IFontElement)this).OnFontFamilyChanged(oldValue, value);
                        }
                }
 
@@ -111,9 +114,10 @@ namespace Xamarin.Forms
                        {
                                if (_fontSize == value)
                                        return;
+                               var oldValue = _fontSize;
                                _fontSize = value;
                                OnPropertyChanged();
-                               UpdateStructFromFontProperties();
+                               ((IFontElement)this).OnFontSizeChanged(oldValue, value);
                        }
                }
 
@@ -149,23 +153,34 @@ namespace Xamarin.Forms
                        _inUpdate = false;
                }
 
-               void UpdateStructFromFontProperties()
+               void OnSomeFontPropertyChanged()
                {
-                       if (_inUpdate)
-                               return;
-                       _inUpdate = true;
-
-                       if (FontFamily != null)
                        {
-                               Font = Font.OfSize(FontFamily, FontSize).WithAttributes(FontAttributes);
-                       }
-                       else
-                       {
-                               Font = Font.SystemFontOfSize(FontSize).WithAttributes(FontAttributes);
-                       }
+                               if (_inUpdate)
+                                       return;
+                               _inUpdate = true;
 
-                       _inUpdate = false;
+                               if (FontFamily != null)
+                                       Font = Font.OfSize(FontFamily, FontSize).WithAttributes(FontAttributes);
+                               else
+                                       Font = Font.SystemFontOfSize(FontSize).WithAttributes(FontAttributes);
+
+                               _inUpdate = false;
+                       }
                }
-       }
+
 #pragma warning restore
+
+               void IFontElement.OnFontFamilyChanged(string oldValue, string newValue) =>
+                       OnSomeFontPropertyChanged();
+
+               void IFontElement.OnFontSizeChanged(double oldValue, double newValue) =>
+                       OnSomeFontPropertyChanged();
+
+               double IFontElement.FontSizeDefaultValueCreator() =>
+                       Device.GetNamedSize(NamedSize.Default, typeof(Label));
+
+               void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) =>
+                       OnSomeFontPropertyChanged();
+       }
 }
\ No newline at end of file
index 944309f..71df260 100644 (file)
     <Compile Include="XmlnsDefinitionAttribute.cs" />
     <Compile Include="PlatformConfiguration\macOSSpecific\TabbedPage.cs" />
     <Compile Include="PlatformConfiguration\macOSSpecific\TabsStyle.cs" />
+    <Compile Include="FontElement.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
   <ItemGroup>