mirror of
https://github.com/rbalsleyMSFT/FFU.git
synced 2026-06-14 02:09:35 -06:00
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:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user