From b46b904504f85777d5729ea384a3329854c7e92d Mon Sep 17 00:00:00 2001 From: rbalsleyMSFT <53497092+rbalsleyMSFT@users.noreply.github.com> Date: Wed, 18 Jun 2025 13:52:25 -0700 Subject: [PATCH] Refactor USB drive event handlers to core module Moves the event handling logic for the USB drive selection controls from the main UI script to the `FFUUI.Core.Handlers` module. This change improves code organization by centralizing UI logic. The handlers were also improved to correctly manage the "Select All" checkbox state. --- FFUDevelopment/BuildFFUVM_UI.ps1 | 25 ----------- .../FFUUI.Core/FFUUI.Core.Handlers.psm1 | 44 +++++++++++++++++++ 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/FFUDevelopment/BuildFFUVM_UI.ps1 b/FFUDevelopment/BuildFFUVM_UI.ps1 index b0a5d5a..2a79b99 100644 --- a/FFUDevelopment/BuildFFUVM_UI.ps1 +++ b/FFUDevelopment/BuildFFUVM_UI.ps1 @@ -194,31 +194,6 @@ $window.Add_Loaded({ $script:uiState.Controls.chkLatestCU.IsEnabled = -not $script:uiState.Controls.chkPreviewCU.IsChecked # USB Drive Detection/Selection logic (Keep existing logic) - $script:uiState.Controls.chkSelectAllUSBDrives.Add_Checked({ - foreach ($item in $script:uiState.Controls.lstUSBDrives.Items) { $item.IsSelected = $true } - $script:uiState.Controls.lstUSBDrives.Items.Refresh() - }) - $script:uiState.Controls.chkSelectAllUSBDrives.Add_Unchecked({ - foreach ($item in $script:uiState.Controls.lstUSBDrives.Items) { $item.IsSelected = $false } - $script:uiState.Controls.lstUSBDrives.Items.Refresh() - }) - $script:uiState.Controls.lstUSBDrives.Add_KeyDown({ - param($eventSource, $keyEvent) - if ($keyEvent.Key -eq 'Space') { - $selectedItem = $script:uiState.Controls.lstUSBDrives.SelectedItem - if ($selectedItem) { - $selectedItem.IsSelected = !$selectedItem.IsSelected - $script:uiState.Controls.lstUSBDrives.Items.Refresh() - $allSelected = -not ($script:uiState.Controls.lstUSBDrives.Items | Where-Object { -not $_.IsSelected }) - $script:uiState.Controls.chkSelectAllUSBDrives.IsChecked = $allSelected - } - } - }) - $script:uiState.Controls.lstUSBDrives.Add_SelectionChanged({ - param($eventSource, $selChangeEvent) - $allSelected = -not ($script:uiState.Controls.lstUSBDrives.Items | Where-Object { -not $_.IsSelected }) - $script:uiState.Controls.chkSelectAllUSBDrives.IsChecked = $allSelected - }) $script:uiState.Controls.usbSection.Visibility = if ($script:uiState.Controls.chkBuildUSBDriveEnable.IsChecked) { 'Visible' } else { 'Collapsed' } $script:uiState.Controls.usbSelectionPanel.Visibility = if ($script:uiState.Controls.chkSelectSpecificUSBDrives.IsChecked) { 'Visible' } else { 'Collapsed' } $script:uiState.Controls.chkBuildUSBDriveEnable.Add_Checked({ diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 index 1c6bebd..7c0d4ef 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 @@ -3,6 +3,7 @@ function Register-EventHandlers { WriteLog "Registering UI event handlers..." # Build Tab Event Handlers + # Build USB Drive Settings Event Handlers $State.Controls.btnCheckUSBDrives.Add_Click({ param($eventSource, $routedEventArgs) $window = [System.Windows.Window]::GetWindow($eventSource) @@ -17,6 +18,49 @@ function Register-EventHandlers { $localState.Controls.lstUSBDrives.SelectedIndex = 0 } }) + + $State.Controls.chkSelectAllUSBDrives.Add_Checked({ + param($eventSource, $routedEventArgs) + $window = [System.Windows.Window]::GetWindow($eventSource) + $localState = $window.Tag + foreach ($item in $localState.Controls.lstUSBDrives.Items) { $item.IsSelected = $true } + $localState.Controls.lstUSBDrives.Items.Refresh() + }) + $State.Controls.chkSelectAllUSBDrives.Add_Unchecked({ + param($eventSource, $routedEventArgs) + # This event also fires for indeterminate state, so only act if it's explicitly false. + if ($eventSource.IsChecked -eq $false) { + $window = [System.Windows.Window]::GetWindow($eventSource) + $localState = $window.Tag + foreach ($item in $localState.Controls.lstUSBDrives.Items) { $item.IsSelected = $false } + $localState.Controls.lstUSBDrives.Items.Refresh() + } + }) + $State.Controls.lstUSBDrives.Add_KeyDown({ + 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' checkbox state + $items = $localState.Controls.lstUSBDrives.Items + $allSelected = $items.Count -gt 0 -and ($items | Where-Object { -not $_.IsSelected }).Count -eq 0 + $localState.Controls.chkSelectAllUSBDrives.IsChecked = $allSelected + } + } + }) + $State.Controls.lstUSBDrives.Add_SelectionChanged({ + param($eventSource, $selChangeEvent) + $window = [System.Windows.Window]::GetWindow($eventSource) + $localState = $window.Tag + $items = $localState.Controls.lstUSBDrives.Items + # Update the 'Select All' checkbox state based on current selections + $allSelected = $items.Count -gt 0 -and ($items | Where-Object { -not $_.IsSelected }).Count -eq 0 + $localState.Controls.chkSelectAllUSBDrives.IsChecked = $allSelected + }) # Hyper-V tab event handlers $State.Controls.cmbVMSwitchName.Add_SelectionChanged({