feat: replace PathIcon with Button.

This commit is contained in:
Zhang Dian
2025-06-02 02:12:24 +08:00
parent 7f160489ff
commit b81d6c69a7
2 changed files with 13 additions and 30 deletions

View File

@@ -124,16 +124,17 @@
BorderThickness="{TemplateBinding BorderThickness}" BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="3"> CornerRadius="3">
<DockPanel LastChildFill="True"> <DockPanel LastChildFill="True">
<PathIcon <Button
Name="{x:Static u:ClosableTag.PART_CloseButton}" Name="{x:Static u:ClosableTag.PART_CloseButton}"
Theme="{StaticResource InnerPathIcon}" Theme="{DynamicResource InnerIconButton}"
Classes="Small"
Margin="4,0,0,0"
Background="Transparent"
Data="{DynamicResource ClosableTagCloseIconGlyph}"
DockPanel.Dock="Right" DockPanel.Dock="Right"
Width="12"
Height="12"
Margin="4,0,0,0"
Foreground="{DynamicResource SemiColorText2}" Foreground="{DynamicResource SemiColorText2}"
Cursor="Hand" /> Command="{TemplateBinding Command}"
CommandParameter="{TemplateBinding}"
Content="{StaticResource ClosableTagCloseIconGlyph}" />
<ContentPresenter <ContentPresenter
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}" Content="{TemplateBinding Content}"
@@ -145,10 +146,10 @@
</Border> </Border>
</ControlTemplate> </ControlTemplate>
</Setter> </Setter>
<Style Selector="^ /template/ PathIcon#PART_CloseButton:pointerover"> <Style Selector="^ /template/ Button#PART_CloseButton:pointerover">
<Setter Property="Foreground" Value="{DynamicResource SemiColorText1}" /> <Setter Property="Foreground" Value="{DynamicResource SemiColorText1}" />
</Style> </Style>
<Style Selector="^ /template/ PathIcon#PART_CloseButton:pressed"> <Style Selector="^ /template/ Button#PART_CloseButton:pressed">
<Setter Property="Foreground" Value="{DynamicResource SemiColorText0}" /> <Setter Property="Foreground" Value="{DynamicResource SemiColorText0}" />
</Style> </Style>
</ControlTheme> </ControlTheme>

View File

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