Skip to content

Commit

Permalink
adding markup extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed May 1, 2020
1 parent cb528b5 commit 3295c36
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 0 deletions.
117 changes: 117 additions & 0 deletions src/AP.MobileToolkit.Fonts/Markup/CsharpMarkupExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using System;
using AP.MobileToolkit.Fonts;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Xaml;

namespace AP.MobileToolkit.Markup
{
public static class CSharpMarkupExtensions
{
public static TElement SetIcon<TElement>(this TElement element, string icon, Color? color = null)
where TElement : Element, IFontElement
{
if (!FontRegistry.HasFont(icon, out var font))
{
return element;
}

var glyphColor = color ?? Color.Default;
var glyph = font.GetGlyph(icon);
switch (element)
{
case Label label:
SetIcon(label, font.FontFileName, glyph, glyphColor);
break;
case Span span:
SetIcon(span, font.FontFileName, glyph, glyphColor);
break;
case Editor editor:
SetIcon(editor, font.FontFileName, glyph, glyphColor);
break;
case Button button:
SetIcon(button, font.FontFileName, glyph, glyphColor);
break;
case SearchBar searchBar:
SetIcon(searchBar, font.FontFileName, glyph, glyphColor);
break;
}
return element;
}

public static FontImageSource SetIcon(this FontImageSource imageSource, string icon, Color? color = null)
{
if (FontRegistry.HasFont(icon, out var font))
{
imageSource.Glyph = font.GetGlyph(icon);
imageSource.FontFamily = font.FontFileName;
imageSource.Color = color ?? Color.Default;
}

return imageSource;
}

private static void SetIcon(Label element, string fontFamily, string glyph, Color color)
{
element.Text = glyph;
element.FontFamily = fontFamily;
element.TextColor = color;
}

private static void SetIcon(Span element, string fontFamily, string glyph, Color color)
{
element.Text = glyph;
element.FontFamily = fontFamily;
element.TextColor = color;
}

private static void SetIcon(Editor element, string fontFamily, string glyph, Color color)
{
element.Text = glyph;
element.FontFamily = fontFamily;
element.TextColor = color;
}

private static void SetIcon(Button element, string fontFamily, string glyph, Color color)
{
element.Text = glyph;
element.FontFamily = fontFamily;
element.TextColor = color;
}

private static void SetIcon(SearchBar element, string fontFamily, string glyph, Color color)
{
// TODO: We'll want to introduce an effect that adds the icon to the Search Bar rather than setting the text.
element.Text = glyph;
element.FontFamily = fontFamily;
element.TextColor = color;
}

private static void SetIcon(SearchHandler element, string fontFamily, string glyph, Color color)
{
element.QueryIcon = new FontImageSource
{
Glyph = glyph,
FontFamily = fontFamily,
Color = color
};
}

private class CsharpCodeHelper : IProvideValueTarget, IServiceProvider
{
public CsharpCodeHelper(object targetObject)
{
TargetObject = targetObject;
}

public object TargetObject { get; }

public object TargetProperty => null;

public object GetService(Type serviceType)
{
return this;
}
}
}
}
68 changes: 68 additions & 0 deletions tests/AP.MobileToolkit.Fonts.Tests/CsharpMarkupTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using AP.MobileToolkit.Fonts.Tests.Mocks;
using AP.MobileToolkit.Markup;
using Xamarin.Forms;
using Xunit;

namespace AP.MobileToolkit.Fonts.Tests
{
public class CsharpMarkupTests
{
public CsharpMarkupTests()
{
Xamarin.Forms.Mocks.MockForms.Init();
FontRegistry.Clear();
FontRegistry.RegisterFonts(SampleFontAwesomeRegular.Font, SampleFontAwesomeSolid.Font);
}

[Fact]
public void SetIconSetsTextAndFontFamilyOnLabel()
{
var label = new Label().SetIcon("far fa-calendar");

// "far fa-calendar", "\uf133"
Assert.Equal(SampleFontAwesomeRegular.FontFile, label.FontFamily);
Assert.Equal("\uf133", label.Text);
}

[Fact]
public void SetIconSetsTextAndFontFamilyOnSpan()
{
var label = new Span().SetIcon("far fa-calendar");

// "far fa-calendar", "\uf133"
Assert.Equal(SampleFontAwesomeRegular.FontFile, label.FontFamily);
Assert.Equal("\uf133", label.Text);
}

[Fact]
public void SetIconSetsTextAndFontFamilyOnEditor()
{
var label = new Editor().SetIcon("far fa-calendar");

// "far fa-calendar", "\uf133"
Assert.Equal(SampleFontAwesomeRegular.FontFile, label.FontFamily);
Assert.Equal("\uf133", label.Text);
}

[Fact]
public void SetIconSetsTextAndFontFamilyOnButton()
{
var label = new Button().SetIcon("far fa-calendar");

// "far fa-calendar", "\uf133"
Assert.Equal(SampleFontAwesomeRegular.FontFile, label.FontFamily);
Assert.Equal("\uf133", label.Text);
}

[Fact]
public void SetIconSetsTextAndFontFamilyOnSearchBar()
{
var label = new SearchBar().SetIcon("far fa-calendar");

// "far fa-calendar", "\uf133"
Assert.Equal(SampleFontAwesomeRegular.FontFile, label.FontFamily);
Assert.Equal("\uf133", label.Text);
}
}
}

0 comments on commit 3295c36

Please sign in to comment.