From c21a1d159d6c907825ba8f20919ce3b73e4d22a0 Mon Sep 17 00:00:00 2001 From: Dong Bin Date: Tue, 20 May 2025 20:26:19 +0800 Subject: [PATCH 1/3] feat: relax template part. --- src/Ursa/Controls/Badge/Badge.cs | 73 ++++++++++++++++---------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/src/Ursa/Controls/Badge/Badge.cs b/src/Ursa/Controls/Badge/Badge.cs index fe3f26e..5a583ee 100644 --- a/src/Ursa/Controls/Badge/Badge.cs +++ b/src/Ursa/Controls/Badge/Badge.cs @@ -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 BadgeThemeProperty = AvaloniaProperty.Register(nameof(BadgeTheme)); - public ControlTheme BadgeTheme - { - get => GetValue(BadgeThemeProperty); - set => SetValue(BadgeThemeProperty, value); - } - public static readonly StyledProperty DotProperty = AvaloniaProperty.Register(nameof(Dot)); - public bool Dot - { - get => GetValue(DotProperty); - set => SetValue(DotProperty, value); - } - public static readonly StyledProperty CornerPositionProperty = AvaloniaProperty.Register(nameof(CornerPosition)); - public CornerPosition CornerPosition - { - get => GetValue(CornerPositionProperty); - set => SetValue(CornerPositionProperty, value); - } - public static readonly StyledProperty OverflowCountProperty = AvaloniaProperty.Register(nameof(OverflowCount)); - public int OverflowCount - { - get => GetValue(OverflowCountProperty); - set => SetValue(OverflowCountProperty, value); - } - public static readonly StyledProperty BadgeFontSizeProperty = AvaloniaProperty.Register(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.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(PART_BadgeContainer); + _badgeContainer = e.NameScope.Find(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) ] }; - } } } \ No newline at end of file From cda52829c7f1c6b78d16aca90dc2c49de184a471 Mon Sep 17 00:00:00 2001 From: Dong Bin Date: Tue, 20 May 2025 20:28:45 +0800 Subject: [PATCH 2/3] feat: add documentation. --- src/Ursa/Controls/Badge/Badge.cs | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/Ursa/Controls/Badge/Badge.cs b/src/Ursa/Controls/Badge/Badge.cs index 5a583ee..51bf964 100644 --- a/src/Ursa/Controls/Badge/Badge.cs +++ b/src/Ursa/Controls/Badge/Badge.cs @@ -8,6 +8,9 @@ using Ursa.Common; namespace Ursa.Controls; +/// +/// Represents a Badge control that can display a notification indicator or count on top of other content. +/// [TemplatePart(PART_BadgeContainer, typeof(Control))] public class Badge : HeaderedContentControl { @@ -15,18 +18,33 @@ public class Badge : HeaderedContentControl public const string PART_BadgeContainer = "PART_BadgeContainer"; public const string PART_HeaderPresenter = "PART_HeaderPresenter"; + /// + /// Defines the theme applied to the badge. + /// public static readonly StyledProperty BadgeThemeProperty = AvaloniaProperty.Register(nameof(BadgeTheme)); + /// + /// Defines whether the badge should be displayed as a dot. + /// public static readonly StyledProperty DotProperty = AvaloniaProperty.Register(nameof(Dot)); + /// + /// Defines the corner position where the badge should be displayed. + /// public static readonly StyledProperty CornerPositionProperty = AvaloniaProperty.Register(nameof(CornerPosition)); + /// + /// Defines the maximum count to display before showing overflow indicator. + /// public static readonly StyledProperty OverflowCountProperty = AvaloniaProperty.Register(nameof(OverflowCount)); + /// + /// Defines the font size of the badge text. + /// public static readonly StyledProperty BadgeFontSizeProperty = AvaloniaProperty.Register(nameof(BadgeFontSize)); @@ -38,36 +56,52 @@ public class Badge : HeaderedContentControl DotProperty.Changed.AddClassHandler((badge, _) => badge.UpdateBadgePosition()); } + /// + /// Gets or sets the theme applied to the badge. + /// public ControlTheme BadgeTheme { get => GetValue(BadgeThemeProperty); set => SetValue(BadgeThemeProperty, value); } + /// + /// Gets or sets a value indicating whether the badge should be displayed as a dot. + /// public bool Dot { get => GetValue(DotProperty); set => SetValue(DotProperty, value); } + /// + /// Gets or sets the corner position where the badge should be displayed. + /// public CornerPosition CornerPosition { get => GetValue(CornerPositionProperty); set => SetValue(CornerPositionProperty, value); } + /// + /// Gets or sets the maximum count to display before showing overflow indicator. + /// public int OverflowCount { get => GetValue(OverflowCountProperty); set => SetValue(OverflowCountProperty, value); } + /// + /// Gets or sets the font size of the badge text. + /// public double BadgeFontSize { get => GetValue(BadgeFontSizeProperty); set => SetValue(BadgeFontSizeProperty, value); } + /// protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { _badgeContainer?.RemoveHandler(SizeChangedEvent, OnBadgeSizeChanged); @@ -76,11 +110,17 @@ public class Badge : HeaderedContentControl _badgeContainer?.AddHandler(SizeChangedEvent, OnBadgeSizeChanged); } + /// + /// Handles the size changed event of the badge container and updates its position. + /// private void OnBadgeSizeChanged(object? sender, SizeChangedEventArgs e) { UpdateBadgePosition(); } + /// + /// Updates the badge position based on the current corner position and size. + /// private void UpdateBadgePosition() { var vertical = CornerPosition is CornerPosition.BottomLeft or CornerPosition.BottomRight ? 1 : -1; From 5128b03851259763696e9acaf1b2f189060f383b Mon Sep 17 00:00:00 2001 From: Dong Bin <14807942+rabbitism@users.noreply.github.com> Date: Tue, 20 May 2025 20:32:50 +0800 Subject: [PATCH 3/3] Fix typo Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/Ursa/Controls/Badge/Badge.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Ursa/Controls/Badge/Badge.cs b/src/Ursa/Controls/Badge/Badge.cs index 51bf964..7a5a8f0 100644 --- a/src/Ursa/Controls/Badge/Badge.cs +++ b/src/Ursa/Controls/Badge/Badge.cs @@ -101,7 +101,7 @@ public class Badge : HeaderedContentControl set => SetValue(BadgeFontSizeProperty, value); } - /// + /// protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { _badgeContainer?.RemoveHandler(SizeChangedEvent, OnBadgeSizeChanged);