Skip to content

Commit 70a94ab

Browse files
Decoupled UI and data (#4051)
* Decoupled UI and data * Fix bad variable naming * Fix mistype * Fix mistype v2 Editing from mobile is hard
1 parent 0e41122 commit 70a94ab

14 files changed

+276
-189
lines changed

functions/private/Get-WinUtilCheckBoxes.ps1

Lines changed: 0 additions & 92 deletions
This file was deleted.

functions/private/Initialize-InstallAppEntry.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ function Initialize-InstallAppEntry {
4444
$checkBox.Name = $appKey
4545
$checkbox.Style = $sync.Form.Resources.AppEntryCheckboxStyle
4646
$checkbox.Add_Checked({
47-
Invoke-WPFSelectedAppsUpdate -type "Add" -checkbox $this
47+
Invoke-WPFSelectedCheckboxesUpdate -type "Add" -checkboxName $this.Parent.Tag
4848
$borderElement = $this.Parent
4949
$borderElement.SetResourceReference([Windows.Controls.Control]::BackgroundProperty, "AppInstallSelectedColor")
5050
})
5151

5252
$checkbox.Add_Unchecked({
53-
Invoke-WPFSelectedAppsUpdate -type "Remove" -checkbox $this
53+
Invoke-WPFSelectedCheckboxesUpdate -type "Remove" -checkboxName $this.Parent.Tag
5454
$borderElement = $this.Parent
5555
$borderElement.SetResourceReference([Windows.Controls.Control]::BackgroundProperty, "AppInstallUnselectedColor")
5656
})

functions/private/Invoke-WinUtilCurrentSystem.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Function Invoke-WinUtilCurrentSystem {
66
Checks to see what tweaks have already been applied and what programs are installed, and checks the according boxes
77
88
.EXAMPLE
9-
Get-WinUtilCheckBoxes "WPFInstall"
9+
InvokeWinUtilCurrentSystem -Checkbox "winget"
1010
1111
#>
1212

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
function Update-WinUtilSelections {
2+
<#
3+
4+
.SYNOPSIS
5+
Updates the $sync.selected variables with a given preset.
6+
7+
.PARAMETER flatJson
8+
The flattened json list of $sync values to select.
9+
#>
10+
11+
param (
12+
$flatJson
13+
)
14+
15+
Write-Debug "JSON to import: $($flatJson)"
16+
17+
foreach ($cbkey in $flatJson) {
18+
$group = if ($cbkey.StartsWith("WPFInstall")) { "Install" }
19+
elseif ($cbkey.StartsWith("WPFTweaks")) { "Tweaks" }
20+
elseif ($cbkey.StartsWith("WPFToggle")) { "Toggle" }
21+
elseif ($cbkey.StartsWith("WPFFeature")) { "Feature" }
22+
else { "na" }
23+
24+
switch ($group) {
25+
"Install" {
26+
if (!$sync.selectedApps.Contains($cbkey)) {
27+
$sync.selectedApps.Add($cbkey)
28+
# 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
29+
[System.Collections.Generic.List[pscustomobject]]$sync.selectedApps = $sync.SelectedApps | Sort-Object
30+
}
31+
}
32+
"Tweaks" {
33+
if (!$sync.selectedTweaks.Contains($cbkey)) {
34+
$sync.selectedTweaks.Add($cbkey)
35+
}
36+
}
37+
"Toggle" {
38+
if (!$sync.selectedToggles.Contains($cbkey)) {
39+
$sync.selectedToggles.Add($cbkey)
40+
}
41+
}
42+
"Feature" {
43+
if (!$sync.selectedFeatures.Contains($cbkey)) {
44+
$sync.selectedFeatures.Add($cbkey)
45+
}
46+
}
47+
default {
48+
Write-Host "Unknown group for checkbox: $($cbkey)"
49+
}
50+
}
51+
}
52+
53+
Write-Debug "-------------------------------------"
54+
Write-Debug "Selected Apps: $($sync.selectedApps)"
55+
Write-Debug "Selected Tweaks: $($sync.selectedTweaks)"
56+
Write-Debug "Selected Toggles: $($sync.selectedToggles)"
57+
Write-Debug "Selected Features: $($sync.selectedFeatures)"
58+
Write-Debug "--------------------------------------"
59+
}

functions/public/Invoke-WPFFeatureInstall.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function Invoke-WPFFeatureInstall {
1212
return
1313
}
1414

15-
$Features = (Get-WinUtilCheckBoxes)["WPFFeature"]
15+
$Features = $sync.selectedFeatures
1616

1717
Invoke-WPFRunspace -ArgumentList $Features -DebugPreference $DebugPreference -ScriptBlock {
1818
param($Features, $DebugPreference)

functions/public/Invoke-WPFImpex.ps1

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ function Invoke-WPFImpex {
4444
try {
4545
$Config = ConfigDialog
4646
if ($Config) {
47-
$jsonFile = Get-WinUtilCheckBoxes -unCheck $false | ConvertTo-Json
47+
$allConfs = $sync.selectedApps + $sync.selectedTweaks + $sync.selectedToggles + $sync.selectedFeatures
48+
$jsonFile = $allConfs | ConvertTo-Json
4849
$jsonFile | Out-File $Config -Force
4950
"iex ""& { `$(irm https://christitus.com/win) } -Config '$Config'""" | Set-Clipboard
5051
}
@@ -66,21 +67,12 @@ function Invoke-WPFImpex {
6667
Write-Error "Failed to load the JSON file from the specified path or URL: $_"
6768
return
6869
}
69-
$flattenedJson = $jsonFile.PSObject.Properties.Where({ $_.Name -ne "Install" -and $_.Name -ne "WPFToggle" }).ForEach({ $_.Value })
70-
Invoke-WPFPresets -preset $flattenedJson -imported $true
71-
72-
# Restore toggle switch states
73-
$importedToggles = if ($jsonFile.WPFToggle) { $jsonFile.WPFToggle } else { @() }
74-
$allToggles = $sync.GetEnumerator() | Where-Object { $_.Key -like "WPFToggle*" -and $_.Value -is [System.Windows.Controls.CheckBox] }
75-
foreach ($toggle in $allToggles) {
76-
if ($importedToggles -contains $toggle.Key) {
77-
$sync[$toggle.Key].IsChecked = $true
78-
Write-Debug "Restoring toggle: $($toggle.Key) = checked"
79-
} else {
80-
$sync[$toggle.Key].IsChecked = $false
81-
Write-Debug "Restoring toggle: $($toggle.Key) = unchecked"
82-
}
83-
}
70+
# TODO how to handle old style? detected json type then flatten it in a func?
71+
# $flattenedJson = $jsonFile.PSObject.Properties.Where({ $_.Name -ne "Install" }).ForEach({ $_.Value })
72+
$flattenedJson = $jsonFile
73+
Update-WinUtilSelections -flatJson $flattenedJson
74+
# TODO test with toggles
75+
Reset-WPFCheckBoxes -doToggles $true
8476
}
8577
} catch {
8678
Write-Error "An error occurred while importing: $_"

functions/public/Invoke-WPFPresets.ps1

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ function Invoke-WPFPresets {
22
<#
33
44
.SYNOPSIS
5-
Sets the options in the tweaks panel to the given preset
5+
Sets the checkboxes in winutil to the given preset
66
77
.PARAMETER preset
8-
The preset to set the options to
8+
The preset to set the checkboxes to
99
1010
.PARAMETER imported
1111
If the preset is imported from a file, defaults to false
@@ -17,7 +17,7 @@ function Invoke-WPFPresets {
1717

1818
param (
1919
[Parameter(position=0)]
20-
[Array]$preset = "",
20+
[Array]$preset = $null,
2121

2222
[Parameter(position=1)]
2323
[bool]$imported = $false,
@@ -32,33 +32,19 @@ function Invoke-WPFPresets {
3232
$CheckBoxesToCheck = $sync.configs.preset.$preset
3333
}
3434

35-
$CheckBoxes = ($sync.GetEnumerator()).where{ $_.Value -is [System.Windows.Controls.CheckBox] -and $_.Name -notlike "WPFToggle*" -and $_.Name -like "$checkboxfilterpattern"}
36-
Write-Debug "Getting checkboxes to set, number of checkboxes: $($CheckBoxes.Count)"
37-
38-
if ($CheckBoxesToCheck -ne "") {
39-
$debugMsg = "CheckBoxes to Check are: "
40-
$CheckBoxesToCheck | ForEach-Object { $debugMsg += "$_, " }
41-
$debugMsg = $debugMsg -replace (',\s*$', '')
42-
Write-Debug "$debugMsg"
43-
}
44-
45-
foreach ($CheckBox in $CheckBoxes) {
46-
$checkboxName = $CheckBox.Key
47-
48-
if (-not $CheckBoxesToCheck) {
49-
$sync.$checkboxName.IsChecked = $false
50-
continue
51-
}
52-
53-
# Check if the checkbox name exists in the flattened JSON hashtable
54-
if ($CheckBoxesToCheck -contains $checkboxName) {
55-
# If it exists, set IsChecked to true
56-
$sync.$checkboxName.IsChecked = $true
57-
Write-Debug "$checkboxName is checked"
58-
} else {
59-
# If it doesn't exist, set IsChecked to false
60-
$sync.$checkboxName.IsChecked = $false
61-
Write-Debug "$checkboxName is not checked"
35+
# clear out the filtered pattern
36+
if (!$preset) {
37+
switch ($checkboxfilterpattern) {
38+
"WPFTweak*" { $sync.selectedTweaks = [System.Collections.Generic.List[string]]::new() }
39+
"WPFInstall*" { $sync.selectedApps = [System.Collections.Generic.List[string]]::new() }
40+
"WPFeatures" { $sync.selectedFeatures = [System.Collections.Generic.List[string]]::new() }
41+
"WPFToggle" { $sync.selectedToggles = [System.Collections.Generic.List[string]]::new() }
42+
default {}
6243
}
6344
}
45+
else {
46+
Update-WinUtilSelections -flatJson $CheckBoxesToCheck
47+
}
48+
49+
Reset-WPFCheckBoxes -doToggles $false -checkboxfilterpattern $checkboxfilterpattern
6450
}

0 commit comments

Comments
 (0)