Merge pull request #674 from irihitech/badge
Relax TemplatePart usage of Badge
This commit is contained in:
@@ -8,59 +8,47 @@ using Ursa.Common;
|
|||||||
|
|
||||||
namespace Ursa.Controls;
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
[TemplatePart(PART_BadgeContainer, typeof(Border))]
|
/// <summary>
|
||||||
|
/// Represents a Badge control that can display a notification indicator or count on top of other content.
|
||||||
|
/// </summary>
|
||||||
|
[TemplatePart(PART_BadgeContainer, typeof(Control))]
|
||||||
public class Badge : HeaderedContentControl
|
public class Badge : HeaderedContentControl
|
||||||
{
|
{
|
||||||
public const string PART_ContentPresenter = "PART_ContentPresenter";
|
public const string PART_ContentPresenter = "PART_ContentPresenter";
|
||||||
public const string PART_BadgeContainer = "PART_BadgeContainer";
|
public const string PART_BadgeContainer = "PART_BadgeContainer";
|
||||||
public const string PART_HeaderPresenter = "PART_HeaderPresenter";
|
public const string PART_HeaderPresenter = "PART_HeaderPresenter";
|
||||||
|
|
||||||
private Border? _badgeContainer;
|
/// <summary>
|
||||||
|
/// Defines the theme applied to the badge.
|
||||||
|
/// </summary>
|
||||||
public static readonly StyledProperty<ControlTheme> BadgeThemeProperty =
|
public static readonly StyledProperty<ControlTheme> BadgeThemeProperty =
|
||||||
AvaloniaProperty.Register<Badge, ControlTheme>(nameof(BadgeTheme));
|
AvaloniaProperty.Register<Badge, ControlTheme>(nameof(BadgeTheme));
|
||||||
|
|
||||||
public ControlTheme BadgeTheme
|
/// <summary>
|
||||||
{
|
/// Defines whether the badge should be displayed as a dot.
|
||||||
get => GetValue(BadgeThemeProperty);
|
/// </summary>
|
||||||
set => SetValue(BadgeThemeProperty, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<bool> DotProperty =
|
public static readonly StyledProperty<bool> DotProperty =
|
||||||
AvaloniaProperty.Register<Badge, bool>(nameof(Dot));
|
AvaloniaProperty.Register<Badge, bool>(nameof(Dot));
|
||||||
|
|
||||||
public bool Dot
|
/// <summary>
|
||||||
{
|
/// Defines the corner position where the badge should be displayed.
|
||||||
get => GetValue(DotProperty);
|
/// </summary>
|
||||||
set => SetValue(DotProperty, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<CornerPosition> CornerPositionProperty =
|
public static readonly StyledProperty<CornerPosition> CornerPositionProperty =
|
||||||
AvaloniaProperty.Register<Badge, CornerPosition>(nameof(CornerPosition));
|
AvaloniaProperty.Register<Badge, CornerPosition>(nameof(CornerPosition));
|
||||||
|
|
||||||
public CornerPosition CornerPosition
|
/// <summary>
|
||||||
{
|
/// Defines the maximum count to display before showing overflow indicator.
|
||||||
get => GetValue(CornerPositionProperty);
|
/// </summary>
|
||||||
set => SetValue(CornerPositionProperty, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<int> OverflowCountProperty =
|
public static readonly StyledProperty<int> OverflowCountProperty =
|
||||||
AvaloniaProperty.Register<Badge, int>(nameof(OverflowCount));
|
AvaloniaProperty.Register<Badge, int>(nameof(OverflowCount));
|
||||||
|
|
||||||
public int OverflowCount
|
/// <summary>
|
||||||
{
|
/// Defines the font size of the badge text.
|
||||||
get => GetValue(OverflowCountProperty);
|
/// </summary>
|
||||||
set => SetValue(OverflowCountProperty, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<double> BadgeFontSizeProperty =
|
public static readonly StyledProperty<double> BadgeFontSizeProperty =
|
||||||
AvaloniaProperty.Register<Badge, double>(nameof(BadgeFontSize));
|
AvaloniaProperty.Register<Badge, double>(nameof(BadgeFontSize));
|
||||||
|
|
||||||
public double BadgeFontSize
|
private Control? _badgeContainer;
|
||||||
{
|
|
||||||
get => GetValue(BadgeFontSizeProperty);
|
|
||||||
set => SetValue(BadgeFontSizeProperty, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
static Badge()
|
static Badge()
|
||||||
{
|
{
|
||||||
@@ -68,22 +56,76 @@ public class Badge : HeaderedContentControl
|
|||||||
DotProperty.Changed.AddClassHandler<Badge>((badge, _) => badge.UpdateBadgePosition());
|
DotProperty.Changed.AddClassHandler<Badge>((badge, _) => badge.UpdateBadgePosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the theme applied to the badge.
|
||||||
|
/// </summary>
|
||||||
|
public ControlTheme BadgeTheme
|
||||||
|
{
|
||||||
|
get => GetValue(BadgeThemeProperty);
|
||||||
|
set => SetValue(BadgeThemeProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a value indicating whether the badge should be displayed as a dot.
|
||||||
|
/// </summary>
|
||||||
|
public bool Dot
|
||||||
|
{
|
||||||
|
get => GetValue(DotProperty);
|
||||||
|
set => SetValue(DotProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the corner position where the badge should be displayed.
|
||||||
|
/// </summary>
|
||||||
|
public CornerPosition CornerPosition
|
||||||
|
{
|
||||||
|
get => GetValue(CornerPositionProperty);
|
||||||
|
set => SetValue(CornerPositionProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the maximum count to display before showing overflow indicator.
|
||||||
|
/// </summary>
|
||||||
|
public int OverflowCount
|
||||||
|
{
|
||||||
|
get => GetValue(OverflowCountProperty);
|
||||||
|
set => SetValue(OverflowCountProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the font size of the badge text.
|
||||||
|
/// </summary>
|
||||||
|
public double BadgeFontSize
|
||||||
|
{
|
||||||
|
get => GetValue(BadgeFontSizeProperty);
|
||||||
|
set => SetValue(BadgeFontSizeProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||||
{
|
{
|
||||||
_badgeContainer?.RemoveHandler(SizeChangedEvent, OnBadgeSizeChanged);
|
_badgeContainer?.RemoveHandler(SizeChangedEvent, OnBadgeSizeChanged);
|
||||||
base.OnApplyTemplate(e);
|
base.OnApplyTemplate(e);
|
||||||
_badgeContainer = e.NameScope.Find<Border>(PART_BadgeContainer);
|
_badgeContainer = e.NameScope.Find<Control>(PART_BadgeContainer);
|
||||||
_badgeContainer?.AddHandler(SizeChangedEvent, OnBadgeSizeChanged);
|
_badgeContainer?.AddHandler(SizeChangedEvent, OnBadgeSizeChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnBadgeSizeChanged(object? sender, SizeChangedEventArgs e) => UpdateBadgePosition();
|
/// <summary>
|
||||||
|
/// Handles the size changed event of the badge container and updates its position.
|
||||||
|
/// </summary>
|
||||||
|
private void OnBadgeSizeChanged(object? sender, SizeChangedEventArgs e)
|
||||||
|
{
|
||||||
|
UpdateBadgePosition();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates the badge position based on the current corner position and size.
|
||||||
|
/// </summary>
|
||||||
private void UpdateBadgePosition()
|
private void UpdateBadgePosition()
|
||||||
{
|
{
|
||||||
var vertical = CornerPosition is CornerPosition.BottomLeft or CornerPosition.BottomRight ? 1 : -1;
|
var vertical = CornerPosition is CornerPosition.BottomLeft or CornerPosition.BottomRight ? 1 : -1;
|
||||||
var horizontal = CornerPosition is CornerPosition.TopRight 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)
|
if (_badgeContainer is not null && Presenter?.Child is not null)
|
||||||
{
|
|
||||||
_badgeContainer.RenderTransform = new TransformGroup
|
_badgeContainer.RenderTransform = new TransformGroup
|
||||||
{
|
{
|
||||||
Children =
|
Children =
|
||||||
@@ -93,6 +135,5 @@ public class Badge : HeaderedContentControl
|
|||||||
vertical * _badgeContainer.Bounds.Height / 2)
|
vertical * _badgeContainer.Bounds.Height / 2)
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user