From 094e084316dd32bd4d10ce5037920e8df4ebca84 Mon Sep 17 00:00:00 2001 From: rbalsleyMSFT <53497092+rbalsleyMSFT@users.noreply.github.com> Date: Thu, 19 Jun 2025 16:37:32 -0700 Subject: [PATCH] Refactor and centralize UI panel visibility logic Consolidates the logic for showing and hiding UI elements into dedicated functions, `Update-DriverDownloadPanelVisibility` and `Update-OfficePanelVisibility`. This change simplifies the event handlers for the "Download Drivers" checkbox by using a single handler for both checked and unchecked states. It also moves the initial UI setup from the main script into the core initialization module, improving code organization and reducing duplication. --- FFUDevelopment/BuildFFUVM_UI.ps1 | 19 +-------- .../FFUUI.Core/FFUUI.Core.Handlers.psm1 | 35 +++++----------- .../FFUUI.Core/FFUUI.Core.Initialize.psm1 | 13 ++++++ FFUDevelopment/FFUUI.Core/FFUUI.Core.psm1 | 42 +++++++++++++++++++ 4 files changed, 66 insertions(+), 43 deletions(-) diff --git a/FFUDevelopment/BuildFFUVM_UI.ps1 b/FFUDevelopment/BuildFFUVM_UI.ps1 index 10ee23c..886b06a 100644 --- a/FFUDevelopment/BuildFFUVM_UI.ps1 +++ b/FFUDevelopment/BuildFFUVM_UI.ps1 @@ -135,24 +135,7 @@ $window.Add_Loaded({ Register-EventHandlers -State $script:uiState - # Drivers tab UI logic - $makeList = @('Microsoft', 'Dell', 'HP', 'Lenovo') - foreach ($m in $makeList) { - [void]$script:uiState.Controls.cmbMake.Items.Add($m) - } - if ($script:uiState.Controls.cmbMake.Items.Count -gt 0) { - $script:uiState.Controls.cmbMake.SelectedIndex = 0 - } - $script:uiState.Controls.spMakeSection.Visibility = if ($script:uiState.Controls.chkDownloadDrivers.IsChecked) { - 'Visible' - } - else { - 'Collapsed' - } - $script:uiState.Controls.btnGetModels.Visibility = if ($script:uiState.Controls.chkDownloadDrivers.IsChecked) { 'Visible' } else { 'Collapsed' } - $script:uiState.Controls.spModelFilterSection.Visibility = 'Collapsed' - $script:uiState.Controls.lstDriverModels.Visibility = 'Collapsed' - $script:uiState.Controls.spDriverActionButtons.Visibility = 'Collapsed' + # Office interplay (Keep existing logic) $script:uiState.Flags.installAppsCheckedByOffice = $false diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 index 1a80870..504272d 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 @@ -704,31 +704,16 @@ function Register-EventHandlers { } }) - $State.Controls.chkDownloadDrivers.Add_Checked({ - param($eventSource, $routedEventArgs) - $window = [System.Windows.Window]::GetWindow($eventSource) - $localState = $window.Tag - $localState.Controls.cmbMake.Visibility = 'Visible' - $localState.Controls.btnGetModels.Visibility = 'Visible' - $localState.Controls.spMakeSection.Visibility = 'Visible' - $localState.Controls.spModelFilterSection.Visibility = 'Visible' - $localState.Controls.lstDriverModels.Visibility = 'Visible' - $localState.Controls.spDriverActionButtons.Visibility = 'Visible' - }) - $State.Controls.chkDownloadDrivers.Add_Unchecked({ - param($eventSource, $routedEventArgs) - $window = [System.Windows.Window]::GetWindow($eventSource) - $localState = $window.Tag - $localState.Controls.cmbMake.Visibility = 'Collapsed' - $localState.Controls.btnGetModels.Visibility = 'Collapsed' - $localState.Controls.spMakeSection.Visibility = 'Collapsed' - $localState.Controls.spModelFilterSection.Visibility = 'Collapsed' - $localState.Controls.lstDriverModels.Visibility = 'Collapsed' - $localState.Controls.spDriverActionButtons.Visibility = 'Collapsed' - $localState.Controls.lstDriverModels.ItemsSource = $null - $localState.Data.allDriverModels.Clear() - $localState.Controls.txtModelFilter.Text = "" - }) + # Define a single handler for the Download Drivers checkbox + $driverDownloadCheckboxHandler = { + param($eventSource, $routedEventArgs) + $window = [System.Windows.Window]::GetWindow($eventSource) + if ($null -ne $window) { + Update-DriverDownloadPanelVisibility -State $window.Tag + } + } + $State.Controls.chkDownloadDrivers.Add_Checked($driverDownloadCheckboxHandler) + $State.Controls.chkDownloadDrivers.Add_Unchecked($driverDownloadCheckboxHandler) $State.Controls.btnGetModels.Add_Click({ param($eventSource, $routedEventArgs) diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Initialize.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Initialize.psm1 index 43902d6..0bdab62 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Initialize.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Initialize.psm1 @@ -239,6 +239,16 @@ function Initialize-UIDefaults { $State.Controls.chkCopyDrivers.IsChecked = $State.Defaults.generalDefaults.CopyDrivers $State.Controls.chkCopyPEDrivers.IsChecked = $State.Defaults.generalDefaults.CopyPEDrivers $State.Controls.chkCompressDriversToWIM.IsChecked = $State.Defaults.generalDefaults.CompressDownloadedDriversToWim + + # Drivers tab UI logic + $makeList = @('Microsoft', 'Dell', 'HP', 'Lenovo') + foreach ($m in $makeList) { + [void]$State.Controls.cmbMake.Items.Add($m) + } + if ($State.Controls.cmbMake.Items.Count -gt 0) { + $State.Controls.cmbMake.SelectedIndex = 0 + } + Update-DriverDownloadPanelVisibility -State $State # Set initial state for driver checkbox interplay Update-DriverCheckboxStates -State $State @@ -246,6 +256,9 @@ function Initialize-UIDefaults { # Set initial state for InstallApps checkbox based on updates Update-InstallAppsState -State $State + # Set initial state for Office panel visibility + Update-OfficePanelVisibility -State $State + } function Initialize-DynamicUIElements { diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.psm1 index 402371a..08ed5cb 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.psm1 @@ -253,6 +253,48 @@ function Update-DriverCheckboxStates { } } +# Function to manage the visibility of Office UI panels +function Update-OfficePanelVisibility { + param([PSCustomObject]$State) + + if ($State.Controls.chkInstallOffice.IsChecked) { + $State.Controls.OfficePathStackPanel.Visibility = 'Visible' + $State.Controls.OfficePathGrid.Visibility = 'Visible' + $State.Controls.CopyOfficeConfigXMLStackPanel.Visibility = 'Visible' + # Show/hide XML file path based on checkbox state + $State.Controls.OfficeConfigurationXMLFileStackPanel.Visibility = if ($State.Controls.chkCopyOfficeConfigXML.IsChecked) { 'Visible' } else { 'Collapsed' } + $State.Controls.OfficeConfigurationXMLFileGrid.Visibility = if ($State.Controls.chkCopyOfficeConfigXML.IsChecked) { 'Visible' } else { 'Collapsed' } + } + else { + $State.Controls.OfficePathStackPanel.Visibility = 'Collapsed' + $State.Controls.OfficePathGrid.Visibility = 'Collapsed' + $State.Controls.CopyOfficeConfigXMLStackPanel.Visibility = 'Collapsed' + $State.Controls.OfficeConfigurationXMLFileStackPanel.Visibility = 'Collapsed' + $State.Controls.OfficeConfigurationXMLFileGrid.Visibility = 'Collapsed' + } +} + +# Function to manage the visibility of the driver download UI panels +function Update-DriverDownloadPanelVisibility { + param([PSCustomObject]$State) + + if ($State.Controls.chkDownloadDrivers.IsChecked) { + $State.Controls.spMakeSection.Visibility = 'Visible' + $State.Controls.btnGetModels.Visibility = 'Visible' + # The other panels are shown/hidden by the Get Models button click handler + } + else { + $State.Controls.spMakeSection.Visibility = 'Collapsed' + $State.Controls.btnGetModels.Visibility = 'Collapsed' + $State.Controls.spModelFilterSection.Visibility = 'Collapsed' + $State.Controls.lstDriverModels.Visibility = 'Collapsed' + $State.Controls.spDriverActionButtons.Visibility = 'Collapsed' + $State.Controls.lstDriverModels.ItemsSource = $null + $State.Data.allDriverModels.Clear() + $State.Controls.txtModelFilter.Text = "" + } +} + # -------------------------------------------------------------------------- # SECTION: Module Export # --------------------------------------------------------------------------