| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include <QAction> |
| 6 | #include <QApplication> |
| 7 | #include <QList> |
| 8 | #include <QMenu> |
| 9 | #include <QPainter> |
| 10 | #include <QTimer> |
| 11 | #include <QWindow> |
| 12 | |
| 13 | #include "TrayIcon.h" |
| 14 | #include "UserSettingsPage.h" |
| 15 | |
| 16 | MsgCountComposedIcon::MsgCountComposedIcon(const QIcon &icon) |
| 17 | : QIconEngine() |
| 18 | , icon_{icon} |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | void |
| 23 | MsgCountComposedIcon::paint(QPainter *painter, |
| 24 | const QRect &rect, |
| 25 | QIcon::Mode mode, |
| 26 | QIcon::State state) |
| 27 | { |
| 28 | painter->setRenderHint(QPainter::TextAntialiasing); |
| 29 | painter->setRenderHint(QPainter::SmoothPixmapTransform); |
| 30 | painter->setRenderHint(QPainter::Antialiasing); |
| 31 | |
| 32 | icon_.paint(painter, rect, Qt::AlignCenter, mode, state); |
| 33 | |
| 34 | if (msgCount <= 0) |
| 35 | return; |
| 36 | |
| 37 | QColor backgroundColor("red"); |
| 38 | QColor textColor("white"); |
| 39 | |
| 40 | QBrush brush; |
| 41 | brush.setStyle(Qt::SolidPattern); |
| 42 | brush.setColor(backgroundColor); |
| 43 | |
| 44 | QFont f; |
| 45 | f.setPixelSize(int(BubbleDiameter * 0.6)); |
| 46 | f.setWeight(QFont::Bold); |
| 47 | |
| 48 | painter->setBrush(brush); |
| 49 | painter->setPen(Qt::NoPen); |
| 50 | painter->setFont(f); |
| 51 | |
| 52 | QRectF bubble(rect.width() - BubbleDiameter, |
| 53 | rect.height() - BubbleDiameter, |
| 54 | BubbleDiameter, |
| 55 | BubbleDiameter); |
| 56 | painter->drawEllipse(bubble); |
| 57 | painter->setPen(QPen(textColor)); |
| 58 | painter->setBrush(Qt::NoBrush); |
| 59 | painter->drawText(bubble, Qt::AlignCenter, QString::number(msgCount)); |
| 60 | } |
| 61 | |
| 62 | QIconEngine * |
| 63 | MsgCountComposedIcon::clone() const |
| 64 | { |
| 65 | return new MsgCountComposedIcon(*this); |
| 66 | } |
| 67 | |
| 68 | QList<QSize> |
| 69 | MsgCountComposedIcon::availableSizes(QIcon::Mode mode, QIcon::State state) |
| 70 | { |
| 71 | Q_UNUSED(mode); |
| 72 | Q_UNUSED(state); |
| 73 | QList<QSize> sizes; |
| 74 | sizes.append(QSize(24, 24)); |
| 75 | sizes.append(QSize(32, 32)); |
| 76 | sizes.append(QSize(48, 48)); |
| 77 | sizes.append(QSize(64, 64)); |
| 78 | sizes.append(QSize(128, 128)); |
| 79 | sizes.append(QSize(256, 256)); |
| 80 | return sizes; |
| 81 | } |
| 82 | |
| 83 | QPixmap |
| 84 | MsgCountComposedIcon::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) |
| 85 | { |
| 86 | QImage img(size, QImage::Format_ARGB32); |
| 87 | img.fill(qRgba(0, 0, 0, 0)); |
| 88 | QPixmap result = QPixmap::fromImage(img, Qt::NoFormatConversion); |
| 89 | { |
| 90 | QPainter painter(&result); |
| 91 | paint(&painter, QRect(QPoint(0, 0), size), mode, state); |
| 92 | } |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | TrayIcon::TrayIcon(const QString &filename, QWindow *parent) |
| 97 | : QSystemTrayIcon(parent) |
| 98 | , icon(filename) |
| 99 | { |
| 100 | #if defined(Q_OS_MACOS) || defined(Q_OS_WIN) |
| 101 | setIcon(icon); |
| 102 | #else |
| 103 | auto icon_ = new MsgCountComposedIcon(icon); |
| 104 | setIcon(QIcon(icon_)); |
| 105 | #endif |
| 106 | |
| 107 | QMenu *menu = new QMenu(); |
| 108 | setContextMenu(menu); |
| 109 | |
| 110 | viewAction_ = new QAction(tr("Show"), this); |
| 111 | quitAction_ = new QAction(tr("Quit"), this); |
| 112 | |
| 113 | connect(viewAction_, &QAction::triggered, parent, &QWindow::show); |
| 114 | connect(quitAction_, &QAction::triggered, this, QApplication::quit); |
| 115 | |
| 116 | menu->addAction(viewAction_); |
| 117 | menu->addAction(quitAction_); |
| 118 | |
| 119 | QString toolTip = QLatin1String("nheko"); |
| 120 | QString profile = UserSettings::instance()->profile(); |
| 121 | if (!profile.isEmpty()) |
| 122 | toolTip.append(QStringLiteral(" | %1").arg(profile)); |
| 123 | |
| 124 | setToolTip(toolTip); |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | TrayIcon::setUnreadCount(int count) |
| 129 | { |
| 130 | qGuiApp->setBadgeNumber(count); |
| 131 | if (count != previousCount) { |
| 132 | QString toolTip = QLatin1String("nheko"); |
| 133 | QString profile = UserSettings::instance()->profile(); |
| 134 | if (!profile.isEmpty()) |
| 135 | toolTip.append(QStringLiteral(" | %1").arg(profile)); |
| 136 | |
| 137 | if (count != 0) |
| 138 | toolTip.append(tr("\n%n unread message(s)", "", count)); |
| 139 | |
| 140 | setToolTip(toolTip); |
| 141 | } |
| 142 | |
| 143 | #if !defined(Q_OS_MACOS) && !defined(Q_OS_WIN) |
| 144 | if (count != previousCount) { |
| 145 | auto i = new MsgCountComposedIcon(icon); |
| 146 | i->msgCount = count; |
| 147 | setIcon(QIcon(i)); |
| 148 | previousCount = count; |
| 149 | } |
| 150 | #else |
| 151 | (void)previousCount; |
| 152 | #endif |
| 153 | } |
| 154 | |
| 155 | #include "moc_TrayIcon.cpp" |