Skip to content

Commit

Permalink
Added basic support for IconSource and its subclasses (#192)
Browse files Browse the repository at this point in the history
* Ported IconSource and its sub classes from WinUI

* Resolved suggestions

* Added headers with copyright & license notices to avoid potential licensing issues

* Removed an unnecessary duplicate class

and moved code into an existing file to resolved CS0436 warning
  • Loading branch information
ShankarBUS authored Nov 6, 2020
1 parent e8335aa commit 7643681
Show file tree
Hide file tree
Showing 6 changed files with 390 additions and 0 deletions.
72 changes: 72 additions & 0 deletions ModernWpf.Controls/Common/SharedHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,78 @@ public static void RaiseAutomationPropertyChangedEvent(UIElement element, object
}
}

public static IconElement MakeIconElementFrom(IconSource iconSource)
{
if (iconSource is FontIconSource fontIconSource)
{
FontIcon fontIcon = new FontIcon();

fontIcon.Glyph = fontIconSource.Glyph;
fontIcon.FontSize = fontIconSource.FontSize;
if (fontIconSource.Foreground is Brush newForeground)
{
fontIcon.Foreground = newForeground;
}

if (fontIconSource.FontFamily != null)
{
fontIcon.FontFamily = fontIconSource.FontFamily;
}

fontIcon.FontWeight = fontIconSource.FontWeight;
fontIcon.FontStyle = fontIconSource.FontStyle;
//fontIcon.IsTextScaleFactorEnabled(fontIconSource.IsTextScaleFactorEnabled());
//fontIcon.MirroredWhenRightToLeft(fontIconSource.MirroredWhenRightToLeft());

return fontIcon;
}
else if (iconSource is SymbolIconSource symbolIconSource)
{
SymbolIcon symbolIcon = new SymbolIcon();
symbolIcon.Symbol = symbolIconSource.Symbol;
if (symbolIconSource.Foreground is Brush newForeground)
{
symbolIcon.Foreground = newForeground;
}

return symbolIcon;
}
else if (iconSource is BitmapIconSource bitmapIconSource)
{
BitmapIcon bitmapIcon = new BitmapIcon();

if (bitmapIconSource.UriSource != null)
{
bitmapIcon.UriSource = bitmapIconSource.UriSource;
}

bitmapIcon.ShowAsMonochrome = bitmapIconSource.ShowAsMonochrome;
if (bitmapIconSource.Foreground is Brush newForeground)
{
bitmapIcon.Foreground = newForeground;
}

return bitmapIcon;
}
else if (iconSource is PathIconSource pathIconSource)
{
PathIcon pathIcon = new PathIcon();

if (pathIconSource.Data != null)
{
pathIcon.Data = pathIconSource.Data;
}
if (pathIconSource.Foreground is Brush newForeground)
{
pathIcon.Foreground = newForeground;
}

return pathIcon;
}

return null;
}

