-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add missing to image converter
- Loading branch information
Showing
4 changed files
with
149 additions
and
7 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
25 changes: 25 additions & 0 deletions
25
src/IconPacks.Avalonia.BoxIcons/Converter/PackIconBoxIconsKindToImageConverter.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,25 @@ | ||
using Avalonia.Media; | ||
|
||
namespace IconPacks.Avalonia.Converter | ||
{ | ||
public class PackIconBoxIconsKindToImageConverter : PackIconKindToImageConverterBase | ||
{ | ||
/// <inheritdoc /> | ||
protected override string GetPathData(object iconKind) | ||
{ | ||
string data = null; | ||
if (iconKind is PackIconBoxIconsKind kind) | ||
{ | ||
PackIconDataFactory<PackIconBoxIconsKind>.DataIndex.Value?.TryGetValue(kind, out data); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override ScaleTransform GetScaleTransform(object iconKind) | ||
{ | ||
return new ScaleTransform(1, -1); | ||
} | ||
} | ||
} |
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
115 changes: 115 additions & 0 deletions
115
src/IconPacks.Avalonia.Core/Converter/PackIconKindToImageConverterBase.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,115 @@ | ||
using Avalonia.Media; | ||
using System.Globalization; | ||
using System; | ||
using Avalonia.Data; | ||
|
||
namespace IconPacks.Avalonia.Converter | ||
{ | ||
public abstract class PackIconKindToImageConverterBase : MarkupConverter | ||
{ | ||
/// <summary> | ||
/// Gets or sets the brush to draw the icon. | ||
/// </summary> | ||
public IBrush Brush { get; set; } = Brushes.Black; | ||
|
||
/// <summary> | ||
/// Gets or sets the flip orientation for the icon. | ||
/// </summary> | ||
public PackIconFlipOrientation Flip { get; set; } = PackIconFlipOrientation.Normal; | ||
|
||
/// <summary> | ||
/// Gets or sets the rotation (angle) for the icon. | ||
/// </summary> | ||
public double RotationAngle { get; set; } = 0d; | ||
|
||
/// <summary> | ||
/// Gets the path data for the given kind. | ||
/// </summary> | ||
protected abstract string GetPathData(object iconKind); | ||
|
||
/// <summary> | ||
/// Gets the ScaleTransform for the given kind. | ||
/// </summary> | ||
/// <param name="iconKind">The icon kind to draw.</param> | ||
protected virtual ScaleTransform GetScaleTransform(object iconKind) | ||
{ | ||
return new ScaleTransform(1, 1); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the <see cref="TransformGroup" /> for the <see cref="DrawingGroup" />. | ||
/// </summary> | ||
/// <param name="iconKind">The icon kind to draw.</param> | ||
protected Transform GetTransformGroup(object iconKind) | ||
{ | ||
var transformGroup = new TransformGroup(); | ||
transformGroup.Children.Add(this.GetScaleTransform(iconKind)); // scale | ||
transformGroup.Children.Add(new ScaleTransform( | ||
this.Flip == PackIconFlipOrientation.Horizontal || this.Flip == PackIconFlipOrientation.Both ? -1 : 1, | ||
this.Flip == PackIconFlipOrientation.Vertical || this.Flip == PackIconFlipOrientation.Both ? -1 : 1 | ||
)); // flip | ||
transformGroup.Children.Add(new RotateTransform(this.RotationAngle)); // rotate | ||
|
||
return transformGroup; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the <see cref="T:System.Windows.Media.DrawingGroup" /> object that will be used for the <see cref="T:System.Windows.Media.DrawingImage" />. | ||
/// </summary> | ||
protected virtual DrawingGroup GetDrawingGroup(object iconKind, IBrush foregroundBrush, string path) | ||
{ | ||
var geometryDrawing = new GeometryDrawing | ||
{ | ||
Geometry = Geometry.Parse(path), | ||
Brush = foregroundBrush | ||
}; | ||
|
||
var drawingGroup = new DrawingGroup | ||
{ | ||
Children = { geometryDrawing }, | ||
Transform = this.GetTransformGroup(iconKind) | ||
}; | ||
|
||
return drawingGroup; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the ImageSource for the given kind. | ||
/// </summary> | ||
protected IImage CreateImageSource(object iconKind, IBrush foregroundBrush) | ||
{ | ||
var path = this.GetPathData(iconKind); | ||
|
||
if (string.IsNullOrEmpty(path)) | ||
{ | ||
return null; | ||
} | ||
|
||
var drawingImage = new DrawingImage(GetDrawingGroup(iconKind, foregroundBrush, path)); | ||
return drawingImage; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override object Convert(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
if (value is not Enum) | ||
{ | ||
return BindingNotification.UnsetValue; | ||
} | ||
|
||
var imageSource = CreateImageSource(value, parameter as IBrush ?? this.Brush ?? Brushes.Black); | ||
if (imageSource is null) | ||
{ | ||
return BindingNotification.UnsetValue; | ||
} | ||
|
||
return imageSource; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
return BindingNotification.UnsetValue; | ||
} | ||
} | ||
} |