From 5f7b91136f96823cc696dd356d6137e50cce26ba Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Wed, 15 Dec 2021 20:00:35 +0200 Subject: [PATCH] [Windows] Detect new Windows Terminal and disable unsupported set_console_visible code. (cherry picked from commit 9aef3a93dd26dd3f7e7d03283cbcf85d52f6dada) --- platform/windows/os_windows.cpp | 21 ++++++++++++++++++--- platform/windows/os_windows.h | 2 ++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index 1a6c77173be..aa29387c1ee 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -2229,11 +2229,25 @@ bool OS_Windows::is_window_focused() const { return window_focused; } +bool OS_Windows::_is_win11_terminal() const { + HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); + DWORD dwMode = 0; + if (GetConsoleMode(hStdOut, &dwMode)) { + return ((dwMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == ENABLE_VIRTUAL_TERMINAL_PROCESSING); + } else { + return false; + } +} + void OS_Windows::set_console_visible(bool p_enabled) { if (console_visible == p_enabled) return; - ShowWindow(GetConsoleWindow(), p_enabled ? SW_SHOW : SW_HIDE); - console_visible = p_enabled; + + if (!_is_win11_terminal()) { + // GetConsoleWindow is not supported by the Windows Terminal. + ShowWindow(GetConsoleWindow(), p_enabled ? SW_SHOW : SW_HIDE); + console_visible = p_enabled; + } } bool OS_Windows::is_console_visible() const { @@ -2840,7 +2854,8 @@ Error OS_Windows::execute(const String &p_path, const List &p_arguments, } DWORD creation_flags = NORMAL_PRIORITY_CLASS & CREATE_NO_WINDOW; - if (p_path == get_executable_path() && GetConsoleWindow() != NULL) { + if (p_path == get_executable_path() && GetConsoleWindow() != NULL && _is_win11_terminal()) { + // Open a new terminal as a workaround for Windows Terminal bug. creation_flags |= CREATE_NEW_CONSOLE; } diff --git a/platform/windows/os_windows.h b/platform/windows/os_windows.h index c623523d72a..106aaf3c2f6 100644 --- a/platform/windows/os_windows.h +++ b/platform/windows/os_windows.h @@ -371,6 +371,8 @@ class OS_Windows : public OS { CrashHandler crash_handler; + bool _is_win11_terminal() const; + void _drag_event(float p_x, float p_y, int idx); void _touch_event(bool p_pressed, float p_x, float p_y, int idx);