Style.ps1
Set-StrictMode -Version 3 $script:Styles = @{} function Add-OxyStyle { [cmdletbinding()] param( [string]$StyleName, [hashtable]$Config ) $filteredTypes = New-Object Collections.Generic.List[object] $result = @{} foreach ($filter in $Config.Keys) { if ($filter -match "^\[") { $result[$filter] = $Config[$filter] continue } $filterType = $filter -replace "\..+$", "" $filterProp = $filter -replace "^.+\.", "" $filteredTypes.Clear() foreach ($t in $TYPE_HASH.Keys) { if ($t -like $filterType) { $filteredTypes.Add($TYPE_HASH[$t]) } } if ($filteredTypes.Count -eq 0) { Write-Error "No matching types for '$filterType'" return } $originalValue = $Config[$filter] if ($originalValue -is [Array] -and $originalValue.Count -eq 2 -and $originalValue[1] -is [string] -and $originalValue[1] -match "^(pt|in|cm|pt)$") { $originalValue = $originalValue[0].ToString() + $originalValue[1].ToString() } $matchCount = 0 foreach ($t in $filteredTypes) { if ($filterProp -eq "*") { if ($originalValue -isnot [scriptblock]) { Write-Error "Filter $filter's property is wildcard, but no scriptblock is specified" return } $value = $originalValue ++$matchCount } else { $prop = $t.GetProperties() | where { $_.Name -eq $filterProp } if ($null -eq $prop -or !$prop.CanWrite) { continue } Write-Verbose "$filter=$originalValue (class: $($t.FullName))" ++$matchCount $value = Convert-ParameterValue $prop.PropertyType.FullName $originalValue } if ($result.Contains($t.FullName)) { $result[$t.Fullname][$filterProp] = $value } else { $result[$t.FullName] = @{ $filterProp = $value } } } if ($matchCount -eq 0) { Write-Error "No matching property for '$filterProp'" return } } $Styles[$StyleName] = $result } function Remove-OxyStyle { param( [string]$StyleName ) $styles.Remove($StyleName) } function Get-OxyStyle { [cmdletbinding()] param( [string]$StyleName ) if (!(Test-OxyStyleName $StyleName)) { Write-Error "Unknown style: $StyleName" return } $Styles[$StyleName] } function Test-OxyStyleName { [cmdletbinding()] param( [string]$StyleName ) $Styles.Contains($StyleName) } function Apply-OxyStyle { param( [object]$Object, [string]$StyleName, [Management.Automation.InvocationInfo]$InvocationInfo ) $style = $Styles[$StyleName] if ($style -eq $null) { return } $config = $style[$Object.GetType().FullName] if ($null -ne $config) { foreach ($p in $config.Keys) { if ($p -eq "*") { [void]$config["*"].Invoke($Object, $InvocationInfo) } else { $Object.$p = $config[$p] } } } if ($StyleName -ne "local" -and $Styles.ContainsKey("local")) { Apply-OxyStyle $Object "local" $InvocationInfo } } function Apply-OxyStyleEvent { param( [OxyPlot.PlotModel]$PlotModel, [string]$StyleName, [string]$EventName, [Management.Automation.InvocationInfo]$InvocationInfo ) $style = $Styles[$StyleName] if ($style -eq $null) { Write-Error "Unknown style: $StyleName" return } [scriptblock]$script = $style["[$EventName]"] if ($script -eq $null) { return } [void]$script.Invoke($PlotModel, $InvocationInfo) } function Set-OxyDefaultStyle { param( [string]$StyleName ) $Styles["default"] = $Styles[$StyleName] } |