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

@@ -66,10 +66,15 @@
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}"
IsVisible="{Binding Header, RelativeSource={RelativeSource TemplatedParent}, Converter={x:Static ObjectConverters.IsNotNull}}"
RenderTransformOrigin=".5,.5"
Theme="{TemplateBinding BadgeTheme}"
UseLayoutRounding="False">
<Border.IsVisible>
<MultiBinding Converter="{x:Static BoolConverters.Or}">
<Binding Path="Header" RelativeSource="{RelativeSource TemplatedParent}" Converter="{x:Static ObjectConverters.IsNotNull}" />
<Binding Path="Dot" RelativeSource="{RelativeSource TemplatedParent}" />
</MultiBinding>
</Border.IsVisible>
<ContentPresenter
Name="{x:Static u:Badge.PART_HeaderPresenter}"
HorizontalAlignment="Center"
@@ -90,33 +95,13 @@
</ControlTemplate>
</Setter>
<Style Selector="^[Dot=True]">
<Setter Property="Template">
<ControlTemplate TargetType="{x:Type u:Badge}">
<Panel
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
VerticalAlignment="{TemplateBinding VerticalAlignment}"
ClipToBounds="False">
<ContentPresenter
Name="{x:Static u:Badge.PART_ContentPresenter}"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
VerticalAlignment="{TemplateBinding VerticalAlignment}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Foreground="{DynamicResource BadgeContentForeground}" />
<Border
Name="{x:Static u:Badge.PART_BadgeContainer}"
Width="{DynamicResource BadgeDotWidth}"
Height="{DynamicResource BadgeDotHeight}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}"
RenderTransformOrigin=".5,.5" />
</Panel>
</ControlTemplate>
</Setter>
<Style Selector="^[Dot=True] /template/ Border#PART_BadgeContainer">
<Setter Property="Padding" Value="0" />
<Setter Property="MinWidth" Value="{DynamicResource BadgeDotWidth}" />
<Setter Property="MinHeight" Value="{DynamicResource BadgeDotHeight}" />
</Style>
<Style Selector="^[Dot=True] /template/ ContentPresenter#PART_HeaderPresenter">
<Setter Property="IsVisible" Value="False" />
</Style>
<Style Selector="^[CornerPosition=TopLeft] /template/ Border#PART_BadgeContainer">

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)
]
};
}
}