forked from zserge/tray
-
Notifications
You must be signed in to change notification settings - Fork 16
/
QtTrayMenu.cpp
162 lines (137 loc) · 4 KB
/
QtTrayMenu.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
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
#include "QtTrayMenu.h"
#include <QApplication>
int argc = 1;
char *argvArray[] = {(char*)"TrayMenuApp", nullptr};
bool debug = false;
QtTrayMenu::QtTrayMenu()
: trayIcon(nullptr), trayStruct(nullptr), continueRunning(true), app(nullptr)
{
if (QApplication::instance())
{
app = dynamic_cast<QApplication *>(QApplication::instance());
if (!app) {
fprintf(stderr, "QCoreApplication is not a QApplication, please contact support.");
}
}
else
{
app = new QApplication(argc, &argvArray[0]);
}
if (debug)
app->installEventFilter(this);
}
QtTrayMenu::~QtTrayMenu()
{
delete trayIcon;
trayIcon = nullptr; // Set to nullptr after deletion
// Delete app only if it was created within this class
if (app && app != QApplication::instance())
{
delete app;
app = nullptr; // Set to nullptr after deletion
}
}
int QtTrayMenu::init(struct tray *tray)
{
if (trayIcon)
return -1; // Already initialized
this->trayStruct = tray;
if (app->applicationName().isEmpty() || app->applicationName() == "TrayMenuApp")
app->setApplicationName(tray->tooltip);
trayIcon = new QSystemTrayIcon(QIcon(tray->icon_filepath));
trayIcon->setToolTip(QString::fromUtf8(tray->tooltip));
connect(trayIcon, &QSystemTrayIcon::activated, this, &QtTrayMenu::onTrayActivated);
connect(this, &QtTrayMenu::exitRequested, this, &QtTrayMenu::onExitRequested);
auto *menu = new QMenu;
createMenu(tray->menu, menu);
trayIcon->setContextMenu(menu);
trayIcon->show();
return 0;
}
void QtTrayMenu::update(struct tray *tray)
{
this->trayStruct = tray;
if (trayIcon) {
auto newIcon = QIcon(tray->icon_filepath);
if (!newIcon.isNull())
trayIcon->setIcon(newIcon);
trayIcon->setToolTip(QString::fromUtf8(tray->tooltip));
}
auto *existingMenu = trayIcon->contextMenu();
if (existingMenu) {
existingMenu->clear(); // Remove all actions
createMenu(tray->menu, existingMenu);
}
}
int QtTrayMenu::loop(int blocking)
{
if (!continueRunning) {
return -1;
}
if (!app || app->closingDown()) {
printf("Application is not in a valid state or is closing down.\n");
return -1;
}
if (blocking)
{
app->exec();
return -1;
}
else
{
app->processEvents();
return 0;
}
}
void QtTrayMenu::exit()
{
continueRunning = false;
emit exitRequested();
}
void QtTrayMenu::createMenu(struct tray_menu_item *items, QMenu *menu)
{
while (items && items->text)
{
auto *action = new QAction(QString::fromUtf8(items->text), menu);
action->setDisabled(items->disabled == 1);
action->setCheckable(true);
action->setChecked(items->checked == 1);
action->setProperty("tray_menu_item", QVariant::fromValue((void *)items));
connect(action, &QAction::triggered, this, &QtTrayMenu::onMenuItemTriggered);
if (items->submenu)
{
auto submenu = new QMenu;
createMenu(items->submenu, submenu);
action->setMenu(submenu);
}
menu->addAction(action);
items++;
}
}
bool QtTrayMenu::eventFilter(QObject *watched, QEvent *event) {
qDebug() << "Event Type:" << event->type();
return QObject::eventFilter(watched, event);
}
void QtTrayMenu::onTrayActivated(QSystemTrayIcon::ActivationReason reason)
{
if (reason == QSystemTrayIcon::Trigger && trayStruct->cb)
{
trayStruct->cb(trayStruct);
}
}
void QtTrayMenu::onMenuItemTriggered()
{
auto *action = qobject_cast<QAction *>(sender());
struct tray_menu_item *menuItem = getTrayMenuItem(action);
if (menuItem && menuItem->cb)
{
menuItem->cb(menuItem);
}
}
struct tray_menu_item *QtTrayMenu::getTrayMenuItem(QAction *action)
{
return (struct tray_menu_item *)action->property("tray_menu_item").value<void *>();
}
void QtTrayMenu::onExitRequested() {
app->quit();
}