Skip to content

Commit

Permalink
feat: add missing to image converter
Browse files Browse the repository at this point in the history
  • Loading branch information
punker76 committed Oct 27, 2024
1 parent 6800f88 commit 7700d2a
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/AvaloniaApp/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@
Kind="{Binding #BoxIcons.SelectedItem, Mode=OneWay}"
Foreground="{DynamicResource SystemControlHighlightAccentBrush}" />

<Label Content="Button MarkupExtension" />
<Button Content="{iconPacks:BoxIcons RegularAtom}" Margin="10" />
<Label Content="Image ValueConverter" />
<Image Source="{Binding #BoxIcons.SelectedItem, Mode=OneWay, Converter={iconPacks:PackIconBoxIconsKindToImageConverter Brush=Goldenrod}}" Width="24" Height="24"
HorizontalAlignment="Left" Margin="10" />

<Label Content="Image MarkupExtension" />
<Image Source="{iconPacks:BoxIconsImage Kind=RegularAtom, Brush=Brown}" Width="24" Height="24" HorizontalAlignment="Left" Margin="10" />

<Label Content="Button MarkupExtension" />
<Button Content="{iconPacks:BoxIcons RegularAtom}" Margin="10" />

<Separator />

<Label Content="Flip Orientation" />
Expand Down
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);
}
}
}
8 changes: 3 additions & 5 deletions src/IconPacks.Avalonia.BoxIcons/PackIconBoxIconsKind.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
namespace IconPacks.Avalonia
{
#if !(NETFX_CORE || WINDOWS_UWP)
using System.ComponentModel;
#endif
using System.ComponentModel;

namespace IconPacks.Avalonia
{
/// ******************************************
/// This code is auto generated. Do not amend.
/// ******************************************
Expand Down
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;
}
}
}

0 comments on commit 7700d2a

Please sign in to comment.