From 78d7bb92623ea81d77e5df49f0732f2fa3580eea Mon Sep 17 00:00:00 2001 From: rbalsleyMSFT <53497092+rbalsleyMSFT@users.noreply.github.com> Date: Tue, 12 Aug 2025 12:14:03 -0700 Subject: [PATCH] feat: Add VM switch selection logic and persist custom names/IPs in UI - Implemented Select-VMSwitchFromConfig function to handle VM switch selection based on configuration. - Enhanced Register-EventHandlers to persist custom VM switch name and IP address when 'Other' is selected. - Improved user experience by ensuring relevant fields are populated correctly based on user input and configuration settings. --- .../FFUUI.Core/FFUUI.Core.Config.psm1 | 54 ++++++++++++++++++- .../FFUUI.Core/FFUUI.Core.Handlers.psm1 | 25 ++++++++- 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Config.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Config.psm1 index 6216a36..030d78b 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Config.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Config.psm1 @@ -261,6 +261,58 @@ function Invoke-LoadConfiguration { } } +function Select-VMSwitchFromConfig { + param( + [Parameter(Mandatory = $true)] + [psobject]$State, + [Parameter(Mandatory = $true)] + [psobject]$ConfigContent + ) + + # Select VM switch based on configuration; fall back to 'Other' with custom name. + $combo = $State.Controls.cmbVMSwitchName + if ($null -eq $combo) { + WriteLog "LoadConfig Error: 'cmbVMSwitchName' control not found." + return + } + + $configSwitch = $ConfigContent.VMSwitchName + if ($null -eq $configSwitch -or [string]::IsNullOrWhiteSpace($configSwitch)) { + WriteLog "LoadConfig Info: VMSwitchName in config was empty or null. Leaving selection unchanged." + return + } + + $itemFound = $false + foreach ($item in $combo.Items) { + if ($null -ne $item -and $item.ToString().Equals($configSwitch, [System.StringComparison]::OrdinalIgnoreCase)) { + $itemFound = $true + break + } + } + + if ($itemFound) { + $combo.SelectedItem = ($combo.Items | Where-Object { $_.ToString().Equals($configSwitch, [System.StringComparison]::OrdinalIgnoreCase) } | Select-Object -First 1) + $State.Controls.txtCustomVMSwitchName.Visibility = 'Collapsed' + WriteLog "LoadConfig: Selected existing VM switch '$configSwitch'." + } + else { + # Ensure 'Other' exists + $otherExists = $false + foreach ($item in $combo.Items) { + if ($null -ne $item -and $item.ToString() -eq 'Other') { $otherExists = $true; break } + } + if (-not $otherExists) { $combo.Items.Add('Other') | Out-Null } + + # Select 'Other' and populate custom name + $combo.SelectedItem = 'Other' + $State.Controls.txtCustomVMSwitchName.Visibility = 'Visible' + $State.Controls.txtCustomVMSwitchName.Text = $configSwitch + $State.Data.customVMSwitchName = $configSwitch + $State.Data.customVMHostIP = $ConfigContent.VMHostIPAddress + WriteLog "LoadConfig: VMSwitchName '$configSwitch' not found. Selected 'Other' and populated custom VM Switch Name textbox." + } +} + function Update-UIFromConfig { param( [Parameter(Mandatory = $true)] @@ -305,7 +357,7 @@ function Update-UIFromConfig { Set-UIValue -ControlName 'chkRemoveUpdates' -PropertyName 'IsChecked' -ConfigObject $ConfigContent -ConfigKey 'RemoveUpdates' -State $State # Hyper-V Settings - Set-UIValue -ControlName 'cmbVMSwitchName' -PropertyName 'SelectedItem' -ConfigObject $ConfigContent -ConfigKey 'VMSwitchName' -State $State + Select-VMSwitchFromConfig -State $State -ConfigContent $ConfigContent Set-UIValue -ControlName 'txtVMHostIPAddress' -PropertyName 'Text' -ConfigObject $ConfigContent -ConfigKey 'VMHostIPAddress' -State $State Set-UIValue -ControlName 'txtDiskSize' -PropertyName 'Text' -ConfigObject $ConfigContent -ConfigKey 'Disksize' -TransformValue { param($val) $val / 1GB } -State $State Set-UIValue -ControlName 'txtMemory' -PropertyName 'Text' -ConfigObject $ConfigContent -ConfigKey 'Memory' -TransformValue { param($val) $val / 1GB } -State $State diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 index 95f5d25..59bf3a4 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 @@ -207,7 +207,12 @@ function Register-EventHandlers { $selectedItem = $eventSource.SelectedItem if ($selectedItem -eq 'Other') { $localState.Controls.txtCustomVMSwitchName.Visibility = 'Visible' - $localState.Controls.txtVMHostIPAddress.Text = '' # Clear IP for custom + if ([string]::IsNullOrWhiteSpace($localState.Controls.txtCustomVMSwitchName.Text) -and $null -ne $localState.Data.customVMSwitchName) { + $localState.Controls.txtCustomVMSwitchName.Text = $localState.Data.customVMSwitchName + } + if ($null -ne $localState.Data.customVMHostIP -and -not [string]::IsNullOrWhiteSpace($localState.Data.customVMHostIP)) { + $localState.Controls.txtVMHostIPAddress.Text = $localState.Data.customVMHostIP + } } else { $localState.Controls.txtCustomVMSwitchName.Visibility = 'Collapsed' @@ -220,6 +225,24 @@ function Register-EventHandlers { } }) + # Persist custom VM switch name/IP when user edits them while 'Other' is selected + $State.Controls.txtVMHostIPAddress.Add_LostFocus({ + param($eventSource, $routedEventArgs) + $window = [System.Windows.Window]::GetWindow($eventSource) + $localState = $window.Tag + if ($localState.Controls.cmbVMSwitchName.SelectedItem -eq 'Other') { + $localState.Data.customVMHostIP = $localState.Controls.txtVMHostIPAddress.Text + } + }) + $State.Controls.txtCustomVMSwitchName.Add_LostFocus({ + param($eventSource, $routedEventArgs) + $window = [System.Windows.Window]::GetWindow($eventSource) + $localState = $window.Tag + if ($localState.Controls.cmbVMSwitchName.SelectedItem -eq 'Other') { + $localState.Data.customVMSwitchName = $localState.Controls.txtCustomVMSwitchName.Text + } + }) + # Windows Settings tab Event Handlers $State.Controls.txtISOPath.Add_TextChanged({ param($eventSource, $textChangedEventArgs)