Refactor USB drive UI logic into core modules

Moves event handlers and initial state configuration for the USB drive creation settings from the main UI script into the core handler and initialization modules.

This change centralizes the UI logic, improving code organization and maintainability.
This commit is contained in:
rbalsleyMSFT
2025-06-18 17:30:23 -07:00
parent 9b8a6c36db
commit 49b0772076
3 changed files with 48 additions and 29 deletions
-29
View File
@@ -194,35 +194,6 @@ $window.Add_Loaded({
$script:uiState.Controls.chkLatestCU.IsEnabled = -not $script:uiState.Controls.chkPreviewCU.IsChecked $script:uiState.Controls.chkLatestCU.IsEnabled = -not $script:uiState.Controls.chkPreviewCU.IsChecked
# USB Drive Detection/Selection logic (Keep existing logic) # USB Drive Detection/Selection logic (Keep existing logic)
$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({
$script:uiState.Controls.usbSection.Visibility = 'Visible'
$script:uiState.Controls.chkSelectSpecificUSBDrives.IsEnabled = $true
})
$script:uiState.Controls.chkBuildUSBDriveEnable.Add_Unchecked({
$script:uiState.Controls.usbSection.Visibility = 'Collapsed'
$script:uiState.Controls.chkSelectSpecificUSBDrives.IsEnabled = $false
$script:uiState.Controls.chkSelectSpecificUSBDrives.IsChecked = $false
$script:uiState.Controls.lstUSBDrives.Items.Clear()
})
$script:uiState.Controls.chkSelectSpecificUSBDrives.Add_Checked({
$script:uiState.Controls.usbSelectionPanel.Visibility = 'Visible'
})
$script:uiState.Controls.chkSelectSpecificUSBDrives.Add_Unchecked({
$script:uiState.Controls.usbSelectionPanel.Visibility = 'Collapsed'
$script:uiState.Controls.lstUSBDrives.Items.Clear()
})
$script:uiState.Controls.chkSelectSpecificUSBDrives.IsEnabled = $script:uiState.Controls.chkBuildUSBDriveEnable.IsChecked
$script:uiState.Controls.chkAllowExternalHardDiskMedia.Add_Checked({
$script:uiState.Controls.chkPromptExternalHardDiskMedia.IsEnabled = $true
})
$script:uiState.Controls.chkAllowExternalHardDiskMedia.Add_Unchecked({
$script:uiState.Controls.chkPromptExternalHardDiskMedia.IsEnabled = $false
$script:uiState.Controls.chkPromptExternalHardDiskMedia.IsChecked = $false
})
# Set initial state based on defaults
$script:uiState.Controls.chkPromptExternalHardDiskMedia.IsEnabled = $script:uiState.Controls.chkAllowExternalHardDiskMedia.IsChecked
# APPLICATIONS tab UI logic (Keep existing logic) # APPLICATIONS tab UI logic (Keep existing logic)
$script:uiState.Controls.chkInstallWingetApps.Visibility = if ($script:uiState.Controls.chkInstallApps.IsChecked) { 'Visible' } else { 'Collapsed' } $script:uiState.Controls.chkInstallWingetApps.Visibility = if ($script:uiState.Controls.chkInstallApps.IsChecked) { 'Visible' } else { 'Collapsed' }
@@ -4,6 +4,49 @@ function Register-EventHandlers {
# Build Tab Event Handlers # Build Tab Event Handlers
# Build USB Drive Settings Event Handlers # Build USB Drive Settings Event Handlers
$State.Controls.chkBuildUSBDriveEnable.Add_Checked({
param($eventSource, $routedEventArgs)
$window = [System.Windows.Window]::GetWindow($eventSource)
$localState = $window.Tag
$localState.Controls.usbSection.Visibility = 'Visible'
$localState.Controls.chkSelectSpecificUSBDrives.IsEnabled = $true
})
$State.Controls.chkBuildUSBDriveEnable.Add_Unchecked({
param($eventSource, $routedEventArgs)
$window = [System.Windows.Window]::GetWindow($eventSource)
$localState = $window.Tag
$localState.Controls.usbSection.Visibility = 'Collapsed'
$localState.Controls.chkSelectSpecificUSBDrives.IsEnabled = $false
$localState.Controls.chkSelectSpecificUSBDrives.IsChecked = $false
$localState.Controls.lstUSBDrives.Items.Clear()
})
$State.Controls.chkSelectSpecificUSBDrives.Add_Checked({
param($eventSource, $routedEventArgs)
$window = [System.Windows.Window]::GetWindow($eventSource)
$localState = $window.Tag
$localState.Controls.usbSelectionPanel.Visibility = 'Visible'
})
$State.Controls.chkSelectSpecificUSBDrives.Add_Unchecked({
param($eventSource, $routedEventArgs)
$window = [System.Windows.Window]::GetWindow($eventSource)
$localState = $window.Tag
$localState.Controls.usbSelectionPanel.Visibility = 'Collapsed'
$localState.Controls.lstUSBDrives.Items.Clear()
})
$State.Controls.chkAllowExternalHardDiskMedia.Add_Checked({
param($eventSource, $routedEventArgs)
$window = [System.Windows.Window]::GetWindow($eventSource)
$localState = $window.Tag
$localState.Controls.chkPromptExternalHardDiskMedia.IsEnabled = $true
})
$State.Controls.chkAllowExternalHardDiskMedia.Add_Unchecked({
param($eventSource, $routedEventArgs)
$window = [System.Windows.Window]::GetWindow($eventSource)
$localState = $window.Tag
$localState.Controls.chkPromptExternalHardDiskMedia.IsEnabled = $false
$localState.Controls.chkPromptExternalHardDiskMedia.IsChecked = $false
})
$State.Controls.btnCheckUSBDrives.Add_Click({ $State.Controls.btnCheckUSBDrives.Add_Click({
param($eventSource, $routedEventArgs) param($eventSource, $routedEventArgs)
$window = [System.Windows.Window]::GetWindow($eventSource) $window = [System.Windows.Window]::GetWindow($eventSource)
@@ -180,6 +180,10 @@ function Initialize-UIDefaults {
$State.Controls.chkRemoveFFU.IsChecked = $State.Defaults.generalDefaults.RemoveFFU $State.Controls.chkRemoveFFU.IsChecked = $State.Defaults.generalDefaults.RemoveFFU
$State.Controls.chkRemoveApps.IsChecked = $State.Defaults.generalDefaults.RemoveApps $State.Controls.chkRemoveApps.IsChecked = $State.Defaults.generalDefaults.RemoveApps
$State.Controls.chkRemoveUpdates.IsChecked = $State.Defaults.generalDefaults.RemoveUpdates $State.Controls.chkRemoveUpdates.IsChecked = $State.Defaults.generalDefaults.RemoveUpdates
$State.Controls.usbSection.Visibility = if ($State.Controls.chkBuildUSBDriveEnable.IsChecked) { 'Visible' } else { 'Collapsed' }
$State.Controls.usbSelectionPanel.Visibility = if ($State.Controls.chkSelectSpecificUSBDrives.IsChecked) { 'Visible' } else { 'Collapsed' }
$State.Controls.chkSelectSpecificUSBDrives.IsEnabled = $State.Controls.chkBuildUSBDriveEnable.IsChecked
$State.Controls.chkPromptExternalHardDiskMedia.IsEnabled = $State.Controls.chkAllowExternalHardDiskMedia.IsChecked
# Hyper-V Settings defaults from General Defaults # Hyper-V Settings defaults from General Defaults
$State.Controls.txtDiskSize.Text = $State.Defaults.generalDefaults.DiskSizeGB $State.Controls.txtDiskSize.Text = $State.Defaults.generalDefaults.DiskSizeGB
@@ -237,6 +241,7 @@ function Initialize-UIDefaults {
# Set initial state for InstallApps checkbox based on updates # Set initial state for InstallApps checkbox based on updates
Update-InstallAppsState -State $State Update-InstallAppsState -State $State
} }
function Initialize-DynamicUIElements { function Initialize-DynamicUIElements {