-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
49 lines (45 loc) · 1.48 KB
/
mainwindow.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
#include "mainwindow.h"
#include <QSystemTrayIcon>
#include <QMenu>
#include <memory>
#include <QDebug>
#include <QApplication>
#include "displayfixer.h"
#include "nativeeventfilter.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
m_trayIcon = std::unique_ptr<QSystemTrayIcon>(new QSystemTrayIcon());
QMenu *menu = new QMenu(this);
DisplayFixer *fixer = new DisplayFixer;
menu->setTitle("Display Switcher Thingy");
auto fix = menu->addAction("Fix Display");
auto quit = menu->addAction("Quit");
connect(quit, &QAction::triggered, this, &MainWindow::quit_action);
connect(fix, &QAction::triggered, [=] (bool) { fixer->fixit(); });
registerHotKey();
connect(m_nef.get(), &NativeEventFilter::hotkeyReceived, [=] () { fixer->fixit(); });
m_trayIcon->setIcon(QIcon(":/icon/icon.svg"));
m_trayIcon->setContextMenu(menu);
m_trayIcon->show();
}
void MainWindow::registerHotKey()
{
wchar_t appName[24] = L"display_switcher_thingy";
m_hotkeyId = GlobalAddAtom(appName);
if (m_hotkeyId == 0)
qDebug() << "Failed to get hotkey ID";
if (RegisterHotKey(NULL, m_hotkeyId, MOD_ALT | MOD_SHIFT | MOD_NOREPEAT, 0x44))
qDebug() << "Registered hotkey mod-shift-d";
m_nef = std::make_unique<NativeEventFilter>();
}
MainWindow::~MainWindow()
{
UnregisterHotKey(NULL, m_hotkeyId);
GlobalDeleteAtom(m_hotkeyId);
}
void MainWindow::quit_action(bool)
{
qDebug() << "Quit triggered";
QApplication::exit();
}