@@ -1495,8 +1495,43 @@ int wmain(int argc, wchar_t* argv[]) {
14951495 // Detach from console entirely to prevent flash
14961496 FreeConsole ();
14971497
1498- bool result = focus_console_window ();
1499- return result ? 0 : 1 ;
1498+ // Protocol handlers have focus restrictions - use aggressive approach
1499+ HWND targetWnd = get_saved_console_window_handle ();
1500+ if (!targetWnd) {
1501+ // Fallback: find any terminal window
1502+ EnumWindows ([](HWND hwnd, LPARAM lParam) -> BOOL {
1503+ wchar_t className[256 ];
1504+ GetClassNameW (hwnd, className, 256 );
1505+ if (IsWindowVisible (hwnd) &&
1506+ (wcscmp (className, L" CASCADIA_HOSTING_WINDOW_CLASS" ) == 0 ||
1507+ wcscmp (className, L" ConsoleWindowClass" ) == 0 )) {
1508+ *(HWND*)lParam = hwnd;
1509+ return FALSE ;
1510+ }
1511+ return TRUE ;
1512+ }, (LPARAM)&targetWnd);
1513+ }
1514+
1515+ if (targetWnd) {
1516+ // Simulate user input to bypass focus restrictions
1517+ INPUT input = {0 };
1518+ input.type = INPUT_MOUSE;
1519+ SendInput (1 , &input, sizeof (INPUT));
1520+
1521+ // Restore if minimized
1522+ if (IsIconic (targetWnd)) {
1523+ ShowWindow (targetWnd, SW_RESTORE);
1524+ }
1525+
1526+ // Try SwitchToThisWindow (works better for protocol handlers)
1527+ SwitchToThisWindow (targetWnd, TRUE );
1528+
1529+ // Also try standard approach
1530+ force_foreground_window (targetWnd);
1531+ return 0 ;
1532+ }
1533+
1534+ return 1 ;
15001535 }
15011536
15021537 if (doRegister) {
@@ -1527,6 +1562,22 @@ int wmain(int argc, wchar_t* argv[]) {
15271562 // Save the terminal window handle for click-to-focus
15281563 // Walk process tree to find the actual terminal/IDE window
15291564 HWND terminalWnd = find_ancestor_window ();
1565+
1566+ // Fallback: if process tree didn't find a window, search for any terminal
1567+ if (!terminalWnd) {
1568+ EnumWindows ([](HWND hwnd, LPARAM lParam) -> BOOL {
1569+ wchar_t className[256 ];
1570+ GetClassNameW (hwnd, className, 256 );
1571+ if (IsWindowVisible (hwnd) &&
1572+ (wcscmp (className, L" CASCADIA_HOSTING_WINDOW_CLASS" ) == 0 ||
1573+ wcscmp (className, L" ConsoleWindowClass" ) == 0 )) {
1574+ *(HWND*)lParam = hwnd;
1575+ return FALSE ;
1576+ }
1577+ return TRUE ;
1578+ }, (LPARAM)&terminalWnd);
1579+ }
1580+
15301581 if (terminalWnd) {
15311582 save_console_window_handle (terminalWnd);
15321583 }
0 commit comments