Skip to content

Commit ab5d5ae

Browse files
committed
Hook delegates can be garbage‑collected
1 parent fc72b4b commit ab5d5ae

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

Flow.Launcher.Infrastructure/QuickSwitch/QuickSwitch.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public static class QuickSwitch
5959
private static HWINEVENTHOOK _locationChangeHook = HWINEVENTHOOK.Null;
6060
private static HWINEVENTHOOK _destroyChangeHook = HWINEVENTHOOK.Null;
6161

62+
private static readonly WINEVENTPROC _fgProc = ForegroundChangeCallback;
63+
private static readonly WINEVENTPROC _locProc = LocationChangeCallback;
64+
private static readonly WINEVENTPROC _desProc = DestroyChangeCallback;
65+
6266
private static DispatcherTimer _dragMoveTimer = null;
6367

6468
// A list of all file dialog windows that are auto switched already
@@ -68,7 +72,8 @@ public static class QuickSwitch
6872
// Note: Here we do not start & stop the timer beacause when there are many dialog windows
6973
// Unhooking and hooking will take too much time which can make window position weird
7074
// So we start & stop the timer when we find a file dialog window
71-
/*private static HWINEVENTHOOK _moveSizeHook = HWINEVENTHOOK.Null;*/
75+
/*private static HWINEVENTHOOK _moveSizeHook = HWINEVENTHOOK.Null;
76+
private static HWINEVENTHOOK _moveProc = MoveSizeCallBack;*/
7277

7378
private static readonly SemaphoreSlim _navigationLock = new(1, 1);
7479

@@ -151,23 +156,23 @@ public static void SetupQuickSwitch(bool enabled)
151156
PInvoke.EVENT_SYSTEM_FOREGROUND,
152157
PInvoke.EVENT_SYSTEM_FOREGROUND,
153158
PInvoke.GetModuleHandle((PCWSTR)null),
154-
ForegroundChangeCallback,
159+
_fgProc,
155160
0,
156161
0,
157162
PInvoke.WINEVENT_OUTOFCONTEXT);
158163
_locationChangeHook = PInvoke.SetWinEventHook(
159164
PInvoke.EVENT_OBJECT_LOCATIONCHANGE,
160165
PInvoke.EVENT_OBJECT_LOCATIONCHANGE,
161166
PInvoke.GetModuleHandle((PCWSTR)null),
162-
LocationChangeCallback,
167+
_locProc,
163168
0,
164169
0,
165170
PInvoke.WINEVENT_OUTOFCONTEXT);
166171
_destroyChangeHook = PInvoke.SetWinEventHook(
167172
PInvoke.EVENT_OBJECT_DESTROY,
168173
PInvoke.EVENT_OBJECT_DESTROY,
169174
PInvoke.GetModuleHandle((PCWSTR)null),
170-
DestroyChangeCallback,
175+
_desProc,
171176
0,
172177
0,
173178
PInvoke.WINEVENT_OUTOFCONTEXT);
@@ -268,14 +273,14 @@ private static unsafe void InvokeShowQuickSwitchWindow(bool dialogWindowChanged)
268273
_moveSizeHook = HWINEVENTHOOK.Null;
269274
}
270275
271-
// Call MoveSizeCallBack when the window is moved or resized
276+
// Call _moveProc when the window is moved or resized
272277
uint processId;
273278
var threadId = PInvoke.GetWindowThreadProcessId(_dialogWindow.Handle, &processId);
274279
_moveSizeHook = PInvoke.SetWinEventHook(
275280
PInvoke.EVENT_SYSTEM_MOVESIZESTART,
276281
PInvoke.EVENT_SYSTEM_MOVESIZEEND,
277282
PInvoke.GetModuleHandle((PCWSTR)null),
278-
MoveSizeCallBack,
283+
_moveProc,
279284
processId,
280285
threadId,
281286
PInvoke.WINEVENT_OUTOFCONTEXT);

0 commit comments

Comments
 (0)