From 62359498198b85e0a80d1a56c58db261d653a38f Mon Sep 17 00:00:00 2001 From: "Mr.k" Date: Sun, 28 Jul 2024 02:11:11 +0300 Subject: [PATCH] Implement Default Theming - Add Error Checking & Quitting Early upon XML Related Failures - Simple Code Formatting here & there --- config/themes.json | 115 ++++++----------------- functions/private/Set-WinUtilUiTheme.ps1 | 71 ++++++++++---- scripts/main.ps1 | 28 +++++- 3 files changed, 109 insertions(+), 105 deletions(-) diff --git a/config/themes.json b/config/themes.json index b36ca511..7d98e04f 100644 --- a/config/themes.json +++ b/config/themes.json @@ -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" } } diff --git a/functions/private/Set-WinUtilUiTheme.ps1 b/functions/private/Set-WinUtilUiTheme.ps1 index 46d55afa..9bed8f61 100644 --- a/functions/private/Set-WinUtilUiTheme.ps1 +++ b/functions/private/Set-WinUtilUiTheme.ps1 @@ -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; } diff --git a/scripts/main.ps1 b/scripts/main.ps1 index 0c83c54c..e217a5b6 100644 --- a/scripts/main.ps1 +++ b/scripts/main.ps1 @@ -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 #=========================================================================== @@ -279,11 +299,11 @@ Add-Type @" foreach ($proc in (Get-Process).where{ $_.MainWindowTitle -and $_.MainWindowTitle -like "*titus*" }) { # Check if the process's MainWindowHandle is valid - if ($proc.MainWindowHandle -ne [System.IntPtr]::Zero) { + if ($proc.MainWindowHandle -ne [System.IntPtr]::Zero) { Write-Debug "MainWindowHandle: $($proc.Id) $($proc.MainWindowTitle) $($proc.MainWindowHandle)" $windowHandle = $proc.MainWindowHandle - } else { - Write-Warning "Process found, but no MainWindowHandle: $($proc.Id) $($proc.MainWindowTitle)" + } else { + Write-Warning "Process found, but no MainWindowHandle: $($proc.Id) $($proc.MainWindowTitle)" } }