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.
This commit is contained in:
rbalsleyMSFT
2025-08-12 12:14:03 -07:00
parent 3c545be5c5
commit 78d7bb9262
2 changed files with 77 additions and 2 deletions
@@ -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 { function Update-UIFromConfig {
param( param(
[Parameter(Mandatory = $true)] [Parameter(Mandatory = $true)]
@@ -305,7 +357,7 @@ function Update-UIFromConfig {
Set-UIValue -ControlName 'chkRemoveUpdates' -PropertyName 'IsChecked' -ConfigObject $ConfigContent -ConfigKey 'RemoveUpdates' -State $State Set-UIValue -ControlName 'chkRemoveUpdates' -PropertyName 'IsChecked' -ConfigObject $ConfigContent -ConfigKey 'RemoveUpdates' -State $State
# Hyper-V Settings # 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 '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 '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 Set-UIValue -ControlName 'txtMemory' -PropertyName 'Text' -ConfigObject $ConfigContent -ConfigKey 'Memory' -TransformValue { param($val) $val / 1GB } -State $State
@@ -207,7 +207,12 @@ function Register-EventHandlers {
$selectedItem = $eventSource.SelectedItem $selectedItem = $eventSource.SelectedItem
if ($selectedItem -eq 'Other') { if ($selectedItem -eq 'Other') {
$localState.Controls.txtCustomVMSwitchName.Visibility = 'Visible' $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 { else {
$localState.Controls.txtCustomVMSwitchName.Visibility = 'Collapsed' $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 # Windows Settings tab Event Handlers
$State.Controls.txtISOPath.Add_TextChanged({ $State.Controls.txtISOPath.Add_TextChanged({
param($eventSource, $textChangedEventArgs) param($eventSource, $textChangedEventArgs)