Skip to content

Code Quality: Reduce redundant SelectedItems updates in SelectionChanged#18495

Open
muko wants to merge 2 commits into
files-community:mainfrom
muko:fix/reduce-selection-changed-event
Open

Code Quality: Reduce redundant SelectedItems updates in SelectionChanged#18495
muko wants to merge 2 commits into
files-community:mainfrom
muko:fix/reduce-selection-changed-event

Conversation

@muko
Copy link
Copy Markdown
Contributor

@muko muko commented May 17, 2026

Resolved / Related Issues

When selecting files or folders, SelectionChanged is raised multiple times even when the actual selection does not change.
For example, when opening a folder by double-clicking, SelectionChanged is fired repeatedly without any real change in SelectedItems.

The root cause is that SelectedItems was always assigned from a new list instance:

SelectedItems = FileList.SelectedItems.Cast<ListedItem>().Where(x => x is not null).ToList();

Since ToList() creates a new list instance on every call, the setter was always triggered and caused unnecessary side effects (such as PropertyChanged-driven updates).

Steps used to test these changes

  • Open a file by double-clicking
  • Open a folder by double-clicking
  • Open a folder using keyboard navigation
  • Select multiple items by mouse

@yair100 yair100 added the ready for review Pull requests that are ready for review label May 17, 2026
if (e is not null && e.AddedItems.Count == 0 && e.RemovedItems.Count == 0)
return;

var selectedItems = FileList.SelectedItems.Cast<ListedItem>().Where(x => x is not null).ToList();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since BaseGroupableLayoutPage.FileList_SelectionChanged is the base handler all other layouts use, can we move the SequenceEqual check there instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The intent is to return early before allocating a new list instance via ToList().

if (e is null && SelectedItems?.Count == 0)
return;

if (e is not null && e.AddedItems.Count == 0 && e.RemovedItems.Count == 0)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The SequenceEqual check already covers this since it compares the final selection state rather than how it changed. If AddedItems and RemovedItems are both empty, SequenceEqual will return true anyway. Can you clarify why this separate check is needed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I’ll think about moving it to the base class.

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

Labels

ready for review Pull requests that are ready for review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants