-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BitmapIcon markup extension #3130
Closed
Sergio0694
wants to merge
30
commits into
CommunityToolkit:master
from
Sergio0694:feature/bitmap-icon-extension
Closed
Changes from 6 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
3057376
Added FontIcon markup extension
Sergio0694 728f1ce
Removed unnecessary using directives
Sergio0694 800efba
Renamed the FontIcon markup extension
Sergio0694 c2f00be
Added FontIcon tests
Sergio0694 4493ffc
Fixed a typo in the test XAML
Sergio0694 6418ba5
Merge branch 'master' into feature/font-icon-extension
michael-hawker e8d4309
Update UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs
Sergio0694 6638416
Update UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs
Sergio0694 452a276
Update UnitTests/Extensions/Test_FontIconExtensionMarkupExtension.cs
Sergio0694 5577118
Added new FontIcon properties
Sergio0694 9018d88
Merge branch 'feature/font-icon-extension' of https://github.com/Serg…
Sergio0694 870306c
Added more tests for the new properties
Sergio0694 c6d6023
Added FontIconSourceExtension
Sergio0694 e90b0eb
Added BitmapIconExtension
Sergio0694 8dda65e
Merge branch 'master' into feature/bitmap-icon-extension
Sergio0694 cea30df
Merge branch 'master' into feature/font-icon-source-extension
Sergio0694 1b34996
Merge branch 'master' into feature/font-icon-extension
Sergio0694 23dcec0
Merge branch 'master' into feature/bitmap-icon-extension
Sergio0694 63c2b6e
Merge branch 'master' into feature/font-icon-source-extension
Sergio0694 96b22d4
Merge branch 'master' into feature/font-icon-extension
Sergio0694 7d6f9ec
Merge branch 'master' into feature/bitmap-icon-extension
Sergio0694 91947c1
Merge branch 'master' into feature/font-icon-source-extension
Sergio0694 0d8f538
Added tests for the FontIconSource extension
Sergio0694 1af9349
Fixed namespace for BitmapIconExtension type
Sergio0694 bdcfd9a
Added tests for BitmapIconExtension type
Sergio0694 88844c1
Merge branch 'master' into feature/bitmap-icon-extension
Sergio0694 d0421d8
Merge branch 'master' into feature/font-icon-source-extension
Sergio0694 1d9ad41
Merge branch 'master' into feature/font-icon-extension
Sergio0694 ae5f94b
Merge pull request #14 from Sergio0694/feature/font-icon-source-exten…
Sergio0694 76cfed2
Merge branch 'feature/font-icon-extension' into feature/bitmap-icon-e…
Sergio0694 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
Microsoft.Toolkit.Uwp.UI/Extensions/Markup/BitmapIconExtension.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,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() | ||
{ | ||
return new BitmapIcon | ||
{ | ||
ShowAsMonochrome = ShowAsMonochrome, | ||
UriSource = Source | ||
}; | ||
} | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
UnitTests/Extensions/Test_BitmapIconExtensionMarkupExtension.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,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}"" /> | ||
Sergio0694 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</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"); | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can be a body-less method:
There was a problem hiding this comment.
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! 😊
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.