Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3057376
Added FontIcon markup extension
Sergio0694 Jan 25, 2020
728f1ce
Removed unnecessary using directives
Sergio0694 Jan 25, 2020
800efba
Renamed the FontIcon markup extension
Sergio0694 Jan 25, 2020
c2f00be
Added FontIcon tests
Sergio0694 Jan 25, 2020
4493ffc
Fixed a typo in the test XAML
Sergio0694 Jan 26, 2020
6418ba5
Merge branch 'master' into feature/font-icon-extension
michael-hawker Feb 11, 2020
e8d4309
Update UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs
Sergio0694 Feb 12, 2020
6638416
Update UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs
Sergio0694 Feb 12, 2020
452a276
Update UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs
Sergio0694 Feb 12, 2020
5577118
Added new FontIcon properties
Sergio0694 Feb 12, 2020
9018d88
Merge branch 'feature/font-icon-extension' of https://github.com/Serg…
Sergio0694 Feb 12, 2020
870306c
Added more tests for the new properties
Sergio0694 Feb 12, 2020
c6d6023
Added FontIconSourceExtension
Sergio0694 Feb 12, 2020
e90b0eb
Added BitmapIconExtension
Sergio0694 Feb 12, 2020
8dda65e
Merge branch 'master' into feature/bitmap-icon-extension
Sergio0694 Feb 12, 2020
cea30df
Merge branch 'master' into feature/font-icon-source-extension
Sergio0694 Feb 12, 2020
1b34996
Merge branch 'master' into feature/font-icon-extension
Sergio0694 Mar 15, 2020
23dcec0
Merge branch 'master' into feature/bitmap-icon-extension
Sergio0694 Mar 15, 2020
63c2b6e
Merge branch 'master' into feature/font-icon-source-extension
Sergio0694 Mar 15, 2020
96b22d4
Merge branch 'master' into feature/font-icon-extension
Sergio0694 Mar 24, 2020
7d6f9ec
Merge branch 'master' into feature/bitmap-icon-extension
Sergio0694 Mar 24, 2020
91947c1
Merge branch 'master' into feature/font-icon-source-extension
Sergio0694 Mar 24, 2020
0d8f538
Added tests for the FontIconSource extension
Sergio0694 Mar 24, 2020
1af9349
Fixed namespace for BitmapIconExtension type
Sergio0694 Mar 24, 2020
bdcfd9a
Added tests for BitmapIconExtension type
Sergio0694 Mar 24, 2020
88844c1
Merge branch 'master' into feature/bitmap-icon-extension
Sergio0694 Mar 24, 2020
d0421d8
Merge branch 'master' into feature/font-icon-source-extension
Sergio0694 Mar 24, 2020
1d9ad41
Merge branch 'master' into feature/font-icon-extension
Sergio0694 Mar 24, 2020
ae5f94b
Merge pull request #14 from Sergio0694/feature/font-icon-source-exten…
Sergio0694 Mar 24, 2020
76cfed2
Merge branch 'feature/font-icon-extension' into feature/bitmap-icon-e…
Sergio0694 Mar 24, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Microsoft.Toolkit.Uwp.UI/Extensions/Markup/BitmapIconExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Markup;

