You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nheko/src/TrayIcon.cpp

152 lines
4.2 KiB

// SPDX-FileCopyrightText: 2017 Konstantinos Sideris <siderisk@auth.gr>
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
8 years ago
#include <QAction>
#include <QApplication>
#include <QList>
7 years ago
#include <QMenu>
#include <QPainter>
#include <QTimer>
8 years ago
#include "TrayIcon.h"
#if defined(Q_OS_MAC)
#include <QtMacExtras>
#endif
8 years ago
MsgCountComposedIcon::MsgCountComposedIcon(const QString &filename)
: QIconEngine()
8 years ago
{
icon_ = QIcon(filename);
8 years ago
}
void
MsgCountComposedIcon::paint(QPainter *painter,
const QRect &rect,
QIcon::Mode mode,
QIcon::State state)
8 years ago
{
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));
8 years ago
}
QIconEngine *
MsgCountComposedIcon::clone() const
8 years ago
{
return new MsgCountComposedIcon(*this);
8 years ago
}
QList<QSize>
MsgCountComposedIcon::availableSizes(QIcon::Mode mode, QIcon::State state) const
{
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
MsgCountComposedIcon::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
{
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;
}
8 years ago
TrayIcon::TrayIcon(const QString &filename, QWidget *parent)
: QSystemTrayIcon(parent)
8 years ago
{
#if defined(Q_OS_MAC) || defined(Q_OS_WIN)
setIcon(QIcon(filename));
#else
icon_ = new MsgCountComposedIcon(filename);
setIcon(QIcon(icon_));
#endif
8 years ago
QMenu *menu = new QMenu(parent);
viewAction_ = new QAction(tr("Show"), parent);
quitAction_ = new QAction(tr("Quit"), parent);
8 years ago
connect(viewAction_, SIGNAL(triggered()), parent, SLOT(show()));
connect(quitAction_, &QAction::triggered, this, QApplication::quit);
8 years ago
menu->addAction(viewAction_);
menu->addAction(quitAction_);
8 years ago
setContextMenu(menu);
8 years ago
}
void
TrayIcon::setUnreadCount(int count)
8 years ago
{
// Use the native badge counter in MacOS.
#if defined(Q_OS_MAC)
// currently, to avoid writing obj-c code, ignore deprecated warnings on the badge functions
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
auto labelText = count == 0 ? "" : QString::number(count);
if (labelText == QtMac::badgeLabelText())
return;
QtMac::setBadgeLabelText(labelText);
#pragma clang diagnostic pop
#elif defined(Q_OS_WIN)
// FIXME: Find a way to use Windows apis for the badge counter (if any).
#else
if (count == icon_->msgCount)
return;
// Custom drawing on Linux.
MsgCountComposedIcon *tmp = static_cast<MsgCountComposedIcon *>(icon_->clone());
tmp->msgCount = count;
8 years ago
setIcon(QIcon(tmp));
8 years ago
icon_ = tmp;
#endif
8 years ago
}