Skip to content

Conversation

@danwalmsley
Copy link
Contributor

@danwalmsley danwalmsley commented Oct 21, 2025

Currently if your underlying INCC collection emits move events for the selected item, the selection model will clear the selection. Because it turns the move into a Remove and Add.

This PR properly handles the move events, so a move operation occurs on the selection model too.

The selection is now maintained.

before:
https://github.com/user-attachments/assets/b8f8cb64-510d-4ecc-8ab5-c555d72b5cc2

after:
https://github.com/user-attachments/assets/ebde2759-3e82-4882-be64-f904b2911558

@danwalmsley danwalmsley requested review from Copilot and grokys October 21, 2025 09:47
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes an issue where moving a selected item in an INCC (INotifyCollectionChanged) collection would incorrectly clear the selection. The fix properly handles move events by transforming them into dedicated move operations instead of treating them as separate remove and add operations.

Key Changes:

  • Added logic to detect and handle move operations within the selection model
  • Introduced OnSelectionMoved virtual method to manage selection state during moves
  • Enhanced CollectionChangeState to track deselected ranges for move detection

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
SelectionNodeBase.cs Added move detection logic and OnSelectionMoved method to handle selection preservation during move operations
SelectionModel.cs Implemented OnSelectionMoved override to update selected and anchor indexes during moves
SelectionModelTests_Single.cs Updated existing test and added new test to verify selection is preserved during move operations
SelectionModelTests_Multiple.cs Updated existing test and added new test to verify multiple selection is preserved during move operations

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

{
var relativeBegin = range.Begin - e.OldStartingIndex;
var begin = insertIndex + relativeBegin;
var end = begin + (range.End - range.Begin);
Copy link

Copilot AI Oct 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The calculation for end could be simplified for clarity. Instead of begin + (range.End - range.Begin), consider using insertIndex + (range.End - e.OldStartingIndex) to make the relationship to the original range more explicit.

Suggested change
var end = begin + (range.End - range.Begin);
var end = insertIndex + (range.End - e.OldStartingIndex);

Copilot uses AI. Check for mistakes.
IReadOnlyList<IndexRange> newSelectedRanges,
IReadOnlyList<T> movedItems)
{
_ = movedItems;
Copy link

Copilot AI Oct 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The discard _ = movedItems; is unnecessary since unused parameters in method overrides don't generate warnings. Consider removing this line for cleaner code.

Suggested change
_ = movedItems;

Copilot uses AI. Check for mistakes.
@danwalmsley danwalmsley added the backport-candidate-11.3.x Consider this PR for backporting to 11.3 branch label Oct 21, 2025
@avaloniaui-bot
Copy link

You can test this PR using the following package version. 12.0.999-cibuild0059461-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-candidate-11.3.x Consider this PR for backporting to 11.3 branch enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants