Skip to content

Commit

Permalink
Allow icons in notification area to be recreated whenever the taskbar…
Browse files Browse the repository at this point in the history
… is (re-)created. This resolves issue #16.
  • Loading branch information
namreeb committed May 10, 2019
1 parent 5d458fc commit 1fd85b5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
29 changes: 16 additions & 13 deletions wowreeb/NotifyIcon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,20 @@ NotifyIcon::NotifyIcon(HWND window, unsigned int id, HICON icon, const TCHAR *ti
if (id > maxId)
throw std::runtime_error("Too many icons");

NOTIFYICONDATA add;
ZeroMemory(&_addMessage, sizeof(_addMessage));

ZeroMemory(&add, sizeof(add));
_addMessage.cbSize = sizeof(_addMessage);
_addMessage.hWnd = window;
_addMessage.uID = id;
_addMessage.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
_addMessage.uCallbackMessage = WM_APP | (id << MenuBits);
_addMessage.hIcon = icon;

add.cbSize = sizeof(add);
add.hWnd = window;
add.uID = id;
add.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
add.uCallbackMessage = WM_APP | (id << MenuBits);
add.hIcon = icon;
StringCchCopy(_addMessage.szTip, ARRAYSIZE(_addMessage.szTip), tip);

StringCchCopy(add.szTip, ARRAYSIZE(add.szTip), tip);
_addMessage.uVersion = NOTIFYICON_VERSION_4;

add.uVersion = NOTIFYICON_VERSION_4;

if (!Shell_NotifyIcon(NIM_ADD, &add))
throw std::runtime_error("Shell_NotifyIcon failed");
CreateIcon();
}

NotifyIcon::~NotifyIcon()
Expand All @@ -75,6 +72,12 @@ NotifyIcon::~NotifyIcon()
Shell_NotifyIcon(NIM_DELETE, &del);
}

void NotifyIcon::CreateIcon()
{
if (!Shell_NotifyIcon(NIM_ADD, &_addMessage))
throw std::runtime_error("Shell_NotifyIcon failed");
}

void NotifyIcon::ToggleMenu()
{
auto menu = CreatePopupMenu();
Expand Down
4 changes: 4 additions & 0 deletions wowreeb/NotifyIcon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class NotifyIcon
private:
static_assert(IconBits > 0 && MenuBits > 0 && IconBits + MenuBits == 14, "IconBits + MenuBits must be 14");

NOTIFYICONDATA _addMessage;

HWND _window;
unsigned int _id;

Expand All @@ -62,6 +64,8 @@ class NotifyIcon
NotifyIcon(HWND window, unsigned int id, HICON icon, const TCHAR *tip);
~NotifyIcon();

void CreateIcon();

void ToggleMenu();

void ClearMenu();
Expand Down
23 changes: 21 additions & 2 deletions wowreeb/NotifyIconMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,23 @@ LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
if (singleton->Command(hWnd, iconId, menuId))
return TRUE;
}
else if (msg >= WM_APP)
else if (msg >= WM_APP && msg <= 0xBFFF)
{
if (singleton->WindowProc(hWnd, msg - WM_APP, wParam, lParam))
return TRUE;
}
else if (msg == WM_CREATE)
singleton->Create();
else if (singleton->IsTaskbarCreated(msg))
singleton->TaskbarCreated();
}

return DefWindowProc(hWnd, msg, wParam, lParam);
}
}

NotifyIconMgr::NotifyIconMgr(HINSTANCE hInstance) : _shutdown(false), _messageThread(&NotifyIconMgr::MessageLoop, this, hInstance)
NotifyIconMgr::NotifyIconMgr(HINSTANCE hInstance)
: _taskbarCreated(0u), _shutdown(false), _messageThread(&NotifyIconMgr::MessageLoop, this, hInstance)
{
if (!!singleton)
throw std::runtime_error("Cannot instantiate more than one NotifyIconMgr");
Expand Down Expand Up @@ -124,6 +129,20 @@ void NotifyIconMgr::MessageLoop(HINSTANCE hInstance)
}
}

void NotifyIconMgr::Create()
{
_taskbarCreated = RegisterWindowMessage(_T("TaskbarCreated"));
}

// tell all notification area icons to recreate themselves
void NotifyIconMgr::TaskbarCreated()
{
std::lock_guard<std::mutex> guard(_mutex);

for (auto &icon : _icons)
icon->CreateIcon();
}

bool NotifyIconMgr::Command(HWND hWnd, unsigned int iconId, unsigned int menuId)
{
std::shared_ptr<NotifyIcon> icon;
Expand Down
6 changes: 6 additions & 0 deletions wowreeb/NotifyIconMgr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class NotifyIcon;
class NotifyIconMgr
{
private:
UINT _taskbarCreated;

bool _shutdown;

std::thread _messageThread;
Expand All @@ -54,6 +56,10 @@ class NotifyIconMgr
_messageThread.join();
}

void Create();
bool IsTaskbarCreated(UINT msg) const { return _taskbarCreated > 0 && _taskbarCreated == msg; }
void TaskbarCreated();

bool Command(HWND hWnd, unsigned int iconId, unsigned int menuId);
bool WindowProc(HWND hWnd, unsigned int iconId, WPARAM wParam, LPARAM lParam);

Expand Down

0 comments on commit 1fd85b5

Please sign in to comment.