Add a new 'Invoke-WPFPopup' Function to Better Handle Showing/Hiding/Toggling of Popups

This commit is contained in:
Mr.k 2024-10-26 17:12:52 +03:00
parent 411bc66d7e
commit fb21479531
No known key found for this signature in database
2 changed files with 109 additions and 74 deletions

View File

@ -0,0 +1,91 @@
function Invoke-WPFPopup {
param (
[Parameter(position=0)]
[ValidateSet("Show","Hide","Toggle", ErrorMessage="Action '{0}' is not part of the valid set '{1}'.")]
[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")
}
}
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.")
}
$PopupsNotFound = [System.Collections.Generic.List[string]]::new($Popups.Count)
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
}
} 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.")
}
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
}
}
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.")
}
}

View File

@ -251,12 +251,7 @@ $commonKeyEvents = {
$sync["Form"].Add_PreViewKeyDown($commonKeyEvents) $sync["Form"].Add_PreViewKeyDown($commonKeyEvents)
$sync["Form"].Add_MouseLeftButtonDown({ $sync["Form"].Add_MouseLeftButtonDown({
if ($sync.SettingsPopup -eq $null -or Invoke-WPFPopup -Action "Hide" -Popups @("Settings", "Theme")
$sync.ThemePopup -eq $null) {
Write-Host "Either Settings or Theme Popup is null, this's not allowed to happen in the first place. Please double check your UI code." -ForegroundColor Red
}
$sync.SettingsPopup.IsOpen = $false
$sync.ThemePopup.IsOpen = $false
$sync["Form"].DragMove() $sync["Form"].DragMove()
}) })
@ -274,12 +269,7 @@ $sync["Form"].Add_MouseDoubleClick({
$sync["Form"].Add_Deactivated({ $sync["Form"].Add_Deactivated({
Write-Debug "WinUtil lost focus" Write-Debug "WinUtil lost focus"
if ($sync.SettingsPopup -eq $null -or Invoke-WPFPopup -Action "Hide" -Popups @("Settings", "Theme")
$sync.ThemePopup -eq $null) {
Write-Host "Either Settings or Theme Popup is null, this's not allowed to happen in the first place. Please double check your UI code." -ForegroundColor Red
}
$sync.SettingsPopup.IsOpen = $false
$sync.ThemePopup.IsOpen = $false
}) })
$sync["Form"].Add_ContentRendered({ $sync["Form"].Add_ContentRendered({
@ -520,90 +510,53 @@ Set-WinUtilTaskbaritem -overlay "logo"
$sync["Form"].Add_Activated({ $sync["Form"].Add_Activated({
Set-WinUtilTaskbaritem -overlay "logo" Set-WinUtilTaskbaritem -overlay "logo"
}) })
# Define event handler for ThemeButton click
$sync["ThemeButton"].Add_Click({ $sync["ThemeButton"].Add_Click({
if ($sync.SettingsPopup -eq $null -or Write-Debug "ThemeButton clicked"
$sync.ThemePopup -eq $null) { Invoke-WPFPopup -PopupActionTable @{ "Settings" = "Hide"; "Theme" = "Toggle" }
Write-Host "Either Settings or Theme Popup is null, this's not allowed to happen in the first place. Please double check your UI code." -ForegroundColor Red
}
# Hide the settings popup and toggle the themes popup
$sync.ThemePopup.IsOpen = -not $sync.ThemePopup.IsOpen
$sync.SettingsPopup.IsOpen = $false
$_.Handled = $false $_.Handled = $false
}) })
# Define event handlers for menu items
$sync["AutoThemeMenuItem"].Add_Click({ $sync["AutoThemeMenuItem"].Add_Click({
if ($sync.ThemePopup -eq $null) { Write-Debug "About clicked"
Write-Host "Theme Popup is null, this's not allowed to happen in the first place. Please double check your UI code." -ForegroundColor Red Invoke-WPFPopup -Action "Hide" -Popups @("Theme")
}
$sync.ThemePopup.IsOpen = $false
Invoke-WinutilThemeChange -theme "Auto" Invoke-WinutilThemeChange -theme "Auto"
$_.Handled = $false $_.Handled = $false
}) })
# Define event handlers for menu items
$sync["DarkThemeMenuItem"].Add_Click({ $sync["DarkThemeMenuItem"].Add_Click({
if ($sync.ThemePopup -eq $null) { Write-Debug "Dark Theme clicked"
Write-Host "Theme Popup is null, this's not allowed to happen in the first place. Please double check your UI code." -ForegroundColor Red Invoke-WPFPopup -Action "Hide" -Popups @("Theme")
}
$sync.ThemePopup.IsOpen = $false
Invoke-WinutilThemeChange -theme "Dark" Invoke-WinutilThemeChange -theme "Dark"
$_.Handled = $false $_.Handled = $false
}) })
# Define event handlers for menu items
$sync["LightThemeMenuItem"].Add_Click({ $sync["LightThemeMenuItem"].Add_Click({
if ($sync.ThemePopup -eq $null) { Write-Debug "Light Theme clicked"
Write-Host "Theme Popup is null, this's not allowed to happen in the first place. Please double check your UI code." -ForegroundColor Red Invoke-WPFPopup -Action "Hide" -Popups @("Theme")
}
$sync.ThemePopup.IsOpen = $false
Invoke-WinutilThemeChange -theme "Light" Invoke-WinutilThemeChange -theme "Light"
$_.Handled = $false $_.Handled = $false
}) })
# Define event handler for button click
$sync["SettingsButton"].Add_Click({ $sync["SettingsButton"].Add_Click({
Write-Debug "SettingsButton clicked" Write-Debug "SettingsButton clicked"
if ($sync.SettingsPopup -eq $null -or Invoke-WPFPopup -PopupActionTable @{ "Settings" = "Toggle"; "Theme" = "Hide" }
$sync.ThemePopup -eq $null) {
Write-Host "Either Settings or Theme Popup is null, this's not allowed to happen in the first place. Please double check your UI code." -ForegroundColor Red
}
# Hide the themes popup and toggle the settings popup
$sync.SettingsPopup.IsOpen = -not $sync.SettingsPopup.IsOpen
$sync.ThemePopup.IsOpen = $false
$_.Handled = $false $_.Handled = $false
}) })
# Define event handlers for menu items
$sync["ImportMenuItem"].Add_Click({ $sync["ImportMenuItem"].Add_Click({
# Handle Import menu item click
Write-Debug "Import clicked" Write-Debug "Import clicked"
if ($sync.SettingsPopup -eq $null) { Invoke-WPFPopup -Action "Hide" -Popups @("Settings")
Write-Host "Either Settings is null, this's not allowed to happen in the first place. Please double check your UI code." -ForegroundColor Red
}
$sync.SettingsPopup.IsOpen = $false
Invoke-WPFImpex -type "import" Invoke-WPFImpex -type "import"
$_.Handled = $false $_.Handled = $false
}) })
$sync["ExportMenuItem"].Add_Click({ $sync["ExportMenuItem"].Add_Click({
# Handle Export menu item click
Write-Debug "Export clicked" Write-Debug "Export clicked"
if ($sync.SettingsPopup -eq $null) { Invoke-WPFPopup -Action "Hide" -Popups @("Settings")
Write-Host "Either Settings is null, this's not allowed to happen in the first place. Please double check your UI code." -ForegroundColor Red
}
$sync.SettingsPopup.IsOpen = $false
Invoke-WPFImpex -type "export" Invoke-WPFImpex -type "export"
$_.Handled = $false $_.Handled = $false
}) })
$sync["AboutMenuItem"].Add_Click({ $sync["AboutMenuItem"].Add_Click({
# Handle Export menu item click
Write-Debug "About clicked" Write-Debug "About clicked"
if ($sync.SettingsPopup -eq $null) { Invoke-WPFPopup -Action "Hide" -Popups @("Settings")
Write-Host "Either Settings is null, this's not allowed to happen in the first place. Please double check your UI code." -ForegroundColor Red
}
$sync.SettingsPopup.IsOpen = $false
$authorInfo = @" $authorInfo = @"
Author : <a href="https://github.com/ChrisTitusTech">@christitustech</a> Author : <a href="https://github.com/ChrisTitusTech">@christitustech</a>
Runspace : <a href="https://github.com/DeveloperDurp">@DeveloperDurp</a> Runspace : <a href="https://github.com/DeveloperDurp">@DeveloperDurp</a>
@ -611,33 +564,24 @@ MicroWin : <a href="https://github.com/KonTy">@KonTy</a>, <a href="https://githu
GitHub : <a href="https://github.com/ChrisTitusTech/winutil">ChrisTitusTech/winutil</a> GitHub : <a href="https://github.com/ChrisTitusTech/winutil">ChrisTitusTech/winutil</a>
Version : <a href="https://github.com/ChrisTitusTech/winutil/releases/tag/$($sync.version)">$($sync.version)</a> Version : <a href="https://github.com/ChrisTitusTech/winutil/releases/tag/$($sync.version)">$($sync.version)</a>
"@ "@
Show-CustomDialog -Title "About" -Message $authorInfo Show-CustomDialog -Title "About" -Message $authorInfo
}) })
$sync["SponsorMenuItem"].Add_Click({ $sync["SponsorMenuItem"].Add_Click({
# Handle Export menu item click
Write-Debug "Sponsors clicked" Write-Debug "Sponsors clicked"
if ($sync.SettingsPopup -eq $null) { Invoke-WPFPopup -Action "Hide" -Popups @("Settings")
Write-Host "Either Settings is null, this's not allowed to happen in the first place. Please double check your UI code." -ForegroundColor Red
}
$sync.SettingsPopup.IsOpen = $false
$authorInfo = @" $authorInfo = @"
<a href="https://github.com/sponsors/ChrisTitusTech">Current sponsors for ChrisTitusTech:</a> <a href="https://github.com/sponsors/ChrisTitusTech">Current sponsors for ChrisTitusTech:</a>
"@ "@
$authorInfo += "`n" $authorInfo += "`n"
try { try {
# Call the function to get the sponsors
$sponsors = Invoke-WinUtilSponsors $sponsors = Invoke-WinUtilSponsors
# Append the sponsors to the authorInfo
foreach ($sponsor in $sponsors) { foreach ($sponsor in $sponsors) {
$authorInfo += "<a href=`"https://github.com/sponsors/ChrisTitusTech`">$sponsor</a>`n" $authorInfo += "<a href=`"https://github.com/sponsors/ChrisTitusTech`">$sponsor</a>`n"
} }
} catch { } catch {
$authorInfo += "An error occurred while fetching or processing the sponsors: $_`n" $authorInfo += "An error occurred while fetching or processing the sponsors: $_`n"
} }
Show-CustomDialog -Title "Sponsors" -Message $authorInfo -EnableScroll $true Show-CustomDialog -Title "Sponsors" -Message $authorInfo -EnableScroll $true
}) })