Skip to content

Commit

Permalink
Handle TaskbarCreated message.
Browse files Browse the repository at this point in the history
Fixes icon disappearing upon Explorer restart (#9) and
more gracefully handles "early" starts without an available
task bar.
  • Loading branch information
res2k committed May 12, 2024
1 parent a5a0c8e commit f8e53eb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
20 changes: 19 additions & 1 deletion HDRTray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
}

static std::unique_ptr<NotifyIcon> notify_icon;
static UINT msg_TaskbarCreated;

//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
Expand All @@ -177,9 +178,13 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
switch (message)
{
case WM_CREATE:
msg_TaskbarCreated = RegisterWindowMessage(L"TaskbarCreated");
notify_icon.reset(new NotifyIcon(hWnd));
if (!notify_icon->Add())
return -1;
{
// Set up a timer, this is the amount of time we wait for TaskbarCreated
SetTimer(hWnd, 1, 30000, nullptr);
}
break;
case WM_COMMAND:
{
Expand Down Expand Up @@ -216,7 +221,20 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
break;
case NotifyIcon::MESSAGE:
return notify_icon->HandleMessage(hWnd, wParam, lParam);
case WM_TIMER:
KillTimer(hWnd, 1);
// No TaskbarCreated was received, exit
if (!notify_icon->WasAdded())
DestroyWindow(hWnd);
break;
default:
if (message == msg_TaskbarCreated)
{
// Taskbar was created (Explorer restart, DPI change), so re-create notify icon
notify_icon->Remove();
if (!notify_icon->Add())
DestroyWindow(hWnd);
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
Expand Down
7 changes: 7 additions & 0 deletions NotifyIcon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ NotifyIcon::~NotifyIcon()
DestroyMenu(popup_menu);
}

bool NotifyIcon::WasAdded() const
{
return added;
}

bool NotifyIcon::Add()
{
FetchHDRStatus();
Expand All @@ -106,13 +111,15 @@ bool NotifyIcon::Add()
wrap_Shell_NotifyIconW(NIM_SETVERSION, &notify_setversion);

UpdateIcon();
added = true;
return true;
}

void NotifyIcon::Remove()
{
auto notify_delete = notify_template;
wrap_Shell_NotifyIconW(NIM_DELETE, &notify_delete);
added = false;
}

void NotifyIcon::UpdateHDRStatus()
Expand Down
2 changes: 2 additions & 0 deletions NotifyIcon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

class NotifyIcon
{
bool added = false;
NOTIFYICONDATAW notify_template;

enum { iconsetDarkMode = 0, iconsetLightMode, numIconsets };
Expand All @@ -44,6 +45,7 @@ class NotifyIcon
NotifyIcon(HWND hwnd);
~NotifyIcon();

bool WasAdded() const;
bool Add();
void Remove();

Expand Down

0 comments on commit f8e53eb

Please sign in to comment.