-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
src/AP.MobileToolkit.Fonts/Markup/CsharpMarkupExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |