matrixion/src/TrayIcon.cpp

156 lines
3.9 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: 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>
#include <QApplication>
#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"
#include "UserSettingsPage.h"
2017-05-21 16:36:06 +03:00
MsgCountComposedIcon::MsgCountComposedIcon(const QIcon &icon)
2017-08-20 13:47:22 +03:00
: QIconEngine()
, icon_{icon}
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.setPixelSize(int(BubbleDiameter * 0.6));
f.setWeight(QFont::Bold);
2021-09-18 01:22:33 +03:00
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
}
QList<QSize>
2021-12-28 22:09:08 +03:00
MsgCountComposedIcon::availableSizes(QIcon::Mode mode, QIcon::State state)
{
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;
}
QPixmap
2017-10-01 18:15:23 +03:00
MsgCountComposedIcon::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
{
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;
}
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)
, icon(filename)
2017-05-21 16:36:06 +03:00
{
2024-01-09 05:17:28 +03:00
#if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
setIcon(icon);
#else
auto icon_ = new MsgCountComposedIcon(icon);
2021-09-18 01:22:33 +03:00
setIcon(QIcon(icon_));
#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_);
QString toolTip = QLatin1String("nheko");
QString profile = UserSettings::instance()->profile();
if (!profile.isEmpty())
toolTip.append(QStringLiteral(" | %1").arg(profile));
setToolTip(toolTip);
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
{
2023-06-04 04:51:58 +03:00
qGuiApp->setBadgeNumber(count);
if (count != previousCount) {
QString toolTip = QLatin1String("nheko");
QString profile = UserSettings::instance()->profile();
if (!profile.isEmpty())
toolTip.append(QStringLiteral(" | %1").arg(profile));
if (count != 0)
toolTip.append(tr("\n%n unread message(s)", "", count));
setToolTip(toolTip);
}
#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;
}
#else
(void)previousCount;
#endif
2017-05-21 16:36:06 +03:00
}
#include "moc_TrayIcon.cpp"