From f0afb6735a93f514b03700d8209890bcfab25b0f Mon Sep 17 00:00:00 2001 From: MyDrift Date: Tue, 5 Nov 2024 22:40:31 +0100 Subject: [PATCH] move logic to json template - remove Invoke-WPFToggle.ps1 - generalize Get-WinUtilToggleStatus - add bingsearch reg key for testing - use Invoke-WinUtilTweaks for actions - replace Add-Click with checked & unchecked to make undo work --- config/tweaks.json | 9 ++ functions/private/Get-WinUtilToggleStatus.ps1 | 88 +++++-------------- functions/private/Invoke-WinUtilTweaks.ps1 | 4 + functions/public/Invoke-WPFToggle.ps1 | 39 -------- functions/public/Invoke-WPFUIElements.ps1 | 11 ++- 5 files changed, 42 insertions(+), 109 deletions(-) delete mode 100644 functions/public/Invoke-WPFToggle.ps1 diff --git a/config/tweaks.json b/config/tweaks.json index 93ed630c..27db404f 100644 --- a/config/tweaks.json +++ b/config/tweaks.json @@ -3302,6 +3302,15 @@ "panel": "2", "Order": "a101_", "Type": "Toggle", + "registry": [ + { + "Path": "HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Search", + "Name": "BingSearchEnabled", + "Value": "0", + "OriginalValue": "1", + "Type": "DWord" + } + ], "link": "https://christitustech.github.io/winutil/dev/tweaks/Customize-Preferences/BingSearch" }, "WPFToggleNumLock": { diff --git a/functions/private/Get-WinUtilToggleStatus.ps1 b/functions/private/Get-WinUtilToggleStatus.ps1 index 422d1d17..ea422c57 100644 --- a/functions/private/Get-WinUtilToggleStatus.ps1 +++ b/functions/private/Get-WinUtilToggleStatus.ps1 @@ -13,75 +13,29 @@ Function Get-WinUtilToggleStatus { #> Param($ToggleSwitch) - if($ToggleSwitch -eq "WPFToggleDarkMode") { - $app = (Get-ItemProperty -path 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize').AppsUseLightTheme - $system = (Get-ItemProperty -path 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize').SystemUsesLightTheme - return $app -eq 0 -and $system -eq 0 - } - if($ToggleSwitch -eq "WPFToggleBingSearch") { - $bingsearch = (Get-ItemProperty -path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Search').BingSearchEnabled - return $bingsearch -ne 0 - } - if($ToggleSwitch -eq "WPFToggleNumLock") { - $numlockvalue = (Get-ItemProperty -path 'HKCU:\Control Panel\Keyboard').InitialKeyboardIndicators - return $numlockvalue -eq 2 - } - if($ToggleSwitch -eq "WPFToggleVerboseLogon") { - $VerboseStatusvalue = (Get-ItemProperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System').VerboseStatus - return $VerboseStatusvalue -eq 1 - } - if($ToggleSwitch -eq "WPFToggleShowExt") { - $hideextvalue = (Get-ItemProperty -path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced').HideFileExt - return $hideextvalue -eq 0 - } - if($ToggleSwitch -eq "WPFToggleSnapWindow") { - $hidesnap = (Get-ItemProperty -path 'HKCU:\Control Panel\Desktop').WindowArrangementActive - return $hidesnap -ne 0 - } - if($ToggleSwitch -eq "WPFToggleSnapFlyout") { - $hidesnap = (Get-ItemProperty -path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced').EnableSnapAssistFlyout - return $hidesnap -ne 0 - } - if($ToggleSwitch -eq "WPFToggleSnapSuggestion") { - $hidesnap = (Get-ItemProperty -path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced').SnapAssist - return $hidesnap -ne 0 - } - if($ToggleSwitch -eq "WPFToggleMouseAcceleration") { - $MouseSpeed = (Get-ItemProperty -path 'HKCU:\Control Panel\Mouse').MouseSpeed - $MouseThreshold1 = (Get-ItemProperty -path 'HKCU:\Control Panel\Mouse').MouseThreshold1 - $MouseThreshold2 = (Get-ItemProperty -path 'HKCU:\Control Panel\Mouse').MouseThreshold2 - return $MouseSpeed -eq 1 -and $MouseThreshold1 -eq 6 -and $MouseThreshold2 -eq 10 - } - if($ToggleSwitch -eq "WPFToggleTaskbarSearch") { - $SearchButton = (Get-ItemProperty -path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Search").SearchboxTaskbarMode - return $SearchButton -ne 0 - } - if ($ToggleSwitch -eq "WPFToggleStickyKeys") { - $StickyKeys = (Get-ItemProperty -path 'HKCU:\Control Panel\Accessibility\StickyKeys').Flags - return $StickyKeys -ne 58 - } - if ($ToggleSwitch -eq "WPFToggleTaskView") { - $TaskView = (Get-ItemProperty -path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced').ShowTaskViewButton - return $TaskView -ne 0 - } + $ToggleSwitchReg = $sync.configs.tweaks.$ToggleSwitch.registry - if ($ToggleSwitch -eq "WPFToggleHiddenFiles") { - $HiddenFiles = (Get-ItemProperty -path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced').Hidden - return $HiddenFiles -ne 0 - } + if ($ToggleSwitchReg) { + $count = 0 - if ($ToggleSwitch -eq "WPFToggleTaskbarWidgets") { - $TaskbarWidgets = (Get-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced").TaskBarDa - return $TaskbarWidgets -ne 0 - } - if ($ToggleSwitch -eq "WPFToggleTaskbarAlignment") { - $TaskbarAlignment = (Get-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced").TaskbarAl - return $TaskbarAlignment -ne 0 - } - if ($ToggleSwitch -eq "WPFToggleDetailedBSoD") { - $DetailedBSoD1 = (Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\CrashControl').DisplayParameters - $DetailedBSoD2 = (Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\CrashControl').DisableEmoticon - return !(($DetailedBSoD1 -eq 0) -or ($DetailedBSoD2 -eq 0) -or !$DetailedBSoD1 -or !$DetailedBSoD2) + foreach ($regentry in $ToggleSwitchReg) { + $regstate = (Get-ItemProperty -path $($regentry.Path)).$($regentry.Name) + if ($regstate -eq $regentry.Value) { + $count += 1 + } else { + write-debug "$($regentry.Name) is false (state: $regstate, value: $($regentry.Value), original: $($regentry.OriginalValue))" + } + } + + if ($count -eq $ToggleSwitchReg.Count) { + write-debug "$($ToggleSwitchReg.Name) is true (count: $count)" + return $true + } else { + write-debug "$($ToggleSwitchReg.Name) is false (count: $count)" + return $false + } + } else { + return $false } } diff --git a/functions/private/Invoke-WinUtilTweaks.ps1 b/functions/private/Invoke-WinUtilTweaks.ps1 index 8bb06faf..55577d54 100644 --- a/functions/private/Invoke-WinUtilTweaks.ps1 +++ b/functions/private/Invoke-WinUtilTweaks.ps1 @@ -21,6 +21,10 @@ function Invoke-WinUtilTweaks { $KeepServiceStartup = $true ) + if ($Checkbox -contains "Toggle") { + $CheckBox = $sync.configs.tweaks.$CheckBox + } + Write-Debug "Tweaks: $($CheckBox)" if($undo) { $Values = @{ diff --git a/functions/public/Invoke-WPFToggle.ps1 b/functions/public/Invoke-WPFToggle.ps1 deleted file mode 100644 index c4fbd105..00000000 --- a/functions/public/Invoke-WPFToggle.ps1 +++ /dev/null @@ -1,39 +0,0 @@ -function Invoke-WPFToggle { - - <# - - .SYNOPSIS - Invokes the scriptblock for the given toggle - - .PARAMETER Button - The name of the toggle to invoke - - #> - - Param ([string]$Button) - - # Use this to get the name of the button - #[System.Windows.MessageBox]::Show("$Button","Chris Titus Tech's Windows Utility","OK","Info") - - $ToggleStatus = (Get-WinUtilToggleStatus $Button) - - Switch -Wildcard ($Button) { - - "WPFToggleDarkMode" {Invoke-WinUtilDarkMode $ToggleStatus} - "WPFToggleBingSearch" {Invoke-WinUtilBingSearch $ToggleStatus} - "WPFToggleNumLock" {Invoke-WinUtilNumLock $ToggleStatus} - "WPFToggleVerboseLogon" {Invoke-WinUtilVerboseLogon $ToggleStatus} - "WPFToggleShowExt" {Invoke-WinUtilShowExt $ToggleStatus} - "WPFToggleSnapWindow" {Invoke-WinUtilSnapWindow $ToggleStatus} - "WPFToggleSnapFlyout" {Invoke-WinUtilSnapFlyout $ToggleStatus} - "WPFToggleSnapSuggestion" {Invoke-WinUtilSnapSuggestion $ToggleStatus} - "WPFToggleMouseAcceleration" {Invoke-WinUtilMouseAcceleration $ToggleStatus} - "WPFToggleStickyKeys" {Invoke-WinUtilStickyKeys $ToggleStatus} - "WPFToggleTaskbarWidgets" {Invoke-WinUtilTaskbarWidgets $ToggleStatus} - "WPFToggleTaskbarSearch" {Invoke-WinUtilTaskbarSearch $ToggleStatus} - "WPFToggleTaskView" {Invoke-WinUtilTaskView $ToggleStatus} - "WPFToggleHiddenFiles" {Invoke-WinUtilHiddenFiles $ToggleStatus} - "WPFToggleTaskbarAlignment" {Invoke-WinUtilTaskbarAlignment $ToggleStatus} - "WPFToggleDetailedBSoD" {Invoke-WinUtilDetailedBSoD $ToggleStatus} - } -} diff --git a/functions/public/Invoke-WPFUIElements.ps1 b/functions/public/Invoke-WPFUIElements.ps1 index 3851536d..8ce21f88 100644 --- a/functions/public/Invoke-WPFUIElements.ps1 +++ b/functions/public/Invoke-WPFUIElements.ps1 @@ -192,11 +192,16 @@ function Invoke-WPFUIElements { $sync[$entryInfo.Name] = $checkBox - $sync[$entryInfo.Name].IsChecked = Get-WinUtilToggleStatus $sync[$entryInfo.Name].Name + $sync[$entryInfo.Name].IsChecked = Get-WinUtilToggleStatus $entryInfo.Name - $sync[$entryInfo.Name].Add_Click({ + $sync[$entryInfo.Name].Add_Checked({ [System.Object]$Sender = $args[0] - Invoke-WPFToggle $Sender.name + Invoke-WinUtilTweaks $sender.name + }) + + $sync[$entryInfo.Name].Add_Unchecked({ + [System.Object]$Sender = $args[0] + Invoke-WinUtiltweaks $sender.name -undo $true }) }