From ce879520584ec354ec6edc6b782cf027a3909491 Mon Sep 17 00:00:00 2001 From: Jaime Bernardo Date: Tue, 30 May 2023 15:58:42 +0100 Subject: [PATCH] [Peek]Close on activation shortcut if focused (#26480) --- src/modules/peek/peek/dllmain.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/modules/peek/peek/dllmain.cpp b/src/modules/peek/peek/dllmain.cpp index 0ecd7d75c4..809828c600 100644 --- a/src/modules/peek/peek/dllmain.cpp +++ b/src/modules/peek/peek/dllmain.cpp @@ -62,6 +62,7 @@ private: bool m_alwaysRunNotElevated = true; HANDLE m_hProcess = 0; + DWORD m_processPid = 0; HANDLE m_hInvokeEvent; @@ -233,7 +234,7 @@ private: return false; } - bool is_explorer_or_desktop_window_focused() + bool is_peek_or_explorer_or_desktop_window_focused() { HWND foregroundWindowHandle = GetForegroundWindow(); if (foregroundWindowHandle == NULL) @@ -241,6 +242,16 @@ private: return false; } + DWORD pid{}; + if (GetWindowThreadProcessId(foregroundWindowHandle, &pid)!=0) + { + // If the foreground window is the Peek window, send activation signal. + if (m_processPid != 0 && pid == m_processPid) + { + return true; + } + } + if (is_desktop_window(foregroundWindowHandle)) { return true; @@ -276,6 +287,7 @@ private: std::optional processStartedInfo = RunNonElevatedFailsafe(runExecutablePath, executable_args, modulePath, PROCESS_QUERY_INFORMATION | SYNCHRONIZE | PROCESS_TERMINATE); if (processStartedInfo.has_value()) { + m_processPid = processStartedInfo.value().processID; m_hProcess = processStartedInfo.value().processHandle.release(); } else @@ -303,6 +315,7 @@ private: } m_hProcess = sei.hProcess; + m_processPid = GetProcessId(m_hProcess); } } @@ -401,6 +414,7 @@ public: } CloseHandle(m_hProcess); m_hProcess = 0; + m_processPid = 0; } m_enabled = false; @@ -436,8 +450,8 @@ public: { Logger::trace(L"Peek hotkey pressed"); - // Only activate and consume the shortcut if it is an explorer or desktop window is the foreground application. - if (is_explorer_or_desktop_window_focused()) + // Only activate and consume the shortcut if a Peek, explorer or desktop window is the foreground application. + if (is_peek_or_explorer_or_desktop_window_focused()) { // TODO: fix VK_SPACE DestroyWindow in viewer app if (!is_viewer_running())