feat: maxCount.

This commit is contained in:
Zhang Dian
2024-06-23 22:01:01 +08:00
parent c49be02e82
commit 29dab32b92
3 changed files with 31 additions and 12 deletions

View File

@@ -65,7 +65,7 @@
<u:Avatar Classes="ExtraLarge" /> <u:Avatar Classes="ExtraLarge" />
</StackPanel> </StackPanel>
<StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal">
<u:AvatarGroup> <u:AvatarGroup MaxCount="3">
<u:Avatar Classes="Red" Content="LL" /> <u:Avatar Classes="Red" Content="LL" />
<u:Avatar Classes="" Content="CX" /> <u:Avatar Classes="" Content="CX" />
<u:Avatar Classes="Amber" Content="RM" /> <u:Avatar Classes="Amber" Content="RM" />

View File

@@ -10,14 +10,14 @@ public class AvatarGroup : ItemsControl
private static readonly FuncTemplate<Panel?> DefaultPanel = new(() => new AvatarGroupPanel()); private static readonly FuncTemplate<Panel?> DefaultPanel = 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));
public static readonly StyledProperty<OverlapFromType> OverlapFromProperty = public static readonly StyledProperty<OverlapFromType> OverlapFromProperty =
AvaloniaProperty.Register<AvatarGroup, OverlapFromType>( AvaloniaProperty.Register<AvatarGroup, OverlapFromType>(
nameof(OverlapFrom), defaultValue: OverlapFromType.Start); nameof(OverlapFrom), defaultValue: OverlapFromType.Start);
public int MaxCount public int? MaxCount
{ {
get => GetValue(MaxCountProperty); get => GetValue(MaxCountProperty);
set => SetValue(MaxCountProperty, value); set => SetValue(MaxCountProperty, value);

View File

@@ -11,16 +11,19 @@ public class AvatarGroupPanel : Panel
Size size = new Size(); Size size = new Size();
availableSize = availableSize.WithWidth(double.PositiveInfinity); availableSize = availableSize.WithWidth(double.PositiveInfinity);
var children = Children; var children = Children;
if (children.Count > 0) if (children.Count <= 0) return size;
children[0].Measure(availableSize);
Size first = children[0].DesiredSize;
var group = this.GetLogicalAncestors().OfType<AvatarGroup>().FirstOrDefault();
var count = children.Count;
if (group?.MaxCount is not null && group.MaxCount >= 0)
{ {
children[0].Measure(availableSize); count = group.MaxCount.Value + 1;
Size first = children[0].DesiredSize;
var width = first.Width + first.Width * (children.Count - 1) * 0.75;
size = size.WithWidth(width);
size = size.WithHeight(first.Height);
} }
size = size.WithWidth(size.Width); var width = first.Width + first.Width * (count - 1) * 0.75;
size = size.WithWidth(width);
size = size.WithHeight(first.Height);
return size; return size;
} }
@@ -28,10 +31,18 @@ public class AvatarGroupPanel : Panel
{ {
Rect rect = new Rect(finalSize); Rect rect = new Rect(finalSize);
double num = 0d; double num = 0d;
var children = Children;
var group = this.GetLogicalAncestors().OfType<AvatarGroup>().FirstOrDefault(); var group = this.GetLogicalAncestors().OfType<AvatarGroup>().FirstOrDefault();
var overlapFrom = group?.OverlapFrom; var overlapFrom = group?.OverlapFrom;
var children = Children; int? maxCount = null;
for (var i = 0; i < children.Count; i++) var count = children.Count;
if (group?.MaxCount is not null && group.MaxCount >= 0)
{
maxCount = group.MaxCount;
count = maxCount.Value + 1;
}
for (var i = 0; i < count; i++)
{ {
if (overlapFrom is OverlapFromType.Start) if (overlapFrom is OverlapFromType.Start)
{ {
@@ -49,6 +60,14 @@ public class AvatarGroupPanel : Panel
children[i].Arrange(rect); children[i].Arrange(rect);
} }
if (maxCount is not null && children.Count > 0)
{
if (children[maxCount.Value] is Avatar avatar)
{
avatar.Content = $"+{children.Count - maxCount}";
}
}
return finalSize; return finalSize;
} }
} }