Browse Source

Adress copilot review

pull/20798/head
timunie 6 months ago
parent
commit
392f07b827
  1. 10
      src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs
  2. 35
      src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
  3. 2
      tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_AutoSelect.cs

10
src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs

@ -67,18 +67,16 @@ namespace Avalonia.Controls
} }
/// <summary> /// <summary>
/// Validates the tab/panel/page selection taking into account the visibility of each item /// <b>Obsolete. No-op.</b> This method is no longer used and will be removed in a future release.
/// as well as the current selection.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// Derived controls may re-implement this based on their default style / control template /// This method does nothing and should not be overridden or relied upon. Validation is now handled by TabControl.
/// and any specialized selection needs.
/// </remarks> /// </remarks>
// TODO-13: Remove this unused method // 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.")] [Obsolete("The necessary validation is now handled by the TabControl. This method will be removed in the next major release.")]
protected virtual void ValidateSelection() protected virtual void ValidateSelection()
{ {
// Method is now obsolete and is no longer implemented // Obsolete: no-op. Will be removed in a future release.
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -156,6 +154,8 @@ namespace Avalonia.Controls
{ {
newPaletteColors.Add(palette.GetColor(colorIndex, shadeIndex)); newPaletteColors.Add(palette.GetColor(colorIndex, shadeIndex));
} }
}
}
} }
SetCurrentValue(PaletteColorsProperty, newPaletteColors); SetCurrentValue(PaletteColorsProperty, newPaletteColors);

35
src/Avalonia.Controls/Primitives/SelectingItemsControl.cs

@ -1157,19 +1157,36 @@ namespace Avalonia.Controls.Primitives
} }
} }
private int? _pendingAutoScrollAnchorIndex;
private void AutoScrollToSelectedItemIfNecessary(int anchorIndex) private void AutoScrollToSelectedItemIfNecessary(int anchorIndex)
{ {
if (AutoScrollToSelectedItem && if (!(AutoScrollToSelectedItem && !_hasScrolledToSelectedItem && Presenter != null && anchorIndex >= 0 && IsAttachedToVisualTree))
!_hasScrolledToSelectedItem && return;
Presenter is object &&
anchorIndex >= 0 && if (!IsEffectivelyVisible)
IsAttachedToVisualTree)
{ {
if (!IsEffectivelyVisible) // Defer scroll until visible
{ _pendingAutoScrollAnchorIndex = anchorIndex;
return; 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 => Dispatcher.UIThread.Post(state =>
{ {
ScrollIntoView((int)state!); ScrollIntoView((int)state!);

2
tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_AutoSelect.cs

@ -158,7 +158,7 @@ namespace Avalonia.Controls.UnitTests.Primitives
// Item 50 is at 50 * 50 = 2500. // Item 50 is at 50 * 50 = 2500.
// ListBox height is 100, so it should be visible if offset is between 2400 and 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);
} }
} }

Loading…
Cancel
Save