summary refs log tree commit diff
path: root/src/TrayIcon.cc
blob: ac84aacad9d2aff31592db94ae97bb86299c8ed3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * nheko Copyright (C) 2017  Konstantinos Sideris <siderisk@auth.gr>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QApplication>
#include <QList>
#include <QMenu>
#include <QTimer>

#include "TrayIcon.h"

#if defined(Q_OS_MAC)
#include <QtMacExtras>
#endif

MsgCountComposedIcon::MsgCountComposedIcon(const QString &filename)
  : QIconEngine()
{
        icon_ = QIcon(filename);
}

void
MsgCountComposedIcon::paint(QPainter *painter,
                            const QRect &rect,
                            QIcon::Mode mode,
                            QIcon::State state)
{
        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);

        painter->setBrush(brush);
        painter->setPen(Qt::NoPen);
        painter->setFont(QFont("Open Sans", 8, QFont::Black));

        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));
}

QIconEngine *
MsgCountComposedIcon::clone() const
{
        return new MsgCountComposedIcon(*this);
}

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;
}

TrayIcon::TrayIcon(const QString &filename, QWidget *parent)
  : QSystemTrayIcon(parent)
{
#if defined(Q_OS_MAC) || defined(Q_OS_WIN)
        setIcon(QIcon(filename));
#else
        icon_ = new MsgCountComposedIcon(filename);
        setIcon(QIcon(icon_));
#endif

        QMenu *menu = new QMenu(parent);
        viewAction_ = new QAction(tr("Show"), parent);
        quitAction_ = new QAction(tr("Quit"), parent);

        connect(viewAction_, SIGNAL(triggered()), parent, SLOT(show()));
        connect(quitAction_, &QAction::triggered, this, QApplication::quit);

        menu->addAction(viewAction_);
        menu->addAction(quitAction_);

        setContextMenu(menu);
}

void
TrayIcon::setUnreadCount(int count)
{
// Use the native badge counter in MacOS.
#if defined(Q_OS_MAC)
        auto labelText = count == 0 ? "" : QString::number(count);

        if (labelText == QtMac::badgeLabelText())
                return;

        QtMac::setBadgeLabelText(labelText);
#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;

        setIcon(QIcon(tmp));

        icon_ = tmp;
#endif
}