mirror of
https://github.com/ChrisTitusTech/winutil
synced 2026-04-05 22:28:31 +00:00
Unite preferences (#4133)
* One preference file * Set default before converting types
This commit is contained in:
@@ -8,20 +8,14 @@ function Invoke-WinutilThemeChange {
|
||||
modifying various UI elements such as colors, margins, corner radii, font families, etc.
|
||||
If the '-init' switch is used, it initializes the theme based on the system's current dark mode setting.
|
||||
|
||||
.PARAMETER init
|
||||
A switch parameter. If set to $true, the function initializes the theme based on the system’s current dark mode setting.
|
||||
|
||||
.EXAMPLE
|
||||
Invoke-WinutilThemeChange
|
||||
# Toggles the theme between 'Light' and 'Dark'.
|
||||
|
||||
.EXAMPLE
|
||||
Invoke-WinutilThemeChange -init
|
||||
# Initializes the theme based on the system's dark mode and applies the shared theme.
|
||||
|
||||
#>
|
||||
param (
|
||||
[switch]$init = $false,
|
||||
[string]$theme
|
||||
[string]$theme = "Auto"
|
||||
)
|
||||
|
||||
function Set-WinutilTheme {
|
||||
@@ -129,48 +123,30 @@ function Invoke-WinutilThemeChange {
|
||||
}
|
||||
}
|
||||
|
||||
$LightPreferencePath = "$winutildir\LightTheme.ini"
|
||||
$DarkPreferencePath = "$winutildir\DarkTheme.ini"
|
||||
$sync.preferences.theme = $theme
|
||||
Set-Preferences -save
|
||||
Set-WinutilTheme -currentTheme "shared"
|
||||
|
||||
if ($init) {
|
||||
Set-WinutilTheme -currentTheme "shared"
|
||||
if (Test-Path $LightPreferencePath) {
|
||||
$theme = "Light"
|
||||
}
|
||||
elseif (Test-Path $DarkPreferencePath) {
|
||||
$theme = "Dark"
|
||||
}
|
||||
else {
|
||||
$theme = "Auto"
|
||||
}
|
||||
}
|
||||
|
||||
switch ($theme) {
|
||||
switch ($sync.preferences.theme) {
|
||||
"Auto" {
|
||||
$systemUsesDarkMode = Get-WinUtilToggleStatus WPFToggleDarkMode
|
||||
if ($systemUsesDarkMode) {
|
||||
Set-WinutilTheme -currentTheme "Dark"
|
||||
$theme = "Dark"
|
||||
}
|
||||
else{
|
||||
Set-WinutilTheme -currentTheme "Light"
|
||||
$theme = "Light"
|
||||
}
|
||||
|
||||
|
||||
Set-WinutilTheme -currentTheme $theme
|
||||
$themeButtonIcon = [char]0xF08C
|
||||
Remove-Item $LightPreferencePath -Force -ErrorAction SilentlyContinue
|
||||
Remove-Item $DarkPreferencePath -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
"Dark" {
|
||||
Set-WinutilTheme -currentTheme $theme
|
||||
Set-WinutilTheme -currentTheme $sync.preferences.theme
|
||||
$themeButtonIcon = [char]0xE708
|
||||
$null = New-Item $DarkPreferencePath -Force
|
||||
Remove-Item $LightPreferencePath -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
"Light" {
|
||||
Set-WinutilTheme -currentTheme $theme
|
||||
Set-WinutilTheme -currentTheme $sync.preferences.theme
|
||||
$themeButtonIcon = [char]0xE706
|
||||
$null = New-Item $LightPreferencePath -Force
|
||||
Remove-Item $DarkPreferencePath -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
function Set-PackageManagerPreference {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Sets the currently selected package manager to global "ManagerPreference" in sync.
|
||||
Also persists preference across Winutil restarts via preference.ini.
|
||||
|
||||
Reads from preference.ini if no argument sent.
|
||||
|
||||
.PARAMETER preferredPackageManager
|
||||
The PackageManager that was selected.
|
||||
#>
|
||||
param(
|
||||
[Parameter(Position=0, Mandatory=$false)]
|
||||
[PackageManagers]$preferredPackageManager
|
||||
)
|
||||
|
||||
$preferencePath = "$winutildir\preferences.ini"
|
||||
$oldChocoPath = "$winutildir\preferChocolatey.ini"
|
||||
|
||||
#Try loading from file if no argument given.
|
||||
if ($null -eq $preferredPackageManager) {
|
||||
# Backwards compat for preferChocolatey.ini
|
||||
if (Test-Path -Path $oldChocoPath) {
|
||||
$preferredPackageManager = [PackageManagers]::Choco
|
||||
Remove-Item -Path $oldChocoPath
|
||||
}
|
||||
elseif (Test-Path -Path $preferencePath) {
|
||||
$potential = Get-Content -Path $preferencePath -TotalCount 1
|
||||
$preferredPackageManager = [PackageManagers]$potential
|
||||
}
|
||||
else {
|
||||
Write-Debug "Creating new preference file, defaulting to winget."
|
||||
$preferredPackageManager = [PackageManagers]::Winget
|
||||
}
|
||||
}
|
||||
|
||||
$sync["ManagerPreference"] = [PackageManagers]::$preferredPackageManager
|
||||
Write-Debug "Manager Preference changed to '$($sync["ManagerPreference"])'"
|
||||
|
||||
|
||||
# Write preference to file to persist across restarts.
|
||||
Out-File -FilePath $preferencePath -InputObject $sync["ManagerPreference"]
|
||||
}
|
||||
83
functions/private/Set-Preferences.ps1
Normal file
83
functions/private/Set-Preferences.ps1
Normal file
@@ -0,0 +1,83 @@
|
||||
function Set-Preferences{
|
||||
|
||||
param(
|
||||
[switch]$save=$false
|
||||
)
|
||||
|
||||
# TODO delete this function sometime later
|
||||
function Clean-OldPrefs{
|
||||
if (Test-Path -Path "$winutildir\LightTheme.ini") {
|
||||
$sync.preferences.theme = "Light"
|
||||
Remove-Item -Path "$winutildir\LightTheme.ini"
|
||||
}
|
||||
|
||||
if (Test-Path -Path "$winutildir\DarkTheme.ini") {
|
||||
$sync.preferences.theme = "Dark"
|
||||
Remove-Item -Path "$winutildir\DarkTheme.ini"
|
||||
}
|
||||
|
||||
# check old prefs, if its first line has no =, then absorb it as pm
|
||||
if (Test-Path -Path $iniPath) {
|
||||
$oldPM = Get-Content $iniPath
|
||||
if ($oldPM -notlike "*=*") {
|
||||
$sync.preferences.packagemanager = $oldPM
|
||||
}
|
||||
}
|
||||
|
||||
if (Test-Path -Path "$winutildir\preferChocolatey.ini") {
|
||||
$sync.preferences.packagemanager = "Choco"
|
||||
Remove-Item -Path "$winutildir\preferChocolatey.ini"
|
||||
}
|
||||
}
|
||||
|
||||
function Save-Preferences{
|
||||
$ini = ""
|
||||
foreach($key in $sync.preferences.Keys) {
|
||||
$pref = "$($key)=$($sync.preferences.$key)"
|
||||
Write-Debug "Saving pref: $($pref)"
|
||||
$ini = $ini + $pref + "`r`n"
|
||||
}
|
||||
$ini | Out-File $iniPath
|
||||
}
|
||||
|
||||
function Load-Preferences{
|
||||
Clean-OldPrefs
|
||||
if (Test-Path -Path $iniPath) {
|
||||
$iniData = Get-Content "$winutildir\preferences.ini"
|
||||
foreach ($line in $iniData) {
|
||||
if ($line -like "*=*") {
|
||||
$arr = $line -split "=",-2
|
||||
$key = $arr[0] -replace "\s",""
|
||||
$value = $arr[1] -replace "\s",""
|
||||
Write-Debug "Preference: Key = '$($key)' Value ='$($value)'"
|
||||
$sync.preferences.$key = $value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# write defaults in case preferences dont exist
|
||||
if ($null -eq $sync.preferences.theme) {
|
||||
$sync.preferences.theme = "Auto"
|
||||
}
|
||||
if ($null -eq $sync.preferences.packagemanager) {
|
||||
$sync.preferences.packagemanager = "Winget"
|
||||
}
|
||||
|
||||
# convert packagemanager to enum
|
||||
if ($sync.preferences.packagemanager -eq "Choco") {
|
||||
$sync.preferences.packagemanager = [PackageManagers]::Choco
|
||||
}
|
||||
elseif ($sync.preferences.packagemanager -eq "Winget") {
|
||||
$sync.preferences.packagemanager = [PackageManagers]::Winget
|
||||
}
|
||||
}
|
||||
|
||||
$iniPath = "$winutildir\preferences.ini"
|
||||
|
||||
if ($save) {
|
||||
Save-Preferences
|
||||
}
|
||||
else {
|
||||
Load-Preferences
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ function Invoke-WPFGetInstalled {
|
||||
if (($sync.ChocoRadioButton.IsChecked -eq $false) -and ((Test-WinUtilPackageManager -winget) -eq "not-installed") -and $checkbox -eq "winget") {
|
||||
return
|
||||
}
|
||||
$managerPreference = $sync["ManagerPreference"]
|
||||
$managerPreference = $sync.preferences.packagemanager
|
||||
|
||||
Invoke-WPFRunspace -ParameterList @(("managerPreference", $managerPreference),("checkbox", $checkbox)) -ScriptBlock {
|
||||
param (
|
||||
|
||||
@@ -19,7 +19,7 @@ function Invoke-WPFInstall {
|
||||
return
|
||||
}
|
||||
|
||||
$ManagerPreference = $sync["ManagerPreference"]
|
||||
$ManagerPreference = $sync.preferences.packagemanager
|
||||
|
||||
$handle = Invoke-WPFRunspace -ParameterList @(("PackagesToInstall", $PackagesToInstall),("ManagerPreference", $ManagerPreference)) -ScriptBlock {
|
||||
param($PackagesToInstall, $ManagerPreference)
|
||||
|
||||
@@ -30,7 +30,7 @@ function Invoke-WPFUnInstall {
|
||||
|
||||
if($confirm -eq "No") {return}
|
||||
|
||||
$ManagerPreference = $sync["ManagerPreference"]
|
||||
$ManagerPreference = $sync.preferences.packagemanager
|
||||
|
||||
Invoke-WPFRunspace -ParameterList @(("PackagesToUninstall", $PackagesToUninstall),("ManagerPreference", $ManagerPreference)) -ScriptBlock {
|
||||
param($PackagesToUninstall, $ManagerPreference)
|
||||
|
||||
@@ -66,7 +66,7 @@ $sync.configs.applications.PSObject.Properties | ForEach-Object {
|
||||
$sync.configs.applicationsHashtable[$_.Name] = $_.Value
|
||||
}
|
||||
|
||||
Set-PackageManagerPreference
|
||||
Set-Preferences
|
||||
|
||||
if ($PARAM_NOUI) {
|
||||
Show-CTTLogo
|
||||
@@ -154,7 +154,7 @@ $sync.Form.Add_Loaded({
|
||||
})
|
||||
})
|
||||
|
||||
Invoke-WinutilThemeChange -init $true
|
||||
Invoke-WinutilThemeChange -theme $sync.preferences.theme
|
||||
|
||||
|
||||
# Now call the function with the final merged config
|
||||
@@ -177,10 +177,16 @@ Invoke-WPFUIElements -configVariable $sync.configs.feature -targetGridName "feat
|
||||
$xaml.SelectNodes("//*[@Name]") | ForEach-Object {$sync["$("$($psitem.Name)")"] = $sync["Form"].FindName($psitem.Name)}
|
||||
|
||||
#Persist Package Manager preference across winutil restarts
|
||||
$sync.ChocoRadioButton.Add_Checked({Set-PackageManagerPreference Choco})
|
||||
$sync.WingetRadioButton.Add_Checked({Set-PackageManagerPreference Winget})
|
||||
$sync.ChocoRadioButton.Add_Checked({
|
||||
$sync.preferences.packagemanager = [PackageManagers]::Choco
|
||||
Set-Preferences -save
|
||||
})
|
||||
$sync.WingetRadioButton.Add_Checked({
|
||||
$sync.preferences.packagemanager = [PackageManagers]::Winget
|
||||
Set-Preferences -save
|
||||
})
|
||||
|
||||
switch ($sync["ManagerPreference"]) {
|
||||
switch ($sync.preferences.packagemanager) {
|
||||
"Choco" {$sync.ChocoRadioButton.IsChecked = $true; break}
|
||||
"Winget" {$sync.WingetRadioButton.IsChecked = $true; break}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ $sync.PSScriptRoot = $PSScriptRoot
|
||||
$sync.version = "#{replaceme}"
|
||||
$sync.configs = @{}
|
||||
$sync.Buttons = [System.Collections.Generic.List[PSObject]]::new()
|
||||
$sync.preferences = @{}
|
||||
$sync.ProcessRunning = $false
|
||||
$sync.selectedApps = [System.Collections.Generic.List[string]]::new()
|
||||
$sync.selectedTweaks = [System.Collections.Generic.List[string]]::new()
|
||||
|
||||
Reference in New Issue
Block a user