2023-07-13 15:46:00 -05:00
|
|
|
function Set-WinUtilUITheme {
|
|
|
|
<#
|
2024-08-06 15:35:17 -05:00
|
|
|
.SYNOPSIS
|
|
|
|
Sets the theme of the XAML file
|
2023-07-13 15:46:00 -05:00
|
|
|
|
2024-08-06 15:35:17 -05:00
|
|
|
.PARAMETER inputXML
|
|
|
|
A string representing the XAML object to modify
|
2023-07-13 15:46:00 -05:00
|
|
|
|
2024-08-06 15:35:17 -05:00
|
|
|
.PARAMETER themeName
|
|
|
|
The name of the theme to set the XAML to. Defaults to 'matrix'
|
2023-10-19 17:12:55 -05:00
|
|
|
|
2024-08-06 15:35:17 -05:00
|
|
|
.EXAMPLE
|
|
|
|
Set-WinUtilUITheme -inputXAML $inputXAML
|
2023-07-13 15:46:00 -05:00
|
|
|
#>
|
2024-08-06 15:35:17 -05:00
|
|
|
|
2023-07-13 15:46:00 -05:00
|
|
|
param
|
|
|
|
(
|
2024-07-27 18:11:11 -05:00
|
|
|
[Parameter(Mandatory, position=0)]
|
2024-08-06 15:35:17 -05:00
|
|
|
[string]$inputXML,
|
2024-07-27 18:11:11 -05:00
|
|
|
[Parameter(position=1)]
|
2024-08-06 15:35:17 -05:00
|
|
|
[string]$themeName = 'matrix'
|
2023-07-13 15:46:00 -05:00
|
|
|
)
|
|
|
|
|
2024-07-27 18:11:11 -05:00
|
|
|
function Invoke-Theming {
|
|
|
|
param (
|
|
|
|
[Parameter(Mandatory, position=0)]
|
|
|
|
[string] $XMLToProcess,
|
|
|
|
|
|
|
|
[Parameter(Mandatory, position=1)]
|
|
|
|
[PSCustomObject] $theme
|
|
|
|
)
|
2023-07-13 15:46:00 -05:00
|
|
|
|
2024-07-27 18:11:11 -05:00
|
|
|
if ($XMLToProcess -eq "") {
|
|
|
|
throw [GenericException]::new("[Invoke-Theming] 'XMLToProcess' can not be an empty string")
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-07-13 15:46:00 -05:00
|
|
|
# Loop through all key-value pairs in the selected theme
|
2024-07-27 18:11:11 -05:00
|
|
|
foreach ($property in $theme.PSObject.Properties) {
|
2023-07-13 15:46:00 -05:00
|
|
|
$key = $property.Name
|
|
|
|
$value = $property.Value
|
|
|
|
# Add curly braces around the key
|
|
|
|
$formattedKey = "{$key}"
|
|
|
|
# Replace the key with the value in the input XML
|
2024-07-27 18:11:11 -05:00
|
|
|
$XMLToProcess = $XMLToProcess.Replace($formattedKey, $value)
|
2023-07-13 15:46:00 -05:00
|
|
|
}
|
2024-07-27 18:11:11 -05:00
|
|
|
} catch {
|
|
|
|
throw [GenericException]::new("[Invoke-Theming] Failed to apply theme, StackTrace: $($psitem.Exception.StackTrace)")
|
2023-07-13 15:46:00 -05:00
|
|
|
}
|
2024-07-27 18:11:11 -05:00
|
|
|
|
2024-07-31 13:56:56 -05:00
|
|
|
return $XMLToProcess
|
2024-07-27 18:11:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.")
|
|
|
|
}
|
|
|
|
|
2024-07-31 13:56:56 -05:00
|
|
|
$defaultTheme = $themes."_default"
|
|
|
|
if (-NOT $defaultTheme) {
|
2024-07-27 18:11:11 -05:00
|
|
|
throw [GenericException]::new("[Set-WinUtilTheme] Did not find '_default' theme in the themes config file.")
|
2023-07-13 15:46:00 -05:00
|
|
|
}
|
|
|
|
|
2024-07-27 18:11:11 -05:00
|
|
|
# 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
|
2024-07-31 13:56:56 -05:00
|
|
|
}
|
2024-07-27 18:11:11 -05:00
|
|
|
|
2024-07-31 13:56:56 -05:00
|
|
|
$inputXML = Invoke-Theming -XMLToProcess $inputXML -theme $defaultTheme
|
2024-07-27 18:11:11 -05:00
|
|
|
|
2023-07-13 15:46:00 -05:00
|
|
|
}
|
|
|
|
catch {
|
2024-07-27 18:11:11 -05:00
|
|
|
Write-Warning "[Set-WinUtilTheme] Unable to apply theme"
|
|
|
|
$err = $psitem.Exception.StackTrace
|
|
|
|
Write-Warning $err
|
|
|
|
}
|
|
|
|
|
|
|
|
$returnVal = @{
|
|
|
|
err="$err";
|
|
|
|
processedXML="$inputXML";
|
2023-07-13 15:46:00 -05:00
|
|
|
}
|
|
|
|
|
2024-07-27 18:11:11 -05:00
|
|
|
return $returnVal;
|
2023-07-13 15:46:00 -05:00
|
|
|
}
|