Skip to content

Commit

Permalink
systray: Notify user once between two activations of the main window
Browse files Browse the repository at this point in the history
This cannot be done without leaving some indication to the user that a
notification has been issued in the past, which may anyway appear as a
lack even when notifications are issued with every new message.

This is achieved by changing the icon semi-permanently (until the next
activation of the main window), and by silently updating the tooltip
when new notifications are issued with the total number of unread
messages.

This icon change is included in the minimal notification mode, as it is
very non-intrusive.
  • Loading branch information
Tamaranch committed Dec 21, 2022
1 parent c611a01 commit 4eddfc6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
33 changes: 30 additions & 3 deletions client/systemtrayicon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "linuxutils.h"
#include <settings.h>
#include <qt_connection_util.h>
#include <accountregistry.h>

SystemTrayIcon::SystemTrayIcon(MainWindow* parent)
: QSystemTrayIcon(parent)
Expand All @@ -40,10 +41,14 @@ SystemTrayIcon::SystemTrayIcon(MainWindow* parent)
showHideAction->setText(visible ? tr("Hide") : tr("Show"));
});

setIcon(QIcon::fromTheme(appIconName(), QIcon(":/icon.png")));
m_appIcon = QIcon::fromTheme(appIconName(), QIcon(":/icon.png"));
m_unreadIcon = QIcon::fromTheme("mail-unread", m_appIcon);
m_notified = false;
setIcon(m_appIcon);
setToolTip("Quaternion");
setContextMenu(contextMenu);
connect( this, &SystemTrayIcon::activated, this, &SystemTrayIcon::systemTrayIconAction);
connect(qApp, &QApplication::focusChanged, this, &SystemTrayIcon::focusChanged);
}

void SystemTrayIcon::newRoom(Quotient::Room* room)
Expand All @@ -57,9 +62,22 @@ void SystemTrayIcon::unreadStatsChanged(Quotient::Room* room)
{
using namespace Quotient;
const auto mode = Settings().get<QString>("UI/notifications", "intrusive");
if (mode == "none")
int nNotifs = 0;

if (qApp->activeWindow() != nullptr || room->notificationCount() == 0)
return;
if (qApp->activeWindow() != nullptr && room->notificationCount() > 0) {

for (auto* c: Accounts.accounts())
for (auto* r: c->allRooms())
nNotifs += r->notificationCount();
setToolTip(tr("%Ln notification(s)", "", nNotifs));

if (!m_notified) {
setIcon(m_unreadIcon);
m_notified = true;
if (mode == "none")
return;

showMessage(
//: %1 is the room display name
tr("Notification in %1").arg(room->displayName()),
Expand Down Expand Up @@ -90,3 +108,12 @@ void SystemTrayIcon::showHide()
m_parent->setFocus();
}
}

void SystemTrayIcon::focusChanged(QWidget* old)
{
if (m_notified && old == nullptr && qApp->activeWindow() != nullptr) {
setIcon(m_appIcon);
setToolTip("Quaternion");
m_notified = false;
}
}
4 changes: 4 additions & 0 deletions client/systemtrayicon.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ class SystemTrayIcon: public QSystemTrayIcon
private slots:
void unreadStatsChanged(Quotient::Room* room);
void systemTrayIconAction(QSystemTrayIcon::ActivationReason reason);
void focusChanged(QWidget* old);

private:
MainWindow* m_parent;
QIcon m_appIcon;
QIcon m_unreadIcon;
bool m_notified;
void showHide();
};

0 comments on commit 4eddfc6

Please sign in to comment.