feat: simple AvatarGroupPanel.

This commit is contained in:
Zhang Dian
2024-06-20 18:38:20 +08:00
parent 5fc2c33fed
commit f7a5032f8d
2 changed files with 47 additions and 2 deletions

View File

@@ -1,7 +1,6 @@
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Templates; using Avalonia.Controls.Templates;
using Avalonia.Layout;
namespace Ursa.Controls; namespace Ursa.Controls;
@@ -10,7 +9,7 @@ public class AvatarGroup : ItemsControl
public const string PART_RenderMore = "PART_RenderMore"; public const string PART_RenderMore = "PART_RenderMore";
private static readonly FuncTemplate<Panel?> DefaultPanel = private static readonly FuncTemplate<Panel?> DefaultPanel =
new(() => new StackPanel { Orientation = Orientation.Horizontal }); new(() => new AvatarGroupPanel());
public static readonly StyledProperty<int> MaxCountProperty = AvaloniaProperty.Register<AvatarGroup, int>( public static readonly StyledProperty<int> MaxCountProperty = AvaloniaProperty.Register<AvatarGroup, int>(
nameof(MaxCount)); nameof(MaxCount));

View File

@@ -0,0 +1,46 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
namespace Ursa.Controls;
public class AvatarGroupPanel : Panel
{
protected override Size MeasureOverride(Size availableSize)
{
Size size = new Size();
availableSize = availableSize.WithWidth(double.PositiveInfinity);
var children = Children;
foreach (var child in children)
{
child.Measure(availableSize);
Size desiredSize = child.DesiredSize;
size = size.WithWidth(size.Width + desiredSize.Width);
size = size.WithHeight(Math.Max(size.Height, desiredSize.Height));
}
size = size.WithWidth(size.Width);
return size;
}
protected override Size ArrangeOverride(Size finalSize)
{
Rect rect = new Rect(finalSize);
double num = 0d;
var children = Children;
foreach (var child in children)
{
Size desiredSize = child.DesiredSize;
double width = desiredSize.Width;
double height = Math.Max(desiredSize.Height, finalSize.Height);
rect = rect.WithX(rect.X + num);
rect = rect.WithWidth(width);
rect = rect.WithHeight(height);
num = width;
child.Arrange(rect);
}
RaiseEvent(new RoutedEventArgs(StackPanel.HorizontalSnapPointsChangedEvent));
return finalSize;
}
}