Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
[All] Implement IFontElement on Picker, DatePicker, and TimePicker (#662
Browse files Browse the repository at this point in the history
)

* [C] Implement IFontElement on Picker/TimePicker/DatePicker

* Add InvalidateMeasure calls

* Add behavior to macOS renderers
  • Loading branch information
pauldipietro authored and rmarinho committed Dec 12, 2017
1 parent 8e3fbd6 commit abe2f3d
Show file tree
Hide file tree
Showing 23 changed files with 1,078 additions and 35 deletions.
Expand Up @@ -24,13 +24,37 @@ protected override void Build(StackLayout stackLayout)
new DatePicker { MaximumDate = new DateTime(2087, 9, 13) });
var textColorContainer = new ViewContainer<DatePicker>(Test.DatePicker.TextColor,
new DatePicker { Date = new DateTime(1978, 12, 24), TextColor = Color.Lime });
var fontAttributesContainer = new ViewContainer<DatePicker>(Test.DatePicker.FontAttributes,
new DatePicker { FontAttributes = FontAttributes.Bold });

var fontFamilyContainer = new ViewContainer<DatePicker>(Test.DatePicker.FontFamily,
new DatePicker());
// Set font family based on available fonts per platform
switch(Device.RuntimePlatform)
{
case Device.Android:
fontFamilyContainer.View.FontFamily = "sans-serif-thin";
break;
case Device.iOS:
fontFamilyContainer.View.FontFamily = "Courier";
break;
default:
fontFamilyContainer.View.FontFamily = "Garamond";
break;
}

var fontSizeContainer = new ViewContainer<DatePicker>(Test.DatePicker.FontSize,
new DatePicker { FontSize = 24 });

Add(dateContainer);
Add(dateSelectedContainer);
Add(formatDateContainer);
Add(minimumDateContainer);
Add(maximumDateContainer);
Add(textColorContainer);
Add(fontAttributesContainer);
Add(fontFamilyContainer);
Add(fontSizeContainer);
}
}
}
34 changes: 34 additions & 0 deletions Xamarin.Forms.Controls/CoreGalleryPages/PickerCoreGalleryPage.cs
Expand Up @@ -28,10 +28,44 @@ protected override void Build(StackLayout stackLayout)
textColorContainer.View.Items.Add("Item 2");
textColorContainer.View.Items.Add("Item 3");

var fontAttributesContainer = new ViewContainer<Picker>(Test.Picker.FontAttributes,
new Picker { FontAttributes = FontAttributes.Bold });
fontAttributesContainer.View.Items.Add("Item 1");
fontAttributesContainer.View.Items.Add("Item 2");
fontAttributesContainer.View.Items.Add("Item 3");

var fontFamilyContainer = new ViewContainer<Picker>(Test.Picker.FontFamily,
new Picker());
// Set font family based on available fonts per platform
switch(Device.RuntimePlatform)
{
case Device.Android:
fontFamilyContainer.View.FontFamily = "sans-serif-thin";
break;
case Device.iOS:
fontFamilyContainer.View.FontFamily = "Courier";
break;
default:
fontFamilyContainer.View.FontFamily = "Garamond";
break;
}
fontFamilyContainer.View.Items.Add("Item 1");
fontFamilyContainer.View.Items.Add("Item 2");
fontFamilyContainer.View.Items.Add("Item 3");

var fontSizeContainer = new ViewContainer<Picker>(Test.Picker.FontSize,
new Picker { FontSize = 24 });
fontSizeContainer.View.Items.Add("Item 1");
fontSizeContainer.View.Items.Add("Item 2");
fontSizeContainer.View.Items.Add("Item 3");

Add(itemsContainer);
Add(selectedIndexContainer);
Add(titleContainer);
Add(textColorContainer);
Add(fontAttributesContainer);
Add(fontFamilyContainer);
Add(fontSizeContainer);
}
}
}
Expand Up @@ -15,10 +15,34 @@ protected override void Build(StackLayout stackLayout)
new TimePicker { Time = new TimeSpan(14, 45, 50) });
var textColorContainer = new ViewContainer<TimePicker>(Test.TimePicker.TextColor,
new TimePicker { Time = new TimeSpan(14, 45, 50), TextColor = Color.Lime });
var fontAttributesContainer = new ViewContainer<TimePicker>(Test.TimePicker.FontAttributes,
new TimePicker { FontAttributes = FontAttributes.Bold });

