Skip to content

Commit 2b2b11e

Browse files
committed
Fix Settings Popup not closing after losing focus - Add trivial null checks for better debugging of UI code
1 parent 0e7a34c commit 2b2b11e

File tree

1 file changed

+51
-29
lines changed

1 file changed

+51
-29
lines changed

Diff for: scripts/main.ps1

+51-29
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,12 @@ $commonKeyEvents = {
254254
$sync["Form"].Add_PreViewKeyDown($commonKeyEvents)
255255

256256
$sync["Form"].Add_MouseLeftButtonDown({
257-
# Hide Settings and Theme Popup on click anywhere else
258-
if ($sync.SettingsButton.IsOpen -or
259-
$sync.ThemePopup.IsOpen) {
260-
$sync.SettingsPopup.IsOpen = $false
261-
$sync.ThemePopup.IsOpen = $false
262-
}
257+
if ($sync.SettingsPopup -eq $null -or
258+
$sync.ThemePopup -eq $null) {
259+
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
260+
}
261+
$sync.SettingsPopup.IsOpen = $false
262+
$sync.ThemePopup.IsOpen = $false
263263
$sync["Form"].DragMove()
264264
})
265265

@@ -277,12 +277,12 @@ $sync["Form"].Add_MouseDoubleClick({
277277

278278
$sync["Form"].Add_Deactivated({
279279
Write-Debug "WinUtil lost focus"
280-
# Hide Settings and Theme Popup on Winutil Focus Loss
281-
if ($sync.SettingsButton.IsOpen -or
282-
$sync.ThemePopup.IsOpen) {
283-
$sync.SettingsPopup.IsOpen = $false
284-
$sync.ThemePopup.IsOpen = $false
280+
if ($sync.SettingsPopup -eq $null -or
281+
$sync.ThemePopup -eq $null) {
282+
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
285283
}
284+
$sync.SettingsPopup.IsOpen = $false
285+
$sync.ThemePopup.IsOpen = $false
286286
})
287287

288288
$sync["Form"].Add_ContentRendered({
@@ -525,29 +525,39 @@ $sync["Form"].Add_Activated({
525525
})
526526
# Define event handler for ThemeButton click
527527
$sync["ThemeButton"].Add_Click({
528-
if ($sync.ThemePopup.IsOpen) {
529-
$sync.ThemePopup.IsOpen = $false
530-
}
531-
else{
532-
$sync.ThemePopup.IsOpen = $true
528+
if ($sync.SettingsPopup -eq $null -or
529+
$sync.ThemePopup -eq $null) {
530+
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
533531
}
532+
# Hide the settings popup and toggle the themes popup
533+
$sync.ThemePopup.IsOpen = -not $sync.ThemePopup.IsOpen
534534
$sync.SettingsPopup.IsOpen = $false
535+
$_.Handled = $false
535536
})
536537

537538
# Define event handlers for menu items
538539
$sync["AutoThemeMenuItem"].Add_Click({
540+
if ($sync.ThemePopup -eq $null) {
541+
Write-Host "Theme Popup is null, this's not allowed to happen in the first place. Please double check your UI code." -ForegroundColor Red
542+
}
539543
$sync.ThemePopup.IsOpen = $false
540544
Invoke-WinutilThemeChange -theme "Auto"
541545
$_.Handled = $false
542546
})
543547
# Define event handlers for menu items
544548
$sync["DarkThemeMenuItem"].Add_Click({
549+
if ($sync.ThemePopup -eq $null) {
550+
Write-Host "Theme Popup is null, this's not allowed to happen in the first place. Please double check your UI code." -ForegroundColor Red
551+
}
545552
$sync.ThemePopup.IsOpen = $false
546553
Invoke-WinutilThemeChange -theme "Dark"
547554
$_.Handled = $false
548555
})
549556
# Define event handlers for menu items
550557
$sync["LightThemeMenuItem"].Add_Click({
558+
if ($sync.ThemePopup -eq $null) {
559+
Write-Host "Theme Popup is null, this's not allowed to happen in the first place. Please double check your UI code." -ForegroundColor Red
560+
}
551561
$sync.ThemePopup.IsOpen = $false
552562
Invoke-WinutilThemeChange -theme "Light"
553563
$_.Handled = $false
@@ -557,37 +567,46 @@ $sync["LightThemeMenuItem"].Add_Click({
557567
# Define event handler for button click
558568
$sync["SettingsButton"].Add_Click({
559569
Write-Debug "SettingsButton clicked"
560-
if ($sync.SettingsPopup.IsOpen) {
561-
$sync.SettingsPopup.IsOpen = $false
562-
}
563-
else{
564-
$sync.SettingsPopup.IsOpen = $true
570+
if ($sync.SettingsPopup -eq $null -or
571+
$sync.ThemePopup -eq $null) {
572+
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
565573
}
574+
# Hide the themes popup and toggle the settings popup
575+
$sync.SettingsPopup.IsOpen = -not $sync.SettingsPopup.IsOpen
566576
$sync.ThemePopup.IsOpen = $false
567577
$_.Handled = $false
568578
})
569579

570580
# Define event handlers for menu items
571581
$sync["ImportMenuItem"].Add_Click({
572-
# Handle Import menu item click
573-
Write-Debug "Import clicked"
574-
$sync["SettingsPopup"].IsOpen = $false
575-
Invoke-WPFImpex -type "import"
576-
$_.Handled = $false
582+
# Handle Import menu item click
583+
Write-Debug "Import clicked"
584+
if ($sync.SettingsPopup -eq $null) {
585+
Write-Host "Either Settings is null, this's not allowed to happen in the first place. Please double check your UI code." -ForegroundColor Red
586+
}
587+
$sync.SettingsPopup.IsOpen = $false
588+
Invoke-WPFImpex -type "import"
589+
$_.Handled = $false
577590
})
578591

579592
$sync["ExportMenuItem"].Add_Click({
580593
# Handle Export menu item click
581594
Write-Debug "Export clicked"
582-
$sync["SettingsPopup"].IsOpen = $false
595+
if ($sync.SettingsPopup -eq $null) {
596+
Write-Host "Either Settings is null, this's not allowed to happen in the first place. Please double check your UI code." -ForegroundColor Red
597+
}
598+
$sync.SettingsPopup.IsOpen = $false
583599
Invoke-WPFImpex -type "export"
584600
$_.Handled = $false
585601
})
586602

587603
$sync["AboutMenuItem"].Add_Click({
588604
# Handle Export menu item click
589605
Write-Debug "About clicked"
590-
$sync["SettingsPopup"].IsOpen = $false
606+
if ($sync.SettingsPopup -eq $null) {
607+
Write-Host "Either Settings is null, this's not allowed to happen in the first place. Please double check your UI code." -ForegroundColor Red
608+
}
609+
$sync.SettingsPopup.IsOpen = $false
591610
$authorInfo = @"
592611
Author : <a href="https://github.com/ChrisTitusTech">@christitustech</a>
593612
Runspace : <a href="https://github.com/DeveloperDurp">@DeveloperDurp</a>
@@ -602,7 +621,10 @@ Version : <a href="https://github.com/ChrisTitusTech/winutil/releases/tag/$($sy
602621
$sync["SponsorMenuItem"].Add_Click({
603622
# Handle Export menu item click
604623
Write-Debug "Sponsors clicked"
605-
$sync["SettingsPopup"].IsOpen = $false
624+
if ($sync.SettingsPopup -eq $null) {
625+
Write-Host "Either Settings is null, this's not allowed to happen in the first place. Please double check your UI code." -ForegroundColor Red
626+
}
627+
$sync.SettingsPopup.IsOpen = $false
606628
$authorInfo = @"
607629
<a href="https://github.com/sponsors/ChrisTitusTech">Current sponsors for ChrisTitusTech:</a>
608630
"@

0 commit comments

Comments
 (0)