fix: update ClosableTag behavior and styling.

This commit is contained in:
Zhang Dian
2025-06-01 00:06:17 +08:00
parent 7805e04af4
commit 7f160489ff
2 changed files with 23 additions and 12 deletions

View File

@@ -8,10 +8,11 @@ using Avalonia.Input;
namespace Ursa.Controls;
[TemplatePart(PART_CloseButton, typeof(PathIcon))]
public class ClosableTag: ContentControl
public class ClosableTag : ContentControl
{
public const string PART_CloseButton = "PART_CloseButton";
private PathIcon? _icon;
public static readonly StyledProperty<ICommand?> CommandProperty = AvaloniaProperty.Register<ClosableTag, ICommand?>(
nameof(Command));
@@ -24,21 +25,21 @@ public class ClosableTag: ContentControl
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
if (_icon != null)
if (_icon is not null)
{
_icon.PointerPressed -= OnPointerPressed;
_icon.PointerReleased -= OnPointerReleased;
}
_icon = e.NameScope.Find<PathIcon>(PART_CloseButton);
if (_icon != null)
if (_icon is not null)
{
_icon.PointerPressed += OnPointerPressed;
_icon.PointerReleased += OnPointerReleased;
}
}
private void OnPointerPressed(object? sender, PointerPressedEventArgs args)
private void OnPointerReleased(object? sender, PointerReleasedEventArgs args)
{
if (Command != null && Command.CanExecute(null))
if (Command is not null && Command.CanExecute(null))
{
Command.Execute(this);
}