feat: remove Template when Dot=True, add Badge Tests, format Badge class.
This commit is contained in:
@@ -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">
|
||||
|
||||
@@ -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)
|
||||
]
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
79
tests/HeadlessTest.Ursa/Controls/BadgeTests/BadgeTests.cs
Normal file
79
tests/HeadlessTest.Ursa/Controls/BadgeTests/BadgeTests.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Presenters;
|
||||
using Avalonia.Controls.Templates;
|
||||
using Avalonia.Headless.XUnit;
|
||||
using UrsaControls = Ursa.Controls;
|
||||
|
||||
namespace HeadlessTest.Ursa.Controls.BadgeTests;
|
||||
|
||||
public class BadgeTests
|
||||
{
|
||||
[AvaloniaFact]
|
||||
public void Badge_Container_Should_DisAppear_If_Header_Is_Null_And_Dot_Equals_False()
|
||||
{
|
||||
// Arrange
|
||||
var window = new Window();
|
||||
var badge = new UrsaControls.Badge
|
||||
{
|
||||
Header = string.Empty,
|
||||
Dot = true
|
||||
};
|
||||
window.Content = badge;
|
||||
window.Show();
|
||||
|
||||
Assert.True(badge.IsVisible);
|
||||
|
||||
var badgeContainer = badge.GetTemplateChildren().OfType<Border>()
|
||||
.FirstOrDefault(a => a.Name == UrsaControls.Badge.PART_BadgeContainer);
|
||||
|
||||
Assert.NotNull(badgeContainer);
|
||||
|
||||
Assert.True(badgeContainer.IsVisible);
|
||||
|
||||
// Act
|
||||
badge.Header = null;
|
||||
// Assert
|
||||
Assert.True(badgeContainer.IsVisible);
|
||||
|
||||
// Act
|
||||
badge.Dot = false;
|
||||
// Assert
|
||||
Assert.False(badgeContainer.IsVisible);
|
||||
}
|
||||
|
||||
[AvaloniaFact]
|
||||
public void Badge_Container_Should_Overflow_If_Number_Larger_Than_OverflowCount()
|
||||
{
|
||||
// Arrange
|
||||
var window = new Window();
|
||||
var badge = new UrsaControls.Badge
|
||||
{
|
||||
Header = 0,
|
||||
OverflowCount = 10
|
||||
};
|
||||
window.Content = badge;
|
||||
window.Show();
|
||||
|
||||
Assert.True(badge.IsVisible);
|
||||
|
||||
var header = badge.GetTemplateChildren().OfType<ContentPresenter>()
|
||||
.FirstOrDefault(a => a.Name == UrsaControls.Badge.PART_HeaderPresenter);
|
||||
|
||||
Assert.NotNull(header);
|
||||
Assert.Equal(header.Content, 0);
|
||||
|
||||
// Act
|
||||
badge.Header = 10;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(header.Content, 10);
|
||||
|
||||
// Act
|
||||
badge.Header = 11;
|
||||
Assert.Equal(header.Content, "10+");
|
||||
|
||||
// Act
|
||||
badge.OverflowCount = 100;
|
||||
Assert.Equal(header.Content, 11);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user