|
| 1 | +function Reset-WPFCheckBoxes { |
| 2 | + <# |
| 3 | +
|
| 4 | + .SYNOPSIS |
| 5 | + Set winutil checkboxs to match $sync.selected values. |
| 6 | + Should only need to be run if $sync.selected updated outside of UI (i.e. presets or import) |
| 7 | +
|
| 8 | + .PARAMETER doToggles |
| 9 | + Whether or not to set UI toggles. WARNING: they will trigger if altered |
| 10 | +
|
| 11 | + .PARAMETER checkboxfilterpattern |
| 12 | + The Pattern to use when filtering through CheckBoxes, defaults to "**" |
| 13 | + Used to make reset blazingly fast. |
| 14 | + #> |
| 15 | + |
| 16 | + param ( |
| 17 | + [Parameter(position=0)] |
| 18 | + [bool]$doToggles = $false, |
| 19 | + |
| 20 | + [Parameter(position=1)] |
| 21 | + [string]$checkboxfilterpattern = "**" |
| 22 | + ) |
| 23 | + |
| 24 | + $CheckBoxesToCheck = $sync.selectedApps + $sync.selectedTweaks + $sync.selectedFeatures |
| 25 | + $CheckBoxes = ($sync.GetEnumerator()).where{ $_.Value -is [System.Windows.Controls.CheckBox] -and $_.Name -notlike "WPFToggle*" -and $_.Name -like "$checkboxfilterpattern"} |
| 26 | + Write-Debug "Getting checkboxes to set, number of checkboxes: $($CheckBoxes.Count)" |
| 27 | + |
| 28 | + if ($CheckBoxesToCheck -ne "") { |
| 29 | + $debugMsg = "CheckBoxes to Check are: " |
| 30 | + $CheckBoxesToCheck | ForEach-Object { $debugMsg += "$_, " } |
| 31 | + $debugMsg = $debugMsg -replace (',\s*$', '') |
| 32 | + Write-Debug "$debugMsg" |
| 33 | + } |
| 34 | + |
| 35 | + foreach ($CheckBox in $CheckBoxes) { |
| 36 | + $checkboxName = $CheckBox.Key |
| 37 | + if (-not $CheckBoxesToCheck) { |
| 38 | + $sync.$checkBoxName.IsChecked = $false |
| 39 | + continue |
| 40 | + } |
| 41 | + |
| 42 | + # Check if the checkbox name exists in the flattened JSON hashtable |
| 43 | + if ($CheckBoxesToCheck -contains $checkboxName) { |
| 44 | + # If it exists, set IsChecked to true |
| 45 | + $sync.$checkboxName.IsChecked = $true |
| 46 | + Write-Debug "$checkboxName is checked" |
| 47 | + } else { |
| 48 | + # If it doesn't exist, set IsChecked to false |
| 49 | + $sync.$checkboxName.IsChecked = $false |
| 50 | + Write-Debug "$checkboxName is not checked" |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + # Update Installs tab UI values |
| 55 | + $count = $sync.SelectedApps.Count |
| 56 | + $sync.WPFselectedAppsButton.Content = "Selected Apps: $count" |
| 57 | + # 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 |
| 58 | + $sync.selectedAppsstackPanel.Children.Clear() |
| 59 | + $sync.selectedApps | Foreach-Object { Add-SelectedAppsMenuItem -name $($sync.configs.applicationsHashtable.$_.Content) -key $_ } |
| 60 | + |
| 61 | + if($doToggles) { |
| 62 | + # Restore toggle switch states |
| 63 | + $importedToggles = $sync.selectedToggles |
| 64 | + $allToggles = $sync.GetEnumerator() | Where-Object { $_.Key -like "WPFToggle*" -and $_.Value -is [System.Windows.Controls.CheckBox] } |
| 65 | + foreach ($toggle in $allToggles) { |
| 66 | + if ($importedToggles -contains $toggle.Key) { |
| 67 | + $sync[$toggle.Key].IsChecked = $true |
| 68 | + Write-Debug "Restoring toggle: $($toggle.Key) = checked" |
| 69 | + } else { |
| 70 | + $sync[$toggle.Key].IsChecked = $false |
| 71 | + Write-Debug "Restoring toggle: $($toggle.Key) = unchecked" |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments