From 50060452ea115b85e2af672434db8a1c1e140f1f Mon Sep 17 00:00:00 2001 From: Leonard Hecker Date: Fri, 13 Dec 2024 09:13:03 -0800 Subject: [PATCH] Pass foreground rights to the emperor (#18325) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Forgetti di spaghetti in #18215. Closes #18324 ## Validation Steps Performed * Launch through the start menu * Explicitly minimize * Then... * Launch through the start menu again ✅ * Launch via wtd.exe in Win+R ✅ * Launch via wtd.exe in another Terminal ✅ * Launch via handoff ✅ --- src/cascadia/WindowsTerminal/WindowEmperor.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/cascadia/WindowsTerminal/WindowEmperor.cpp b/src/cascadia/WindowsTerminal/WindowEmperor.cpp index 66bc375d0af..6c25d4c8ade 100644 --- a/src/cascadia/WindowsTerminal/WindowEmperor.cpp +++ b/src/cascadia/WindowsTerminal/WindowEmperor.cpp @@ -170,6 +170,14 @@ static wil::unique_mutex acquireMutexOrAttemptHandoff(const wchar_t* className, .cbData = gsl::narrow(payload.size()), .lpData = payload.data(), }; + + // Allow the existing instance to gain foreground rights. + DWORD processId = 0; + if (GetWindowThreadProcessId(hwnd, &processId) && processId) + { + AllowSetForegroundWindow(processId); + } + if (SendMessageTimeoutW(hwnd, WM_COPYDATA, 0, reinterpret_cast(&cds), SMTO_ABORTIFHUNG | SMTO_ERRORONEXIT, 5000, nullptr)) { return {};