@@ -48,9 +48,9 @@ public static class QuickSwitch
4848 private static IQuickSwitchExplorer _lastExplorer = null ;
4949 private static readonly object _lastExplorerLock = new ( ) ;
5050
51- private static readonly List < IQuickSwitchDialog > _quickSwitchDialogs = new ( )
51+ private static readonly Dictionary < IQuickSwitchDialog , IQuickSwitchDialogWindow > _quickSwitchDialogs = new ( )
5252 {
53- new WindowsDialog ( )
53+ { new WindowsDialog ( ) , null }
5454 } ;
5555
5656 private static IQuickSwitchDialogWindow _dialogWindow = null ;
@@ -179,6 +179,12 @@ public static void SetupQuickSwitch(bool enabled)
179179 _quickSwitchExplorers [ explorer ] = null ;
180180 }
181181
182+ // Remove dialog windows
183+ foreach ( var dialog in _quickSwitchDialogs . Keys )
184+ {
185+ _quickSwitchDialogs [ dialog ] = null ;
186+ }
187+
182188 // Remove dialog window handle
183189 var dialogWindowExists = false ;
184190 lock ( _dialogWindowLock )
@@ -402,14 +408,27 @@ uint dwmsEventTime
402408 // Check if it is a file dialog window
403409 var isDialogWindow = false ;
404410 var dialogWindowChanged = false ;
405- foreach ( var dialog in _quickSwitchDialogs )
411+ foreach ( var dialog in _quickSwitchDialogs . Keys )
406412 {
407- if ( dialog . CheckDialogWindow ( hwnd ) )
413+ IQuickSwitchDialogWindow dialogWindow ;
414+ var existingDialogWindow = _quickSwitchDialogs [ dialog ] ;
415+ if ( existingDialogWindow != null && existingDialogWindow . Handle == hwnd )
416+ {
417+ // If the dialog window is already in the list, no need to check again
418+ dialogWindow = existingDialogWindow ;
419+ }
420+ else
421+ {
422+ dialogWindow = dialog . CheckDialogWindow ( hwnd ) ;
423+ }
424+
425+ // If the dialog window is found, set it
426+ if ( dialogWindow != null )
408427 {
409428 lock ( _dialogWindowLock )
410429 {
411430 dialogWindowChanged = _dialogWindow == null || _dialogWindow . Handle != hwnd ;
412- _dialogWindow = dialog . DialogWindow ;
431+ _dialogWindow = dialogWindow ;
413432 }
414433
415434 isDialogWindow = true ;
@@ -667,21 +686,30 @@ private static IQuickSwitchDialogWindow GetDialogWindow(HWND hwnd)
667686 }
668687
669688 // Then check all dialog windows
670- foreach ( var dialog in _quickSwitchDialogs )
689+ foreach ( var dialogWindow in _quickSwitchDialogs . Values )
671690 {
672- if ( dialog . DialogWindow . Handle == hwnd )
691+ if ( dialogWindow . Handle == hwnd )
673692 {
674- return dialog . DialogWindow ;
693+ return dialogWindow ;
675694 }
676695 }
677696
678- // Finally search for the dialog window
679- foreach ( var dialog in _quickSwitchDialogs )
697+ // Finally search for the dialog window again
698+ foreach ( var dialog in _quickSwitchDialogs . Keys )
680699 {
681- if ( dialog . CheckDialogWindow ( hwnd ) )
700+ IQuickSwitchDialogWindow dialogWindow ;
701+ var existingDialogWindow = _quickSwitchDialogs [ dialog ] ;
702+ if ( existingDialogWindow != null && existingDialogWindow . Handle == hwnd )
682703 {
683- return dialog . DialogWindow ;
704+ // If the dialog window is already in the list, no need to check again
705+ dialogWindow = existingDialogWindow ;
684706 }
707+ else
708+ {
709+ dialogWindow = dialog . CheckDialogWindow ( hwnd ) ;
710+ }
711+
712+ return dialogWindow ;
685713 }
686714
687715 return null ;
@@ -820,8 +848,9 @@ public static void Dispose()
820848 }
821849
822850 // Dispose dialogs
823- foreach ( var dialog in _quickSwitchDialogs )
851+ foreach ( var dialog in _quickSwitchDialogs . Keys )
824852 {
853+ _quickSwitchDialogs [ dialog ] ? . Dispose ( ) ;
825854 dialog . Dispose ( ) ;
826855 }
827856 _quickSwitchDialogs . Clear ( ) ;
0 commit comments