mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2024-11-24 03:45:30 +00:00
Improve 'Invoke-WPFPopup' by @MyDrift-user
Thanks for the improvements :)
This commit is contained in:
parent
e6c1394b5d
commit
fc53ca802b
@ -1,91 +1,54 @@
|
|||||||
function Invoke-WPFPopup {
|
function Invoke-WPFPopup {
|
||||||
param (
|
param (
|
||||||
[Parameter(position=0)]
|
[ValidateSet("Show", "Hide", "Toggle")]
|
||||||
[ValidateSet("Show","Hide","Toggle", ErrorMessage="Action '{0}' is not part of the valid set '{1}'.")]
|
|
||||||
[string]$Action = "",
|
[string]$Action = "",
|
||||||
|
|
||||||
[Parameter(position=1)]
|
|
||||||
[string[]]$Popups = @(),
|
[string[]]$Popups = @(),
|
||||||
|
|
||||||
[Parameter(position=2)]
|
|
||||||
[ValidateScript({
|
[ValidateScript({
|
||||||
$PossibleActions = @("Show", "Hide", "Toggle")
|
$invalid = $_.GetEnumerator() | Where-Object { $_.Value -notin @("Show", "Hide", "Toggle") }
|
||||||
[hashtable]$UnExpectedPairs = @{}
|
if ($invalid) {
|
||||||
Foreach ($pair in $_.GetEnumerator()) {
|
throw "Found invalid Popup-Action pair(s): " + ($invalid | ForEach-Object { "$($_.Key) = $($_.Value)" } -join "; ")
|
||||||
$key = $pair.Name
|
|
||||||
$value = $pair.Value
|
|
||||||
if (-not ($value -in $PossibleActions)) {
|
|
||||||
$UnExpectedPairs.Add("$key", "$value")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($UnExpectedPairs.Count -gt 0) {
|
|
||||||
$UnExpectedPairsAsString = "@{"
|
|
||||||
Foreach ($pair in $UnExpectedPairs.GetEnumerator()) { $UnExpectedPairsAsString += "`"$($pair.Name)`" = `"$($pair.Value)`"; " }
|
|
||||||
$UnExpectedPairsAsString = $UnExpectedPairsAsString -replace (';\s*$', '')
|
|
||||||
$UnExpectedPairsAsString += "}"
|
|
||||||
throw "Found Unexpected pair(s), these Popup & Action pair(s) are: $UnExpectedPairsAsString"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Return true for passing validation checks
|
|
||||||
$true
|
$true
|
||||||
})]
|
})]
|
||||||
[hashtable]$PopupActionTable = @{}
|
[hashtable]$PopupActionTable = @{}
|
||||||
)
|
)
|
||||||
|
|
||||||
if ($PopupActionTable.Count -eq 0 -and $Action -eq "" -and $Popups.Count -eq 0) {
|
if (-not $PopupActionTable.Count -and (-not $Action -or -not $Popups.Count)) {
|
||||||
throw [GenericException]::new("No Parameter was provided, please use either 'PopupActionTable' on its own, or use 'Action' and 'Popups' on their own, depending on your use case.")
|
throw "Provide either 'PopupActionTable' or both 'Action' and 'Popups'."
|
||||||
}
|
|
||||||
if ($PopupActionTable.Count -gt 0 -and ($Action -ne "" -or $Popups.Count -gt 0)) {
|
|
||||||
throw [GenericException]::new("Only use 'PopupActionTable' on its own, or use 'Action' and 'Popups' on their own, depending on your use case.")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$PopupsNotFound = [System.Collections.Generic.List[string]]::new($Popups.Count)
|
if ($PopupActionTable.Count -and ($Action -or $Popups.Count)) {
|
||||||
|
throw "Use 'PopupActionTable' on its own, or 'Action' with 'Popups'."
|
||||||
|
}
|
||||||
|
|
||||||
if ($PopupActionTable.Count -gt 0) {
|
# Collect popups and actions
|
||||||
Foreach ($popupActionPair in $PopupActionTable.GetEnumerator()) {
|
$PopupsToProcess = if ($PopupActionTable.Count) {
|
||||||
$popup = $popupActionPair.Name + "Popup"
|
$PopupActionTable.GetEnumerator() | ForEach-Object { [PSCustomObject]@{ Name = "$($_.Key)Popup"; Action = $_.Value } }
|
||||||
$action = $popupActionPair.Value
|
|
||||||
if ($sync.$popup -eq $null) {
|
|
||||||
$PopupsNotFound.Add("$popup") | Out-Null
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
switch ($action) {
|
|
||||||
'Show' { $actionAsBool = $true }
|
|
||||||
'Hide' { $actionAsBool = $false }
|
|
||||||
'Toggle' { $actionAsBool = -not $sync.$popup.IsOpen }
|
|
||||||
default { throw [GenericException]::new("Action can only be `"Show`" or `"Hide`" or `"Toggle`".") }
|
|
||||||
}
|
|
||||||
$sync.$popup.IsOpen = $actionAsBool
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if ($Action -eq "" -or $Popups.Count -eq 0) {
|
$Popups | ForEach-Object { [PSCustomObject]@{ Name = "$_`Popup"; Action = $Action } }
|
||||||
throw [GenericException]::new("Please provide both the 'Action' and 'Popups' Parameters, with the appropriate values foreach parameter.")
|
}
|
||||||
|
|
||||||
|
$PopupsNotFound = @()
|
||||||
|
|
||||||
|
# Apply actions
|
||||||
|
foreach ($popupEntry in $PopupsToProcess) {
|
||||||
|
$popupName = $popupEntry.Name
|
||||||
|
|
||||||
|
if (-not $sync.$popupName) {
|
||||||
|
$PopupsNotFound += $popupName
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
Foreach ($popup in $Popups) {
|
|
||||||
$popup += "Popup"
|
$sync.$popupName.IsOpen = switch ($popupEntry.Action) {
|
||||||
if ($sync.$popup -eq $null) {
|
"Show" { $true }
|
||||||
$PopupsNotFound.Add("$popup") | Out-Null
|
"Hide" { $false }
|
||||||
continue
|
"Toggle" { -not $sync.$popupName.IsOpen }
|
||||||
}
|
|
||||||
switch ($action) {
|
|
||||||
'Show' { $actionAsBool = $true }
|
|
||||||
'Hide' { $actionAsBool = $false }
|
|
||||||
'Toggle' { $actionAsBool = -not $sync.$popup.IsOpen }
|
|
||||||
default { throw [GenericException]::new("Action can only be `"Show`" or `"Hide`" or `"Toggle`".") }
|
|
||||||
}
|
|
||||||
$sync.$popup.IsOpen = $actionAsBool
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($PopupsNotFound.Count -gt 0) {
|
if ($PopupsNotFound.Count -gt 0) {
|
||||||
$PopupsNotFoundAsString = "@("
|
throw "Could not find the following popups: $($PopupsNotFound -join ', ')"
|
||||||
Foreach ($popupNotFound in $PopupsNotFound) {
|
|
||||||
$PopupsNotFoundAsString += "$popupNotFound"
|
|
||||||
$PopupsNotFoundAsString += ", "
|
|
||||||
}
|
|
||||||
$PopupsNotFoundAsString = $PopupsNotFoundAsString -replace (',\s*$', '')
|
|
||||||
$PopupsNotFoundAsString += ")"
|
|
||||||
throw [GenericException]::new("Could not find $PopupsNotFoundAsString Popups in `$sync variable.")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user