namespace Microsoft.Toolkit.Uwp.UI.Extensions
{
/// <summary>
/// Custom <see cref="MarkupExtension"/> which can provide <see cref="BitmapIcon"/> values.
/// </summary>
[Bindable]
[MarkupExtensionReturnType(ReturnType = typeof(BitmapIcon))]
public sealed class BitmapIconExtension : MarkupExtension
{
/// <summary>
/// Gets or sets the <see cref="Uri"/> representing the image to display.
/// </summary>
public Uri Source { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to display the icon as monochrome.
/// </summary>
public bool ShowAsMonochrome { get; set; }

/// <inheritdoc/>
protected override object ProvideValue()
Copy link
Contributor

Choose a reason for hiding this comment

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

protected override object ProvideValue() [](start = 8, length = 40)

It can be a body-less method:

protected override object ProvideValue() => new BitmapIcon
{
    ShowAsMonochrome = ShowAsMonochrome,
    UriSource = Source
};

Copy link
Member Author

Choose a reason for hiding this comment

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

Not really a fan of how the body expression looks in small methods returning an object like this, as at a glance it's easy to confuse the brackets under the method with the actual brackets for the method body, when in fact they belong to that constructor instead.
I'd prefer to leave this as is if that's alright, but let me know what you think! 😊

Copy link
Member

Choose a reason for hiding this comment

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

@vgromfeld do you have any pros/cons around the styling of this one way or the other, I know this is a newer feature of C# compared to when the Toolkit started, so I don't think we have any explicit styling guidelines setup for this scenario. Honestly though, I'm fine with either.

Copy link
Contributor

Choose a reason for hiding this comment

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

The body-less generally allows us to remove all the unneeded characters and write more compact code. It might be less natural first but really helps to read the code faster once you get use to it 😀.
VS2019 code style is recommending this approach. It definitively don't worth a full restyling of all the toolkit code but should be more and more considered when writing new code.

{
return new BitmapIcon
{
ShowAsMonochrome = ShowAsMonochrome,
UriSource = Source
};
}
}
}
76 changes: 76 additions & 0 deletions Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIconExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Windows.UI.Text;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Media;

namespace Microsoft.Toolkit.Uwp.UI.Extensions
{
/// <summary>
/// Custom <see cref="MarkupExtension"/> which can provide <see cref="FontIcon"/> values.
/// </summary>
[Bindable]
[MarkupExtensionReturnType(ReturnType = typeof(FontIcon))]
public class FontIconExtension : MarkupExtension
{
/// <summary>
/// Gets or sets the <see cref="string"/> representing the icon to display.
/// </summary>
public string Glyph { get; set; }

/// <summary>
/// Gets or sets the size of the icon to display.
/// </summary>
public double FontSize { get; set; }

/// <summary>
/// Gets or sets the font family to use to display the icon. If <see langword="null"/>, "Segoe MDL2 Assets" will be used.
/// </summary>
public FontFamily FontFamily { get; set; }

/// <summary>
/// Gets or sets the thickness of the icon glyph.
/// </summary>
public FontWeight FontWeight { get; set; } = FontWeights.Normal;

/// <summary>
/// Gets or sets the font style for the icon glyph.
/// </summary>
public FontStyle FontStyle { get; set; } = FontStyle.Normal;

/// <summary>
/// Gets or sets a value indicating whether automatic text enlargement, to reflect the system text size setting, is enabled.
/// </summary>
public bool IsTextScaleFactorEnabled { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the icon is mirrored when the flow direction is right to left.
/// </summary>
public bool MirroredWhenRightToLeft { get; set; }

/// <inheritdoc/>
protected override object ProvideValue()
{
var fontIcon = new FontIcon
{
Glyph = Glyph,
FontFamily = FontFamily ?? new FontFamily("Segoe MDL2 Assets"),
FontWeight = FontWeight,
FontStyle = FontStyle,
IsTextScaleFactorEnabled = IsTextScaleFactorEnabled,
MirroredWhenRightToLeft = MirroredWhenRightToLeft
};

if (FontSize > 0)
{
fontIcon.FontSize = FontSize;
}

return fontIcon;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Windows.UI.Text;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Media;

namespace Microsoft.Toolkit.Uwp.UI.Extensions
{
/// <summary>
/// Custom <see cref="MarkupExtension"/> which can provide <see cref="FontIconSource"/> values.
/// </summary>
[Bindable]
[MarkupExtensionReturnType(ReturnType = typeof(FontIconSource))]
public class FontIconSourceExtension : MarkupExtension
{
/// <summary>
/// Gets or sets the <see cref="string"/> representing the icon to display.
/// </summary>
public string Glyph { get; set; }

/// <summary>
/// Gets or sets the size of the icon to display.
/// </summary>
public double FontSize { get; set; }

/// <summary>
/// Gets or sets the font family to use to display the icon. If <see langword="null"/>, "Segoe MDL2 Assets" will be used.
/// </summary>
public FontFamily FontFamily { get; set; }

/// <summary>
/// Gets or sets the thickness of the icon glyph.
/// </summary>
public FontWeight FontWeight { get; set; } = FontWeights.Normal;

/// <summary>
/// Gets or sets the font style for the icon glyph.
/// </summary>
public FontStyle FontStyle { get; set; } = FontStyle.Normal;

/// <summary>
/// Gets or sets a value indicating whether automatic text enlargement, to reflect the system text size setting, is enabled.
/// </summary>
public bool IsTextScaleFactorEnabled { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the icon is mirrored when the flow direction is right to left.
/// </summary>
public bool MirroredWhenRightToLeft { get; set; }

/// <inheritdoc/>
protected override object ProvideValue()
{
var fontIcon = new FontIconSource
{
Glyph = Glyph,
FontFamily = FontFamily ?? new FontFamily("Segoe MDL2 Assets"),
FontWeight = FontWeight,
FontStyle = FontStyle,
IsTextScaleFactorEnabled = IsTextScaleFactorEnabled,
MirroredWhenRightToLeft = MirroredWhenRightToLeft
};

if (FontSize > 0)
{
fontIcon.FontSize = FontSize;
}

return fontIcon;
}
}
}
5 changes: 0 additions & 5 deletions Microsoft.Toolkit.Uwp.UI/Extensions/Markup/NullableBool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Markup;

Expand Down
1 change: 0 additions & 1 deletion Microsoft.Toolkit.Uwp.UI/Extensions/Markup/OnDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Windows.ApplicationModel;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Markup;
Expand Down
85 changes: 85 additions & 0 deletions UnitTests/Extensions/Test_BitmapIconExtensionMarkupExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Linq;
using Microsoft.Toolkit.Uwp.UI.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;

namespace UnitTests.Extensions
{
[TestClass]
public class Test_BitmapIconExtensionMarkupExtension
{
[TestCategory("BitmapIconExtensionMarkupExtension")]
[UITestMethod]
public void Test_BitmapIconExtension_MarkupExtension_ProvideImage()
{
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions"">
<Button x:Name=""RootButton"">
<Button.Flyout>
<MenuFlyout>
<MenuFlyoutItem Icon=""{ex:BitmapIcon Source=/Assets/StoreLogo.png}"" />
</MenuFlyout>
</Button.Flyout>
</Button>
</Page>") as FrameworkElement;

var button = treeroot.FindChildByName("RootButton") as Button;

Assert.IsNotNull(button, $"Could not find the {nameof(Button)} control in tree.");

var item = ((MenuFlyout)button.Flyout)?.Items?.FirstOrDefault() as MenuFlyoutItem;

Assert.IsNotNull(button, $"Could not find the target {nameof(MenuFlyoutItem)} control.");

var icon = item.Icon as BitmapIcon;

Assert.IsNotNull(icon, $"Could not find the {nameof(BitmapIcon)} element in button.");

Assert.AreEqual(icon.UriSource, new Uri("ms-resource:///Files/Assets/StoreLogo.png"), "Expected ms-resource:///Files/Assets/StoreLogo.png uri.");
Assert.AreEqual(icon.ShowAsMonochrome, false, "Expected icon not to be monochrome");
}

[TestCategory("BitmapIconExtensionMarkupExtension")]
[UITestMethod]
public void Test_BitmapIconExtension_MarkupExtension_ProvideImageAndMonochrome()
{
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions"">
<Button x:Name=""RootButton"">
<Button.Flyout>
<MenuFlyout>
<MenuFlyoutItem Icon=""{ex:BitmapIcon Source=/Assets/StoreLogo.png, ShowAsMonochrome=True}"" />
</MenuFlyout>
</Button.Flyout>
</Button>
</Page>") as FrameworkElement;

var button = treeroot.FindChildByName("RootButton") as Button;

Assert.IsNotNull(button, $"Could not find the {nameof(Button)} control in tree.");

var item = ((MenuFlyout)button.Flyout)?.Items?.FirstOrDefault() as MenuFlyoutItem;

Assert.IsNotNull(button, $"Could not find the target {nameof(MenuFlyoutItem)} control.");

var icon = item.Icon as BitmapIcon;

Assert.IsNotNull(icon, $"Could not find the {nameof(BitmapIcon)} element in button.");

Assert.AreEqual(icon.UriSource, new Uri("ms-resource:///Files/Assets/StoreLogo.png"), "Expected ms-resource:///Files/Assets/StoreLogo.png uri.");
Assert.AreEqual(icon.ShowAsMonochrome, true, "Expected icon to be monochrome");
}
}
}
92 changes: 92 additions & 0 deletions UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Windows.UI.Text;
using Microsoft.Toolkit.Uwp.UI.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;

namespace UnitTests.Extensions
{
[TestClass]
public class Test_FontIconExtensionMarkupExtension
{
[TestCategory("FontIconExtensionMarkupExtension")]
[UITestMethod]
public void Test_FontIconExtension_MarkupExtension_ProvideSegoeMdl2Asset()
{
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions"">
<AppBarButton x:Name=""Check"" Icon=""{ex:FontIcon Glyph=&#xE105;}""/>
</Page>") as FrameworkElement;

var button = treeroot.FindChildByName("Check") as AppBarButton;

Assert.IsNotNull(button, $"Could not find the {nameof(AppBarButton)} control in tree.");

var icon = button.Icon as FontIcon;

Assert.IsNotNull(icon, $"Could not find the {nameof(FontIcon)} element in button.");

Assert.AreEqual(icon.Glyph, "\uE105", "Expected icon glyph to be E105.");
Assert.AreEqual(icon.FontFamily.Source, "Segoe MDL2 Assets", "Expected font family to be Segoe MDL2 Assets");
}

[TestCategory("FontIconExtensionMarkupExtension")]
[UITestMethod]
public void Test_FontIconExtension_MarkupExtension_ProvideSegoeUI()
{
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions"">
<AppBarButton x:Name=""Check"" Icon=""{ex:FontIcon Glyph=&#xE14D;, FontFamily='Segoe UI'}""/>
</Page>") as FrameworkElement;

var button = treeroot.FindChildByName("Check") as AppBarButton;

Assert.IsNotNull(button, $"Could not find the {nameof(AppBarButton)} control in tree.");

var icon = button.Icon as FontIcon;

Assert.IsNotNull(icon, $"Could not find the {nameof(FontIcon)} element in button.");

Assert.AreEqual(icon.Glyph, "\uE14D", "Expected icon glyph to be E14D.");
Assert.AreEqual(icon.FontFamily.Source, "Segoe UI", "Expected font family to be Segoe UI");
}

[TestCategory("FontIconExtensionMarkupExtension")]
[UITestMethod]
public void Test_FontIconExtension_MarkupExtension_ProvideCustomFontIcon()
{
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions"">
<AppBarButton x:Name=""Check"" Icon=""{ex:FontIcon Glyph=&#xE14D;, FontSize=7, FontFamily='Segoe UI', FontWeight=Bold, FontStyle=Italic, IsTextScaleFactorEnabled=True, MirroredWhenRightToLeft=True}""/>
</Page>") as FrameworkElement;

var button = treeroot.FindChildByName("Check") as AppBarButton;

Assert.IsNotNull(button, $"Could not find the {nameof(AppBarButton)} control in tree.");

var icon = button.Icon as FontIcon;

Assert.IsNotNull(icon, $"Could not find the {nameof(FontIcon)} element in button.");

Assert.AreEqual(icon.Glyph, "\uE14D", "Expected icon glyph to be E14D.");
Assert.AreEqual(icon.FontSize, 7.0, "Expected font size of 7");
Assert.AreEqual(icon.FontFamily.Source, "Segoe MDL2 Assets", "Expected font family to be Segoe UI");
Assert.AreEqual(icon.FontWeight, FontWeights.Bold, "Expected bold font weight");
Assert.AreEqual(icon.FontStyle, FontStyle.Italic, "Expected italic font style");
Assert.AreEqual(icon.IsTextScaleFactorEnabled, true, "Expected IsTextScaleFactorEnabled set to true");
Assert.AreEqual(icon.MirroredWhenRightToLeft, true, "Expected MirroredWhenRightToLeft set to true");
}
}
}
Loading