-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraymenu.cxx
73 lines (63 loc) · 2.19 KB
/
traymenu.cxx
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
#include "traymenu.hxx"
#include <QApplication>
#include <QIcon>
#include <QSystemTrayIcon>
#include <QThread>
#include "QHotkey/qhotkey.h"
#include "config.hxx"
#include "selectionwindow.hxx"
static void addGlobalKey(QAction *action, std::string_view name, bool *retryAdd) {
for (const auto &key : Config::parseHotkeys(config->root["globalkeys"][name])) {
auto *hotkey = new QHotkey(key, true, action);
QObject::connect(hotkey, &QHotkey::activated, action, [action]() {
action->activate(QAction::Trigger);
});
if (!hotkey->isRegistered()) {
if (*retryAdd) {
// sometimes the killed duplicate is still holding the keybind, so wait a bit for it to finish
QThread::sleep(50);
*retryAdd = false;
hotkey->setRegistered(true);
if (!hotkey->isRegistered()) {
qInfo() << "Failed to register" << key << "for" << name;
}
}
}
}
}
TrayMenu::TrayMenu(QWidget *parent) : QMenu(parent) {
auto *takeScreenshot = new QAction(QIcon(":/icon.svg"), "Take screenshot", this);
connect(takeScreenshot, &QAction::triggered, this, [this]() {
auto *win = new SelectionWindow(this);
win->setVisible(true);
});
this->addAction(takeScreenshot);
auto *picker = new QAction(QIcon("find-location-symbolic"), "Color picker", this);
connect(picker, &QAction::triggered, this, [this]() {
auto *win = new SelectionWindow(this);
win->setPicking(true);
win->setVisible(true);
});
this->addAction(picker);
if (QHotkey::isPlatformSupported()) {
bool retryAdd = true;
addGlobalKey(takeScreenshot, "screenshot", &retryAdd);
addGlobalKey(picker, "picker", &retryAdd);
}
addSeparator();
auto *exit = new QAction(QIcon::fromTheme("exit"), "Exit", this);
connect(exit, &QAction::triggered, this, []() {
QApplication::exit(0);
});
this->addAction(exit);
}
TrayMenu::~TrayMenu() {}
void TrayMenu::createTrayIcon() {
auto *tray = new QSystemTrayIcon(QIcon(":/icon.svg"), this);
tray->setToolTip(QApplication::applicationDisplayName());
tray->setContextMenu(this);
tray->setVisible(true);
}