var fontFamilyContainer = new ViewContainer<TimePicker>(Test.TimePicker.FontFamily,
new TimePicker());
// Set font family based on available fonts per platform
switch(Device.RuntimePlatform)
{
case Device.Android:
fontFamilyContainer.View.FontFamily = "sans-serif-thin";
break;
case Device.iOS:
fontFamilyContainer.View.FontFamily = "Courier";
break;
default:
fontFamilyContainer.View.FontFamily = "Garamond";
break;
}

var fontSizeContainer = new ViewContainer<TimePicker>(Test.TimePicker.FontSize,
new TimePicker { FontSize = 24 });

Add(formatContainer);
Add(timeContainer);
Add(textColorContainer);
Add(fontAttributesContainer);
Add(fontFamilyContainer);
Add(fontSizeContainer);
}
}
}
43 changes: 42 additions & 1 deletion Xamarin.Forms.Core/DatePicker.cs
@@ -1,10 +1,11 @@
using System;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform;

namespace Xamarin.Forms
{
[RenderWith(typeof(_DatePickerRenderer))]
public class DatePicker : View, ITextElement,IElementConfiguration<DatePicker>
public class DatePicker : View, IFontElement, ITextElement,IElementConfiguration<DatePicker>
{
public static readonly BindableProperty FormatProperty = BindableProperty.Create(nameof(Format), typeof(string), typeof(DatePicker), "d");

Expand All @@ -20,6 +21,12 @@ public class DatePicker : View, ITextElement,IElementConfiguration<DatePicker>
validateValue: ValidateMaximumDate, coerceValue: CoerceMaximumDate);

public static readonly BindableProperty TextColorProperty = TextElement.TextColorProperty;

public static readonly BindableProperty FontFamilyProperty = FontElement.FontFamilyProperty;

public static readonly BindableProperty FontSizeProperty = FontElement.FontSizeProperty;

public static readonly BindableProperty FontAttributesProperty = FontElement.FontAttributesProperty;

readonly Lazy<PlatformConfigurationRegistry<DatePicker>> _platformConfigurationRegistry;

Expand Down Expand Up @@ -58,6 +65,40 @@ public Color TextColor
set { SetValue(TextElement.TextColorProperty, value); }
}

public FontAttributes FontAttributes
{
get { return (FontAttributes)GetValue(FontAttributesProperty); }
set { SetValue(FontAttributesProperty, value); }
}

public string FontFamily
{
get { return (string)GetValue(FontFamilyProperty); }
set { SetValue(FontFamilyProperty, value); }
}

[TypeConverter(typeof(FontSizeConverter))]
public double FontSize
{
get { return (double)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}

void IFontElement.OnFontFamilyChanged(string oldValue, string newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);

void IFontElement.OnFontSizeChanged(double oldValue, double newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);

void IFontElement.OnFontChanged(Font oldValue, Font newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);

double IFontElement.FontSizeDefaultValueCreator() =>
Device.GetNamedSize(NamedSize.Default, (DatePicker)this);

void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);

public event EventHandler<DateChangedEventArgs> DateSelected;

