From ed5b3d6b863389e036fb5db4e8e50b01803229d0 Mon Sep 17 00:00:00 2001 From: "Dustin L. Howett" Date: Thu, 11 Feb 2021 21:43:43 -0800 Subject: [PATCH 1/3] Fix the Host Proxy DLL reference in ServerLib Fixes #9128 --- src/server/lib/server.vcxproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server/lib/server.vcxproj b/src/server/lib/server.vcxproj index ec7ec05432c..4ceafacd986 100644 --- a/src/server/lib/server.vcxproj +++ b/src/server/lib/server.vcxproj @@ -58,13 +58,13 @@ - + {e437b604-3e98-4f40-a927-e173e818ea4b} - $(IntDir)..\Host.ProxyDll;%(AdditionalIncludeDirectories) + $(IntDir)..\OpenConsoleProxy;%(AdditionalIncludeDirectories) From 73ded760d15a8ac3800c6793b60dd311efcbd7a6 Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Fri, 12 Feb 2021 12:32:42 -0600 Subject: [PATCH 2/3] When running a commandline in an existing window, ACTIVATE that window (by force) --- src/cascadia/WindowsTerminal/AppHost.cpp | 1 + src/cascadia/WindowsTerminal/IslandWindow.cpp | 25 +++++++++++++++++++ src/cascadia/WindowsTerminal/IslandWindow.h | 2 ++ src/cascadia/WindowsTerminal/pch.h | 1 + 4 files changed, 29 insertions(+) diff --git a/src/cascadia/WindowsTerminal/AppHost.cpp b/src/cascadia/WindowsTerminal/AppHost.cpp index bc45ea9605a..492a21d1d27 100644 --- a/src/cascadia/WindowsTerminal/AppHost.cpp +++ b/src/cascadia/WindowsTerminal/AppHost.cpp @@ -530,6 +530,7 @@ bool AppHost::HasWindow() void AppHost::_DispatchCommandline(winrt::Windows::Foundation::IInspectable /*sender*/, Remoting::CommandlineArgs args) { + _window->SummonWindow(); _logic.ExecuteCommandline(args.Commandline(), args.CurrentDirectory()); } diff --git a/src/cascadia/WindowsTerminal/IslandWindow.cpp b/src/cascadia/WindowsTerminal/IslandWindow.cpp index ff3a4967e2c..5cb3aa88773 100644 --- a/src/cascadia/WindowsTerminal/IslandWindow.cpp +++ b/src/cascadia/WindowsTerminal/IslandWindow.cpp @@ -844,5 +844,30 @@ void IslandWindow::_ApplyWindowSize() SWP_FRAMECHANGED | SWP_NOACTIVATE)); } +winrt::fire_and_forget IslandWindow::SummonWindow() +{ + // On the foreground thread: + co_await winrt::resume_foreground(_rootGrid.Dispatcher()); + + // From: https://stackoverflow.com/a/59659421 + // > The trick is to make windows ‘think’ that our process and the target + // > window (hwnd) are related by attaching the threads (using + // > AttachThreadInput API) and using an alternative API: BringWindowToTop. + // If the window is minimized, then restore it. We don't want to do this + // always though, because SW_RESTORE'ing a maximized window will + // restore-down it. + if (IsIconic(_window.get())) + { + LOG_IF_WIN32_BOOL_FALSE(ShowWindow(_window.get(), SW_RESTORE)); + } + const DWORD windowThreadProcessId = GetWindowThreadProcessId(GetForegroundWindow(), LPDWORD(0)); + const DWORD currentThreadId = GetCurrentThreadId(); + + LOG_IF_WIN32_BOOL_FALSE(AttachThreadInput(windowThreadProcessId, currentThreadId, true)); + LOG_IF_WIN32_BOOL_FALSE(BringWindowToTop(_window.get())); + LOG_IF_WIN32_BOOL_FALSE(ShowWindow(_window.get(), SW_SHOW)); + LOG_IF_WIN32_BOOL_FALSE(AttachThreadInput(windowThreadProcessId, currentThreadId, false)); +} + DEFINE_EVENT(IslandWindow, DragRegionClicked, _DragRegionClickedHandlers, winrt::delegate<>); DEFINE_EVENT(IslandWindow, WindowCloseButtonClicked, _windowCloseButtonClickedHandler, winrt::delegate<>); diff --git a/src/cascadia/WindowsTerminal/IslandWindow.h b/src/cascadia/WindowsTerminal/IslandWindow.h index fe0cae1ec65..f17b81c252e 100644 --- a/src/cascadia/WindowsTerminal/IslandWindow.h +++ b/src/cascadia/WindowsTerminal/IslandWindow.h @@ -38,6 +38,8 @@ class IslandWindow : void FlashTaskbar(); void SetTaskbarProgress(const size_t state, const size_t progress); + winrt::fire_and_forget SummonWindow(); + #pragma endregion DECLARE_EVENT(DragRegionClicked, _DragRegionClickedHandlers, winrt::delegate<>); diff --git a/src/cascadia/WindowsTerminal/pch.h b/src/cascadia/WindowsTerminal/pch.h index b416ccccc4f..149f8a97e37 100644 --- a/src/cascadia/WindowsTerminal/pch.h +++ b/src/cascadia/WindowsTerminal/pch.h @@ -58,6 +58,7 @@ Module Name: // Additional headers for various xaml features. We need: // * Controls for grid // * Media for ScaleTransform +#include #include #include From ed172c9e87760be356075c97173d0297e283590d Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Fri, 12 Feb 2021 14:29:55 -0600 Subject: [PATCH 3/3] some cleanup from review --- src/cascadia/WindowsTerminal/IslandWindow.cpp | 19 +++++++++++++++---- src/cascadia/WindowsTerminal/pch.h | 1 + 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/cascadia/WindowsTerminal/IslandWindow.cpp b/src/cascadia/WindowsTerminal/IslandWindow.cpp index 5cb3aa88773..0850f7e08df 100644 --- a/src/cascadia/WindowsTerminal/IslandWindow.cpp +++ b/src/cascadia/WindowsTerminal/IslandWindow.cpp @@ -844,6 +844,14 @@ void IslandWindow::_ApplyWindowSize() SWP_FRAMECHANGED | SWP_NOACTIVATE)); } +// Method Description: +// - Force activate this window. This method will bring us to the foreground and +// activate us. If the window is minimized, it will restore the window. If the +// window is on another desktop, the OS will switch to that desktop. +// Arguments: +// - +// Return Value: +// - winrt::fire_and_forget IslandWindow::SummonWindow() { // On the foreground thread: @@ -854,19 +862,22 @@ winrt::fire_and_forget IslandWindow::SummonWindow() // > window (hwnd) are related by attaching the threads (using // > AttachThreadInput API) and using an alternative API: BringWindowToTop. // If the window is minimized, then restore it. We don't want to do this - // always though, because SW_RESTORE'ing a maximized window will - // restore-down it. + // always though, because if you SW_RESTORE a maximized window, it will + // restore-down the window. if (IsIconic(_window.get())) { LOG_IF_WIN32_BOOL_FALSE(ShowWindow(_window.get(), SW_RESTORE)); } - const DWORD windowThreadProcessId = GetWindowThreadProcessId(GetForegroundWindow(), LPDWORD(0)); + const DWORD windowThreadProcessId = GetWindowThreadProcessId(GetForegroundWindow(), nullptr); const DWORD currentThreadId = GetCurrentThreadId(); LOG_IF_WIN32_BOOL_FALSE(AttachThreadInput(windowThreadProcessId, currentThreadId, true)); + // Just in case, add the thread detach as a scope_exit, to make _sure_ we do it. + auto detachThread = wil::scope_exit([windowThreadProcessId, currentThreadId]() { + LOG_IF_WIN32_BOOL_FALSE(AttachThreadInput(windowThreadProcessId, currentThreadId, false)); + }); LOG_IF_WIN32_BOOL_FALSE(BringWindowToTop(_window.get())); LOG_IF_WIN32_BOOL_FALSE(ShowWindow(_window.get(), SW_SHOW)); - LOG_IF_WIN32_BOOL_FALSE(AttachThreadInput(windowThreadProcessId, currentThreadId, false)); } DEFINE_EVENT(IslandWindow, DragRegionClicked, _DragRegionClickedHandlers, winrt::delegate<>); diff --git a/src/cascadia/WindowsTerminal/pch.h b/src/cascadia/WindowsTerminal/pch.h index 149f8a97e37..2a4fb9c4a60 100644 --- a/src/cascadia/WindowsTerminal/pch.h +++ b/src/cascadia/WindowsTerminal/pch.h @@ -56,6 +56,7 @@ Module Name: #include // Additional headers for various xaml features. We need: +// * Core so we can resume_foreground // * Controls for grid // * Media for ScaleTransform #include