diff --git a/demo/Ursa.Demo/Pages/IconButtonDemo.axaml b/demo/Ursa.Demo/Pages/IconButtonDemo.axaml index 44f7c93..a6e611a 100644 --- a/demo/Ursa.Demo/Pages/IconButtonDemo.axaml +++ b/demo/Ursa.Demo/Pages/IconButtonDemo.axaml @@ -22,6 +22,14 @@ Data="{StaticResource iconGlyph}" /> + + + + + - + - + - + @@ -99,6 +103,16 @@ + + + + @@ -106,8 +120,8 @@ - - diff --git a/src/Ursa/Common/IconPlacement.cs b/src/Ursa/Common/IconPlacement.cs new file mode 100644 index 0000000..c38cec6 --- /dev/null +++ b/src/Ursa/Common/IconPlacement.cs @@ -0,0 +1,7 @@ +namespace Ursa.Common; + +public enum IconPlacement +{ + Left, + Right, +} \ No newline at end of file diff --git a/src/Ursa/Controls/IconButton.cs b/src/Ursa/Controls/IconButton.cs index 0016137..b4c5039 100644 --- a/src/Ursa/Controls/IconButton.cs +++ b/src/Ursa/Controls/IconButton.cs @@ -1,11 +1,17 @@ using Avalonia; using Avalonia.Controls; +using Avalonia.Controls.Metadata; +using Avalonia.Controls.Primitives; using Avalonia.Controls.Templates; +using Ursa.Common; namespace Ursa.Controls; +[PseudoClasses(PC_Right)] public class IconButton: Button { + public const string PC_Right = ":right"; + public static readonly StyledProperty IconProperty = AvaloniaProperty.Register( nameof(Icon)); @@ -32,4 +38,32 @@ public class IconButton: Button get => GetValue(IsLoadingProperty); set => SetValue(IsLoadingProperty, value); } + + public static readonly StyledProperty IconPlacementProperty = AvaloniaProperty.Register( + nameof(IconPlacement), defaultValue: IconPlacement.Left); + + public IconPlacement IconPlacement + { + get => GetValue(IconPlacementProperty); + set => SetValue(IconPlacementProperty, value); + } + + static IconButton() + { + IconPlacementProperty.Changed.AddClassHandler((o, e) => + { + o.SetPlacement(e.NewValue.Value); + }); + } + + protected override void OnApplyTemplate(TemplateAppliedEventArgs e) + { + base.OnApplyTemplate(e); + SetPlacement(IconPlacement); + } + + private void SetPlacement(IconPlacement placement) + { + PseudoClasses.Set(PC_Right, placement == IconPlacement.Right); + } } \ No newline at end of file