diff --git a/LANCommander.Launcher.Avalonia/Controls/IconButton.cs b/LANCommander.Launcher.Avalonia/Controls/IconButton.cs new file mode 100644 index 00000000..486479aa --- /dev/null +++ b/LANCommander.Launcher.Avalonia/Controls/IconButton.cs @@ -0,0 +1,120 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Controls.Primitives; + +namespace LANCommander.Launcher.Avalonia.Controls; + +public enum IconPosition +{ + Left, + Right, +} + +public class IconButton : Button +{ + public static readonly StyledProperty IconTypeProperty = + AvaloniaProperty.Register(nameof(IconType), IconVariant.Regular); + + public static readonly StyledProperty IconValueProperty = + AvaloniaProperty.Register(nameof(IconValue)); + + public static readonly StyledProperty SizeProperty = + AvaloniaProperty.Register(nameof(Size), 16); + + public static readonly StyledProperty TextProperty = + AvaloniaProperty.Register(nameof(Text)); + + public static readonly StyledProperty IconPlacementProperty = + AvaloniaProperty.Register(nameof(IconPlacement), IconPosition.Left); + + public IconVariant IconType + { + get => GetValue(IconTypeProperty); + set => SetValue(IconTypeProperty, value); + } + + public string? IconValue + { + get => GetValue(IconValueProperty); + set => SetValue(IconValueProperty, value); + } + + public double Size + { + get => GetValue(SizeProperty); + set => SetValue(SizeProperty, value); + } + + public string? Text + { + get => GetValue(TextProperty); + set => SetValue(TextProperty, value); + } + + public IconPosition IconPlacement + { + get => GetValue(IconPlacementProperty); + set => SetValue(IconPlacementProperty, value); + } + + protected override void OnApplyTemplate(TemplateAppliedEventArgs e) + { + base.OnApplyTemplate(e); + UpdateContent(); + } + + static IconButton() + { + VerticalAlignmentProperty.OverrideDefaultValue(global::Avalonia.Layout.VerticalAlignment.Center); + + IconTypeProperty.Changed.AddClassHandler((b, _) => b.UpdateContent()); + IconValueProperty.Changed.AddClassHandler((b, _) => b.UpdateContent()); + SizeProperty.Changed.AddClassHandler((b, _) => b.UpdateContent()); + TextProperty.Changed.AddClassHandler((b, _) => b.UpdateContent()); + IconPlacementProperty.Changed.AddClassHandler((b, _) => b.UpdateContent()); + } + + private void UpdateContent() + { + var icon = new Icon + { + Type = IconType, + Value = IconValue, + Width = Size, + Height = Size, + VerticalAlignment = global::Avalonia.Layout.VerticalAlignment.Center, + }; + + if (Text is null) + { + Content = icon; + return; + } + + var textBlock = new TextBlock + { + Text = Text, + VerticalAlignment = global::Avalonia.Layout.VerticalAlignment.Center, + FontSize = Size, + }; + + var panel = new StackPanel + { + Orientation = global::Avalonia.Layout.Orientation.Horizontal, + Spacing = (Size / 2) + 2, + }; + + if (IconPlacement == IconPosition.Right) + { + panel.Children.Add(textBlock); + panel.Children.Add(icon); + } + else + { + panel.Children.Add(icon); + panel.Children.Add(textBlock); + } + + Content = panel; + } +}