Skip to content

Commit

Permalink
improved notification info (Tray)
Browse files Browse the repository at this point in the history
  • Loading branch information
frank.friemel committed Dec 20, 2022
1 parent e51e27e commit 6a55045
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 8 deletions.
38 changes: 34 additions & 4 deletions src/MainDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ void CMainDlg::OnSysCommand(UINT wParam, CPoint pt)

ATLVERIFY(strAck.LoadString(IDS_TRAY_ACK));

SetInfoText(A2CT(strConfigName.c_str()), strAck);
SetInfoText(A2CT(strConfigName.c_str()), strAck, false);
PutValueToRegistry(HKEY_CURRENT_USER, "TrayAck", "ok");
}
}
Expand Down Expand Up @@ -1265,10 +1265,40 @@ LRESULT CMainDlg::OnSongInfo(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BO
}
}
if (!g_bMute || lParam)
SetInfoText(strNowPlayingLabel, strFmtNowPlaying, hIcon);
{
CMyMutex::AutoSync sync(m_mtxNotification);

if (!m_notificationInfo)
{
m_notificationInfo = std::make_unique<NotificationInfo>(std::wstring(strNowPlayingLabel), std::wstring(strFmtNowPlaying), hIcon);

// schedule new notification to be displayed after 1 second
auto asyncNotification = std::async(std::launch::async, [this]() -> void
{
::Sleep(1000);

CMyMutex::AutoSync sync(m_mtxNotification);

if (m_notificationInfo)
{
const auto notificationInfo = std::move(m_notificationInfo);
sync.Unlock();

if (hIcon)
::DestroyIcon(hIcon);
// now, commit info to Tray
SetInfoText(notificationInfo->GetNowPlayingLabel(),
notificationInfo->GetFmtNowPlaying(), notificationInfo->GetIcon());
}
});

std::swap(m_asyncNotification, asyncNotification);
sync.Unlock();
}
else
{
// just update the notification info
m_notificationInfo->UpdateInfo(std::wstring(strNowPlayingLabel), std::wstring(strFmtNowPlaying), hIcon);
}
}
}
}
return 0l;
Expand Down
58 changes: 58 additions & 0 deletions src/MainDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "DacpService.h"

#include "MyAppMessages.h"
#include <future>

class CMainDlg : public CDialogImpl<CMainDlg>, public CUpdateUI<CMainDlg>,
public CMessageFilter, public CIdleHandler, public CWinDataExchange<CMainDlg>
Expand Down Expand Up @@ -217,6 +218,63 @@ class CMainDlg : public CDialogImpl<CMainDlg>, public CUpdateUI<CMainDlg>,
CComVariant m_strSetupVersion;
ATL::CString m_strReady;

class NotificationInfo final
{
public:
NotificationInfo(std::wstring&& strNowPlayingLabel, std::wstring&& strFmtNowPlaying, HICON hIcon)
: m_strNowPlayingLabel(std::move(strNowPlayingLabel))
, m_strFmtNowPlaying(std::move(strFmtNowPlaying))
, m_hIcon(hIcon)
{
}

~NotificationInfo()
{
if (m_hIcon)
{
::DestroyIcon(m_hIcon);
}
}

NotificationInfo(const NotificationInfo&) = delete;
NotificationInfo& operator=(const NotificationInfo&) = delete;

void UpdateInfo(std::wstring&& strNowPlayingLabel, std::wstring&& strFmtNowPlaying, HICON hIcon)
{
if (m_hIcon)
{
::DestroyIcon(m_hIcon);
}
m_strNowPlayingLabel = std::move(strNowPlayingLabel);
m_strFmtNowPlaying = std::move(strFmtNowPlaying);
m_hIcon = hIcon;
}

PCWSTR GetNowPlayingLabel() const
{
return m_strNowPlayingLabel.c_str();
}

PCWSTR GetFmtNowPlaying() const
{
return m_strFmtNowPlaying.c_str();
}

HICON GetIcon() const
{
return m_hIcon;
}

protected:
std::wstring m_strNowPlayingLabel;
std::wstring m_strFmtNowPlaying;
HICON m_hIcon;
};

std::future<void> m_asyncNotification;
std::unique_ptr<NotificationInfo> m_notificationInfo;
CMyMutex m_mtxNotification;

void PutMMState(typeMMState nMmState);

public:
Expand Down
27 changes: 23 additions & 4 deletions src/TrayIcon.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ class CTrayIconImpl
{
RemoveTray();
}

bool InitTray(LPCTSTR lpszToolTip, HICON hIcon, UINT nID)
{
const CMyMutex::AutoSync sync(m_mtx);

if (m_bInit)
return true;

Expand All @@ -70,6 +73,8 @@ class CTrayIconImpl
}
void UpdateTrayIcon(HICON hIcon)
{
const CMyMutex::AutoSync sync(m_mtx);

if (m_bInit)
{
T* pT = static_cast<T*>(this);
Expand All @@ -88,6 +93,8 @@ class CTrayIconImpl
}
void SetTooltipText(LPCTSTR pszTooltipText)
{
const CMyMutex::AutoSync sync(m_mtx);

if (m_bInit && pszTooltipText)
{
m_nid.uFlags = NIF_TIP;
Expand All @@ -96,12 +103,14 @@ class CTrayIconImpl
Shell_NotifyIcon(NIM_MODIFY, (PNOTIFYICONDATA)&m_nid);
}
}
void SetInfoText(LPCTSTR pszInfoTitle, LPCTSTR pszInfoText, HICON hIcon = NULL)
void SetInfoText(LPCTSTR pszInfoTitle, LPCTSTR pszInfoText, HICON hIcon = NULL, bool avoidDuplicates = true)
{
if (m_bInit && pszInfoText)
const CMyMutex::AutoSync sync(m_mtx);

if (m_bInit && pszInfoText && *pszInfoText && (!avoidDuplicates || m_strPreviousInfoText != std::wstring(pszInfoText)))
{
m_nid.uFlags = NIF_INFO;
m_nid.dwInfoFlags = NIIF_USER | (hIcon ? NIIF_LARGE_ICON : 0);
m_nid.dwInfoFlags = NIIF_USER | NIIF_NOSOUND | (hIcon ? NIIF_LARGE_ICON : 0);
m_nid.uTimeout = 15000;

if (pszInfoTitle)
Expand All @@ -111,10 +120,17 @@ class CTrayIconImpl
m_nid.hBalloonIcon = hIcon;

Shell_NotifyIcon(NIM_MODIFY, (PNOTIFYICONDATA)&m_nid);

if (avoidDuplicates)
{
m_strPreviousInfoText = pszInfoText;
}
}
}
void RemoveTray()
{
const CMyMutex::AutoSync sync(m_mtx);

if (m_bInit)
{
m_nid.uFlags = 0;
Expand Down Expand Up @@ -161,7 +177,10 @@ class CTrayIconImpl
}
return 0;
}
protected:

private:
bool m_bInit;
NOTIFYICONDATA_4 m_nid;
std::wstring m_strPreviousInfoText;
CMyMutex m_mtx;
};

0 comments on commit 6a55045

Please sign in to comment.