forked from ChrisTitusTech/winutil
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve 'Invoke-WPFPopup' by @MyDrift-user
Thanks for the improvements :)
- Loading branch information
1 parent
e6c1394
commit fc53ca8
Showing
1 changed file
with
30 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,54 @@ | ||
function Invoke-WPFPopup { | ||
param ( | ||
[Parameter(position=0)] | ||
[ValidateSet("Show","Hide","Toggle", ErrorMessage="Action '{0}' is not part of the valid set '{1}'.")] | ||
[ValidateSet("Show", "Hide", "Toggle")] | ||
[string]$Action = "", | ||
|
||
[Parameter(position=1)] | ||
[string[]]$Popups = @(), | ||
|
||
[Parameter(position=2)] | ||
[ValidateScript({ | ||
$PossibleActions = @("Show", "Hide", "Toggle") | ||
[hashtable]$UnExpectedPairs = @{} | ||
Foreach ($pair in $_.GetEnumerator()) { | ||
$key = $pair.Name | ||
$value = $pair.Value | ||
if (-not ($value -in $PossibleActions)) { | ||
$UnExpectedPairs.Add("$key", "$value") | ||
} | ||
$invalid = $_.GetEnumerator() | Where-Object { $_.Value -notin @("Show", "Hide", "Toggle") } | ||
if ($invalid) { | ||
throw "Found invalid Popup-Action pair(s): " + ($invalid | ForEach-Object { "$($_.Key) = $($_.Value)" } -join "; ") | ||
} | ||
|
||
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 | ||
})] | ||
[hashtable]$PopupActionTable = @{} | ||
) | ||
|
||
if ($PopupActionTable.Count -eq 0 -and $Action -eq "" -and $Popups.Count -eq 0) { | ||
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.") | ||
} | ||
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.") | ||
if (-not $PopupActionTable.Count -and (-not $Action -or -not $Popups.Count)) { | ||
throw "Provide either 'PopupActionTable' or both 'Action' and 'Popups'." | ||
} | ||
|
||
$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) { | ||
Foreach ($popupActionPair in $PopupActionTable.GetEnumerator()) { | ||
$popup = $popupActionPair.Name + "Popup" | ||
$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 | ||
} | ||
# Collect popups and actions | ||
$PopupsToProcess = if ($PopupActionTable.Count) { | ||
$PopupActionTable.GetEnumerator() | ForEach-Object { [PSCustomObject]@{ Name = "$($_.Key)Popup"; Action = $_.Value } } | ||
} else { | ||
if ($Action -eq "" -or $Popups.Count -eq 0) { | ||
throw [GenericException]::new("Please provide both the 'Action' and 'Popups' Parameters, with the appropriate values foreach parameter.") | ||
$Popups | ForEach-Object { [PSCustomObject]@{ Name = "$_`Popup"; Action = $Action } } | ||
} | ||
|
||
$PopupsNotFound = @() | ||
|
||
# Apply actions | ||
foreach ($popupEntry in $PopupsToProcess) { | ||
$popupName = $popupEntry.Name | ||
|
||
if (-not $sync.$popupName) { | ||
$PopupsNotFound += $popupName | ||
continue | ||
} | ||
Foreach ($popup in $Popups) { | ||
$popup += "Popup" | ||
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 | ||
|
||
$sync.$popupName.IsOpen = switch ($popupEntry.Action) { | ||
"Show" { $true } | ||
"Hide" { $false } | ||
"Toggle" { -not $sync.$popupName.IsOpen } | ||
} | ||
} | ||
|
||
if ($PopupsNotFound.Count -gt 0) { | ||
$PopupsNotFoundAsString = "@(" | ||
Foreach ($popupNotFound in $PopupsNotFound) { | ||
$PopupsNotFoundAsString += "$popupNotFound" | ||
$PopupsNotFoundAsString += ", " | ||
} | ||
$PopupsNotFoundAsString = $PopupsNotFoundAsString -replace (',\s*$', '') | ||
$PopupsNotFoundAsString += ")" | ||
throw [GenericException]::new("Could not find $PopupsNotFoundAsString Popups in `$sync variable.") | ||
throw "Could not find the following popups: $($PopupsNotFound -join ', ')" | ||
} | ||
} |