CmdPal: Add ability to refresh the list page while preserving selection (#45882)

## Summary of the Pull Request

This PR adds ability to invoke soft/incremental refresh that updates
items but keep selection in place (instead of resetting it to the first
item). For now, this is implemented as a hack using an unused parameter
of `ListPage.RaiseItemsChanged`: passing the constant `-2` tells the
view model that this is an incremental change and that it should keep
the current selection.


<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist

- [x] Closes: #45694
- [x] Related to: #44407
<!-- - [ ] Closes: #yyy (add separate lines for additional resolved
issues) -->
- [ ] **Communication:** I've discussed this with core contributors
already. If the work hasn't been agreed, this work might be rejected
- [ ] **Tests:** Added/updated and all pass
- [ ] **Localization:** All end-user-facing strings can be localized
- [ ] **Dev docs:** Added/updated
- [ ] **New binaries:** Added on the required places
- [ ] [JSON for
signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json)
for new binaries
- [ ] [WXS for
installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs)
for new binaries and localization folder
- [ ] [YML for CI
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml)
for new test projects
- [ ] [YML for signed
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml)
- [ ] **Documentation updated:** If checked, please file a pull request
on [our docs
repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys)
and link it here: #xxx

<!-- Provide a more detailed description of the PR, other things fixed,
or any additional comments/features here -->
## Detailed Description of the Pull Request / Additional comments

<!-- Describe how you validated the behavior. Add automated tests
wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed
This commit is contained in:
Jiří Polášek
2026-03-03 06:39:49 +01:00
committed by GitHub
parent 95835a4cfa
commit 1cb99e32ef
3 changed files with 72 additions and 27 deletions

View File

@@ -231,25 +231,7 @@ public sealed partial class ListPage : Page,
if (_scrollOnNextSelectionChange)
{
_scrollOnNextSelectionChange = false;
var scrollTarget = li;
// If the previous item is a separator, also scroll it into view to provide
// better context for the user
var index = ItemView.Items.IndexOf(li);
if (index > 0)
{
var prevItem = ItemView.Items[index - 1] as ListItemViewModel;
if (prevItem?.Type == ListItemType.SectionHeader)
{
scrollTarget = prevItem;
}
}
if (scrollTarget is not null)
{
ItemView.ScrollIntoView(scrollTarget);
}
ScrollToItem(li);
}
// Automation notification for screen readers
@@ -263,6 +245,28 @@ public sealed partial class ListPage : Page,
}
}
private void ScrollToItem(ListItemViewModel li)
{
var scrollTarget = li;
// If the previous item is a separator, also scroll it into view to provide
// better context for the user
var index = ItemView.Items.IndexOf(li);
if (index > 0)
{
var prevItem = ItemView.Items[index - 1] as ListItemViewModel;
if (prevItem?.Type == ListItemType.SectionHeader)
{
scrollTarget = prevItem;
}
}
if (scrollTarget is not null)
{
ItemView.ScrollIntoView(scrollTarget);
}
}
private void Items_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
if (e.OriginalSource is FrameworkElement element &&
@@ -717,6 +721,8 @@ public sealed partial class ListPage : Page,
{
using (SuppressSelectionChangedScope())
{
ListItemViewModel? stickyRestored = null;
if (!forceFirstItem &&
_stickySelectedItem is not null &&
items.Contains(_stickySelectedItem) &&
@@ -724,6 +730,7 @@ public sealed partial class ListPage : Page,
{
// Preserve sticky selection for nested dynamic updates.
ItemView.SelectedItem = _stickySelectedItem;
stickyRestored = _stickySelectedItem;
}
else
{
@@ -741,7 +748,26 @@ public sealed partial class ListPage : Page,
return;
}
ResetScrollToTop();
if (stickyRestored is not null)
{
ScrollToItem(stickyRestored);
}
else
{
ResetScrollToTop();
}
});
}
}
else
{
// Selection is valid and unchanged, just make sure the item is visible
if (_stickySelectedItem is ListItemViewModel li)
{
_ = DispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Low, () =>
{
ItemView.UpdateLayout();
ScrollToItem(li);
});
}
}