Merge pull request #544 from irihitech/avatar

Enhance Avatar & Badge
This commit is contained in:
Dong Bin
2025-01-17 16:10:12 +08:00
committed by GitHub
12 changed files with 391 additions and 354 deletions

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

View File

@@ -3,18 +3,19 @@ using Avalonia.Data.Converters;
namespace Ursa.Converters;
public class BadgeContentOverflowConverter: IMultiValueConverter
public class BadgeContentOverflowConverter : IMultiValueConverter
{
public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
{
string overflowMark = parameter is string s ? s : "+";
if (double.TryParse(values[0]?.ToString(), out var b) && values[1] is int i and > 0)
var overflowMark = parameter as string ?? "+";
if (double.TryParse(values[0]?.ToString(), out var b) && values[1] is int count and > 0)
{
if (b > i)
if (b > count)
{
return i + overflowMark;
return $"{count}{overflowMark}";
}
}
return values[0];
}
}

View File

@@ -1,22 +0,0 @@
using System.Globalization;
using Avalonia.Data.Converters;
namespace Ursa.Converters;
public class DivideByTwoConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is double d)
{
return d / 2;
}
return value;
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}