From 81058db4f57982891904e7d8da68971ab4d91379 Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Fri, 3 Mar 2017 12:39:30 +0100 Subject: [PATCH] [C] move the Font proxying into FontElement --- Xamarin.Forms.Core/Button.cs | 60 ++--------------- Xamarin.Forms.Core/Editor.cs | 4 ++ Xamarin.Forms.Core/Entry.cs | 4 ++ Xamarin.Forms.Core/FontElement.cs | 84 +++++++++++++++++++++++ Xamarin.Forms.Core/IFontElement.cs | 1 + Xamarin.Forms.Core/Label.cs | 103 ++--------------------------- Xamarin.Forms.Core/SearchBar.cs | 4 ++ Xamarin.Forms.Core/Span.cs | 30 ++++++--- 8 files changed, 133 insertions(+), 157 deletions(-) diff --git a/Xamarin.Forms.Core/Button.cs b/Xamarin.Forms.Core/Button.cs index 7d8d659b5..9db917bca 100644 --- a/Xamarin.Forms.Core/Button.cs +++ b/Xamarin.Forms.Core/Button.cs @@ -23,7 +23,7 @@ namespace Xamarin.Forms public static readonly BindableProperty TextColorProperty = BindableProperty.Create("TextColor", typeof(Color), typeof(Button), Color.Default); - public static readonly BindableProperty FontProperty = BindableProperty.Create("Font", typeof(Font), typeof(Button), default(Font), propertyChanged: FontStructPropertyChanged); + public static readonly BindableProperty FontProperty = FontElement.FontProperty; public static readonly BindableProperty FontFamilyProperty = FontElement.FontFamilyProperty; @@ -43,8 +43,6 @@ namespace Xamarin.Forms readonly Lazy> _platformConfigurationRegistry; - bool _cancelEvents; - const double DefaultSpacing = 10; public Color BorderColor @@ -191,47 +189,20 @@ namespace Xamarin.Forms IsEnabledCore = cmd.CanExecute(CommandParameter); } - static void FontStructPropertyChanged(BindableObject bindable, object oldValue, object newValue) - { - var button = (Button)bindable; - - if (button._cancelEvents) - return; - - button.InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); - - button._cancelEvents = true; - - if (button.Font == Font.Default) - { - button.FontFamily = null; - button.FontSize = Device.GetNamedSize(NamedSize.Default, button); - button.FontAttributes = FontAttributes.None; - } - else - { - 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(); + InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); void IFontElement.OnFontSizeChanged(double oldValue, double newValue) => - SpecificFontPropertyChanged(); + InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); double IFontElement.FontSizeDefaultValueCreator() => Device.GetNamedSize(NamedSize.Default, (Button)this); void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) => - SpecificFontPropertyChanged(); + InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); + + void IFontElement.OnFontChanged(Font oldValue, Font newValue) => + InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); void OnCommandChanged() { @@ -266,23 +237,6 @@ namespace Xamarin.Forms oldvalue.SourceChanged -= OnSourceChanged; } - void SpecificFontPropertyChanged() - { - if (_cancelEvents) - return; - - InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); - - _cancelEvents = true; - - if (FontFamily != null) - Font = Font.OfSize(FontFamily, FontSize).WithAttributes(FontAttributes); - else - Font = Font.SystemFontOfSize(FontSize, FontAttributes); - - _cancelEvents = false; - } - [DebuggerDisplay("Image Position = {Position}, Spacing = {Spacing}")] [TypeConverter(typeof(ButtonContentTypeConverter))] public sealed class ButtonContentLayout diff --git a/Xamarin.Forms.Core/Editor.cs b/Xamarin.Forms.Core/Editor.cs index 66c5caa79..ee9d44220 100644 --- a/Xamarin.Forms.Core/Editor.cs +++ b/Xamarin.Forms.Core/Editor.cs @@ -61,6 +61,10 @@ namespace Xamarin.Forms { } + void IFontElement.OnFontChanged(Font oldValue, Font newValue) + { + } + double IFontElement.FontSizeDefaultValueCreator() => Device.GetNamedSize(NamedSize.Default, (Editor)this); diff --git a/Xamarin.Forms.Core/Entry.cs b/Xamarin.Forms.Core/Entry.cs index d499d34e5..610a30d85 100644 --- a/Xamarin.Forms.Core/Entry.cs +++ b/Xamarin.Forms.Core/Entry.cs @@ -101,6 +101,10 @@ namespace Xamarin.Forms { } + void IFontElement.OnFontChanged(Font oldValue, Font newValue) + { + } + public event EventHandler Completed; public event EventHandler TextChanged; diff --git a/Xamarin.Forms.Core/FontElement.cs b/Xamarin.Forms.Core/FontElement.cs index 66545e07f..7c15a48bd 100644 --- a/Xamarin.Forms.Core/FontElement.cs +++ b/Xamarin.Forms.Core/FontElement.cs @@ -2,6 +2,10 @@ namespace Xamarin.Forms { static class FontElement { + public static readonly BindableProperty FontProperty = + BindableProperty.Create("Font", typeof(Font), typeof(IFontElement), default(Font), + propertyChanged: OnFontPropertyChanged); + public static readonly BindableProperty FontFamilyProperty = BindableProperty.Create("FontFamily", typeof(string), typeof(IFontElement), default(string), propertyChanged: OnFontFamilyChanged); @@ -15,13 +19,77 @@ namespace Xamarin.Forms BindableProperty.Create("FontAttributes", typeof(FontAttributes), typeof(IFontElement), FontAttributes.None, propertyChanged: OnFontAttributesChanged); + static readonly BindableProperty CancelEventsProperty = + BindableProperty.Create("CancelEvents", typeof(bool), typeof(FontElement), false); + + static bool GetCancelEvents(BindableObject bindable) => (bool)bindable.GetValue(CancelEventsProperty); + static void SetCancelEvents(BindableObject bindable, bool value) + { + bindable.SetValue(CancelEventsProperty, value); + } + + static void OnFontPropertyChanged(BindableObject bindable, object oldValue, object newValue) + { + if (GetCancelEvents(bindable)) + return; + + SetCancelEvents(bindable, true); + + var font = (Font)newValue; + if (font == Font.Default) { + bindable.ClearValue(FontFamilyProperty); + bindable.ClearValue(FontSizeProperty); + bindable.ClearValue(FontAttributesProperty); + } else { + bindable.SetValue(FontFamilyProperty, font.FontFamily); + if (font.UseNamedSize) + bindable.SetValue(FontSizeProperty, Device.GetNamedSize(font.NamedSize, bindable.GetType(), true)); + else + bindable.SetValue(FontSizeProperty, font.FontSize); + bindable.SetValue(FontAttributesProperty, font.FontAttributes); + } + SetCancelEvents(bindable, false); + } + static void OnFontFamilyChanged(BindableObject bindable, object oldValue, object newValue) { + if (GetCancelEvents(bindable)) + return; + + SetCancelEvents(bindable, true); + + var values = bindable.GetValues(FontSizeProperty, FontAttributesProperty); + var fontSize = (double)values[0]; + var fontAttributes = (FontAttributes)values[1]; + var fontFamily = (string)newValue; + + if (fontFamily != null) + bindable.SetValue(FontProperty, Font.OfSize(fontFamily, fontSize).WithAttributes(fontAttributes)); + else + bindable.SetValue(FontProperty, Font.SystemFontOfSize(fontSize, fontAttributes)); + + SetCancelEvents(bindable, false); ((IFontElement)bindable).OnFontFamilyChanged((string)oldValue, (string)newValue); } static void OnFontSizeChanged(BindableObject bindable, object oldValue, object newValue) { + if (GetCancelEvents(bindable)) + return; + + SetCancelEvents(bindable, true); + + var values = bindable.GetValues(FontFamilyProperty, FontAttributesProperty); + var fontSize = (double)newValue; + var fontAttributes = (FontAttributes)values[1]; + var fontFamily = (string)values[0]; + + if (fontFamily != null) + bindable.SetValue(FontProperty, Font.OfSize(fontFamily, fontSize).WithAttributes(fontAttributes)); + else + bindable.SetValue(FontProperty, Font.SystemFontOfSize(fontSize, fontAttributes)); + + SetCancelEvents(bindable, false); ((IFontElement)bindable).OnFontSizeChanged((double)oldValue, (double)newValue); } @@ -32,6 +100,22 @@ namespace Xamarin.Forms static void OnFontAttributesChanged(BindableObject bindable, object oldValue, object newValue) { + if (GetCancelEvents(bindable)) + return; + + SetCancelEvents(bindable, true); + + var values = bindable.GetValues(FontFamilyProperty, FontSizeProperty); + var fontSize = (double)values[1]; + var fontAttributes = (FontAttributes)newValue; + var fontFamily = (string)values[0]; + + if (fontFamily != null) + bindable.SetValue(FontProperty, Font.OfSize(fontFamily, fontSize).WithAttributes(fontAttributes)); + else + bindable.SetValue(FontProperty, Font.SystemFontOfSize(fontSize, fontAttributes)); + + SetCancelEvents(bindable, false); ((IFontElement)bindable).OnFontAttributesChanged((FontAttributes)oldValue, (FontAttributes)newValue); } } diff --git a/Xamarin.Forms.Core/IFontElement.cs b/Xamarin.Forms.Core/IFontElement.cs index 469b09fa1..044497455 100644 --- a/Xamarin.Forms.Core/IFontElement.cs +++ b/Xamarin.Forms.Core/IFontElement.cs @@ -14,5 +14,6 @@ namespace Xamarin.Forms void OnFontSizeChanged(double oldValue, double newValue); double FontSizeDefaultValueCreator(); void OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue); + void OnFontChanged(Font oldValue, Font newValue); } } \ No newline at end of file diff --git a/Xamarin.Forms.Core/Label.cs b/Xamarin.Forms.Core/Label.cs index e6249323c..052289b67 100644 --- a/Xamarin.Forms.Core/Label.cs +++ b/Xamarin.Forms.Core/Label.cs @@ -23,7 +23,7 @@ namespace Xamarin.Forms public static readonly BindableProperty TextColorProperty = BindableProperty.Create("TextColor", typeof(Color), typeof(Label), Color.Default); - public static readonly BindableProperty FontProperty = BindableProperty.Create("Font", typeof(Font), typeof(Label), default(Font), propertyChanged: FontStructPropertyChanged); + public static readonly BindableProperty FontProperty = FontElement.FontProperty; public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(Label), default(string), propertyChanged: OnTextPropertyChanged); @@ -57,8 +57,6 @@ namespace Xamarin.Forms _platformConfigurationRegistry = new Lazy>(() => new PlatformConfigurationRegistry