Repository: microsoft/terminal (language: cpp)

Issue:
New window instances do not gain foreground rights
### Windows Terminal version

1.23.3471.0

### Windows build number

_No response_

### Other Software

_No response_

### Steps to reproduce

* Set "New instance behavior" to "Create a new window"
* Launch from the start menu (= first window, first tab)
* Explicitly minimize
* Launch from the start menu again

### Expected Behavior

New window is in the foreground.

### Actual Behavior

New window fails to gain foreground rights.


Discussion / hints:
Can we `AllowSetForeground`? I suppose we don't know the PID of the instance holding the mutex, do we. hmm....

Fix patch:
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<DWORD>(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<LPARAM>(&cds), SMTO_ABORTIFHUNG | SMTO_ERRORONEXIT, 5000, nullptr))
             {
                 return {};


Test patch: