feat: remove Template when Dot=True, add Badge Tests, format Badge class.
This commit is contained in:
@@ -66,10 +66,15 @@
|
|||||||
BorderBrush="{TemplateBinding BorderBrush}"
|
BorderBrush="{TemplateBinding BorderBrush}"
|
||||||
BorderThickness="{TemplateBinding BorderThickness}"
|
BorderThickness="{TemplateBinding BorderThickness}"
|
||||||
CornerRadius="{TemplateBinding CornerRadius}"
|
CornerRadius="{TemplateBinding CornerRadius}"
|
||||||
IsVisible="{Binding Header, RelativeSource={RelativeSource TemplatedParent}, Converter={x:Static ObjectConverters.IsNotNull}}"
|
|
||||||
RenderTransformOrigin=".5,.5"
|
RenderTransformOrigin=".5,.5"
|
||||||
Theme="{TemplateBinding BadgeTheme}"
|
Theme="{TemplateBinding BadgeTheme}"
|
||||||
UseLayoutRounding="False">
|
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
|
<ContentPresenter
|
||||||
Name="{x:Static u:Badge.PART_HeaderPresenter}"
|
Name="{x:Static u:Badge.PART_HeaderPresenter}"
|
||||||
HorizontalAlignment="Center"
|
HorizontalAlignment="Center"
|
||||||
@@ -90,33 +95,13 @@
|
|||||||
</ControlTemplate>
|
</ControlTemplate>
|
||||||
</Setter>
|
</Setter>
|
||||||
|
|
||||||
<Style Selector="^[Dot=True]">
|
<Style Selector="^[Dot=True] /template/ Border#PART_BadgeContainer">
|
||||||
<Setter Property="Template">
|
<Setter Property="Padding" Value="0" />
|
||||||
<ControlTemplate TargetType="{x:Type u:Badge}">
|
<Setter Property="MinWidth" Value="{DynamicResource BadgeDotWidth}" />
|
||||||
<Panel
|
<Setter Property="MinHeight" Value="{DynamicResource BadgeDotHeight}" />
|
||||||
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
|
</Style>
|
||||||
VerticalAlignment="{TemplateBinding VerticalAlignment}"
|
<Style Selector="^[Dot=True] /template/ ContentPresenter#PART_HeaderPresenter">
|
||||||
ClipToBounds="False">
|
<Setter Property="IsVisible" Value="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>
|
</Style>
|
||||||
|
|
||||||
<Style Selector="^[CornerPosition=TopLeft] /template/ Border#PART_BadgeContainer">
|
<Style Selector="^[CornerPosition=TopLeft] /template/ Border#PART_BadgeContainer">
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ using Ursa.Common;
|
|||||||
namespace Ursa.Controls;
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
[TemplatePart(PART_BadgeContainer, typeof(Border))]
|
[TemplatePart(PART_BadgeContainer, typeof(Border))]
|
||||||
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";
|
||||||
@@ -18,40 +18,44 @@ public class Badge: HeaderedContentControl
|
|||||||
|
|
||||||
private Border? _badgeContainer;
|
private Border? _badgeContainer;
|
||||||
|
|
||||||
public static readonly StyledProperty<ControlTheme> BadgeThemeProperty = AvaloniaProperty.Register<Badge, ControlTheme>(
|
public static readonly StyledProperty<ControlTheme> BadgeThemeProperty =
|
||||||
nameof(BadgeTheme));
|
AvaloniaProperty.Register<Badge, ControlTheme>(nameof(BadgeTheme));
|
||||||
|
|
||||||
public ControlTheme BadgeTheme
|
public ControlTheme BadgeTheme
|
||||||
{
|
{
|
||||||
get => GetValue(BadgeThemeProperty);
|
get => GetValue(BadgeThemeProperty);
|
||||||
set => SetValue(BadgeThemeProperty, value);
|
set => SetValue(BadgeThemeProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<bool> DotProperty = AvaloniaProperty.Register<Badge, bool>(
|
public static readonly StyledProperty<bool> DotProperty =
|
||||||
nameof(Dot));
|
AvaloniaProperty.Register<Badge, bool>(nameof(Dot));
|
||||||
|
|
||||||
public bool Dot
|
public bool Dot
|
||||||
{
|
{
|
||||||
get => GetValue(DotProperty);
|
get => GetValue(DotProperty);
|
||||||
set => SetValue(DotProperty, value);
|
set => SetValue(DotProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<CornerPosition> CornerPositionProperty = AvaloniaProperty.Register<Badge, CornerPosition>(
|
public static readonly StyledProperty<CornerPosition> CornerPositionProperty =
|
||||||
nameof(CornerPosition));
|
AvaloniaProperty.Register<Badge, CornerPosition>(nameof(CornerPosition));
|
||||||
|
|
||||||
public CornerPosition CornerPosition
|
public CornerPosition CornerPosition
|
||||||
{
|
{
|
||||||
get => GetValue(CornerPositionProperty);
|
get => GetValue(CornerPositionProperty);
|
||||||
set => SetValue(CornerPositionProperty, value);
|
set => SetValue(CornerPositionProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<int> OverflowCountProperty = AvaloniaProperty.Register<Badge, int>(
|
public static readonly StyledProperty<int> OverflowCountProperty =
|
||||||
nameof(OverflowCount));
|
AvaloniaProperty.Register<Badge, int>(nameof(OverflowCount));
|
||||||
|
|
||||||
public int OverflowCount
|
public int OverflowCount
|
||||||
{
|
{
|
||||||
get => GetValue(OverflowCountProperty);
|
get => GetValue(OverflowCountProperty);
|
||||||
set => SetValue(OverflowCountProperty, value);
|
set => SetValue(OverflowCountProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<double> BadgeFontSizeProperty = AvaloniaProperty.Register<Badge, double>(
|
public static readonly StyledProperty<double> BadgeFontSizeProperty =
|
||||||
nameof(BadgeFontSize));
|
AvaloniaProperty.Register<Badge, double>(nameof(BadgeFontSize));
|
||||||
|
|
||||||
public double BadgeFontSize
|
public double BadgeFontSize
|
||||||
{
|
{
|
||||||
@@ -62,6 +66,7 @@ public class Badge: HeaderedContentControl
|
|||||||
static Badge()
|
static Badge()
|
||||||
{
|
{
|
||||||
HeaderProperty.Changed.AddClassHandler<Badge>((badge, _) => badge.UpdateBadgePosition());
|
HeaderProperty.Changed.AddClassHandler<Badge>((badge, _) => badge.UpdateBadgePosition());
|
||||||
|
DotProperty.Changed.AddClassHandler<Badge>((badge, _) => badge.UpdateBadgePosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
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;
|
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 = new Transforms()
|
Children =
|
||||||
{
|
[
|
||||||
new TranslateTransform(horizontal*_badgeContainer.Bounds.Width/2,vertical*_badgeContainer.Bounds.Height/2)
|
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