diff options
author | Stephane Delcroix <stephane@delcroix.org> | 2017-03-03 12:39:30 +0100 |
---|---|---|
committer | Stephane Delcroix <stephane@delcroix.org> | 2017-03-03 12:40:38 +0100 |
commit | 81058db4f57982891904e7d8da68971ab4d91379 (patch) | |
tree | d4b78a3a08f1fabb8c2ed9ddc1b46f9426b14757 /Xamarin.Forms.Core | |
parent | caf2e814120b3abce2050b588c66cfd3589035d3 (diff) | |
download | xamarin-forms-81058db4f57982891904e7d8da68971ab4d91379.tar.gz xamarin-forms-81058db4f57982891904e7d8da68971ab4d91379.tar.bz2 xamarin-forms-81058db4f57982891904e7d8da68971ab4d91379.zip |
[C] move the Font proxying into FontElement
Diffstat (limited to 'Xamarin.Forms.Core')
-rw-r--r-- | Xamarin.Forms.Core/Button.cs | 60 | ||||
-rw-r--r-- | Xamarin.Forms.Core/Editor.cs | 4 | ||||
-rw-r--r-- | Xamarin.Forms.Core/Entry.cs | 4 | ||||
-rw-r--r-- | Xamarin.Forms.Core/FontElement.cs | 84 | ||||
-rw-r--r-- | Xamarin.Forms.Core/IFontElement.cs | 1 | ||||
-rw-r--r-- | Xamarin.Forms.Core/Label.cs | 103 | ||||
-rw-r--r-- | Xamarin.Forms.Core/SearchBar.cs | 4 | ||||
-rw-r--r-- | 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 7d8d659b..9db917bc 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<Button>> _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 66c5caa7..ee9d4422 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 d499d34e..610a30d8 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<TextChangedEventArgs> TextChanged; diff --git a/Xamarin.Forms.Core/FontElement.cs b/Xamarin.Forms.Core/FontElement.cs index 66545e07..7c15a48b 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 469b09fa..04449745 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 e6249323..052289b6 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<PlatformConfigurationRegistry<Label>>(() => new PlatformConfigurationRegistry<Label>(this)); } - bool _cancelEvents; - [Obsolete("Please use the Font attributes which are on the class itself. Obsoleted in v1.3.0")] public Font Font { @@ -135,107 +133,20 @@ namespace Xamarin.Forms set { SetValue(FontSizeProperty, value); } } - static void FontStructPropertyChanged(BindableObject bindable, object oldValue, object newValue) - { - var label = (Label)bindable; - if (label._cancelEvents) - return; - - label._cancelEvents = true; - - var font = (Font)newValue; - if (font == Font.Default) - { - label.FontFamily = null; - label.FontSize = Device.GetNamedSize(NamedSize.Default, label); - label.FontAttributes = FontAttributes.None; - } - else - { - label.FontFamily = font.FontFamily; - if (font.UseNamedSize) - { - label.FontSize = Device.GetNamedSize(font.NamedSize, label.GetType(), true); - } - else - { - label.FontSize = font.FontSize; - } - label.FontAttributes = font.FontAttributes; - } - - label._cancelEvents = false; - - label.InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); - } - double IFontElement.FontSizeDefaultValueCreator() => Device.GetNamedSize(NamedSize.Default, (Label)this); - void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) - { - if (_cancelEvents) - return; - - _cancelEvents = true; - - var attributes = newValue; - -#pragma warning disable 0618 // retain until Font removed - object[] values = GetValues(FontFamilyProperty, FontSizeProperty); - var family = (string)values[0]; - if (family != null) - Font = Font.OfSize(family, (double)values[1]).WithAttributes(attributes); - else - Font = Font.SystemFontOfSize((double)values[1], attributes); -#pragma warning restore 0618 - - _cancelEvents = false; - + void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) => InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); - } - void IFontElement.OnFontFamilyChanged(string oldValue, string newValue) - { - if (_cancelEvents) - return; - - _cancelEvents = true; - -#pragma warning disable 0618 // retain until Font removed - object[] values = GetValues(FontSizeProperty, FontAttributesProperty); - var family = newValue; - if (family != null) - Font = Font.OfSize(family, (double)values[0]).WithAttributes((FontAttributes)values[1]); - else - Font = Font.SystemFontOfSize((double)values[0], (FontAttributes)values[1]); -#pragma warning restore 0618 - - _cancelEvents = false; + void IFontElement.OnFontFamilyChanged(string oldValue, string newValue) => InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); - } - - void IFontElement.OnFontSizeChanged(double oldValue, double newValue) - { - if (_cancelEvents) - return; - - _cancelEvents = true; - -#pragma warning disable 0618 // retain until Font removed - object[] values = GetValues(FontFamilyProperty, FontAttributesProperty); - var size = newValue; - var family = (string)values[0]; - if (family != null) - Font = Font.OfSize(family, size).WithAttributes((FontAttributes)values[1]); - else - Font = Font.SystemFontOfSize(size, (FontAttributes)values[1]); -#pragma warning restore 0618 - - _cancelEvents = false; + void IFontElement.OnFontSizeChanged(double oldValue, double newValue) => InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); - } + + void IFontElement.OnFontChanged(Font oldValue, Font newValue) => + InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged); void OnFormattedTextChanged(object sender, PropertyChangedEventArgs e) { diff --git a/Xamarin.Forms.Core/SearchBar.cs b/Xamarin.Forms.Core/SearchBar.cs index 5e82f84e..a7d01b67 100644 --- a/Xamarin.Forms.Core/SearchBar.cs +++ b/Xamarin.Forms.Core/SearchBar.cs @@ -125,6 +125,10 @@ namespace Xamarin.Forms { } + void IFontElement.OnFontChanged(Font oldValue, Font newValue) + { + } + public event EventHandler SearchButtonPressed; public event EventHandler<TextChangedEventArgs> TextChanged; diff --git a/Xamarin.Forms.Core/Span.cs b/Xamarin.Forms.Core/Span.cs index a410c100..06c42128 100644 --- a/Xamarin.Forms.Core/Span.cs +++ b/Xamarin.Forms.Core/Span.cs @@ -171,16 +171,30 @@ namespace Xamarin.Forms #pragma warning restore - void IFontElement.OnFontFamilyChanged(string oldValue, string newValue) => - OnSomeFontPropertyChanged(); + //Those 4 methods are never used as Span isn't a BO, and doesn't compose with FontElement + void IFontElement.OnFontFamilyChanged(string oldValue, string newValue) + { + throw new NotImplementedException(); + } - void IFontElement.OnFontSizeChanged(double oldValue, double newValue) => - OnSomeFontPropertyChanged(); + void IFontElement.OnFontSizeChanged(double oldValue, double newValue) + { + throw new NotImplementedException(); + } - double IFontElement.FontSizeDefaultValueCreator() => - Device.GetNamedSize(NamedSize.Default, typeof(Label)); + double IFontElement.FontSizeDefaultValueCreator() + { + throw new NotImplementedException(); + } - void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) => - OnSomeFontPropertyChanged(); + void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) + { + throw new NotImplementedException(); + } + + void IFontElement.OnFontChanged(Font oldValue, Font newValue) + { + throw new NotImplementedException(); + } } }
\ No newline at end of file |