From 355af6430ac8c8c24dc236af2ab359e10f010901 Mon Sep 17 00:00:00 2001 From: Omid Mafakher Date: Wed, 6 Dec 2023 15:33:17 +0100 Subject: [PATCH] Support image source (#96) --- .../IconImageSource.cs | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/Projektanker.Icons.Avalonia/IconImageSource.cs diff --git a/src/Projektanker.Icons.Avalonia/IconImageSource.cs b/src/Projektanker.Icons.Avalonia/IconImageSource.cs new file mode 100644 index 00000000..8ff0af3c --- /dev/null +++ b/src/Projektanker.Icons.Avalonia/IconImageSource.cs @@ -0,0 +1,70 @@ +using Avalonia; +using Avalonia.Media; +using System; + +namespace Projektanker.Icons.Avalonia +{ + public class IconImageSource : DrawingImage + { + public static readonly StyledProperty ValueProperty = + AvaloniaProperty.Register(nameof(Value)); + + public static readonly StyledProperty BrushProperty = + AvaloniaProperty.Register(nameof(Brush)); + + public static readonly StyledProperty PenProperty = + AvaloniaProperty.Register(nameof(Pen)); + + public string Value + { + get => GetValue(ValueProperty); + set => SetValue(ValueProperty, value); + } + + public IBrush? Brush + { + get => GetValue(BrushProperty); + set => SetValue(BrushProperty, value); + } + + public IPen? Pen + { + get => GetValue(PenProperty); + set => SetValue(PenProperty, value); + } + + private GeometryDrawing GetGeometryDrawing() + { + if (Drawing is GeometryDrawing geometryDrawing) + return geometryDrawing; + + Drawing = geometryDrawing = new GeometryDrawing(); + return geometryDrawing; + } + + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) + { + base.OnPropertyChanged(change); + if (change.Property == ValueProperty) + { + HandleValueChanged(); + } else if (change.Property == BrushProperty || change.Property == PenProperty) + { + HandleBrushChanged(); + } + } + + private void HandleBrushChanged() + { + var geometry = GetGeometryDrawing(); + geometry.Brush = Brush; + geometry.Pen = Pen; + } + + private void HandleValueChanged() + { + var iconModel = IconProvider.Current.GetIcon(Value); + GetGeometryDrawing().Geometry = StreamGeometry.Parse(iconModel.Path); + } + } +} \ No newline at end of file