public static BindingExpressionBase SetBinding(
this FrameworkElement element,
DependencyProperty dp,
Expand Down
63 changes: 63 additions & 0 deletions ModernWpf/IconSource/BitmapIconSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Windows;
using System.Windows.Media.Imaging;

namespace ModernWpf.Controls
{
/// <summary>
/// Represents an icon source that uses a bitmap as its content.
/// </summary>
public class BitmapIconSource : IconSource
{
/// <summary>
/// Initializes a new instance of the <see cref="BitmapIconSource"/> class.
/// </summary>
public BitmapIconSource()
{
}

/// <summary>
/// Identifies the <see cref="UriSource"/> dependency property.
/// </summary>
public static readonly DependencyProperty UriSourceProperty =
BitmapImage.UriSourceProperty.AddOwner(typeof(BitmapIconSource));

/// <summary>
/// Gets or sets the Uniform Resource Identifier (URI) of the bitmap to use as the icon content.
/// </summary>
/// <returns>
/// The <see cref="Uri"/> of the bitmap to use as the icon content. The default is <see langword="null"/>.
/// </returns>
public Uri UriSource
{
get => (Uri)GetValue(UriSourceProperty);
set => SetValue(UriSourceProperty, value);
}

/// <summary>
/// Identifies the <see cref="ShowAsMonochrome"/> dependency property.
/// </summary>
public static readonly DependencyProperty ShowAsMonochromeProperty =
DependencyProperty.Register(
nameof(ShowAsMonochrome),
typeof(bool),
typeof(BitmapIconSource),
new PropertyMetadata(true));

/// <summary>
/// Gets or sets a value that indicates whether the bitmap is shown in a single color.
/// </summary>
/// <returns>
/// <see langword="true"/> to show the bitmap in a single color;
/// <see langword="false"/> to show the bitmap in full color. The default is <see langword="true"/>.
/// </returns>
public bool ShowAsMonochrome
{
get => (bool)GetValue(ShowAsMonochromeProperty);
set => SetValue(ShowAsMonochromeProperty, value);
}
}
}
135 changes: 135 additions & 0 deletions ModernWpf/IconSource/FontIconSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Windows;
using System.Windows.Media;

namespace ModernWpf.Controls
{
/// <summary>
/// Represents an icon source that uses a glyph from the specified font.
/// </summary>
public class FontIconSource : IconSource
{
const string c_fontIconSourceDefaultFontFamily = "Segoe MDL2 Assets";

/// <summary>
/// Initializes a new instance of the <see cref="FontIconSource"/> class.
/// </summary>
public FontIconSource()
{
}

/// <summary>
/// Identifies the <see cref="FontFamily"/> dependency property.
/// </summary>
public static readonly DependencyProperty FontFamilyProperty =
DependencyProperty.Register(
nameof(FontFamily),
typeof(FontFamily),
typeof(FontIconSource),
new PropertyMetadata(new FontFamily(c_fontIconSourceDefaultFontFamily)));

/// <summary>
/// Gets or sets the font used to display the icon glyph.
/// </summary>
/// <returns>
/// The font used to display the icon glyph.
/// </returns>
public FontFamily FontFamily
{
get => (FontFamily)GetValue(FontFamilyProperty);
set => SetValue(FontFamilyProperty, value);
}

/// <summary>
/// Identifies the <see cref="FontSize"/> dependency property.
/// </summary>
public static readonly DependencyProperty FontSizeProperty =
DependencyProperty.Register(
nameof(FontSize),
typeof(double),
typeof(FontIconSource),
new PropertyMetadata(20.0));

/// <summary>
/// Gets or sets the size of the icon glyph.
/// </summary>
/// <returns>
/// A non-negative value that specifies the font size, measured in pixels.
/// </returns>
public double FontSize
{
get => (double)GetValue(FontSizeProperty);
set => SetValue(FontSizeProperty, value);
}

/// <summary>
/// Identifies the <see cref="FontStyle"/> dependency property.
/// </summary>
public static readonly DependencyProperty FontStyleProperty =
DependencyProperty.Register(
nameof(FontStyle),
typeof(FontStyle),
typeof(FontIconSource),
new PropertyMetadata(FontStyles.Normal));

/// <summary>
/// Gets or sets the font style for the icon glyph.
/// </summary>
/// <returns>
/// A named constant of the enumeration that specifies the style in which the icon glyph is rendered.
/// The default is <see cref="FontStyles.Normal"/>.
/// </returns>
public FontStyle FontStyle
{
get => (FontStyle)GetValue(FontStyleProperty);
set => SetValue(FontStyleProperty, value);
}

/// <summary>
/// Identifies the <see cref="FontWeight"/> dependency property.
/// </summary>
public static readonly DependencyProperty FontWeightProperty =
DependencyProperty.Register(
nameof(FontWeight),
typeof(FontWeight),
typeof(FontIconSource),
new PropertyMetadata(FontWeights.Normal));

/// <summary>
/// Gets or sets the thickness of the icon glyph.
/// </summary>
/// <returns>
/// A value that specifies the thickness of the icon glyph.
/// The default is <see cref="FontWeights.Normal"/>.
/// </returns>
public FontWeight FontWeight
{
get => (FontWeight)GetValue(FontWeightProperty);
set => SetValue(FontWeightProperty, value);
}

/// <summary>
/// Identifies the <see cref="Glyph"/> dependency property.
/// </summary>
public static readonly DependencyProperty GlyphProperty =
DependencyProperty.Register(
nameof(Glyph),
typeof(string),
typeof(FontIconSource),
new PropertyMetadata(string.Empty));

/// <summary>
/// Gets or sets the character code that identifies the icon glyph.
/// </summary>
/// <returns>
/// The hexadecimal character code for the icon glyph.
/// </returns>
public string Glyph
{
get => (string)GetValue(GlyphProperty);
set => SetValue(GlyphProperty, value);
}
}
}
36 changes: 36 additions & 0 deletions ModernWpf/IconSource/IconSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Windows;
using System.Windows.Media;

namespace ModernWpf.Controls
{
/// <summary>
/// Represents the base class for an icon source.
/// </summary>
public class IconSource : DependencyObject
{
/// <summary>
/// Identifies the <see cref="Foreground"/> dependency property.
/// </summary>
public static readonly DependencyProperty ForegroundProperty =
DependencyProperty.Register(
nameof(Foreground),
typeof(Brush),
typeof(IconSource));

/// <summary>
/// Gets or sets a brush that describes the foreground color.
/// </summary>
/// <returns>
/// The brush that paints the foreground of the control. The default is <see langword="null"/>, (a null brush) which is
/// evaluated as Transparent for rendering.
/// </returns>
public Brush Foreground
{
get => (Brush)GetValue(ForegroundProperty);
set => SetValue(ForegroundProperty, value);
}
}
}
42 changes: 42 additions & 0 deletions ModernWpf/IconSource/PathIconSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Windows;
using System.Windows.Media;

namespace ModernWpf.Controls
{
/// <summary>
/// Represents an icon source that uses a vector path as its content.
/// </summary>
public class PathIconSource : IconSource
{
/// <summary>
/// Initializes a new instance of the <see cref="PathIconSource"/> class.
/// </summary>
public PathIconSource()
{
}

/// <summary>
/// Identifies the <see cref="Data"/> dependency property.
/// </summary>
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register(
nameof(Data),
typeof(Geometry),
typeof(PathIconSource));

/// <summary>
/// Gets or sets a <see cref="Geometry"/>.
/// </summary>
/// <returns>
/// A description of the shape to be drawn.
/// </returns>
public Geometry Data
{
get => (Geometry)GetValue(DataProperty);
set => SetValue(DataProperty, value);
}
}
}
Loading

0 comments on commit 7643681

Please sign in to comment.