From 8ccde4fa5965bc53b077aee01a232308b17b73b1 Mon Sep 17 00:00:00 2001 From: "Mr.k" Date: Mon, 26 Aug 2024 17:06:57 +0300 Subject: [PATCH] Fix some logic issue in 'Set-WinUtilUITheme.ps1' Private Function - Rename 'Set-WinUtilUiTheme.ps1' -> 'Set-WinUtilUITheme.ps1' --- functions/private/Set-WinUtilUITheme.ps1 | 74 +++++++++++++++++++ functions/private/Set-WinUtilUiTheme.ps1 | 90 ------------------------ scripts/main.ps1 | 12 ++-- 3 files changed, 80 insertions(+), 96 deletions(-) create mode 100644 functions/private/Set-WinUtilUITheme.ps1 delete mode 100644 functions/private/Set-WinUtilUiTheme.ps1 diff --git a/functions/private/Set-WinUtilUITheme.ps1 b/functions/private/Set-WinUtilUITheme.ps1 new file mode 100644 index 00000000..a5bd78fa --- /dev/null +++ b/functions/private/Set-WinUtilUITheme.ps1 @@ -0,0 +1,74 @@ +function Set-WinUtilUITheme { + <# + .SYNOPSIS + Sets the theme of the XAML file + + .PARAMETER inputXML + A string representing the XAML object to modify + + .PARAMETER customThemeName + The name of the custom theme to set the XAML to. Defaults to 'matrix' + + .PARAMETER defaultThemeName + The name of the default theme to use when setting the XAML. Defaults to '_default' + + .EXAMPLE + Set-WinUtilUITheme -inputXAML $inputXAML + #> + + param ( + [Parameter(Mandatory, position=0)] + [string]$inputXML, + + [Parameter(position=1)] + [string]$customThemeName = 'matrix', + + [Parameter(position=2)] + [string]$defaultThemeName = '_default' + ) + + try { + # Note: + # Reason behind not caching the '$sync.configs.themes` object into a variable, + # because this code can modify the themes object.. meaning it's better to access it + # using the more verbose way, rather than introduce possible bugs into the code, just for the sake of readability. + # + if (-NOT $sync.configs.themes) { + throw [GenericException]::new("[Set-WinUtilTheme] Did not find 'config.themes' inside `$sync variable.") + } + + if (-NOT $sync.configs.themes.$defaultThemeName) { + throw [GenericException]::new("[Set-WinUtilTheme] Did not find '$defaultThemeName' theme in the themes config file.") + } + + if ($sync.configs.themes.$customThemeName) { + # Loop through every default theme option, and modify the custom theme in $sync variable, + # so that it has full options available for other functions to use. + foreach ($option in $sync.configs.themes.$defaultThemeName.PSObject.Properties) { + $optionName = $option.Name + $optionValue = $option.Value + if (-NOT $sync.configs.themes.$customThemeName.$optionName) { + $sync.configs.themes.$customThemeName | Add-Member -MemberType NoteProperty -Name $optionName -Value $optionValue + } + } + } else { + Write-Debug "[Set-WinUtilTheme] Theme '$customThemeName' was not found." + } + + foreach ($property in $sync.configs.themes.$customThemeName.PSObject.Properties) { + $key = $property.Name + $value = $property.Value + # Add curly braces around the key + $formattedKey = "{$key}" + # Replace the key with the value in the input XML + $inputXML = $inputXML.Replace($formattedKey, $value) + } + } + catch { + Write-Debug "[Set-WinUtilTheme] Unable to apply theme" + Write-Debug $psitem.Exception.Message + $inputXML = "" # Make inputXML equal an empty string, indicating something went wrong to the function caller. + } + + return $inputXML; +} diff --git a/functions/private/Set-WinUtilUiTheme.ps1 b/functions/private/Set-WinUtilUiTheme.ps1 deleted file mode 100644 index 9f42faef..00000000 --- a/functions/private/Set-WinUtilUiTheme.ps1 +++ /dev/null @@ -1,90 +0,0 @@ -function Set-WinUtilUITheme { - <# - .SYNOPSIS - Sets the theme of the XAML file - - .PARAMETER inputXML - A string representing the XAML object to modify - - .PARAMETER themeName - The name of the theme to set the XAML to. Defaults to 'matrix' - - .EXAMPLE - Set-WinUtilUITheme -inputXAML $inputXAML - #> - - param - ( - [Parameter(Mandatory, position=0)] - [string]$inputXML, - [Parameter(position=1)] - [string]$themeName = 'matrix' - ) - - function Invoke-Theming { - param ( - [Parameter(Mandatory, position=0)] - [string] $XMLToProcess, - - [Parameter(Mandatory, position=1)] - [PSCustomObject] $theme - ) - - if ($XMLToProcess -eq "") { - throw [GenericException]::new("[Invoke-Theming] 'XMLToProcess' can not be an empty string") - } - - try { - # Loop through all key-value pairs in the selected theme - foreach ($property in $theme.PSObject.Properties) { - $key = $property.Name - $value = $property.Value - # Add curly braces around the key - $formattedKey = "{$key}" - # Replace the key with the value in the input XML - $XMLToProcess = $XMLToProcess.Replace($formattedKey, $value) - } - } catch { - throw [GenericException]::new("[Invoke-Theming] Failed to apply theme, StackTrace: $($psitem.Exception.StackTrace)") - } - - return $XMLToProcess - } - - - try { - # Convert the JSON to a PowerShell object - $themes = $sync.configs.themes - if (-NOT $themes) { - throw [GenericException]::new("[Set-WinUtilTheme] Did not find 'config.themes' inside `$sync variable.") - } - - $defaultTheme = $themes."_default" - if (-NOT $defaultTheme) { - throw [GenericException]::new("[Set-WinUtilTheme] Did not find '_default' theme in the themes config file.") - } - - # First apply the selected theme (if it exists), then apply the default theme - $selectedTheme = $themes.$themeName - if (-NOT $selectedTheme) { - Write-Warning "[Set-WinUtilTheme] Theme '$themeName' was not found." - } else { - $inputXML = Invoke-Theming -XMLToProcess $inputXML -theme $selectedTheme - } - - $inputXML = Invoke-Theming -XMLToProcess $inputXML -theme $defaultTheme - - } - catch { - Write-Warning "[Set-WinUtilTheme] Unable to apply theme" - $err = $psitem.Exception.StackTrace - Write-Warning $err - } - - $returnVal = @{ - err="$err"; - processedXML="$inputXML"; - } - - return $returnVal; -} diff --git a/scripts/main.ps1 b/scripts/main.ps1 index 37c47790..ffc6d6c1 100644 --- a/scripts/main.ps1 +++ b/scripts/main.ps1 @@ -62,16 +62,15 @@ if ((Get-WinUtilToggleStatus WPFToggleDarkMode) -eq $True) { $ctttheme = 'Classic' } -$returnVal = Set-WinUtilUITheme -inputXML $inputXML -themeName $ctttheme -if ($returnVal.err -eq $null) { +$inputXML = Set-WinUtilUITheme -inputXML $inputXML -customThemeName $ctttheme +if ($inputXML -eq "") { Write-Warning "Failed to statically apply theming to xaml content using Set-WinUtilTheme, please check previous Error/Warning messages." Write-Warning "Quitting winutil..." $sync.runspace.Dispose() $sync.runspace.Close() [System.GC]::Collect() - return + exit 1 } -$inputXML = $returnVal.processedXML [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework') [xml]$XAML = $inputXML @@ -94,11 +93,12 @@ try { } if (-NOT ($readerOperationSuccessful)) { - Write-Warning "Failed to parse xaml content using Windows.Markup.XamlReader's Load Method, quitting winutil..." + Write-Warning "Failed to parse xaml content using Windows.Markup.XamlReader's Load Method." + Write-Warning "Quitting winutil..." $sync.runspace.Dispose() $sync.runspace.Close() [System.GC]::Collect() - return + exit 1 } #===========================================================================