static object CoerceDate(BindableObject bindable, object value)
Expand Down
41 changes: 40 additions & 1 deletion Xamarin.Forms.Core/Picker.cs
Expand Up @@ -11,7 +11,7 @@
namespace Xamarin.Forms
{
[RenderWith(typeof(_PickerRenderer))]
public class Picker : View, ITextElement, IElementConfiguration<Picker>
public class Picker : View, IFontElement, ITextElement, IElementConfiguration<Picker>
{
public static readonly BindableProperty TextColorProperty = TextElement.TextColorProperty;

Expand All @@ -30,13 +30,52 @@ public class Picker : View, ITextElement, IElementConfiguration<Picker>
BindableProperty.Create(nameof(SelectedItem), typeof(object), typeof(Picker), null, BindingMode.TwoWay,
propertyChanged: OnSelectedItemChanged);

public static readonly BindableProperty FontFamilyProperty = FontElement.FontFamilyProperty;

public static readonly BindableProperty FontSizeProperty = FontElement.FontSizeProperty;

public static readonly BindableProperty FontAttributesProperty = FontElement.FontAttributesProperty;

readonly Lazy<PlatformConfigurationRegistry<Picker>> _platformConfigurationRegistry;

public Picker()
{
((INotifyCollectionChanged)Items).CollectionChanged += OnItemsCollectionChanged;
_platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<Picker>>(() => new PlatformConfigurationRegistry<Picker>(this));
}
public FontAttributes FontAttributes
{
get { return (FontAttributes)GetValue(FontAttributesProperty); }
set { SetValue(FontAttributesProperty, value); }
}

public string FontFamily
{
get { return (string)GetValue(FontFamilyProperty); }
set { SetValue(FontFamilyProperty, value); }
}

[TypeConverter(typeof(FontSizeConverter))]
public double FontSize
{
get { return (double)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}

void IFontElement.OnFontFamilyChanged(string oldValue, string newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);

void IFontElement.OnFontSizeChanged(double oldValue, double newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);

void IFontElement.OnFontChanged(Font oldValue, Font newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);

double IFontElement.FontSizeDefaultValueCreator() =>
Device.GetNamedSize(NamedSize.Default, (Picker)this);

void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);

public IList<string> Items { get; } = new LockableObservableListWrapper();

Expand Down
43 changes: 42 additions & 1 deletion Xamarin.Forms.Core/TimePicker.cs
@@ -1,10 +1,11 @@
using System;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform;

namespace Xamarin.Forms
{
[RenderWith(typeof(_TimePickerRenderer))]
public class TimePicker : View, ITextElement, IElementConfiguration<TimePicker>
public class TimePicker : View, IFontElement, ITextElement, IElementConfiguration<TimePicker>
{
public static readonly BindableProperty FormatProperty = BindableProperty.Create(nameof(Format), typeof(string), typeof(TimePicker), "t");

Expand All @@ -16,6 +17,12 @@ public class TimePicker : View, ITextElement, IElementConfiguration<TimePicker>
return time.TotalHours < 24 && time.TotalMilliseconds >= 0;
});

public static readonly BindableProperty FontFamilyProperty = FontElement.FontFamilyProperty;

public static readonly BindableProperty FontSizeProperty = FontElement.FontSizeProperty;

public static readonly BindableProperty FontAttributesProperty = FontElement.FontAttributesProperty;

readonly Lazy<PlatformConfigurationRegistry<TimePicker>> _platformConfigurationRegistry;

public TimePicker()
Expand All @@ -41,6 +48,40 @@ public TimeSpan Time
set { SetValue(TimeProperty, value); }
}

public FontAttributes FontAttributes
{
get { return (FontAttributes)GetValue(FontAttributesProperty); }
set { SetValue(FontAttributesProperty, value); }
}

public string FontFamily
{
get { return (string)GetValue(FontFamilyProperty); }
set { SetValue(FontFamilyProperty, value); }
}

[TypeConverter(typeof(FontSizeConverter))]
public double FontSize
{
get { return (double)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}

void IFontElement.OnFontFamilyChanged(string oldValue, string newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);

void IFontElement.OnFontSizeChanged(double oldValue, double newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);

void IFontElement.OnFontChanged(Font oldValue, Font newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);

double IFontElement.FontSizeDefaultValueCreator() =>
Device.GetNamedSize(NamedSize.Default, (TimePicker)this);

void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);

public IPlatformElementConfiguration<T, TimePicker> On<T>() where T : IConfigPlatform
{
return _platformConfigurationRegistry.Value.On<T>();
Expand Down
15 changes: 12 additions & 3 deletions Xamarin.Forms.CustomAttributes/TestAttributes.cs
Expand Up @@ -478,7 +478,10 @@ public enum DatePicker
MaximumDate,
Focus,
IsVisible,
TextColor
TextColor,
FontAttributes,
FontFamily,
FontSize
}

public enum InputView
Expand Down Expand Up @@ -667,7 +670,10 @@ public enum TimePicker {
Format,
Time,
Focus,
TextColor
TextColor,
FontAttributes,
FontFamily,
FontSize
}

public enum WebView {
Expand Down Expand Up @@ -706,7 +712,10 @@ public enum Picker {
Items,
SelectedIndex,
Focus,
TextColor
TextColor,
FontAttributes,
FontFamily,
FontSize
}

public enum FileImageSource {
Expand Down

0 comments on commit abe2f3d

Please sign in to comment.