From 392f07b8273622c99e453c7d7af5ae6cff760930 Mon Sep 17 00:00:00 2001 From: timunie Date: Mon, 30 Mar 2026 19:35:02 +0200 Subject: [PATCH] Adress copilot review --- .../ColorView/ColorView.cs | 10 +++--- .../Primitives/SelectingItemsControl.cs | 35 ++++++++++++++----- .../SelectingItemsControlTests_AutoSelect.cs | 2 +- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs b/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs index ac0009f7e9..838fe624ae 100644 --- a/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs +++ b/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs @@ -67,18 +67,16 @@ namespace Avalonia.Controls } /// - /// Validates the tab/panel/page selection taking into account the visibility of each item - /// as well as the current selection. + /// Obsolete. No-op. This method is no longer used and will be removed in a future release. /// /// - /// Derived controls may re-implement this based on their default style / control template - /// and any specialized selection needs. + /// This method does nothing and should not be overridden or relied upon. Validation is now handled by TabControl. /// // TODO-13: Remove this unused method [Obsolete("The necessary validation is now handled by the TabControl. This method will be removed in the next major release.")] protected virtual void ValidateSelection() { - // Method is now obsolete and is no longer implemented + // Obsolete: no-op. Will be removed in a future release. } /// @@ -156,6 +154,8 @@ namespace Avalonia.Controls { newPaletteColors.Add(palette.GetColor(colorIndex, shadeIndex)); } + } + } } SetCurrentValue(PaletteColorsProperty, newPaletteColors); diff --git a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs index 5f973e11b6..756f2c3ca7 100644 --- a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs +++ b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs @@ -1157,19 +1157,36 @@ namespace Avalonia.Controls.Primitives } } + private int? _pendingAutoScrollAnchorIndex; private void AutoScrollToSelectedItemIfNecessary(int anchorIndex) { - if (AutoScrollToSelectedItem && - !_hasScrolledToSelectedItem && - Presenter is object && - anchorIndex >= 0 && - IsAttachedToVisualTree) + if (!(AutoScrollToSelectedItem && !_hasScrolledToSelectedItem && Presenter != null && anchorIndex >= 0 && IsAttachedToVisualTree)) + return; + + if (!IsEffectivelyVisible) { - if (!IsEffectivelyVisible) - { - return; - } + // Defer scroll until visible + _pendingAutoScrollAnchorIndex = anchorIndex; + LayoutUpdated -= OnLayoutUpdatedForAutoScroll; // Avoid duplicate handlers + LayoutUpdated += OnLayoutUpdatedForAutoScroll; + return; + } + + // If already visible, scroll immediately + Dispatcher.UIThread.Post(state => + { + ScrollIntoView((int)state!); + _hasScrolledToSelectedItem = true; + }, anchorIndex); + } + private void OnLayoutUpdatedForAutoScroll(object? sender, EventArgs e) + { + if (_pendingAutoScrollAnchorIndex != null && IsEffectivelyVisible) + { + int anchorIndex = _pendingAutoScrollAnchorIndex.Value; + LayoutUpdated -= OnLayoutUpdatedForAutoScroll; + _pendingAutoScrollAnchorIndex = null; Dispatcher.UIThread.Post(state => { ScrollIntoView((int)state!); diff --git a/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_AutoSelect.cs b/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_AutoSelect.cs index 1e655ae0a4..4fb3763cad 100644 --- a/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_AutoSelect.cs +++ b/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_AutoSelect.cs @@ -158,7 +158,7 @@ namespace Avalonia.Controls.UnitTests.Primitives // Item 50 is at 50 * 50 = 2500. // ListBox height is 100, so it should be visible if offset is between 2400 and 2500. - Assert.True(offset > 0, $"Expected AutoScrollToSelectedItem to scroll to item 50, but offset was {offset}"); + Assert.InRange(offset, 2400, 2500); } }