using System; using System.Windows; using System.Windows.Controls; using MaterialDesignThemes.Wpf; namespace Artemis.UI.Shared { /// /// Interaction logic for ArtemisIcon.xaml /// public partial class ArtemisIcon : UserControl { /// /// Gets or sets the currently displayed icon as either a or an pointing /// to an SVG /// public static readonly DependencyProperty IconProperty = DependencyProperty.Register(nameof(Icon), typeof(object), typeof(ArtemisIcon), new FrameworkPropertyMetadata(IconPropertyChangedCallback)); /// /// Gets or sets the /// public static readonly DependencyProperty PackIconProperty = DependencyProperty.Register(nameof(PackIcon), typeof(PackIconKind?), typeof(ArtemisIcon), new FrameworkPropertyMetadata(IconPropertyChangedCallback)); /// /// Gets or sets the pointing to the SVG /// public static readonly DependencyProperty SvgSourceProperty = DependencyProperty.Register(nameof(SvgSource), typeof(Uri), typeof(ArtemisIcon), new FrameworkPropertyMetadata(IconPropertyChangedCallback)); private bool _inCallback; /// /// Creates a new instance of the class /// public ArtemisIcon() { InitializeComponent(); } /// /// Gets or sets the currently displayed icon as either a or an pointing /// to an SVG /// public object? Icon { get => GetValue(IconProperty); set => SetValue(IconProperty, value); } /// /// Gets or sets the /// public PackIconKind? PackIcon { get => (PackIconKind?) GetValue(PackIconProperty); set => SetValue(PackIconProperty, value); } /// /// Gets or sets the pointing to the SVG /// public Uri SvgSource { get => (Uri) GetValue(SvgSourceProperty); set => SetValue(SvgSourceProperty, value); } private static void IconPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { ArtemisIcon artemisIcon = (ArtemisIcon) d; if (artemisIcon._inCallback) return; try { artemisIcon._inCallback = true; if (artemisIcon.PackIcon != null) { artemisIcon.Icon = artemisIcon.PackIcon; } else if (artemisIcon.SvgSource != null) { artemisIcon.Icon = artemisIcon.SvgSource; } else if (artemisIcon.Icon is string iconString) { if (Uri.TryCreate(iconString, UriKind.Absolute, out Uri? uriResult)) artemisIcon.Icon = uriResult; else if (Enum.TryParse(typeof(PackIconKind), iconString, true, out object? result)) artemisIcon.Icon = result; } } finally { artemisIcon._inCallback = false; } } } }