Skip to content

Commit

Permalink
we do not want this
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Feb 7, 2024
1 parent 8309901 commit 4ab628d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/cascadia/Remoting/WindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,26 @@ namespace winrt::Microsoft::Terminal::Remoting::implementation
//
// * If we're running unpackaged: the .winmd must be a sibling of the .exe
// * If we're running packaged: the .winmd must be in the package root
_monarch = try_create_instance<Remoting::IMonarch>(MonarchCLSID(),
CLSCTX_LOCAL_SERVER);
// const winrt::com_ptr<::IUnknown> monarch = try_create_instance<::IUnknown>(MonarchCLSID(),
// CLSCTX_LOCAL_SERVER);
// // const auto monarch = try_create_instance<Remoting::IMonarch>(MonarchCLSID(),
// // CLSCTX_LOCAL_SERVER);

// if (monarch)
// {
// const auto hr = (CoAllowSetForegroundWindow(monarch.get(), nullptr));
// if (FAILED(hr))
// {
// DebugBreak();
// }
// }

// _monarch = monarch.try_as<Remoting::IMonarch>();
_monarch = try_create_instance<Remoting::IMonarch>(MonarchCLSID(), CLSCTX_LOCAL_SERVER);
if (_monarch)
{
LOG_LAST_ERROR_IF(!AllowSetForegroundWindow((DWORD)_monarch.GetPID()));
}
}

// Check if we became the king, and if we are, wire up callbacks.
Expand Down Expand Up @@ -489,6 +507,9 @@ namespace winrt::Microsoft::Terminal::Remoting::implementation
summonArgs.ToggleVisibility(false);

args.SummonBehavior(summonArgs);

LOG_LAST_ERROR_IF(!AllowSetForegroundWindow((DWORD)monarch.GetPID()));

monarch.SummonWindow(args);
return true;
}
Expand Down
46 changes: 46 additions & 0 deletions src/cascadia/WindowsTerminal/IslandWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ using VirtualKeyModifiers = winrt::Windows::System::VirtualKeyModifiers;
#define XAML_HOSTING_WINDOW_CLASS_NAME L"CASCADIA_HOSTING_WINDOW_CLASS"
#define IDM_SYSTEM_MENU_BEGIN 0x1000

void _trace(const wchar_t* const pwsz)
{
TraceLoggingWrite(g_hWindowsTerminalProvider,
"TraceMessage",
TraceLoggingDescription("debug print messages"),
TraceLoggingWideString(pwsz, "message", "the message"),
TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
TraceLoggingKeyword(TIL_KEYWORD_TRACE));
}

IslandWindow::IslandWindow() noexcept :
_interopWindowHandle{ nullptr },
_rootGrid{ nullptr },
Expand Down Expand Up @@ -1522,6 +1532,7 @@ void IslandWindow::_globalActivateWindow(const uint32_t dropdownDuration,
// restore-down the window.
if (IsIconic(_window.get()))
{
_trace(L"window was iconic");
if (dropdownDuration > 0)
{
_dropdownWindow(dropdownDuration, toMonitor);
Expand All @@ -1534,16 +1545,47 @@ void IslandWindow::_globalActivateWindow(const uint32_t dropdownDuration,
// able to properly set this as the foreground window.
if (!IsWindowVisible(GetHandle()))
{
_trace(L"window wasn't visible");
ShowWindow(_window.get(), SW_SHOW);
}
ShowWindow(_window.get(), SW_RESTORE);
if (!SetForegroundWindow(_window.get()))
{
const auto gle = GetLastError();
_trace(fmt::format(L"SetForegroundWindow failed: {}", gle).c_str());

const auto fg = GetForegroundWindow();
std::wstring title(GetWindowTextLength(fg) + 1, L'\0');
GetWindowTextW(fg, &title[0], (DWORD)title.size());
DWORD fgPid = 0;
GetWindowThreadProcessId(fg, &fgPid);
_trace(fmt::format(L"Foreground Window is: [{}]={}", (uint64_t)fgPid, title).c_str());

SwitchToThisWindow(_window.get(), false);
}
else
{
_trace(L"got fg?");
}

// LOG_IF_WIN32_BOOL_FALSE(BringWindowToTop(_window.get()));
if (!BringWindowToTop(_window.get()))
{
const auto gle = GetLastError();
_trace(fmt::format(L"BringWindowToTop failed: {}", gle).c_str());
}
else
{
_trace(L"brought to top?");
}

// Once we've been restored, throw us on the active monitor.
_moveToMonitor(oldForegroundWindow, toMonitor);
}
}
else
{
_trace(L"window wasn't iconic");
// Try first to send a message to the current foreground window. If it's not responding, it may
// be waiting on us to finish launching. Passing SMTO_NOTIMEOUTIFNOTHUNG means that we get the same
// behavior as before--that is, waiting for the message loop--but we've done an early return if
Expand All @@ -1569,6 +1611,10 @@ void IslandWindow::_globalActivateWindow(const uint32_t dropdownDuration,
// Throw us on the active monitor.
_moveToMonitor(oldForegroundWindow, toMonitor);
}
else
{
_trace(L"huh");
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions src/cascadia/WindowsTerminal/WindowEmperor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@ void _buildArgsFromCommandline(std::vector<winrt::hstring>& args)
args.emplace_back(L"wt.exe");
}
}

HWND _createToolWindow()
{
WNDCLASS wc{};
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hInstance = reinterpret_cast<HINSTANCE>(&__ImageBase);
wc.lpszClassName = TERMINAL_MESSAGE_CLASS_NAME;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = DefWindowProc; //WindowEmperor::_wndProc;
wc.hIcon = LoadIconW(wc.hInstance, MAKEINTRESOURCEW(IDI_APPICON));
RegisterClass(&wc);
// WINRT_ASSERT(!_window);

return CreateWindow(wc.lpszClassName,
L"Windows Terminal Notification Handoff",
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
HWND_MESSAGE,
nullptr,
wc.hInstance,
nullptr);
}

bool WindowEmperor::_handleLaunchArgs()
try
{
Expand Down Expand Up @@ -114,6 +140,8 @@ try
// }
}
}
auto fake = _createToolWindow();
fake;
return winrt::Microsoft::Terminal::Remoting::WindowManager::SummonForNotification(window);
}
}
Expand Down

0 comments on commit 4ab628d

Please sign in to comment.