diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 index e5a7f1c..32b55f7 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 @@ -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) diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Shared.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Shared.psm1 index a9fcbc5..b130e48 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Shared.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Shared.psm1 @@ -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(