Refactor and improve ListView item selection

Extracts the logic for toggling an item's selection state with the spacebar into a new, reusable function. This change centralizes previously duplicated code, improving maintainability.

The event handling is moved from `KeyDown` to `PreviewKeyDown` to provide a more reliable user experience. Additionally, focus is now correctly restored to the toggled item, preventing the selection from moving unexpectedly.
This commit is contained in:
rbalsleyMSFT
2025-06-20 11:50:47 -07:00
parent 86ec6de894
commit 1675c48fc3
2 changed files with 73 additions and 11 deletions
@@ -92,21 +92,13 @@ function Register-EventHandlers {
})
$State.Controls.lstUSBDrives.Add_KeyDown({
$State.Controls.lstUSBDrives.Add_PreviewKeyDown({
param($eventSource, $keyEvent)
if ($keyEvent.Key -eq 'Space') {
$window = [System.Windows.Window]::GetWindow($eventSource)
$localState = $window.Tag
$selectedItem = $localState.Controls.lstUSBDrives.SelectedItem
if ($selectedItem) {
$selectedItem.IsSelected = -not $selectedItem.IsSelected
$localState.Controls.lstUSBDrives.Items.Refresh()
# After toggling, update the 'Select All' header checkbox state
$headerChk = $localState.Controls.chkSelectAllUSBDrivesHeader
if ($null -ne $headerChk) {
Update-SelectAllHeaderCheckBoxState -ListView $localState.Controls.lstUSBDrives -HeaderCheckBox $headerChk
}
}
Invoke-ListViewItemToggle -ListView $eventSource -State $localState -HeaderCheckBoxKeyName 'chkSelectAllUSBDrivesHeader'
$keyEvent.Handled = $true
}
})
$State.Controls.lstUSBDrives.Add_SelectionChanged({
@@ -401,6 +393,16 @@ function Register-EventHandlers {
-PostClearAction $postClearScriptBlock
})
$State.Controls.lstAppsScriptVariables.Add_PreviewKeyDown({
param($eventSource, $keyEvent)
if ($keyEvent.Key -eq 'Space') {
$window = [System.Windows.Window]::GetWindow($eventSource)
$localState = $window.Tag
Invoke-ListViewItemToggle -ListView $eventSource -State $localState -HeaderCheckBoxKeyName 'chkSelectAllAppsScriptVariables'
$keyEvent.Handled = $true
}
})
$State.Controls.btnCheckWingetModule.Add_Click({
param($eventSource, $routedEventArgs)
$window = [System.Windows.Window]::GetWindow($eventSource)
@@ -504,6 +506,15 @@ function Register-EventHandlers {
-TextBoxesToClear @($localState.Controls.txtWingetSearch) `
-PostClearAction $postClearScriptBlock
})
$State.Controls.lstWingetResults.Add_PreviewKeyDown({
param($eventSource, $keyEvent)
if ($keyEvent.Key -eq 'Space') {
$window = [System.Windows.Window]::GetWindow($eventSource)
$localState = $window.Tag
Invoke-ListViewItemToggle -ListView $eventSource -State $localState -HeaderCheckBoxKeyName 'chkSelectAllWingetResults'
$keyEvent.Handled = $true
}
})
$State.Controls.btnDownloadSelected.Add_Click({
param($eventSource, $routedEventArgs)
@@ -868,6 +879,16 @@ function Register-EventHandlers {
-PostClearAction $postClearScriptBlock
})
$State.Controls.lstDriverModels.Add_PreviewKeyDown({
param($eventSource, $keyEvent)
if ($keyEvent.Key -eq 'Space') {
$window = [System.Windows.Window]::GetWindow($eventSource)
$localState = $window.Tag
Invoke-ListViewItemToggle -ListView $eventSource -State $localState -HeaderCheckBoxKeyName 'chkSelectAllDriverModels'
$keyEvent.Handled = $true
}
})
$State.Controls.btnSaveDriversJson.Add_Click({
param($eventSource, $routedEventArgs)
$window = [System.Windows.Window]::GetWindow($eventSource)