Files
winutil/functions/private/Reset-WPFCheckBoxes.ps1
T
CodingWonders 297b3079e5 Fix slop remains (#4640)
* Fix slop remains

Chris likes to vibe code. AI likes to use different types of dashes (em-dashes, en-dashes), instead of regular dashes (-).

On PowerShell 7 this appears to work. The parser doesn't care. But, what about PowerShell 5? It begins throwing errors about the ampersand. Loading it in the ISE reveals how it stops parsing the syntax correctly when it encounters em-dashes. Rather than displaying them as such, the ISE displays them as —.

Chris, if you want to vibe code, I don't mind. But at least use regular dashes.

Anyway, I won't fix the AI code. I just want to make it work on my beloved powershell 5.

* Apparently this file is important
2026-06-08 09:43:04 -05:00

67 lines
2.8 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 = "**"
)
$CheckBoxesToCheck = $sync.selectedApps + $sync.selectedTweaks + $sync.selectedFeatures
$CheckBoxes = ($sync.GetEnumerator()).where{ $_.Value -is [System.Windows.Controls.CheckBox] -and $_.Name -notlike "WPFToggle*" -and $_.Name -like "$checkboxfilterpattern"}
foreach ($CheckBox in $CheckBoxes) {
$checkboxName = $CheckBox.Key
if (-not $CheckBoxesToCheck) {
$sync.$checkBoxName.IsChecked = $false
continue
}
# Check if the checkbox name exists in the flattened JSON hashtable
if ($CheckBoxesToCheck -contains $checkboxName) {
# If it exists, set IsChecked to true
$sync.$checkboxName.IsChecked = $true
} else {
# If it doesn't exist, set IsChecked to false
$sync.$checkboxName.IsChecked = $false
}
}
# 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 = $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
}
# Toggles not present in the import are intentionally left untouched;
# their current UI state already reflects the real system state.
}
}
}