forked from flameshot-org/flameshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrayicon.cpp
More file actions
200 lines (178 loc) · 5.82 KB
/
trayicon.cpp
File metadata and controls
200 lines (178 loc) · 5.82 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "trayicon.h"
#include "src/core/flameshot.h"
#include "src/core/flameshotdaemon.h"
#include "src/utils/globalvalues.h"
#include "src/utils/confighandler.h"
#include <QApplication>
#include <QMenu>
#include <QTimer>
#include <QUrl>
#include <QVersionNumber>
#if defined(Q_OS_MACOS)
#include <QOperatingSystemVersion>
#endif
TrayIcon::TrayIcon(QObject* parent)
: QSystemTrayIcon(parent)
{
initMenu();
setToolTip(QStringLiteral("Flameshot"));
#if defined(Q_OS_MACOS)
// Because of the following issues on MacOS "Catalina":
// https://bugreports.qt.io/browse/QTBUG-86393
// https://developer.apple.com/forums/thread/126072
auto currentMacOsVersion = QOperatingSystemVersion::current();
if (currentMacOsVersion >= currentMacOsVersion.MacOSBigSur) {
setContextMenu(m_menu);
}
#else
setContextMenu(m_menu);
#endif
QIcon icon =
QIcon::fromTheme("flameshot-tray", QIcon(GlobalValues::iconPathPNG()));
setIcon(icon);
#if defined(Q_OS_MACOS)
if (currentMacOsVersion < currentMacOsVersion.MacOSBigSur) {
// Because of the following issues on MacOS "Catalina":
// https://bugreports.qt.io/browse/QTBUG-86393
// https://developer.apple.com/forums/thread/126072
auto trayIconActivated = [this](QSystemTrayIcon::ActivationReason r) {
if (m_menu->isVisible()) {
m_menu->hide();
} else {
m_menu->popup(QCursor::pos());
}
};
connect(this, &QSystemTrayIcon::activated, this, trayIconActivated);
}
#else
connect(this, &TrayIcon::activated, this, [this](ActivationReason r) {
if (r == Trigger) {
startGuiCapture();
}
});
#endif
#ifdef Q_OS_WIN
// Ensure proper removal of tray icon when program quits on Windows.
connect(qApp, &QCoreApplication::aboutToQuit, this, &TrayIcon::hide);
#endif
show(); // TODO needed?
if (ConfigHandler().showStartupLaunchMessage()) {
showMessage(
"Flameshot",
QObject::tr(
"Hello, I'm here! Click icon in the tray to take a screenshot or "
"click with a right button to see more options."),
icon,
3000);
}
connect(ConfigHandler::getInstance(),
&ConfigHandler::fileChanged,
this,
[this]() {});
}
TrayIcon::~TrayIcon()
{
delete m_menu;
}
#if !defined(DISABLE_UPDATE_CHECKER)
QAction* TrayIcon::appUpdates()
{
return m_appUpdates;
}
#endif
void TrayIcon::initMenu()
{
m_menu = new QMenu();
auto* captureAction = new QAction(tr("&Take Screenshot"), this);
connect(captureAction, &QAction::triggered, this, [this]() {
#if defined(Q_OS_MACOS)
auto currentMacOsVersion = QOperatingSystemVersion::current();
if (currentMacOsVersion >= currentMacOsVersion.MacOSBigSur) {
startGuiCapture();
} else {
// It seems it is not relevant for MacOS BigSur (Wait 400 ms to hide
// the QMenu)
QTimer::singleShot(400, this, [this]() { startGuiCapture(); });
}
#else
// Wait 400 ms to hide the QMenu
QTimer::singleShot(400, this, [this]() {
startGuiCapture();
});
#endif
});
auto* launcherAction = new QAction(tr("&Open Launcher"), this);
connect(launcherAction,
&QAction::triggered,
Flameshot::instance(),
&Flameshot::launcher);
auto* configAction = new QAction(tr("&Configuration"), this);
connect(configAction,
&QAction::triggered,
Flameshot::instance(),
&Flameshot::config);
auto* infoAction = new QAction(tr("&About"), this);
connect(
infoAction, &QAction::triggered, Flameshot::instance(), &Flameshot::info);
#if !defined(DISABLE_UPDATE_CHECKER)
m_appUpdates = new QAction(tr("Check for updates"), this);
connect(m_appUpdates,
&QAction::triggered,
FlameshotDaemon::instance(),
&FlameshotDaemon::checkForUpdates);
connect(FlameshotDaemon::instance(),
&FlameshotDaemon::newVersionAvailable,
this,
[this](const QVersionNumber& version) {
QString newVersion =
tr("New version %1 is available").arg(version.toString());
m_appUpdates->setText(newVersion);
});
#endif
QAction* quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
// recent screenshots
QAction* recentAction = new QAction(tr("&Latest Uploads"), this);
connect(recentAction,
&QAction::triggered,
Flameshot::instance(),
&Flameshot::history);
auto* openSavePathAction = new QAction(tr("&Open Save Path"), this);
connect(openSavePathAction,
&QAction::triggered,
Flameshot::instance(),
&Flameshot::openSavePath);
m_menu->addAction(captureAction);
m_menu->addAction(launcherAction);
m_menu->addSeparator();
m_menu->addAction(recentAction);
m_menu->addAction(openSavePathAction);
m_menu->addSeparator();
m_menu->addAction(configAction);
m_menu->addSeparator();
#if !defined(DISABLE_UPDATE_CHECKER)
m_menu->addAction(m_appUpdates);
#endif
m_menu->addAction(infoAction);
m_menu->addSeparator();
m_menu->addAction(quitAction);
}
#if !defined(DISABLE_UPDATE_CHECKER)
void TrayIcon::enableCheckUpdatesAction(bool enable)
{
if (m_appUpdates != nullptr) {
m_appUpdates->setVisible(enable);
m_appUpdates->setEnabled(enable);
}
if (enable) {
FlameshotDaemon::instance()->getLatestAvailableVersion();
}
}
#endif
void TrayIcon::startGuiCapture()
{
auto* widget = Flameshot::instance()->gui();
#if !defined(DISABLE_UPDATE_CHECKER)
FlameshotDaemon::instance()->showUpdateNotificationIfAvailable(widget);
#endif
}