diff --git a/src/Ursa/Controls/SelectionList/SelectionList.cs b/src/Ursa/Controls/SelectionList/SelectionList.cs index cbce4ea..045421c 100644 --- a/src/Ursa/Controls/SelectionList/SelectionList.cs +++ b/src/Ursa/Controls/SelectionList/SelectionList.cs @@ -14,33 +14,34 @@ using Irihi.Avalonia.Shared.Helpers; namespace Ursa.Controls; [TemplatePart(PART_Indicator, typeof(ContentPresenter))] -public class SelectionList: SelectingItemsControl +public class SelectionList : SelectingItemsControl { public const string PART_Indicator = "PART_Indicator"; private static readonly FuncTemplate DefaultPanel = new(() => new StackPanel()); - + + public static readonly StyledProperty IndicatorProperty = + AvaloniaProperty.Register( + nameof(Indicator)); + private ImplicitAnimationCollection? _implicitAnimations; private ContentPresenter? _indicator; - public static readonly StyledProperty IndicatorProperty = AvaloniaProperty.Register( - nameof(Indicator)); + static SelectionList() + { + SelectionModeProperty.OverrideMetadata( + new StyledPropertyMetadata( + SelectionMode.Single, + coerce: (_, _) => SelectionMode.Single) + ); + SelectedItemProperty.Changed.AddClassHandler((list, args) => + list.OnSelectedItemChanged(args)); + } public Control? Indicator { get => GetValue(IndicatorProperty); set => SetValue(IndicatorProperty, value); } - - static SelectionList() - { - SelectionModeProperty.OverrideMetadata( - new StyledPropertyMetadata( - defaultValue: SelectionMode.Single, - coerce: (_, _) => SelectionMode.Single) - ); - SelectedItemProperty.Changed.AddClassHandler((list, args) => - list.OnSelectedItemChanged(args)); - } private void OnSelectedItemChanged(AvaloniaPropertyChangedEventArgs args) { @@ -50,12 +51,14 @@ public class SelectionList: SelectingItemsControl OpacityProperty.SetValue(0d, _indicator); return; } + var container = ContainerFromItem(newValue); if (container is null) { OpacityProperty.SetValue(0d, _indicator); return; } + OpacityProperty.SetValue(1d, _indicator); InvalidateMeasure(); // InvalidateArrange(); @@ -64,15 +67,12 @@ public class SelectionList: SelectingItemsControl protected override Size ArrangeOverride(Size 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); if (container is null) return size; var bounds = container.Bounds; - if (ItemsPanelRoot?.Bounds.Position is { } p) - { - bounds = bounds.Translate(p); - } + if (ItemsPanelRoot?.Bounds.Position is { } p) bounds = bounds.Translate(p); _indicator.Arrange(bounds); } 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 _indicator?.Arrange(new Rect(new Point(), new Size(1, 1))); } + return size; } @@ -96,23 +97,20 @@ public class SelectionList: SelectingItemsControl protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { base.OnApplyTemplate(e); - _indicator= e.NameScope.Find(PART_Indicator); - EnsureIndicatorAnimation(); + _indicator = e.NameScope.Find(PART_Indicator); } protected override void OnLoaded(RoutedEventArgs 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); if (container is null) return; _indicator.Opacity = 1; var bounds = container.Bounds; - if (ItemsPanelRoot?.Bounds.Position is { } p) - { - bounds = bounds.Translate(p); - } + if (ItemsPanelRoot?.Bounds.Position is { } p) bounds = bounds.Translate(p); _indicator.Arrange(bounds); } } @@ -123,10 +121,7 @@ public class SelectionList: SelectingItemsControl { _indicator.Opacity = 0; SetUpAnimation(); - if (ElementComposition.GetElementVisual(_indicator) is { } v) - { - v.ImplicitAnimations = _implicitAnimations; - } + if (ElementComposition.GetElementVisual(_indicator) is { } v) v.ImplicitAnimations = _implicitAnimations; } } @@ -136,7 +131,7 @@ public class SelectionList: SelectingItemsControl Selection.Clear(); Selection.Select(index); } - + private void SetUpAnimation() { if (_implicitAnimations != null) return; @@ -161,15 +156,13 @@ public class SelectionList: SelectingItemsControl _implicitAnimations[nameof(CompositionVisual.Size)] = sizeAnimation; _implicitAnimations[nameof(CompositionVisual.Opacity)] = opacityAnimation; } - + protected override void OnKeyDown(KeyEventArgs e) { 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); - } base.OnKeyDown(e); } -} +} \ No newline at end of file