2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2017 Konstantinos Sideris <siderisk@auth.gr>
|
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
2022-01-01 06:57:53 +03:00
|
|
|
// SPDX-FileCopyrightText: 2022 Nheko Contributors
|
2021-03-05 02:35:15 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2017-05-21 16:36:06 +03:00
|
|
|
|
2020-01-31 08:12:02 +03:00
|
|
|
#include <QAction>
|
2017-05-21 23:21:02 +03:00
|
|
|
#include <QApplication>
|
2017-10-01 12:13:10 +03:00
|
|
|
#include <QList>
|
2017-10-28 15:46:39 +03:00
|
|
|
#include <QMenu>
|
2020-01-31 08:12:02 +03:00
|
|
|
#include <QPainter>
|
2017-10-01 18:15:23 +03:00
|
|
|
#include <QTimer>
|
2022-01-12 21:09:46 +03:00
|
|
|
#include <QWindow>
|
2017-05-21 16:36:06 +03:00
|
|
|
|
|
|
|
#include "TrayIcon.h"
|
|
|
|
|
2017-07-01 16:34:36 +03:00
|
|
|
#if defined(Q_OS_MAC)
|
|
|
|
#include <QtMacExtras>
|
|
|
|
#endif
|
|
|
|
|
2017-05-21 16:36:06 +03:00
|
|
|
MsgCountComposedIcon::MsgCountComposedIcon(const QString &filename)
|
2017-08-20 13:47:22 +03:00
|
|
|
: QIconEngine()
|
2022-10-04 00:57:30 +03:00
|
|
|
, icon_{QIcon{filename}}
|
2017-05-21 16:36:06 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2017-09-10 12:59:21 +03:00
|
|
|
MsgCountComposedIcon::paint(QPainter *painter,
|
|
|
|
const QRect &rect,
|
|
|
|
QIcon::Mode mode,
|
|
|
|
QIcon::State state)
|
2017-05-21 16:36:06 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
painter->setRenderHint(QPainter::TextAntialiasing);
|
|
|
|
painter->setRenderHint(QPainter::SmoothPixmapTransform);
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing);
|
|
|
|
|
|
|
|
icon_.paint(painter, rect, Qt::AlignCenter, mode, state);
|
|
|
|
|
|
|
|
if (msgCount <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QColor backgroundColor("red");
|
|
|
|
QColor textColor("white");
|
|
|
|
|
|
|
|
QBrush brush;
|
|
|
|
brush.setStyle(Qt::SolidPattern);
|
|
|
|
brush.setColor(backgroundColor);
|
|
|
|
|
|
|
|
QFont f;
|
|
|
|
f.setPointSizeF(8);
|
|
|
|
f.setWeight(QFont::Thin);
|
|
|
|
|
|
|
|
painter->setBrush(brush);
|
|
|
|
painter->setPen(Qt::NoPen);
|
|
|
|
painter->setFont(f);
|
|
|
|
|
|
|
|
QRectF bubble(rect.width() - BubbleDiameter,
|
|
|
|
rect.height() - BubbleDiameter,
|
|
|
|
BubbleDiameter,
|
|
|
|
BubbleDiameter);
|
|
|
|
painter->drawEllipse(bubble);
|
|
|
|
painter->setPen(QPen(textColor));
|
|
|
|
painter->setBrush(Qt::NoBrush);
|
|
|
|
painter->drawText(bubble, Qt::AlignCenter, QString::number(msgCount));
|
2017-05-21 16:36:06 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
QIconEngine *
|
|
|
|
MsgCountComposedIcon::clone() const
|
2017-05-21 16:36:06 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return new MsgCountComposedIcon(*this);
|
2017-05-21 16:36:06 +03:00
|
|
|
}
|
|
|
|
|
2017-10-01 12:13:10 +03:00
|
|
|
QList<QSize>
|
2021-12-28 22:09:08 +03:00
|
|
|
MsgCountComposedIcon::availableSizes(QIcon::Mode mode, QIcon::State state)
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
const
|
|
|
|
#endif
|
2017-10-01 12:13:10 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
Q_UNUSED(mode);
|
|
|
|
Q_UNUSED(state);
|
|
|
|
QList<QSize> sizes;
|
|
|
|
sizes.append(QSize(24, 24));
|
|
|
|
sizes.append(QSize(32, 32));
|
|
|
|
sizes.append(QSize(48, 48));
|
|
|
|
sizes.append(QSize(64, 64));
|
|
|
|
sizes.append(QSize(128, 128));
|
|
|
|
sizes.append(QSize(256, 256));
|
|
|
|
return sizes;
|
2017-10-01 12:13:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap
|
2017-10-01 18:15:23 +03:00
|
|
|
MsgCountComposedIcon::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
|
2017-10-01 12:13:10 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
QImage img(size, QImage::Format_ARGB32);
|
|
|
|
img.fill(qRgba(0, 0, 0, 0));
|
|
|
|
QPixmap result = QPixmap::fromImage(img, Qt::NoFormatConversion);
|
|
|
|
{
|
|
|
|
QPainter painter(&result);
|
|
|
|
paint(&painter, QRect(QPoint(0, 0), size), mode, state);
|
|
|
|
}
|
|
|
|
return result;
|
2017-10-01 12:13:10 +03:00
|
|
|
}
|
|
|
|
|
2022-01-12 21:09:46 +03:00
|
|
|
TrayIcon::TrayIcon(const QString &filename, QWindow *parent)
|
2017-08-20 13:47:22 +03:00
|
|
|
: QSystemTrayIcon(parent)
|
2017-05-21 16:36:06 +03:00
|
|
|
{
|
2017-07-09 12:49:17 +03:00
|
|
|
#if defined(Q_OS_MAC) || defined(Q_OS_WIN)
|
2021-09-18 01:22:33 +03:00
|
|
|
setIcon(QIcon(filename));
|
2017-07-09 12:49:17 +03:00
|
|
|
#else
|
2021-09-18 01:22:33 +03:00
|
|
|
icon_ = new MsgCountComposedIcon(filename);
|
|
|
|
setIcon(QIcon(icon_));
|
2017-07-09 12:49:17 +03:00
|
|
|
#endif
|
2017-05-21 16:36:06 +03:00
|
|
|
|
2022-01-12 21:09:46 +03:00
|
|
|
QMenu *menu = new QMenu();
|
2021-09-18 01:22:33 +03:00
|
|
|
setContextMenu(menu);
|
2021-04-08 13:26:15 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
viewAction_ = new QAction(tr("Show"), this);
|
|
|
|
quitAction_ = new QAction(tr("Quit"), this);
|
2017-05-21 16:36:06 +03:00
|
|
|
|
2022-01-12 21:09:46 +03:00
|
|
|
connect(viewAction_, &QAction::triggered, parent, &QWindow::show);
|
2021-09-18 01:22:33 +03:00
|
|
|
connect(quitAction_, &QAction::triggered, this, QApplication::quit);
|
2017-05-21 16:36:06 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
menu->addAction(viewAction_);
|
|
|
|
menu->addAction(quitAction_);
|
2017-05-21 16:36:06 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
TrayIcon::setUnreadCount(int count)
|
2017-05-21 16:36:06 +03:00
|
|
|
{
|
2017-07-09 12:49:17 +03:00
|
|
|
// Use the native badge counter in MacOS.
|
2017-07-01 16:34:36 +03:00
|
|
|
#if defined(Q_OS_MAC)
|
2019-07-27 00:47:52 +03:00
|
|
|
// currently, to avoid writing obj-c code, ignore deprecated warnings on the badge functions
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
2021-09-18 01:22:33 +03:00
|
|
|
auto labelText = count == 0 ? "" : QString::number(count);
|
2017-10-01 18:15:23 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (labelText == QtMac::badgeLabelText())
|
|
|
|
return;
|
2017-10-01 18:15:23 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
QtMac::setBadgeLabelText(labelText);
|
2019-07-27 00:47:52 +03:00
|
|
|
#pragma clang diagnostic pop
|
2017-07-09 12:49:17 +03:00
|
|
|
#elif defined(Q_OS_WIN)
|
|
|
|
// FIXME: Find a way to use Windows apis for the badge counter (if any).
|
2017-07-01 16:34:36 +03:00
|
|
|
#else
|
2021-09-18 01:22:33 +03:00
|
|
|
if (count == icon_->msgCount)
|
|
|
|
return;
|
2017-10-01 18:15:23 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Custom drawing on Linux.
|
|
|
|
MsgCountComposedIcon *tmp = static_cast<MsgCountComposedIcon *>(icon_->clone());
|
|
|
|
tmp->msgCount = count;
|
2017-05-21 16:36:06 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
setIcon(QIcon(tmp));
|
2017-05-21 16:36:06 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
icon_ = tmp;
|
2017-07-01 16:34:36 +03:00
|
|
|
#endif
|
2017-05-21 16:36:06 +03:00
|
|
|
}
|