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

@@ -2,6 +2,9 @@
xmlns="https://github.com/avaloniaui" xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:u="https://irihi.tech/ursa"> xmlns:u="https://irihi.tech/ursa">
<Design.PreviewWith>
<u:ClosableTag Content="Closable Tag"/>
</Design.PreviewWith>
<ControlTheme x:Key="{x:Type u:TagInput}" TargetType="u:TagInput"> <ControlTheme x:Key="{x:Type u:TagInput}" TargetType="u:TagInput">
<Setter Property="InputTheme" Value="{DynamicResource TagInputTextBoxTheme}" /> <Setter Property="InputTheme" Value="{DynamicResource TagInputTextBoxTheme}" />
<Setter Property="HorizontalAlignment" Value="Stretch" /> <Setter Property="HorizontalAlignment" Value="Stretch" />
@@ -115,7 +118,7 @@
<ControlTemplate TargetType="u:ClosableTag"> <ControlTemplate TargetType="u:ClosableTag">
<Border <Border
Margin="1" Margin="1"
Padding="4,2" Padding="8,4,4,4"
Background="{TemplateBinding Background}" Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" BorderThickness="{TemplateBinding BorderThickness}"
@@ -125,11 +128,12 @@
Name="{x:Static u:ClosableTag.PART_CloseButton}" Name="{x:Static u:ClosableTag.PART_CloseButton}"
Theme="{StaticResource InnerPathIcon}" Theme="{StaticResource InnerPathIcon}"
Classes="Small" Classes="Small"
Margin="4,0" Margin="4,0,0,0"
Background="Transparent" Background="Transparent"
Data="{DynamicResource ClosableTagCloseIconGlyph}" Data="{DynamicResource ClosableTagCloseIconGlyph}"
DockPanel.Dock="Right" DockPanel.Dock="Right"
Foreground="{TemplateBinding Foreground}" /> Foreground="{DynamicResource SemiColorText2}"
Cursor="Hand" />
<ContentPresenter <ContentPresenter
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}" Content="{TemplateBinding Content}"
@@ -141,5 +145,11 @@
</Border> </Border>
</ControlTemplate> </ControlTemplate>
</Setter> </Setter>
<Style Selector="^ /template/ PathIcon#PART_CloseButton:pointerover">
<Setter Property="Foreground" Value="{DynamicResource SemiColorText1}" />
</Style>
<Style Selector="^ /template/ PathIcon#PART_CloseButton:pressed">
<Setter Property="Foreground" Value="{DynamicResource SemiColorText0}" />
</Style>
</ControlTheme> </ControlTheme>
</ResourceDictionary> </ResourceDictionary>

View File

@@ -8,10 +8,11 @@ using Avalonia.Input;
namespace Ursa.Controls; namespace Ursa.Controls;
[TemplatePart(PART_CloseButton, typeof(PathIcon))] [TemplatePart(PART_CloseButton, typeof(PathIcon))]
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 PathIcon? _icon;
public static readonly StyledProperty<ICommand?> CommandProperty = AvaloniaProperty.Register<ClosableTag, ICommand?>( public static readonly StyledProperty<ICommand?> CommandProperty = AvaloniaProperty.Register<ClosableTag, ICommand?>(
nameof(Command)); nameof(Command));
@@ -24,21 +25,21 @@ public class ClosableTag: ContentControl
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
base.OnApplyTemplate(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); _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); Command.Execute(this);
} }