Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force activate existing windows when running a commandline in them #9137

Merged
5 commits merged into from Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cascadia/WindowsTerminal/AppHost.cpp
Expand Up @@ -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());
}

Expand Down
25 changes: 25 additions & 0 deletions src/cascadia/WindowsTerminal/IslandWindow.cpp
Expand Up @@ -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));
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
}

DEFINE_EVENT(IslandWindow, DragRegionClicked, _DragRegionClickedHandlers, winrt::delegate<>);
DEFINE_EVENT(IslandWindow, WindowCloseButtonClicked, _windowCloseButtonClickedHandler, winrt::delegate<>);
2 changes: 2 additions & 0 deletions src/cascadia/WindowsTerminal/IslandWindow.h
Expand Up @@ -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<>);
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/WindowsTerminal/pch.h
Expand Up @@ -58,6 +58,7 @@ Module Name:
// Additional headers for various xaml features. We need:
// * Controls for grid
// * Media for ScaleTransform
#include <winrt/Windows.UI.Core.h>
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
#include <winrt/Windows.UI.Xaml.Controls.h>
#include <winrt/Windows.ui.xaml.media.h>

Expand Down
4 changes: 2 additions & 2 deletions src/server/lib/server.vcxproj
Expand Up @@ -58,13 +58,13 @@
<ClInclude Include="..\WinNTControl.h" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\host\dll\Host.DLL.vcxproj">
<ProjectReference Include="..\..\host\proxy\Host.Proxy.vcxproj">
<Project>{e437b604-3e98-4f40-a927-e173e818ea4b}</Project>
</ProjectReference>
</ItemGroup>
<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>$(IntDir)..\Host.ProxyDll;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(IntDir)..\OpenConsoleProxy;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>
<!-- Careful reordering these. Some default props (contained in these files) are order sensitive. -->
Expand Down