From 49b077207656a8023035f224c83f21bc4ceba329 Mon Sep 17 00:00:00 2001 From: rbalsleyMSFT <53497092+rbalsleyMSFT@users.noreply.github.com> Date: Wed, 18 Jun 2025 17:30:23 -0700 Subject: [PATCH] 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. --- FFUDevelopment/BuildFFUVM_UI.ps1 | 29 ------------- .../FFUUI.Core/FFUUI.Core.Handlers.psm1 | 43 +++++++++++++++++++ .../FFUUI.Core/FFUUI.Core.Initialize.psm1 | 5 +++ 3 files changed, 48 insertions(+), 29 deletions(-) diff --git a/FFUDevelopment/BuildFFUVM_UI.ps1 b/FFUDevelopment/BuildFFUVM_UI.ps1 index dbf1877..77668e3 100644 --- a/FFUDevelopment/BuildFFUVM_UI.ps1 +++ b/FFUDevelopment/BuildFFUVM_UI.ps1 @@ -194,35 +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.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) $script:uiState.Controls.chkInstallWingetApps.Visibility = if ($script:uiState.Controls.chkInstallApps.IsChecked) { 'Visible' } else { 'Collapsed' } diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 index 9050699..89df5fe 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 @@ -4,6 +4,49 @@ function Register-EventHandlers { # Build Tab 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({ param($eventSource, $routedEventArgs) $window = [System.Windows.Window]::GetWindow($eventSource) diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Initialize.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Initialize.psm1 index 098cbbb..09e146c 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Initialize.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Initialize.psm1 @@ -180,6 +180,10 @@ function Initialize-UIDefaults { $State.Controls.chkRemoveFFU.IsChecked = $State.Defaults.generalDefaults.RemoveFFU $State.Controls.chkRemoveApps.IsChecked = $State.Defaults.generalDefaults.RemoveApps $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 $State.Controls.txtDiskSize.Text = $State.Defaults.generalDefaults.DiskSizeGB @@ -237,6 +241,7 @@ function Initialize-UIDefaults { # Set initial state for InstallApps checkbox based on updates Update-InstallAppsState -State $State + } function Initialize-DynamicUIElements {