mirror of
https://github.com/rbalsleyMSFT/FFU.git
synced 2026-06-14 02:09:35 -06:00
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:
@@ -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)
|
||||
|
||||
@@ -441,6 +441,47 @@ function Update-SelectAllHeaderCheckBoxState {
|
||||
}
|
||||
}
|
||||
|
||||
# Function to toggle the IsSelected state of the currently selected ListView item
|
||||
function Invoke-ListViewItemToggle {
|
||||
param(
|
||||
[Parameter(Mandatory)]
|
||||
[System.Windows.Controls.ListView]$ListView,
|
||||
[Parameter(Mandatory)]
|
||||
[psobject]$State,
|
||||
[Parameter(Mandatory)]
|
||||
[string]$HeaderCheckBoxKeyName
|
||||
)
|
||||
|
||||
$selectedItem = $ListView.SelectedItem
|
||||
if ($null -eq $selectedItem) { return }
|
||||
|
||||
# Store the current index to restore focus later
|
||||
$currentIndex = $ListView.SelectedIndex
|
||||
|
||||
# Toggle the IsSelected property
|
||||
$selectedItem.IsSelected = -not $selectedItem.IsSelected
|
||||
$ListView.Items.Refresh()
|
||||
|
||||
# Update the 'Select All' header checkbox state
|
||||
$headerChk = $State.Controls[$HeaderCheckBoxKeyName]
|
||||
if ($null -ne $headerChk) {
|
||||
Update-SelectAllHeaderCheckBoxState -ListView $ListView -HeaderCheckBox $headerChk
|
||||
}
|
||||
|
||||
# Restore selection and focus to the item that was just toggled
|
||||
if ($currentIndex -ge 0 -and $ListView.Items.Count -gt $currentIndex) {
|
||||
$ListView.SelectedIndex = $currentIndex
|
||||
|
||||
# Ensure the UI is updated before trying to find the container
|
||||
$ListView.UpdateLayout()
|
||||
|
||||
$listViewItem = $ListView.ItemContainerGenerator.ContainerFromIndex($currentIndex)
|
||||
if ($null -ne $listViewItem) {
|
||||
$listViewItem.Focus()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Function to sort ListView items
|
||||
function Invoke-ListViewSort {
|
||||
param(
|
||||
|
||||
Reference in New Issue
Block a user