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

Adds FontImage Markup Extension for FontImageSource #6398

Merged
merged 12 commits into from Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions Xamarin.Forms.Xaml.UnitTests/FontImageExtension.xaml
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<TabBar
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="using:Xamarin.Forms.Xaml.UnitTests"
x:Class="Xamarin.Forms.Xaml.UnitTests.FontImageExtension">

<Tab Title="Hello" Icon="{FontImage Size={x:Static local:FontImageExtension.Size}, Color={x:Static local:FontImageExtension.Color}, FontFamily={x:Static local:FontImageExtension.FontFamily}, Glyph={x:Static local:FontImageExtension.Glyph}}" />
<Tab Title="World" Icon="{FontImage Size={x:Static local:FontImageExtension.Size}, Color={x:Static local:FontImageExtension.Color}, FontFamily={x:Static local:FontImageExtension.FontFamily}, Glyph={x:Static local:FontImageExtension.Glyph}}" />
<Tab Title="ContentProperty" Icon="{FontImage MyGlyph, Size={x:Static local:FontImageExtension.Size}, Color={x:Static local:FontImageExtension.Color}, FontFamily={x:Static local:FontImageExtension.FontFamily}}" />

</TabBar>
65 changes: 65 additions & 0 deletions Xamarin.Forms.Xaml.UnitTests/FontImageExtension.xaml.cs
@@ -0,0 +1,65 @@
using NUnit.Framework;

using Xamarin.Forms.Core.UnitTests;

namespace Xamarin.Forms.Xaml.UnitTests
{
public partial class FontImageExtension : TabBar
{
public FontImageExtension() => InitializeComponent();
public FontImageExtension(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}

public static string FontFamily => "MyFontFamily";
public static string Glyph => "MyGlyph";
public static Color Color => Color.Black;
public static double Size = 50d;

[TestFixture]
class Tests
{
[SetUp] public void Setup() => Device.PlatformServices = new MockPlatformServices();
[TearDown] public void TearDown() => Device.PlatformServices = null;

[TestCase(true), TestCase(false)]
public void FontImageExtension_Positive(bool useCompiledXaml)
{
var layout = new FontImageExtension(useCompiledXaml);
var tabs = layout.AllChildren;

foreach (var tab in tabs)
{
Tab myTab = (Tab)tab;
if (myTab == null)
continue;

Assert.That(myTab.Icon, Is.TypeOf<FontImageSource>());

var fontImage = (FontImageSource)myTab.Icon;
Assert.AreEqual(FontFamily, fontImage.FontFamily);
Assert.AreEqual(Glyph, fontImage.Glyph);
Assert.AreEqual(Size, fontImage.Size);
Assert.AreEqual(Color, fontImage.Color);
}
}

[TestCase(true), TestCase(false)]
public void FontImageExtension_Negative(bool useCompiledXaml)
{
var layout = new FontImageExtension(useCompiledXaml);
var tabs = layout.AllChildren;

foreach (var tab in tabs)
{
Tab myTab = (Tab)tab;
if (myTab == null)
continue;

Assert.That(myTab.Icon, Is.Not.TypeOf<ImageSource>());
}
}
}
}
}
30 changes: 30 additions & 0 deletions Xamarin.Forms.Xaml/MarkupExtensions/FontImageExtension.cs
@@ -0,0 +1,30 @@
using System;

namespace Xamarin.Forms.Xaml
{
[AcceptEmptyServiceProvider]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add the Glyph as ContentProperty attribute

Suggested change
[AcceptEmptyServiceProvider]
[AcceptEmptyServiceProvider]
[ContentProperty("Glyph")]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not my first MarkupExtension and I noticed the other platform ones have [ContentProperty] but I am not sure exactly what it is for. How is it used?

Adding this change is not a problem at all

[ContentProperty(nameof(Glyph))]
public class FontImageExtension : IMarkupExtension<ImageSource>
{
public string FontFamily { get; set; }
public string Glyph { get; set; }
public Color Color { get; set; }
public double Size { get; set; } = (double)FontImageSource.SizeProperty.DefaultValue;

public ImageSource ProvideValue(IServiceProvider serviceProvider)
{
return new FontImageSource
{
FontFamily = FontFamily,
Glyph = Glyph,
Color = Color,
Size = Size
};
}

object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
{
return (this as IMarkupExtension<ImageSource>).ProvideValue(serviceProvider);
}
}
}