forked from Stremio/stremio-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemtray.cpp
89 lines (71 loc) · 2.87 KB
/
systemtray.cpp
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
#include "systemtray.h"
#include <QMenu>
#include <QSystemTrayIcon>
#include <QOperatingSystemVersion>
SystemTray::SystemTray(QObject *parent) : QObject(parent)
{
// Create a context menu with two items
QMenu *trayIconMenu = new QMenu();
viewWindowAction = new QAction(tr("Show window"), this);
viewWindowAction->setCheckable(true);
alwaysOnTopAction = new QAction(tr("Always on top"), this);
alwaysOnTopAction->setCheckable(true);
QAction * quitAction = new QAction(tr("Quit"), this);
/* to connect the signals clicks on menu items to the appropriate signals for QML.
* */
connect(trayIconMenu, &QMenu::aboutToShow, this, &SystemTray::signalIconMenuAboutToShow);
connect(viewWindowAction, &QAction::triggered, this, &SystemTray::signalShow);
connect(alwaysOnTopAction, &QAction::triggered, this, &SystemTray::signalAlwaysOnTop);
connect(quitAction, &QAction::triggered, this, &SystemTray::signalQuit);
trayIconMenu->addAction(viewWindowAction);
trayIconMenu->addAction(alwaysOnTopAction);
trayIconMenu->addAction(quitAction);
/* Initialize the tray icon, icon set, and specify the tooltip
* */
trayIcon = new QSystemTrayIcon();
trayIcon->setContextMenu(trayIconMenu);
QIcon icon;
if(QOperatingSystemVersion::current() <= QOperatingSystemVersion::Windows7) {
icon = QIcon(":/images/stremio_window.png");
} else {
icon = QIcon::fromTheme("smartcode-stremio-tray", QIcon(":/images/stremio_tray_white.png"));
icon.setIsMask(true);
}
trayIcon->setIcon(icon);
trayIcon->show();
/* Also connect clicking on the icon to the signal handler of the pressing
* */
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}
/* The method that handles click on the application icon in the system tray
* */
void SystemTray::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason){
case QSystemTrayIcon::Trigger:
// In the case of pressing the signal on the icon tray in the call signal QML layer
#ifndef Q_OS_OSX
emit signalIconActivated();
#endif
break;
default:
break;
}
}
void SystemTray::hideIconTray()
{
trayIcon->hide();
}
void SystemTray::updateVisibleAction(bool isVisible)
{
viewWindowAction->setChecked(isVisible);
}
void SystemTray::updateIsOnTop(bool isOnTop)
{
alwaysOnTopAction->setChecked(isOnTop);
}
void SystemTray::alwaysOnTopEnabled(bool enabled)
{
alwaysOnTopAction->setEnabled(enabled);
}