mirror of
https://github.com/ChrisTitusTech/winutil
synced 2026-04-05 22:28:31 +00:00
Decoupled UI and data (#4051)
* Decoupled UI and data * Fix bad variable naming * Fix mistype * Fix mistype v2 Editing from mobile is hard
This commit is contained in:
@@ -1,92 +0,0 @@
|
||||
Function Get-WinUtilCheckBoxes {
|
||||
|
||||
<#
|
||||
|
||||
.SYNOPSIS
|
||||
Finds all checkboxes that are checked on the specific tab and inputs them into a script.
|
||||
|
||||
.PARAMETER unCheck
|
||||
Whether to uncheck the checkboxes that are checked. Defaults to true
|
||||
|
||||
.OUTPUTS
|
||||
A List containing the name of each checked checkbox
|
||||
|
||||
.EXAMPLE
|
||||
Get-WinUtilCheckBoxes "WPFInstall"
|
||||
|
||||
#>
|
||||
|
||||
Param(
|
||||
[boolean]$unCheck = $false
|
||||
)
|
||||
|
||||
$Output = @{
|
||||
Install = @()
|
||||
WPFTweaks = @()
|
||||
WPFFeature = @()
|
||||
WPFInstall = @()
|
||||
WPFToggle = @()
|
||||
}
|
||||
|
||||
$CheckBoxes = $sync.GetEnumerator() | Where-Object { $_.Value -is [System.Windows.Controls.CheckBox] }
|
||||
|
||||
# Collect toggle switch states
|
||||
foreach ($CheckBox in $CheckBoxes) {
|
||||
if ($CheckBox.Key -like "WPFToggle*" -and $CheckBox.Value.IsChecked -eq $true) {
|
||||
$Output["WPFToggle"] += $CheckBox.Key
|
||||
Write-Debug "Adding toggle: $($CheckBox.Key)"
|
||||
}
|
||||
}
|
||||
|
||||
# First check and add WPFTweaksRestorePoint if checked
|
||||
$RestorePoint = $CheckBoxes | Where-Object { $_.Key -eq 'WPFTweaksRestorePoint' -and $_.Value.IsChecked -eq $true }
|
||||
if ($RestorePoint) {
|
||||
$Output["WPFTweaks"] = @('WPFTweaksRestorePoint')
|
||||
Write-Debug "Adding WPFTweaksRestorePoint as first in WPFTweaks"
|
||||
|
||||
if ($unCheck) {
|
||||
$RestorePoint.Value.IsChecked = $false
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($CheckBox in $CheckBoxes) {
|
||||
if ($CheckBox.Key -eq 'WPFTweaksRestorePoint') { continue } # Skip since it's already handled
|
||||
|
||||
$group = if ($CheckBox.Key.StartsWith("WPFInstall")) { "Install" }
|
||||
elseif ($CheckBox.Key.StartsWith("WPFTweaks")) { "WPFTweaks" }
|
||||
elseif ($CheckBox.Key.StartsWith("WPFFeature")) { "WPFFeature" }
|
||||
if ($group) {
|
||||
if ($CheckBox.Value.IsChecked -eq $true) {
|
||||
$feature = switch ($group) {
|
||||
"Install" {
|
||||
# Get the winget value
|
||||
[PsCustomObject]@{
|
||||
winget="$($sync.configs.applications.$($CheckBox.Name).winget)";
|
||||
choco="$($sync.configs.applications.$($CheckBox.Name).choco)";
|
||||
}
|
||||
|
||||
}
|
||||
default {
|
||||
$CheckBox.Name
|
||||
}
|
||||
}
|
||||
|
||||
if (-not $Output.ContainsKey($group)) {
|
||||
$Output[$group] = @()
|
||||
}
|
||||
if ($group -eq "Install") {
|
||||
$Output["WPFInstall"] += $CheckBox.Name
|
||||
Write-Debug "Adding: $($CheckBox.Name) under: WPFInstall"
|
||||
}
|
||||
|
||||
Write-Debug "Adding: $($feature) under: $($group)"
|
||||
$Output[$group] += $feature
|
||||
|
||||
if ($unCheck) {
|
||||
$CheckBox.Value.IsChecked = $false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $Output
|
||||
}
|
||||
@@ -44,13 +44,13 @@ function Initialize-InstallAppEntry {
|
||||
$checkBox.Name = $appKey
|
||||
$checkbox.Style = $sync.Form.Resources.AppEntryCheckboxStyle
|
||||
$checkbox.Add_Checked({
|
||||
Invoke-WPFSelectedAppsUpdate -type "Add" -checkbox $this
|
||||
Invoke-WPFSelectedCheckboxesUpdate -type "Add" -checkboxName $this.Parent.Tag
|
||||
$borderElement = $this.Parent
|
||||
$borderElement.SetResourceReference([Windows.Controls.Control]::BackgroundProperty, "AppInstallSelectedColor")
|
||||
})
|
||||
|
||||
$checkbox.Add_Unchecked({
|
||||
Invoke-WPFSelectedAppsUpdate -type "Remove" -checkbox $this
|
||||
Invoke-WPFSelectedCheckboxesUpdate -type "Remove" -checkboxName $this.Parent.Tag
|
||||
$borderElement = $this.Parent
|
||||
$borderElement.SetResourceReference([Windows.Controls.Control]::BackgroundProperty, "AppInstallUnselectedColor")
|
||||
})
|
||||
|
||||
@@ -6,7 +6,7 @@ Function Invoke-WinUtilCurrentSystem {
|
||||
Checks to see what tweaks have already been applied and what programs are installed, and checks the according boxes
|
||||
|
||||
.EXAMPLE
|
||||
Get-WinUtilCheckBoxes "WPFInstall"
|
||||
InvokeWinUtilCurrentSystem -Checkbox "winget"
|
||||
|
||||
#>
|
||||
|
||||
|
||||
75
functions/private/Reset-WPFCheckBoxes.ps1
Normal file
75
functions/private/Reset-WPFCheckBoxes.ps1
Normal file
@@ -0,0 +1,75 @@
|
||||
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"}
|
||||
Write-Debug "Getting checkboxes to set, number of checkboxes: $($CheckBoxes.Count)"
|
||||
|
||||
if ($CheckBoxesToCheck -ne "") {
|
||||
$debugMsg = "CheckBoxes to Check are: "
|
||||
$CheckBoxesToCheck | ForEach-Object { $debugMsg += "$_, " }
|
||||
$debugMsg = $debugMsg -replace (',\s*$', '')
|
||||
Write-Debug "$debugMsg"
|
||||
}
|
||||
|
||||
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
|
||||
Write-Debug "$checkboxName is checked"
|
||||
} else {
|
||||
# If it doesn't exist, set IsChecked to false
|
||||
$sync.$checkboxName.IsChecked = $false
|
||||
Write-Debug "$checkboxName is not checked"
|
||||
}
|
||||
}
|
||||
|
||||
# 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
|
||||
$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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
59
functions/private/Update-WinUtilSelections.ps1
Normal file
59
functions/private/Update-WinUtilSelections.ps1
Normal file
@@ -0,0 +1,59 @@
|
||||
function Update-WinUtilSelections {
|
||||
<#
|
||||
|
||||
.SYNOPSIS
|
||||
Updates the $sync.selected variables with a given preset.
|
||||
|
||||
.PARAMETER flatJson
|
||||
The flattened json list of $sync values to select.
|
||||
#>
|
||||
|
||||
param (
|
||||
$flatJson
|
||||
)
|
||||
|
||||
Write-Debug "JSON to import: $($flatJson)"
|
||||
|
||||
foreach ($cbkey in $flatJson) {
|
||||
$group = if ($cbkey.StartsWith("WPFInstall")) { "Install" }
|
||||
elseif ($cbkey.StartsWith("WPFTweaks")) { "Tweaks" }
|
||||
elseif ($cbkey.StartsWith("WPFToggle")) { "Toggle" }
|
||||
elseif ($cbkey.StartsWith("WPFFeature")) { "Feature" }
|
||||
else { "na" }
|
||||
|
||||
switch ($group) {
|
||||
"Install" {
|
||||
if (!$sync.selectedApps.Contains($cbkey)) {
|
||||
$sync.selectedApps.Add($cbkey)
|
||||
# The List type needs to be specified again, because otherwise Sort-Object will convert the list to a string if there is only a single entry
|
||||
[System.Collections.Generic.List[pscustomobject]]$sync.selectedApps = $sync.SelectedApps | Sort-Object
|
||||
}
|
||||
}
|
||||
"Tweaks" {
|
||||
if (!$sync.selectedTweaks.Contains($cbkey)) {
|
||||
$sync.selectedTweaks.Add($cbkey)
|
||||
}
|
||||
}
|
||||
"Toggle" {
|
||||
if (!$sync.selectedToggles.Contains($cbkey)) {
|
||||
$sync.selectedToggles.Add($cbkey)
|
||||
}
|
||||
}
|
||||
"Feature" {
|
||||
if (!$sync.selectedFeatures.Contains($cbkey)) {
|
||||
$sync.selectedFeatures.Add($cbkey)
|
||||
}
|
||||
}
|
||||
default {
|
||||
Write-Host "Unknown group for checkbox: $($cbkey)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Write-Debug "-------------------------------------"
|
||||
Write-Debug "Selected Apps: $($sync.selectedApps)"
|
||||
Write-Debug "Selected Tweaks: $($sync.selectedTweaks)"
|
||||
Write-Debug "Selected Toggles: $($sync.selectedToggles)"
|
||||
Write-Debug "Selected Features: $($sync.selectedFeatures)"
|
||||
Write-Debug "--------------------------------------"
|
||||
}
|
||||
Reference in New Issue
Block a user