feat: relax template part.
This commit is contained in:
@@ -8,59 +8,29 @@ using Ursa.Common;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
[TemplatePart(PART_BadgeContainer, typeof(Border))]
|
||||
[TemplatePart(PART_BadgeContainer, typeof(Control))]
|
||||
public class Badge : HeaderedContentControl
|
||||
{
|
||||
public const string PART_ContentPresenter = "PART_ContentPresenter";
|
||||
public const string PART_BadgeContainer = "PART_BadgeContainer";
|
||||
public const string PART_HeaderPresenter = "PART_HeaderPresenter";
|
||||
|
||||
private Border? _badgeContainer;
|
||||
|
||||
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 bool Dot
|
||||
{
|
||||
get => GetValue(DotProperty);
|
||||
set => SetValue(DotProperty, value);
|
||||
}
|
||||
|
||||
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 int OverflowCount
|
||||
{
|
||||
get => GetValue(OverflowCountProperty);
|
||||
set => SetValue(OverflowCountProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<double> BadgeFontSizeProperty =
|
||||
AvaloniaProperty.Register<Badge, double>(nameof(BadgeFontSize));
|
||||
|
||||
public double BadgeFontSize
|
||||
{
|
||||
get => GetValue(BadgeFontSizeProperty);
|
||||
set => SetValue(BadgeFontSizeProperty, value);
|
||||
}
|
||||
private Control? _badgeContainer;
|
||||
|
||||
static Badge()
|
||||
{
|
||||
@@ -68,22 +38,54 @@ public class Badge : HeaderedContentControl
|
||||
DotProperty.Changed.AddClassHandler<Badge>((badge, _) => badge.UpdateBadgePosition());
|
||||
}
|
||||
|
||||
public ControlTheme BadgeTheme
|
||||
{
|
||||
get => GetValue(BadgeThemeProperty);
|
||||
set => SetValue(BadgeThemeProperty, value);
|
||||
}
|
||||
|
||||
public bool Dot
|
||||
{
|
||||
get => GetValue(DotProperty);
|
||||
set => SetValue(DotProperty, value);
|
||||
}
|
||||
|
||||
public CornerPosition CornerPosition
|
||||
{
|
||||
get => GetValue(CornerPositionProperty);
|
||||
set => SetValue(CornerPositionProperty, value);
|
||||
}
|
||||
|
||||
public int OverflowCount
|
||||
{
|
||||
get => GetValue(OverflowCountProperty);
|
||||
set => SetValue(OverflowCountProperty, value);
|
||||
}
|
||||
|
||||
public double BadgeFontSize
|
||||
{
|
||||
get => GetValue(BadgeFontSizeProperty);
|
||||
set => SetValue(BadgeFontSizeProperty, value);
|
||||
}
|
||||
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
_badgeContainer?.RemoveHandler(SizeChangedEvent, OnBadgeSizeChanged);
|
||||
base.OnApplyTemplate(e);
|
||||
_badgeContainer = e.NameScope.Find<Border>(PART_BadgeContainer);
|
||||
_badgeContainer = e.NameScope.Find<Control>(PART_BadgeContainer);
|
||||
_badgeContainer?.AddHandler(SizeChangedEvent, OnBadgeSizeChanged);
|
||||
}
|
||||
|
||||
private void OnBadgeSizeChanged(object? sender, SizeChangedEventArgs e) => UpdateBadgePosition();
|
||||
private void OnBadgeSizeChanged(object? sender, SizeChangedEventArgs e)
|
||||
{
|
||||
UpdateBadgePosition();
|
||||
}
|
||||
|
||||
private void UpdateBadgePosition()
|
||||
{
|
||||
var vertical = CornerPosition is CornerPosition.BottomLeft or CornerPosition.BottomRight ? 1 : -1;
|
||||
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
|
||||
{
|
||||
Children =
|
||||
@@ -93,6 +95,5 @@ public class Badge : HeaderedContentControl
|
||||
vertical * _badgeContainer.Bounds.Height / 2)
|
||||
]
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user