From 30573764b41823239ca6a45a1076f6a4beb8be7e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 25 Jan 2020 14:53:47 +0100 Subject: [PATCH 01/15] Added FontIcon markup extension --- .../Extensions/Markup/FontIcon.cs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIcon.cs diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIcon.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIcon.cs new file mode 100644 index 00000000000..5d268ea3005 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIcon.cs @@ -0,0 +1,38 @@ +// 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.Xaml.Data; +using Windows.UI.Xaml.Markup; +using Windows.UI.Xaml.Media; + +namespace Microsoft.Toolkit.Uwp.UI.Extensions +{ + /// + /// Custom which can provide values. + /// + [Bindable] + [MarkupExtensionReturnType(ReturnType = typeof(Windows.UI.Xaml.Controls.FontIcon))] + public class FontIcon : MarkupExtension + { + /// + /// Gets or sets the representing the icon to display. + /// + public string Glyph { get; set; } + + /// + /// Gets or sets the font family to use to display the icon. If , "Segoe MDL2 Assets" will be used. + /// + public FontFamily FontFamily { get; set; } + + /// + protected override object ProvideValue() + { + return new Windows.UI.Xaml.Controls.FontIcon + { + Glyph = Glyph, + FontFamily = FontFamily ?? new FontFamily("Segoe MDL2 Assets") + }; + } + } +} From 728f1ce284e380bac234904232a9e6d39b3338a9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 25 Jan 2020 14:53:56 +0100 Subject: [PATCH 02/15] Removed unnecessary using directives --- Microsoft.Toolkit.Uwp.UI/Extensions/Markup/NullableBool.cs | 5 ----- Microsoft.Toolkit.Uwp.UI/Extensions/Markup/OnDevice.cs | 1 - 2 files changed, 6 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/NullableBool.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/NullableBool.cs index cb1831ea4ab..e3a5224dfa8 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/NullableBool.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/NullableBool.cs @@ -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; diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/OnDevice.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/OnDevice.cs index 21fdf480344..866a54383a1 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/OnDevice.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/OnDevice.cs @@ -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; From 800efbaa3c335bd168f243f507b1802c12d15b61 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 25 Jan 2020 15:14:40 +0100 Subject: [PATCH 03/15] Renamed the FontIcon markup extension --- .../Markup/{FontIcon.cs => FontIconExtension.cs} | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) rename Microsoft.Toolkit.Uwp.UI/Extensions/Markup/{FontIcon.cs => FontIconExtension.cs} (81%) diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIcon.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIconExtension.cs similarity index 81% rename from Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIcon.cs rename to Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIconExtension.cs index 5d268ea3005..efebf1111c1 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIcon.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIconExtension.cs @@ -2,6 +2,7 @@ // 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.Xaml.Controls; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Markup; using Windows.UI.Xaml.Media; @@ -9,11 +10,11 @@ namespace Microsoft.Toolkit.Uwp.UI.Extensions { /// - /// Custom which can provide values. + /// Custom which can provide values. /// [Bindable] - [MarkupExtensionReturnType(ReturnType = typeof(Windows.UI.Xaml.Controls.FontIcon))] - public class FontIcon : MarkupExtension + [MarkupExtensionReturnType(ReturnType = typeof(FontIcon))] + public class FontIconExtension : MarkupExtension { /// /// Gets or sets the representing the icon to display. @@ -28,7 +29,7 @@ public class FontIcon : MarkupExtension /// protected override object ProvideValue() { - return new Windows.UI.Xaml.Controls.FontIcon + return new FontIcon { Glyph = Glyph, FontFamily = FontFamily ?? new FontFamily("Segoe MDL2 Assets") From c2f00be1e2d8b6290fb1faa34c0f5c7fb6738400 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 25 Jan 2020 15:18:44 +0100 Subject: [PATCH 04/15] Added FontIcon tests --- .../Test_FontIconExtensionMarkupExtension.cs | 63 +++++++++++++++++++ UnitTests/UnitTests.csproj | 1 + 2 files changed, 64 insertions(+) create mode 100644 UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs diff --git a/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs b/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs new file mode 100644 index 00000000000..9b5cad323c1 --- /dev/null +++ b/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs @@ -0,0 +1,63 @@ +// 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 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(@" + +") 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(@" + +") 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 UI"); + } + } +} diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index 1813e7836db..6b7e93bb5eb 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -134,6 +134,7 @@ + From 4493ffc41e9f0c8887345e8156aef4d7d5e48bc4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 26 Jan 2020 17:22:11 +0100 Subject: [PATCH 05/15] Fixed a typo in the test XAML --- UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs b/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs index 9b5cad323c1..dcfe43f8cda 100644 --- a/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs +++ b/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs @@ -22,7 +22,7 @@ public void Test_FontIconExtension_MarkupExtension_ProvideSegoeMdl2Asset() 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""> - + ") as FrameworkElement; var button = treeroot.FindChildByName("Check") as AppBarButton; @@ -45,7 +45,7 @@ public void Test_FontIconExtension_MarkupExtension_ProvideSegoeUI() 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""> - + ") as FrameworkElement; var button = treeroot.FindChildByName("Check") as AppBarButton; From e8d4309c3d9866131508a8f2bb9ccff5094ee681 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 12 Feb 2020 14:37:21 +0100 Subject: [PATCH 06/15] Update UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs Co-Authored-By: Michael Hawker MSFT (XAML Llama) --- UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs b/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs index dcfe43f8cda..d1c68203ef2 100644 --- a/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs +++ b/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs @@ -22,7 +22,7 @@ public void Test_FontIconExtension_MarkupExtension_ProvideSegoeMdl2Asset() 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""> - + ") as FrameworkElement; var button = treeroot.FindChildByName("Check") as AppBarButton; From 6638416fbfde577b6af763e3d1b9722786349e0a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 12 Feb 2020 14:38:29 +0100 Subject: [PATCH 07/15] Update UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs Co-Authored-By: Michael Hawker MSFT (XAML Llama) --- UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs b/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs index d1c68203ef2..ce00b7cc0e3 100644 --- a/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs +++ b/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs @@ -56,7 +56,7 @@ public void Test_FontIconExtension_MarkupExtension_ProvideSegoeUI() 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.Glyph, "\uE14D", "Expected icon glyph to be E14D."); Assert.AreEqual(icon.FontFamily.Source, "Segoe MDL2 Assets", "Expected font family to be Segoe UI"); } } From 452a276f2d1bde743f858e00c61aa6341887e4c2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 12 Feb 2020 14:39:01 +0100 Subject: [PATCH 08/15] Update UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs Co-Authored-By: Michael Hawker MSFT (XAML Llama) --- UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs b/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs index ce00b7cc0e3..8959c64578b 100644 --- a/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs +++ b/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs @@ -57,7 +57,7 @@ public void Test_FontIconExtension_MarkupExtension_ProvideSegoeUI() 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 MDL2 Assets", "Expected font family to be Segoe UI"); + Assert.AreEqual(icon.FontFamily.Source, "Segoe UI", "Expected font family to be Segoe UI"); } } } From 5577118a17a66af3053ae7b1b71bacafb08196dc Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 12 Feb 2020 14:45:41 +0100 Subject: [PATCH 09/15] Added new FontIcon properties --- .../Extensions/Markup/FontIconExtension.cs | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIconExtension.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIconExtension.cs index efebf1111c1..8394c244fea 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIconExtension.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIconExtension.cs @@ -2,6 +2,7 @@ // 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; @@ -21,19 +22,55 @@ public class FontIconExtension : MarkupExtension /// public string Glyph { get; set; } + /// + /// Gets or sets the size of the icon to display. + /// + public double FontSize { get; set; } + /// /// Gets or sets the font family to use to display the icon. If , "Segoe MDL2 Assets" will be used. /// public FontFamily FontFamily { get; set; } + /// + /// Gets or sets the thickness of the icon glyph. + /// + public FontWeight FontWeight { get; set; } = FontWeights.Normal; + + /// + /// Gets or sets the font style for the icon glyph. + /// + public FontStyle FontStyle { get; set; } = FontStyle.Normal; + + /// + /// Gets or sets a value indicating whether automatic text enlargement, to reflect the system text size setting, is enabled. + /// + public bool IsTextScaleFactorEnabled { get; set; } + + /// + /// Gets or sets a value indicating whether the icon is mirrored when the flow direction is right to left. + /// + public bool MirroredWhenRightToLeft { get; set; } + /// protected override object ProvideValue() { - return new FontIcon + var fontIcon = new FontIcon { Glyph = Glyph, - FontFamily = FontFamily ?? new FontFamily("Segoe MDL2 Assets") + FontFamily = FontFamily ?? new FontFamily("Segoe MDL2 Assets"), + FontWeight = FontWeight, + FontStyle = FontStyle, + IsTextScaleFactorEnabled = IsTextScaleFactorEnabled, + MirroredWhenRightToLeft = MirroredWhenRightToLeft }; + + if (FontSize > 0) + { + fontIcon.FontSize = FontSize; + } + + return fontIcon; } } } From 870306cc3c960b4977be789669c74e11efc70b68 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 12 Feb 2020 14:50:21 +0100 Subject: [PATCH 10/15] Added more tests for the new properties --- .../Test_FontIconExtensionMarkupExtension.cs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs b/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs index 8959c64578b..57021acb75e 100644 --- a/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs +++ b/UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs @@ -2,6 +2,7 @@ // 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; @@ -59,5 +60,33 @@ public void Test_FontIconExtension_MarkupExtension_ProvideSegoeUI() 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(@" + +") 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"); + } } } From c6d602341145e044c21d0196670aef8a9fe1a4db Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 12 Feb 2020 16:04:03 +0100 Subject: [PATCH 11/15] Added FontIconSourceExtension --- .../Markup/FontIconSourceExtension.cs | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIconSourceExtension.cs diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIconSourceExtension.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIconSourceExtension.cs new file mode 100644 index 00000000000..6e909bd1cb2 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/FontIconSourceExtension.cs @@ -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 +{ + /// + /// Custom which can provide values. + /// + [Bindable] + [MarkupExtensionReturnType(ReturnType = typeof(FontIconSource))] + public class FontIconSourceExtension : MarkupExtension + { + /// + /// Gets or sets the representing the icon to display. + /// + public string Glyph { get; set; } + + /// + /// Gets or sets the size of the icon to display. + /// + public double FontSize { get; set; } + + /// + /// Gets or sets the font family to use to display the icon. If , "Segoe MDL2 Assets" will be used. + /// + public FontFamily FontFamily { get; set; } + + /// + /// Gets or sets the thickness of the icon glyph. + /// + public FontWeight FontWeight { get; set; } = FontWeights.Normal; + + /// + /// Gets or sets the font style for the icon glyph. + /// + public FontStyle FontStyle { get; set; } = FontStyle.Normal; + + /// + /// Gets or sets a value indicating whether automatic text enlargement, to reflect the system text size setting, is enabled. + /// + public bool IsTextScaleFactorEnabled { get; set; } + + /// + /// Gets or sets a value indicating whether the icon is mirrored when the flow direction is right to left. + /// + public bool MirroredWhenRightToLeft { get; set; } + + /// + 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; + } + } +} From e90b0eb79815e41dd8e41250641a9d3661dc48f4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 12 Feb 2020 16:06:26 +0100 Subject: [PATCH 12/15] Added BitmapIconExtension --- .../Extensions/Markup/BitmapIconExtension.cs | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Microsoft.Toolkit.Uwp.UI/Extensions/Markup/BitmapIconExtension.cs diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/BitmapIconExtension.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/BitmapIconExtension.cs new file mode 100644 index 00000000000..d1c8c0a554d --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/BitmapIconExtension.cs @@ -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.Markup +{ + /// + /// Custom which can provide values. + /// + [Bindable] + [MarkupExtensionReturnType(ReturnType = typeof(BitmapIcon))] + public sealed class BitmapIconExtension : MarkupExtension + { + /// + /// Gets or sets the representing the image to display. + /// + public Uri Source { get; set; } + + /// + /// Gets or sets a value indicating whether to display the icon as monochrome. + /// + public bool ShowAsMonochrome { get; set; } + + /// + protected override object ProvideValue() + { + return new BitmapIcon + { + ShowAsMonochrome = ShowAsMonochrome, + UriSource = Source + }; + } + } +} From 0d8f53897ab4783eb2265610de9bdbe8f71ea72d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 24 Mar 2020 20:50:49 +0100 Subject: [PATCH 13/15] Added tests for the FontIconSource extension --- ..._FontIconSourceExtensionMarkupExtension.cs | 113 ++++++++++++++++++ UnitTests/UnitTests.csproj | 1 + 2 files changed, 114 insertions(+) create mode 100644 UnitTests/Extensions/Test_FontIconSourceExtensionMarkupExtension.cs diff --git a/UnitTests/Extensions/Test_FontIconSourceExtensionMarkupExtension.cs b/UnitTests/Extensions/Test_FontIconSourceExtensionMarkupExtension.cs new file mode 100644 index 00000000000..1c1c81852a7 --- /dev/null +++ b/UnitTests/Extensions/Test_FontIconSourceExtensionMarkupExtension.cs @@ -0,0 +1,113 @@ +// 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.Diagnostics.CodeAnalysis; +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_FontIconSourceExtensionMarkupExtension + { + [TestCategory("FontIconSourceExtensionMarkupExtension")] + [UITestMethod] + public void Test_FontIconSourceExtension_MarkupExtension_ProvideSegoeMdl2Asset() + { + var treeroot = XamlReader.Load(@" + +") as FrameworkElement; + + var button = treeroot.FindChildByName("Check") as MockSwipeItem; + + Assert.IsNotNull(button, $"Could not find the {nameof(MockSwipeItem)} control in tree."); + + var icon = button.IconSource as FontIconSource; + + 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("FontIconSourceExtensionMarkupExtension")] + [UITestMethod] + public void Test_FontIconSourceExtension_MarkupExtension_ProvideSegoeUI() + { + var treeroot = XamlReader.Load(@" + +") as FrameworkElement; + + var button = treeroot.FindChildByName("Check") as MockSwipeItem; + + Assert.IsNotNull(button, $"Could not find the {nameof(MockSwipeItem)} control in tree."); + + var icon = button.IconSource as FontIconSource; + + 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("FontIconSourceExtensionMarkupExtension")] + [UITestMethod] + public void Test_FontIconSourceExtension_MarkupExtension_ProvideCustomFontIcon() + { + var treeroot = XamlReader.Load(@" + +") as FrameworkElement; + + var button = treeroot.FindChildByName("Check") as MockSwipeItem; + + Assert.IsNotNull(button, $"Could not find the {nameof(MockSwipeItem)} control in tree."); + + var icon = button.IconSource as FontIconSource; + + 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"); + } + } + + [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402", Justification = "Mock control for tests")] + public class MockSwipeItem : Control + { + public IconSource IconSource + { + get => (IconSource)GetValue(IconSourceProperty); + set => SetValue(IconSourceProperty, value); + } + + public static readonly DependencyProperty IconSourceProperty = + DependencyProperty.Register( + nameof(IconSource), + typeof(IconSource), + typeof(MockSwipeItem), + new PropertyMetadata(default(IconSource))); + } +} \ No newline at end of file diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index b4208dc4fef..ec379bfb48b 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -136,6 +136,7 @@ + From 1af9349d0e78e67513cda4988e777798ba5ee2b8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 24 Mar 2020 21:27:38 +0100 Subject: [PATCH 14/15] Fixed namespace for BitmapIconExtension type --- .../Extensions/Markup/BitmapIconExtension.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/BitmapIconExtension.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/BitmapIconExtension.cs index d1c8c0a554d..68ded4e2cea 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/BitmapIconExtension.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/BitmapIconExtension.cs @@ -7,7 +7,7 @@ using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Markup; -namespace Microsoft.Toolkit.Uwp.UI.Extensions.Markup +namespace Microsoft.Toolkit.Uwp.UI.Extensions { /// /// Custom which can provide values. From bdcfd9ae97a0ef3e8e12543d96835708fe949a57 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 24 Mar 2020 21:29:09 +0100 Subject: [PATCH 15/15] Added tests for BitmapIconExtension type --- ...Test_BitmapIconExtensionMarkupExtension.cs | 85 +++++++++++++++++++ UnitTests/UnitTests.csproj | 1 + 2 files changed, 86 insertions(+) create mode 100644 UnitTests/Extensions/Test_BitmapIconExtensionMarkupExtension.cs diff --git a/UnitTests/Extensions/Test_BitmapIconExtensionMarkupExtension.cs b/UnitTests/Extensions/Test_BitmapIconExtensionMarkupExtension.cs new file mode 100644 index 00000000000..7eba199289f --- /dev/null +++ b/UnitTests/Extensions/Test_BitmapIconExtensionMarkupExtension.cs @@ -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(@" + +") 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(@" + +") 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"); + } + } +} \ No newline at end of file diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index b4208dc4fef..43e0197ecea 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -136,6 +136,7 @@ +