-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrayManager.cpp
69 lines (59 loc) · 1.77 KB
/
TrayManager.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
#include "TrayManager.h"
#include <QAction>
#include <QApplication>
TrayManager* TrayManager::mInstance = nullptr;
TrayManager::TrayManager(QObject *parent)
: QObject(parent),
mTrayIcon(new QSystemTrayIcon(this)),
mTrayMenu(new QMenu())
{
// 设置托盘图标
mTrayIcon->setIcon(QIcon(":/ico/LD.ico"));
mTrayIcon->setToolTip("LazyDogTools");
mTrayIcon->setContextMenu(mTrayMenu);
mTrayIcon->show();
}
TrayManager* TrayManager::instance()
{
if (!mInstance)
{
mInstance = new TrayManager();
}
return mInstance;
}
void TrayManager::showNotification(const QString& title, const QString& message)
{
mTrayIcon->showMessage(title, message, QSystemTrayIcon::Information, 3000);
}
void TrayManager::addMenuItem(const QString& name, std::function<void()> callback, QMenu* parentMenu, const QIcon& icon)
{
QMenu* menu = parentMenu ? parentMenu : mTrayMenu;
QAction* action = new QAction(icon, name, menu);
if (callback) {
connect(action, &QAction::triggered, this, [callback]() {
callback();
});
}
menu->addAction(action);
}
void TrayManager::setTriggered(std::function<void ()> callback)
{
connect(mTrayIcon, &QSystemTrayIcon::activated, this, [callback](QSystemTrayIcon::ActivationReason reason) {
if (reason == QSystemTrayIcon::Trigger) {
callback();
}
});
}
QMenu* TrayManager::addSubMenu(const QString& name, QMenu* parentMenu, const QIcon& icon)
{
QMenu* menu = parentMenu ? parentMenu : mTrayMenu;
QMenu* subMenu = new QMenu(name, menu);
subMenu->setIcon(icon);
menu->addMenu(subMenu);
return subMenu;
}
void TrayManager::addSeparator(QMenu* parentMenu)
{
QMenu* menu = parentMenu ? parentMenu : mTrayMenu;
menu->addSeparator();
}