From a486d8d7fc567c191e7259b478b6f03b3800ac4a Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Thu, 20 Jun 2024 21:35:55 +0200 Subject: [PATCH] Fix tray icon badge count not getting updated since 838b091acd07b681fc52f6f705b2bee8ce8519d5 --- src/MainWindow.cpp | 2 +- src/TrayIcon.cpp | 25 ++++++++++++++++--------- src/TrayIcon.h | 13 ++++--------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index ea9f560d..42a1521d 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -60,7 +60,7 @@ MainWindow::MainWindow(QWindow *parent) connect(chat_page_, &ChatPage::closing, this, [this] { switchToLoginPage(""); }); connect(chat_page_, &ChatPage::unreadMessages, this, &MainWindow::setWindowTitle); - connect(chat_page_, SIGNAL(unreadMessages(int)), trayIcon_, SLOT(setUnreadCount(int))); + connect(chat_page_, &ChatPage::unreadMessages, trayIcon_, &TrayIcon::setUnreadCount); connect(chat_page_, &ChatPage::showLoginPage, this, &MainWindow::switchToLoginPage); connect(chat_page_, &ChatPage::showNotification, this, &MainWindow::showNotification); diff --git a/src/TrayIcon.cpp b/src/TrayIcon.cpp index c89e69cc..c62fecea 100644 --- a/src/TrayIcon.cpp +++ b/src/TrayIcon.cpp @@ -12,9 +12,9 @@ #include "TrayIcon.h" -MsgCountComposedIcon::MsgCountComposedIcon(const QString &filename) +MsgCountComposedIcon::MsgCountComposedIcon(const QIcon &icon) : QIconEngine() - , icon_{QIcon{filename}} + , icon_{icon} { } @@ -41,8 +41,8 @@ MsgCountComposedIcon::paint(QPainter *painter, brush.setColor(backgroundColor); QFont f; - f.setPointSizeF(8); - f.setWeight(QFont::Thin); + f.setPixelSize(int(BubbleDiameter * 0.6)); + f.setWeight(QFont::Bold); painter->setBrush(brush); painter->setPen(Qt::NoPen); @@ -66,9 +66,6 @@ MsgCountComposedIcon::clone() const QList MsgCountComposedIcon::availableSizes(QIcon::Mode mode, QIcon::State state) -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - const -#endif { Q_UNUSED(mode); Q_UNUSED(state); @@ -97,11 +94,12 @@ MsgCountComposedIcon::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State s TrayIcon::TrayIcon(const QString &filename, QWindow *parent) : QSystemTrayIcon(parent) + , icon(filename) { #if defined(Q_OS_MACOS) || defined(Q_OS_WIN) - setIcon(QIcon(filename)); + setIcon(icon); #else - icon_ = new MsgCountComposedIcon(filename); + auto icon_ = new MsgCountComposedIcon(icon); setIcon(QIcon(icon_)); #endif @@ -122,6 +120,15 @@ void TrayIcon::setUnreadCount(int count) { qGuiApp->setBadgeNumber(count); + +#if !defined(Q_OS_MACOS) && !defined(Q_OS_WIN) + if (count != previousCount) { + auto i = new MsgCountComposedIcon(icon); + i->msgCount = count; + setIcon(QIcon(i)); + previousCount = count; + } +#endif } #include "moc_TrayIcon.cpp" diff --git a/src/TrayIcon.h b/src/TrayIcon.h index f2009612..7c0bc7b2 100644 --- a/src/TrayIcon.h +++ b/src/TrayIcon.h @@ -15,15 +15,11 @@ class QPainter; class MsgCountComposedIcon final : public QIconEngine { public: - MsgCountComposedIcon(const QString &filename); + MsgCountComposedIcon(const QIcon &icon); void paint(QPainter *p, const QRect &rect, QIcon::Mode mode, QIcon::State state) override; QIconEngine *clone() const override; - QList availableSizes(QIcon::Mode mode, QIcon::State state) -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - const -#endif - override; + QList availableSizes(QIcon::Mode mode, QIcon::State state) override; QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) override; int msgCount = 0; @@ -47,7 +43,6 @@ private: QAction *viewAction_; QAction *quitAction_; -#if !defined(Q_OS_MACOS) && !defined(Q_OS_WIN) - MsgCountComposedIcon *icon_; -#endif + int previousCount = 0; + QIcon icon; };