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

Add tray icon when quake window is minimized #10179

Merged
merged 17 commits into from
Jul 8, 2021
80 changes: 80 additions & 0 deletions src/cascadia/WindowsTerminal/AppHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "../WinRTUtils/inc/WtExeUtils.h"
#include "resource.h"
#include "VirtualDesktopUtils.h"
#include "icon.h"

using namespace winrt::Windows::UI;
using namespace winrt::Windows::UI::Composition;
Expand Down Expand Up @@ -77,6 +78,8 @@ AppHost::AppHost() noexcept :
_window->MouseScrolled({ this, &AppHost::_WindowMouseWheeled });
_window->WindowActivated({ this, &AppHost::_WindowActivated });
_window->HotkeyPressed({ this, &AppHost::_GlobalHotkeyPressed });
_window->NotifyTrayIconPressed({ this, &AppHost::_HandleTrayIconPressed });
_window->NotifyWindowHidden({ this, &AppHost::_HandleWindowHidden });
_window->SetAlwaysOnTop(_logic.GetInitialAlwaysOnTop());
_window->MakeWindow();

Expand Down Expand Up @@ -915,5 +918,82 @@ void AppHost::_HandleSettingsChanged(const winrt::Windows::Foundation::IInspecta
void AppHost::_IsQuakeWindowChanged(const winrt::Windows::Foundation::IInspectable&,
const winrt::Windows::Foundation::IInspectable&)
{
if (_window->IsQuakeWindow() && !_logic.IsQuakeWindow())
{
// If we're exiting quake mode, we should make our
// tray icon disappear.
if (_trayIconData)
{
Shell_NotifyIcon(NIM_DELETE, &_trayIconData.value());
_trayIconData.reset();
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discussion: should we

	else if (!_window->IsQuakeWindow() && _logic.IsQuakeWindow())
	{
        _UpdateTrayIcon();
    }

to show the icon as soon as the quake window is created? Or do we want the icon to only appear when the window is minimized?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know what, I like that. The icon will kinda serve as an indicator that a quake window is alive (hidden or not), and the user can always rely on that icon to get focus the quake window.

In fact, right now the icon is still visible when the quake window is visible. So, it's actually a bit inconsistent because the first time you bring up the quake window, the icon isn't there yet (because it'll only add it on a minimize). So yeah I think I'll add the icon as soon as the window's created.

I guess another route that's possible is to only show the icon when the quake window is minimized, and if it's not (aka. shown/restored), remove the icon? Still kinda like having the icon there in all states though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still kinda like having the icon there in all states though.

me too

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't looked through the spec in a while, but wasn't the plan to have the tray icon serve as a way to target windows with specific names and stuff like that? Like, if the tray icon is supposed to provide functionality outside of quake mode, shouldn't we always show it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[feature today] -> [feature tomorrow] -> [feature in 1 year] 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this iteration of the tray icon (which only works for quake mode), it makes sense to me to show it only if there's a quake window alive. Once I've gotten more of the minimize to tray/tray icon features down that warrant the "always show" behavior, it should be fairly simple to make that change.


_window->IsQuakeWindow(_logic.IsQuakeWindow());
}

void AppHost::_HandleTrayIconPressed()
{
// Currently scoping "minimize to tray" to only
// the quake window.
if (_logic.IsQuakeWindow())
{
_window->SummonWindow({});
leonMSFT marked this conversation as resolved.
Show resolved Hide resolved
}
}

void AppHost::_HandleWindowHidden()
{
if (_logic.IsQuakeWindow())
{
_UpdateTrayIcon();
}
}

// Method Description:
// - Creates and adds an icon to the notification tray.
// Updates the current tray icon with the current window's
// HWND if an icon already exists.
// Arguments:
// - <unused>
// Return Value:
// - <none>
void AppHost::_UpdateTrayIcon()
{
if (!_trayIconData)
{
NOTIFYICONDATA nid{};

// This HWND will receive the callbacks sent by the tray icon.
nid.hWnd = _window->GetHandle();

// App-defined identifier of the icon. The HWND and ID are used
// to identify which icon to operate on when calling Shell_NotifyIcon.
// Multiple icons can be associated with one HWND, but here we're only
// going to be showing one so the ID doesn't really matter.
nid.uID = 1;

nid.uCallbackMessage = WM_APP + 1;
leonMSFT marked this conversation as resolved.
Show resolved Hide resolved

nid.hIcon = static_cast<HICON>(GetActiveAppIconHandle(ICON_SMALL));
StringCchCopy(nid.szTip, ARRAYSIZE(nid.szTip), L"Windows Terminal");
nid.uFlags = NIF_MESSAGE | NIF_SHOWTIP | NIF_TIP | NIF_ICON;
Shell_NotifyIcon(NIM_ADD, &nid);
leonMSFT marked this conversation as resolved.
Show resolved Hide resolved

// For whatever reason, the NIM_ADD call doesn't seem to set the version
// properly, resulting in us being unable to receive the expected notification
// events. We actually have to make a separate NIM_SETVERSION call for it to
// work properly.
nid.uVersion = NOTIFYICON_VERSION_4;
Shell_NotifyIcon(NIM_SETVERSION, &nid);

_trayIconData = nid;
}
else
{
auto nid = _trayIconData.value();
nid.hWnd = _window->GetHandle();
Shell_NotifyIcon(NIM_MODIFY, &nid);
}
leonMSFT marked this conversation as resolved.
Show resolved Hide resolved

}
6 changes: 6 additions & 0 deletions src/cascadia/WindowsTerminal/AppHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,10 @@ class AppHost

void _IsQuakeWindowChanged(const winrt::Windows::Foundation::IInspectable& sender,
const winrt::Windows::Foundation::IInspectable& args);

void _UpdateTrayIcon();
void _HandleTrayIconPressed();
void _HandleWindowHidden();

std::optional<NOTIFYICONDATA> _trayIconData;
};
14 changes: 14 additions & 0 deletions src/cascadia/WindowsTerminal/IslandWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ long IslandWindow::_calculateTotalSize(const bool isWidth, const long clientSize
{
if (wparam == SIZE_MINIMIZED && _isQuakeWindow)
{
_NotifyWindowHiddenHandlers();
ShowWindow(GetHandle(), SW_HIDE);
return 0;
}
Expand Down Expand Up @@ -506,6 +507,19 @@ long IslandWindow::_calculateTotalSize(const bool isWidth, const long clientSize
case WM_THEMECHANGED:
UpdateWindowIconForActiveMetrics(_window.get());
return 0;
case WM_APP + 1:
{
switch (LOWORD(lparam))
{
case NIN_SELECT:
case NIN_KEYSELECT:
{
_NotifyTrayIconPressedHandlers();
return 0;
}
}
break;
}
}

// TODO: handle messages here...
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/WindowsTerminal/IslandWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class IslandWindow :
WINRT_CALLBACK(MouseScrolled, winrt::delegate<void(til::point, int32_t)>);
WINRT_CALLBACK(WindowActivated, winrt::delegate<void()>);
WINRT_CALLBACK(HotkeyPressed, winrt::delegate<void(long)>);
WINRT_CALLBACK(NotifyTrayIconPressed, winrt::delegate<void()>);
WINRT_CALLBACK(NotifyWindowHidden, winrt::delegate<void()>);

protected:
void ForceResize()
Expand Down
19 changes: 12 additions & 7 deletions src/cascadia/WindowsTerminal/icon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,27 @@ static int _GetActiveAppIconResource()
return iconResource;
}

void UpdateWindowIconForActiveMetrics(HWND window)
HANDLE GetActiveAppIconHandle(int size)
{
auto iconResource{ MAKEINTRESOURCEW(_GetActiveAppIconResource()) };

auto smXIcon = size == ICON_SMALL ? SM_CXSMICON : SM_CXICON;
leonMSFT marked this conversation as resolved.
Show resolved Hide resolved
auto smYIcon = size == ICON_SMALL ? SM_CYSMICON : SM_CYICON;

// These handles are loaded with LR_SHARED, so they are safe to "leak".
HANDLE smallIcon{ LoadImageW(wil::GetModuleInstanceHandle(), iconResource, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED) };
LOG_LAST_ERROR_IF_NULL(smallIcon);
HANDLE hIcon{ LoadImageW(wil::GetModuleInstanceHandle(), iconResource, IMAGE_ICON, GetSystemMetrics(smXIcon), GetSystemMetrics(smYIcon), LR_SHARED) };
LOG_LAST_ERROR_IF_NULL(hIcon);

HANDLE largeIcon{ LoadImageW(wil::GetModuleInstanceHandle(), iconResource, IMAGE_ICON, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), LR_SHARED) };
LOG_LAST_ERROR_IF_NULL(largeIcon);
return hIcon;
}

if (smallIcon)
void UpdateWindowIconForActiveMetrics(HWND window)
{
if (auto smallIcon = GetActiveAppIconHandle(ICON_SMALL))
{
SendMessageW(window, WM_SETICON, ICON_SMALL, reinterpret_cast<LPARAM>(smallIcon));
}
if (largeIcon)
else if (auto largeIcon = GetActiveAppIconHandle(ICON_BIG))
leonMSFT marked this conversation as resolved.
Show resolved Hide resolved
{
SendMessageW(window, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(largeIcon));
}
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/WindowsTerminal/icon.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

#pragma once

HANDLE GetActiveAppIconHandle(const int size);
void UpdateWindowIconForActiveMetrics(HWND window);