diff --git a/functions/private/Reset-WPFCheckBoxes.ps1 b/functions/private/Reset-WPFCheckBoxes.ps1 index b19b316e..59ae6533 100644 --- a/functions/private/Reset-WPFCheckBoxes.ps1 +++ b/functions/private/Reset-WPFCheckBoxes.ps1 @@ -59,17 +59,19 @@ function Reset-WPFCheckBoxes { $sync.selectedApps | Foreach-Object { Add-SelectedAppsMenuItem -name $($sync.configs.applicationsHashtable.$_.Content) -key $_ } if($doToggles) { - # Restore toggle switch states + # Restore toggle switch states from imported config. + # Only act on toggles that are explicitly listed in the import — toggles absent + # from the export file were not part of the saved config and should keep whatever + # state the live system already has (set during UI initialisation via Get-WinUtilToggleStatus). $importedToggles = $sync.selectedToggles $allToggles = $sync.GetEnumerator() | Where-Object { $_.Key -like "WPFToggle*" -and $_.Value -is [System.Windows.Controls.CheckBox] } foreach ($toggle in $allToggles) { if ($importedToggles -contains $toggle.Key) { $sync[$toggle.Key].IsChecked = $true Write-Debug "Restoring toggle: $($toggle.Key) = checked" - } else { - $sync[$toggle.Key].IsChecked = $false - Write-Debug "Restoring toggle: $($toggle.Key) = unchecked" } + # Toggles not present in the import are intentionally left untouched; + # their current UI state already reflects the real system state. } } } diff --git a/functions/public/Invoke-WPFImpex.ps1 b/functions/public/Invoke-WPFImpex.ps1 index 67e1b129..08a6138f 100644 --- a/functions/public/Invoke-WPFImpex.ps1 +++ b/functions/public/Invoke-WPFImpex.ps1 @@ -94,7 +94,14 @@ function Invoke-WPFImpex { Update-WinUtilSelections -flatJson $flattenedJson if (!$PARAM_NOUI) { - Reset-WPFCheckBoxes -doToggles $true + # Set flag so toggle Checked/Unchecked events don't trigger registry writes + # while we're programmatically restoring UI state from the imported config + $sync.ImportInProgress = $true + try { + Reset-WPFCheckBoxes -doToggles $true + } finally { + $sync.ImportInProgress = $false + } } } } catch { diff --git a/functions/public/Invoke-WPFUIElements.ps1 b/functions/public/Invoke-WPFUIElements.ps1 index 245bfc3b..15073801 100644 --- a/functions/public/Invoke-WPFUIElements.ps1 +++ b/functions/public/Invoke-WPFUIElements.ps1 @@ -193,13 +193,19 @@ function Invoke-WPFUIElements { $sync[$entryInfo.Name].Add_Checked({ [System.Object]$Sender = $args[0] Invoke-WPFSelectedCheckboxesUpdate -type "Add" -checkboxName $Sender.name - Invoke-WinUtilTweaks $Sender.name + # Skip applying tweaks while an import is restoring toggle states + if (-not $sync.ImportInProgress) { + Invoke-WinUtilTweaks $Sender.name + } }) $sync[$entryInfo.Name].Add_Unchecked({ [System.Object]$Sender = $args[0] Invoke-WPFSelectedCheckboxesUpdate -type "Remove" -checkboxName $Sender.name - Invoke-WinUtiltweaks $Sender.name -undo $true + # Skip undoing tweaks while an import is restoring toggle states + if (-not $sync.ImportInProgress) { + Invoke-WinUtiltweaks $Sender.name -undo $true + } }) } }