Files
winutil/functions/private/Reset-WPFCheckBoxes.ps1
T

54 lines
2.6 KiB
PowerShell

function Reset-WPFCheckBoxes {
<#
.SYNOPSIS
Set winutil checkboxs to match $sync.selected values.
Should only need to be run if $sync.selected updated outside of UI (i.e. presets or import)
.PARAMETER doToggles
Whether or not to set UI toggles. WARNING: they will trigger if altered
.PARAMETER checkboxfilterpattern
The Pattern to use when filtering through CheckBoxes, defaults to "**"
Used to make reset blazingly fast.
#>
param (
[Parameter(position=0)]
[bool]$doToggles = $false,
[Parameter(position=1)]
[string]$checkboxfilterpattern = "**"
)
$selectedSet = [System.Collections.Generic.HashSet[string]]::new([string[]]@($sync.selectedApps + $sync.selectedTweaks + $sync.selectedFeatures + $sync.selectedAppx), [StringComparer]::OrdinalIgnoreCase)
foreach ($syncEntry in $sync.GetEnumerator()) {
if ($syncEntry.Value -is [System.Windows.Controls.CheckBox] -and $syncEntry.Name -notlike "WPFToggle*" -and $syncEntry.Name -like $checkboxfilterpattern) {
$checkboxName = $syncEntry.Key
$sync.$checkboxName.IsChecked = $selectedSet.Contains($checkboxName)
}
}
# Update Installs tab UI values
$count = $sync.SelectedApps.Count
$sync.WPFselectedAppsButton.Content = "Selected Apps: $count"
# On every change, remove all entries inside the Popup Menu. This is done, so we can keep the alphabetical order even if elements are selected in a random way
$sync.selectedAppsstackPanel.Children.Clear()
$sync.selectedApps | Foreach-Object { Add-SelectedAppsMenuItem -name $($sync.configs.applicationsHashtable.$_.Content) -key $_ }
if($doToggles) {
# 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 = [System.Collections.Generic.HashSet[string]]::new([string[]]@($sync.selectedToggles), [StringComparer]::OrdinalIgnoreCase)
foreach ($toggle in $sync.GetEnumerator()) {
if ($toggle.Key -like "WPFToggle*" -and $toggle.Value -is [System.Windows.Controls.CheckBox] -and $importedToggles.Contains($toggle.Key)) {
$sync[$toggle.Key].IsChecked = $true
}
# Toggles not present in the import are intentionally left untouched;
# their current UI state already reflects the real system state.
}
}
}