mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2025-01-16 01:40:35 -06:00
Fix some logic issue in 'Set-WinUtilUITheme.ps1' Private Function - Rename 'Set-WinUtilUiTheme.ps1' -> 'Set-WinUtilUITheme.ps1'
This commit is contained in:
parent
49639ec2f7
commit
8ccde4fa59
74
functions/private/Set-WinUtilUITheme.ps1
Normal file
74
functions/private/Set-WinUtilUITheme.ps1
Normal file
@ -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;
|
||||||
|
}
|
@ -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;
|
|
||||||
}
|
|
@ -62,16 +62,15 @@ if ((Get-WinUtilToggleStatus WPFToggleDarkMode) -eq $True) {
|
|||||||
$ctttheme = 'Classic'
|
$ctttheme = 'Classic'
|
||||||
}
|
}
|
||||||
|
|
||||||
$returnVal = Set-WinUtilUITheme -inputXML $inputXML -themeName $ctttheme
|
$inputXML = Set-WinUtilUITheme -inputXML $inputXML -customThemeName $ctttheme
|
||||||
if ($returnVal.err -eq $null) {
|
if ($inputXML -eq "") {
|
||||||
Write-Warning "Failed to statically apply theming to xaml content using Set-WinUtilTheme, please check previous Error/Warning messages."
|
Write-Warning "Failed to statically apply theming to xaml content using Set-WinUtilTheme, please check previous Error/Warning messages."
|
||||||
Write-Warning "Quitting winutil..."
|
Write-Warning "Quitting winutil..."
|
||||||
$sync.runspace.Dispose()
|
$sync.runspace.Dispose()
|
||||||
$sync.runspace.Close()
|
$sync.runspace.Close()
|
||||||
[System.GC]::Collect()
|
[System.GC]::Collect()
|
||||||
return
|
exit 1
|
||||||
}
|
}
|
||||||
$inputXML = $returnVal.processedXML
|
|
||||||
|
|
||||||
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
|
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
|
||||||
[xml]$XAML = $inputXML
|
[xml]$XAML = $inputXML
|
||||||
@ -94,11 +93,12 @@ try {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (-NOT ($readerOperationSuccessful)) {
|
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.Dispose()
|
||||||
$sync.runspace.Close()
|
$sync.runspace.Close()
|
||||||
[System.GC]::Collect()
|
[System.GC]::Collect()
|
||||||
return
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
#===========================================================================
|
#===========================================================================
|
||||||
|
Loading…
Reference in New Issue
Block a user