Implement Default Theming - Add Error Checking & Quitting Early upon XML Related Failures - Simple Code Formatting here & there

This commit is contained in:
Mr.k 2024-07-28 02:11:11 +03:00
parent 8fa1e175b3
commit 6235949819
No known key found for this signature in database
3 changed files with 109 additions and 105 deletions

View File

@ -1,5 +1,5 @@
{
"Classic": {
"_default": {
"CustomDialogFontSize": "12",
"CustomDialogFontSizeHeader": "14",
"CustomDialogIconSize": "25",
@ -68,35 +68,38 @@
"ButtonCornerRadius": "2",
"BorderColor": "#000000",
"BorderOpacity": "0.2",
"ShadowPulse": "0:0:6"
},
"Classic": {
"ComboBoxBackgroundColor": "#FFFFFF",
"LabelboxForegroundColor": "#000000",
"MainForegroundColor": "#000000",
"MainBackgroundColor": "#FFFFFF",
"LabelBackgroundColor": "#FAFAFA",
"LinkForegroundColor": "#000000",
"LinkHoverForegroundColor": "#000000",
"GroupBorderBackgroundColor": "#000000",
"ComboBoxForegroundColor": "#000000",
"ButtonInstallBackgroundColor": "#FFFFFF",
"ButtonTweaksBackgroundColor": "#FFFFFF",
"ButtonConfigBackgroundColor": "#FFFFFF",
"ButtonUpdatesBackgroundColor": "#FFFFFF",
"ButtonInstallForegroundColor": "#000000",
"ButtonTweaksForegroundColor": "#000000",
"ButtonConfigForegroundColor": "#000000",
"ButtonUpdatesForegroundColor": "#000000",
"ButtonBackgroundColor": "#F5F5F5",
"ButtonBackgroundPressedColor": "#1A1A1A",
"CheckboxMouseOverColor": "#999999",
"ButtonBackgroundMouseoverColor": "#C2C2C2",
"ButtonBackgroundSelectedColor": "#F0F0F0",
"ButtonForegroundColor": "#000000",
"ToggleButtonOnColor": "#2e77ff",
"ShadowPulse": "Forever"
},
"Matrix": {
"CustomDialogFontSize": "12",
"CustomDialogFontSizeHeader": "14",
"CustomDialogIconSize": "25",
"CustomDialogWidth": "400",
"CustomDialogHeight": "200",
"FontSize": "0.35cm",
"FontFamily": "Arial",
"FontSizeHeading": "0.4cm",
"HeaderFontFamily": "Consolas, Monaco",
"CheckBoxBulletDecoratorSize": "14",
"CheckBoxMargin": "15,0,0,2",
"TabButtonFontSize": "14",
"TabButtonWidth": "100",
"TabButtonHeight": "25",
"TabRowHeightInPixels": "50",
"IconFontSize": "14",
"IconButtonSize": "35",
"WinUtilIconSize": "Auto",
"SettingsIconFontSize": "18",
"CloseIconFontSize": "18",
"MicroWinLogoSize": "10",
"ComboBoxBackgroundColor": "#000000",
"LabelboxForegroundColor": "#FFEE58",
"MainForegroundColor": "#9CCC65",
@ -106,17 +109,6 @@
"LinkHoverForegroundColor": "#FFFFFF",
"ComboBoxForegroundColor": "#FFEE58",
"ButtonFontSize": "12",
"ButtonFontFamily": "Arial",
"ButtonWidth": "200",
"ButtonHeight": "25",
"ConfigTabButtonFontSize": "16",
"SearchBarWidth": "200",
"SearchBarHeight": "25",
"SearchBarTextBoxFontSize": "16",
"SearchBarClearButtonFontSize": "14",
"ButtonInstallBackgroundColor": "#222222",
"ButtonTweaksBackgroundColor": "#333333",
"ButtonConfigBackgroundColor": "#444444",
@ -132,40 +124,11 @@
"ButtonForegroundColor": "#9CCC65",
"ToggleButtonOnColor": "#2e77ff",
"ButtonBorderThickness": "1",
"ButtonMargin": "1",
"ButtonCornerRadius": "2",
"BorderColor": "#FFAC1C",
"BorderOpacity": "0.8",
"ShadowPulse": "0:0:3"
},
"Dark": {
"CustomDialogFontSize": "12",
"CustomDialogFontSizeHeader": "14",
"CustomDialogIconSize": "25",
"CustomDialogWidth": "400",
"CustomDialogHeight": "200",
"FontSize": "0.35cm",
"FontFamily": "Arial",
"FontSizeHeading": "0.4cm",
"HeaderFontFamily": "Consolas, Monaco",
"CheckBoxBulletDecoratorSize": "14",
"CheckBoxMargin": "15,0,0,2",
"TabButtonFontSize": "14",
"TabButtonWidth": "100",
"TabButtonHeight": "25",
"TabRowHeightInPixels": "50",
"IconFontSize": "14",
"IconButtonSize": "35",
"WinUtilIconSize": "Auto",
"SettingsIconFontSize": "18",
"CloseIconFontSize": "18",
"MicroWinLogoSize": "10",
"ComboBoxBackgroundColor": "#000000",
"LabelboxForegroundColor": "#FFEE58",
"MainForegroundColor": "#9CCC65",
@ -175,17 +138,6 @@
"LinkHoverForegroundColor": "#FFFFFF",
"ComboBoxForegroundColor": "#FFEE58",
"ButtonFontSize": "12",
"ButtonFontFamily": "Arial",
"ButtonWidth": "200",
"ButtonHeight": "25",
"ConfigTabButtonFontSize": "16",
"SearchBarWidth": "200",
"SearchBarHeight": "25",
"SearchBarTextBoxFontSize": "16",
"SearchBarClearButtonFontSize": "14",
"ButtonInstallBackgroundColor": "#222222",
"ButtonTweaksBackgroundColor": "#333333",
"ButtonConfigBackgroundColor": "#444444",
@ -201,11 +153,6 @@
"ButtonForegroundColor": "#9CCC65",
"ToggleButtonOnColor": "#2e77ff",
"ButtonBorderThickness": "1",
"ButtonMargin": "1",
"ButtonCornerRadius": "2",
"BorderColor": "#FFAC1C",
"BorderOpacity": "0.2",
"ShadowPulse": "Forever"
"BorderColor": "#FFAC1C"
}
}

View File

@ -14,40 +14,77 @@ function Set-WinUtilUITheme {
Set-WinUtilUITheme -inputXAML $inputXAML
#>
param
(
[Parameter(Mandatory=$true, Position=0)]
param (
[Parameter(Mandatory, position=0)]
[string] $inputXML,
[Parameter(Mandatory=$false, Position=1)]
[Parameter(position=1)]
[string] $themeName = 'matrix'
)
try {
# Convert the JSON to a PowerShell object
$themes = $sync.configs.themes
# Select the specified theme
$selectedTheme = $themes.$themeName
function Invoke-Theming {
param (
[Parameter(Mandatory, position=0)]
[string] $XMLToProcess,
if ($selectedTheme) {
[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 $selectedTheme.PSObject.Properties) {
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
$inputXML = $inputXML.Replace($formattedKey, $value)
$XMLToProcess = $XMLToProcess.Replace($formattedKey, $value)
}
} catch {
throw [GenericException]::new("[Invoke-Theming] Failed to apply theme, StackTrace: $($psitem.Exception.StackTrace)")
}
else {
Write-Host "Theme '$themeName' not found."
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 "Unable to apply theme"
Write-Warning $psitem.Exception.StackTrace
Write-Warning "[Set-WinUtilTheme] Unable to apply theme"
$err = $psitem.Exception.StackTrace
Write-Warning $err
}
return $inputXML;
$returnVal = @{
err="$err";
processedXML="$inputXML";
}
return $returnVal;
}

View File

@ -63,15 +63,27 @@ if ((Get-WinUtilToggleStatus WPFToggleDarkMode) -eq $True) {
else {
$ctttheme = 'Classic'
}
$inputXML = Set-WinUtilUITheme -inputXML $inputXML -themeName $ctttheme
$returnVal = Set-WinUtilUITheme -inputXML $inputXML -themeName $ctttheme
if ($returnVal.err -eq $null) {
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
}
$inputXML = $returnVal.processedXML
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML
# Read the XAML file
$readerOperationSuccessful = $false # There's more cases of failure then success.
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
try {
$sync["Form"] = [Windows.Markup.XamlReader]::Load( $reader )
$readerOperationSuccessful = $true
} catch [System.Management.Automation.MethodInvocationException] {
Write-Warning "We ran into a problem with the XAML code. Check the syntax for this control..."
Write-Host $error[0].Exception.Message -ForegroundColor Red
@ -83,6 +95,14 @@ try {
Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."
}
if (-NOT ($readerOperationSuccessful)) {
Write-Warning "Failed to parse xaml content using Windows.Markup.XamlReader's Load Method, quitting winutil..."
$sync.runspace.Dispose()
$sync.runspace.Close()
[System.GC]::Collect()
return
}
#===========================================================================
# Store Form Objects In PowerShell
#===========================================================================