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

Create a window process for the tray icon #10980

Merged
7 commits merged into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/cascadia/WindowsTerminal/TrayIcon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@ TrayIcon::~TrayIcon()
RemoveIconFromTray();
}

void TrayIcon::_CreateWindow()
{
WNDCLASSW wc{};
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hInstance = wil::GetModuleInstanceHandle();
wc.lpszClassName = L"TRAY_ICON_HOSTING_WINDOW_CLASS";
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = DefWindowProcW;
wc.hIcon = static_cast<HICON>(GetActiveAppIconHandle(true));
RegisterClass(&wc);

_trayIconHwnd = wil::unique_hwnd(CreateWindowW(wc.lpszClassName,
wc.lpszClassName,
WS_DISABLED,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
HWND_MESSAGE,
nullptr,
wc.hInstance,
nullptr));

WINRT_VERIFY(_trayIconHwnd);
}

// Method Description:
// - Creates and adds an icon to the notification tray.
// If an icon already exists, update the HWND associated
Expand All @@ -33,6 +59,15 @@ TrayIcon::~TrayIcon()
// - <none>
void TrayIcon::CreateTrayIcon()
{
if (!_trayIconHwnd)
{
// Creating a disabled, non visible window just so we can set it
// as the foreground window when showing the context menu.
// This is done so that the context menu can be dismissed
// when clicking outside of it.
_CreateWindow();
}

NOTIFYICONDATA nid{};
nid.cbSize = sizeof(NOTIFYICONDATA);

Expand Down Expand Up @@ -81,7 +116,7 @@ void TrayIcon::ShowTrayContextMenu(const til::point coord,
{
// We'll need to set our window to the foreground before calling
// TrackPopupMenuEx or else the menu won't dismiss when clicking away.
SetForegroundWindow(_owningHwnd);
SetForegroundWindow(_trayIconHwnd.get());

// User can select menu items with the left and right buttons.
UINT uFlags = TPM_RIGHTBUTTON;
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/WindowsTerminal/TrayIcon.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ class TrayIcon
WINRT_CALLBACK(SummonWindowRequested, winrt::delegate<void(winrt::Microsoft::Terminal::Remoting::SummonWindowSelectionArgs)>);

private:
void _CreateWindow();
HMENU _CreateTrayContextMenu(winrt::Windows::Foundation::Collections::IMapView<uint64_t, winrt::hstring> peasants);

wil::unique_hwnd _trayIconHwnd;
HWND _owningHwnd;
NOTIFYICONDATA _trayIconData;
};