feat: add placement.

This commit is contained in:
rabbitism
2024-01-06 20:44:01 +08:00
parent e5e7f020e3
commit 1312ab6128
4 changed files with 70 additions and 7 deletions

View File

@@ -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<object?> IconProperty = AvaloniaProperty.Register<IconButton, object?>(
nameof(Icon));
@@ -32,4 +38,32 @@ public class IconButton: Button
get => GetValue(IsLoadingProperty);
set => SetValue(IsLoadingProperty, value);
}
public static readonly StyledProperty<IconPlacement> IconPlacementProperty = AvaloniaProperty.Register<IconButton, IconPlacement>(
nameof(IconPlacement), defaultValue: IconPlacement.Left);
public IconPlacement IconPlacement
{
get => GetValue(IconPlacementProperty);
set => SetValue(IconPlacementProperty, value);
}
static IconButton()
{
IconPlacementProperty.Changed.AddClassHandler<IconButton, IconPlacement>((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);
}
}