feat: change animation creation to loaded, thus it works in popup.

This commit is contained in:
Dong Bin
2025-02-25 01:31:59 +08:00
parent 3b129b1128
commit 27146acc49

View File

@@ -14,34 +14,35 @@ using Irihi.Avalonia.Shared.Helpers;
namespace Ursa.Controls; namespace Ursa.Controls;
[TemplatePart(PART_Indicator, typeof(ContentPresenter))] [TemplatePart(PART_Indicator, typeof(ContentPresenter))]
public class SelectionList: SelectingItemsControl public class SelectionList : SelectingItemsControl
{ {
public const string PART_Indicator = "PART_Indicator"; public const string PART_Indicator = "PART_Indicator";
private static readonly FuncTemplate<Panel?> DefaultPanel = new(() => new StackPanel()); private static readonly FuncTemplate<Panel?> DefaultPanel = new(() => new StackPanel());
private ImplicitAnimationCollection? _implicitAnimations; public static readonly StyledProperty<Control?> IndicatorProperty =
private ContentPresenter? _indicator; AvaloniaProperty.Register<SelectionList, Control?>(
public static readonly StyledProperty<Control?> IndicatorProperty = AvaloniaProperty.Register<SelectionList, Control?>(
nameof(Indicator)); nameof(Indicator));
public Control? Indicator private ImplicitAnimationCollection? _implicitAnimations;
{ private ContentPresenter? _indicator;
get => GetValue(IndicatorProperty);
set => SetValue(IndicatorProperty, value);
}
static SelectionList() static SelectionList()
{ {
SelectionModeProperty.OverrideMetadata<SelectionList>( SelectionModeProperty.OverrideMetadata<SelectionList>(
new StyledPropertyMetadata<SelectionMode>( new StyledPropertyMetadata<SelectionMode>(
defaultValue: SelectionMode.Single, SelectionMode.Single,
coerce: (_, _) => SelectionMode.Single) coerce: (_, _) => SelectionMode.Single)
); );
SelectedItemProperty.Changed.AddClassHandler<SelectionList, object?>((list, args) => SelectedItemProperty.Changed.AddClassHandler<SelectionList, object?>((list, args) =>
list.OnSelectedItemChanged(args)); list.OnSelectedItemChanged(args));
} }
public Control? Indicator
{
get => GetValue(IndicatorProperty);
set => SetValue(IndicatorProperty, value);
}
private void OnSelectedItemChanged(AvaloniaPropertyChangedEventArgs<object?> args) private void OnSelectedItemChanged(AvaloniaPropertyChangedEventArgs<object?> args)
{ {
var newValue = args.NewValue.Value; var newValue = args.NewValue.Value;
@@ -50,12 +51,14 @@ public class SelectionList: SelectingItemsControl
OpacityProperty.SetValue(0d, _indicator); OpacityProperty.SetValue(0d, _indicator);
return; return;
} }
var container = ContainerFromItem(newValue); var container = ContainerFromItem(newValue);
if (container is null) if (container is null)
{ {
OpacityProperty.SetValue(0d, _indicator); OpacityProperty.SetValue(0d, _indicator);
return; return;
} }
OpacityProperty.SetValue(1d, _indicator); OpacityProperty.SetValue(1d, _indicator);
InvalidateMeasure(); InvalidateMeasure();
// InvalidateArrange(); // InvalidateArrange();
@@ -64,15 +67,12 @@ public class SelectionList: SelectingItemsControl
protected override Size ArrangeOverride(Size finalSize) protected override Size ArrangeOverride(Size finalSize)
{ {
var size = base.ArrangeOverride(finalSize); var size = base.ArrangeOverride(finalSize);
if(_indicator is not null && SelectedItem is not null) if (_indicator is not null && SelectedItem is not null)
{ {
var container = ContainerFromItem(SelectedItem); var container = ContainerFromItem(SelectedItem);
if (container is null) return size; if (container is null) return size;
var bounds = container.Bounds; var bounds = container.Bounds;
if (ItemsPanelRoot?.Bounds.Position is { } p) if (ItemsPanelRoot?.Bounds.Position is { } p) bounds = bounds.Translate(p);
{
bounds = bounds.Translate(p);
}
_indicator.Arrange(bounds); _indicator.Arrange(bounds);
} }
else else
@@ -80,6 +80,7 @@ public class SelectionList: SelectingItemsControl
// This is a hack. The indicator is not visible, so we arrange it to a 1x1 rectangle // This is a hack. The indicator is not visible, so we arrange it to a 1x1 rectangle
_indicator?.Arrange(new Rect(new Point(), new Size(1, 1))); _indicator?.Arrange(new Rect(new Point(), new Size(1, 1)));
} }
return size; return size;
} }
@@ -96,23 +97,20 @@ public class SelectionList: SelectingItemsControl
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
_indicator= e.NameScope.Find<ContentPresenter>(PART_Indicator); _indicator = e.NameScope.Find<ContentPresenter>(PART_Indicator);
EnsureIndicatorAnimation();
} }
protected override void OnLoaded(RoutedEventArgs e) protected override void OnLoaded(RoutedEventArgs e)
{ {
base.OnLoaded(e); base.OnLoaded(e);
if(_indicator is not null && SelectedItem is not null) EnsureIndicatorAnimation();
if (_indicator is not null && SelectedItem is not null)
{ {
var container = ContainerFromItem(SelectedItem); var container = ContainerFromItem(SelectedItem);
if (container is null) return; if (container is null) return;
_indicator.Opacity = 1; _indicator.Opacity = 1;
var bounds = container.Bounds; var bounds = container.Bounds;
if (ItemsPanelRoot?.Bounds.Position is { } p) if (ItemsPanelRoot?.Bounds.Position is { } p) bounds = bounds.Translate(p);
{
bounds = bounds.Translate(p);
}
_indicator.Arrange(bounds); _indicator.Arrange(bounds);
} }
} }
@@ -123,10 +121,7 @@ public class SelectionList: SelectingItemsControl
{ {
_indicator.Opacity = 0; _indicator.Opacity = 0;
SetUpAnimation(); SetUpAnimation();
if (ElementComposition.GetElementVisual(_indicator) is { } v) if (ElementComposition.GetElementVisual(_indicator) is { } v) v.ImplicitAnimations = _implicitAnimations;
{
v.ImplicitAnimations = _implicitAnimations;
}
} }
} }
@@ -167,9 +162,7 @@ public class SelectionList: SelectingItemsControl
var hotkeys = Application.Current!.PlatformSettings?.HotkeyConfiguration; var hotkeys = Application.Current!.PlatformSettings?.HotkeyConfiguration;
if (e.Key.ToNavigationDirection() is { } direction && direction.IsDirectional()) if (e.Key.ToNavigationDirection() is { } direction && direction.IsDirectional())
{
e.Handled |= MoveSelection(direction, WrapSelection); e.Handled |= MoveSelection(direction, WrapSelection);
}
base.OnKeyDown(e); base.OnKeyDown(e);
} }
} }