feat: remove Template when Dot=True, add Badge Tests, format Badge class.

This commit is contained in:
Zhang Dian
2025-01-16 18:31:03 +08:00
parent 7ff0e63e4d
commit 33bfd67833
3 changed files with 115 additions and 44 deletions

View File

@@ -10,7 +10,7 @@ using Ursa.Common;
namespace Ursa.Controls;
[TemplatePart(PART_BadgeContainer, typeof(Border))]
public class Badge: HeaderedContentControl
public class Badge : HeaderedContentControl
{
public const string PART_ContentPresenter = "PART_ContentPresenter";
public const string PART_BadgeContainer = "PART_BadgeContainer";
@@ -18,40 +18,44 @@ public class Badge: HeaderedContentControl
private Border? _badgeContainer;
public static readonly StyledProperty<ControlTheme> BadgeThemeProperty = AvaloniaProperty.Register<Badge, ControlTheme>(
nameof(BadgeTheme));
public static readonly StyledProperty<ControlTheme> BadgeThemeProperty =
AvaloniaProperty.Register<Badge, ControlTheme>(nameof(BadgeTheme));
public ControlTheme BadgeTheme
{
get => GetValue(BadgeThemeProperty);
set => SetValue(BadgeThemeProperty, value);
}
public static readonly StyledProperty<bool> DotProperty = AvaloniaProperty.Register<Badge, bool>(
nameof(Dot));
public static readonly StyledProperty<bool> DotProperty =
AvaloniaProperty.Register<Badge, bool>(nameof(Dot));
public bool Dot
{
get => GetValue(DotProperty);
set => SetValue(DotProperty, value);
}
public static readonly StyledProperty<CornerPosition> CornerPositionProperty = AvaloniaProperty.Register<Badge, CornerPosition>(
nameof(CornerPosition));
public static readonly StyledProperty<CornerPosition> CornerPositionProperty =
AvaloniaProperty.Register<Badge, CornerPosition>(nameof(CornerPosition));
public CornerPosition CornerPosition
{
get => GetValue(CornerPositionProperty);
set => SetValue(CornerPositionProperty, value);
}
public static readonly StyledProperty<int> OverflowCountProperty = AvaloniaProperty.Register<Badge, int>(
nameof(OverflowCount));
public static readonly StyledProperty<int> OverflowCountProperty =
AvaloniaProperty.Register<Badge, int>(nameof(OverflowCount));
public int OverflowCount
{
get => GetValue(OverflowCountProperty);
set => SetValue(OverflowCountProperty, value);
}
public static readonly StyledProperty<double> BadgeFontSizeProperty = AvaloniaProperty.Register<Badge, double>(
nameof(BadgeFontSize));
public static readonly StyledProperty<double> BadgeFontSizeProperty =
AvaloniaProperty.Register<Badge, double>(nameof(BadgeFontSize));
public double BadgeFontSize
{
@@ -62,6 +66,7 @@ public class Badge: HeaderedContentControl
static Badge()
{
HeaderProperty.Changed.AddClassHandler<Badge>((badge, _) => badge.UpdateBadgePosition());
DotProperty.Changed.AddClassHandler<Badge>((badge, _) => badge.UpdateBadgePosition());
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
@@ -88,12 +93,14 @@ public class Badge: HeaderedContentControl
var horizontal = CornerPosition is CornerPosition.TopRight or CornerPosition.BottomRight ? 1 : -1;
if (_badgeContainer is not null && Presenter?.Child is not null)
{
_badgeContainer.RenderTransform = new TransformGroup()
_badgeContainer.RenderTransform = new TransformGroup
{
Children = new Transforms()
{
new TranslateTransform(horizontal*_badgeContainer.Bounds.Width/2,vertical*_badgeContainer.Bounds.Height/2)
}
Children =
[
new TranslateTransform(
horizontal * _badgeContainer.Bounds.Width / 2,
vertical * _badgeContainer.Bounds.Height / 2)
]
};
}
}