-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from AathifMahir/Fix
New and Improved ImageSource Extension
- Loading branch information
Showing
17 changed files
with
238 additions
and
17 deletions.
There are no files selected for viewing
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
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
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,30 @@ | ||
using MauiIcons.Core.Helpers; | ||
|
||
namespace MauiIcons.Core; | ||
public static class CommonExtension | ||
{ | ||
/// <summary> | ||
/// this is used for seamlessly transforming the MauiIcons Enum Constructs to an ImageSource | ||
/// </summary> | ||
/// <param name="iconColor">sets the color of the icon. Defaults to black or white based on App Theme</param> | ||
/// <param name="iconSize">sets the size of the icon. Defaults to 30.0</param> | ||
/// <param name="iconAutoScaling">sets a value indicating whether the icon should automatically scale. Defaults to false</param> | ||
/// <returns>Icon as ImageSource</returns> | ||
/// <exception cref="MauiIconsExpection"> | ||
/// Thrown when the Enum is not a MauiIcons Construct | ||
/// </exception> | ||
public static ImageSource ToImageSource(this Enum icon, Color? iconColor = null, double iconSize = 30.0, bool iconAutoScaling = false) | ||
{ | ||
if (icon.GetDescription() is null) | ||
throw new MauiIconsExpection("MauiIcons ToImageSourceExtension Only Works on MauiIcons Constructs Not on All the Enum Types"); | ||
|
||
return new FontImageSource() | ||
{ | ||
Glyph = icon.GetDescription(), | ||
Color = iconColor.SetDefaultOrAssignedColor(), | ||
FontFamily = icon.GetFontFamily(), | ||
Size = iconSize, | ||
FontAutoScalingEnabled = iconAutoScaling | ||
}; | ||
} | ||
} |
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
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
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
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
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
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
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
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
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
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
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
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
154 changes: 154 additions & 0 deletions
154
src/MauiIcons.Modules.UnitTest/Extensions/CommonExtensionTests.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,154 @@ | ||
using MauiIcons.Fluent.Filled; | ||
using MauiIcons.Cupertino; | ||
using MauiIcons.Core; | ||
|
||
namespace MauiIcons.Modules.UnitTest.Extensions; | ||
public class CommonExtensionTests : BaseHandlerTest | ||
{ | ||
[Fact] | ||
public void Icon() | ||
{ | ||
// Arrange | ||
Image image; | ||
var icon = FluentFilledIcons.Accessibility16Filled.ToImageSource(); | ||
var iconCode = "\uF102"; | ||
FontImageSource source; | ||
|
||
// Act | ||
image = new Image { Source = icon }; | ||
source = (FontImageSource)image.Source; | ||
|
||
// Assert | ||
image.Source.Should().NotBeNull(); | ||
source.Glyph.Should().Be(iconCode); | ||
source.FontFamily.Should().Be("FluentFilledIcons"); | ||
} | ||
|
||
[Fact] | ||
public void IconChanged() | ||
{ | ||
// Arrange | ||
Image image; | ||
bool changedSignaled = false; | ||
var assignedIcon = CupertinoIcons.Airplane.ToImageSource(); | ||
var assignedIconCode = "\ue900"; | ||
FontImageSource source; | ||
|
||
// Act | ||
image = new Image(); | ||
image.PropertyChanged += (sender, e) => | ||
{ | ||
if (e.PropertyName == "Source") | ||
{ | ||
changedSignaled = true; | ||
} | ||
}; | ||
image.Source = assignedIcon; | ||
source = (FontImageSource)image.Source; | ||
|
||
|
||
// Assert | ||
image.Source.Should().NotBeNull(); | ||
source.Glyph.Should().Be(assignedIconCode); | ||
source.FontFamily.Should().Be("CupertinoIcons"); | ||
changedSignaled.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void IconColorChanged() | ||
{ | ||
// Arrange | ||
Image image; | ||
bool changedSignaled = false; | ||
var assignedColor = Colors.Blue; | ||
var assignedIcon = CupertinoIcons.Airplane.ToImageSource(iconColor: assignedColor); | ||
FontImageSource source; | ||
|
||
// Act | ||
image = new Image(); | ||
image.PropertyChanged += (sender, e) => | ||
{ | ||
if (e.PropertyName == "Source") | ||
{ | ||
changedSignaled = true; | ||
} | ||
}; | ||
image.Source = assignedIcon; | ||
source = (FontImageSource)image.Source; | ||
|
||
// Assert | ||
image.Source.Should().NotBeNull(); | ||
source.Color.Should().Be(assignedColor); | ||
changedSignaled.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void IconColorChangedToDefault() | ||
{ | ||
// Arrange | ||
Image image; | ||
var assignedIcon = CupertinoIcons.Airplane.ToImageSource(); | ||
var assignedColoredIcon = CupertinoIcons.Airplane.ToImageSource(iconColor: Colors.Green); | ||
FontImageSource source; | ||
|
||
// Act | ||
image = new Image { Source = assignedColoredIcon }; | ||
image.Source = assignedIcon; | ||
source = (FontImageSource)image.Source; | ||
|
||
|
||
// Assert | ||
image.Source.Should().NotBeNull(); | ||
source.Color.Should().Be(Colors.Black); | ||
} | ||
|
||
|
||
[Fact] | ||
public void IconSizeChanged() | ||
{ | ||
// Arrange | ||
Image image; | ||
bool changedSignaled = false; | ||
var assignedSize = 25.0; | ||
var assignedIcon = CupertinoIcons.Airplane.ToImageSource(iconSize: assignedSize); | ||
FontImageSource source; | ||
|
||
// Act | ||
image = new Image(); | ||
image.PropertyChanged += (sender, e) => | ||
{ | ||
if (e.PropertyName == "Source") | ||
{ | ||
changedSignaled = true; | ||
} | ||
}; | ||
image.Source = assignedIcon; | ||
source = (FontImageSource)image.Source; | ||
|
||
|
||
// Assert | ||
image.Source.Should().NotBeNull(); | ||
source.Size.Should().Be(assignedSize); | ||
changedSignaled.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void IconSizeChangedToDefault() | ||
{ | ||
// Arrange | ||
Image image; | ||
var defaultSize = 30.0; | ||
var defaultIcon = CupertinoIcons.Airplane.ToImageSource(); | ||
var assignedIcon = CupertinoIcons.Airplane.ToImageSource(iconSize: 40); | ||
FontImageSource source; | ||
|
||
// Act | ||
image = new Image { Source = assignedIcon }; | ||
image.Source = defaultIcon; | ||
source = (FontImageSource)image.Source; | ||
|
||
// Assert | ||
image.Source.Should().NotBeNull(); | ||
source.Size.Should().Be(defaultSize); | ||
} | ||
} |
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