using Avalonia; using Avalonia.Controls; using Avalonia.Media; namespace LANCommander.Launcher.Controls; /// /// Icon variant (weight) corresponding to the Phosphor icon families. /// Bold icons are stroke-based; all others are fill-based. /// public enum IconVariant { Bold, DuoTone, Fill, Light, Regular, Thin, } /// /// Displays a Phosphor icon by variant and name. /// Resolves the StreamGeometry resource keyed as "{Type}_{Value}" /// (e.g. "Regular_ArrowClockwise") from the application resource dictionaries. /// /// Usage: /// <controls:Icon Type="Regular" Value="ArrowClockwise" Width="16" Height="16" /> /// /// Color sets the fill colour via Foreground (and stroke colour for Bold icons). /// Bold icons additionally require Stroke to be set on the control via a style, /// or you can set StrokeThickness directly (recommended value: 16, matching /// the 256-unit viewBox stroke width used by Phosphor's bold weight). /// IsVisible is inherited from Visual and toggles visibility. /// public class Icon : PathIcon { public static readonly StyledProperty TypeProperty = AvaloniaProperty.Register(nameof(Type), IconVariant.Regular); public static readonly StyledProperty ValueProperty = AvaloniaProperty.Register(nameof(Value)); public static readonly StyledProperty ColorProperty = AvaloniaProperty.Register(nameof(Color)); public IconVariant Type { get => GetValue(TypeProperty); set => SetValue(TypeProperty, value); } public string? Value { get => GetValue(ValueProperty); set => SetValue(ValueProperty, value); } public Color? Color { get => GetValue(ColorProperty); set => SetValue(ColorProperty, value); } static Icon() { TypeProperty.Changed.AddClassHandler((c, _) => c.UpdateGeometry()); ValueProperty.Changed.AddClassHandler((c, _) => c.UpdateGeometry()); ColorProperty.Changed.AddClassHandler((c, _) => c.UpdateColor()); } protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) { base.OnAttachedToVisualTree(e); UpdateGeometry(); UpdateColor(); } private void UpdateGeometry() { if (Value is null) return; var key = $"{Type}_{Value}"; if (Application.Current is { } app && app.TryGetResource(key, null, out var resource) && resource is StreamGeometry geometry) Data = geometry; } private void UpdateColor() { if (Color is { } color) Foreground = new SolidColorBrush(color); } }