-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwuake_window.cpp
228 lines (187 loc) · 5.68 KB
/
wuake_window.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <QtDebug>
#include <QApplication>
#include <QGuiApplication>
#include <QDesktopWidget>
#include <QMessageBox>
#include <QVBoxLayout>
#include <QTabBar>
#include <QKeySequence>
#include <QRect>
#include <QMenu>
#include <QScreen>
#include <Windows.h>
#include "QHotkey"
#include "wuake_window.h"
#include "wuake_tab_page.h"
WuakeWindow::WuakeWindow(QWidget *parent) :
QDialog(parent, Qt::ToolTip | Qt::FramelessWindowHint),
mIsShowing(false)
//QDialog(parent)
{
//resize(819, 475);
mTrayIcon = new QSystemTrayIcon(this);
mTrayIcon->setIcon(QIcon(":/res/images/app.png"));
mTrayIcon->setToolTip(QString("Wuake\n") + tr("Drop-down terminal for Windows"));
QMenu* menu = new QMenu(this);
mExitAct = menu->addAction(tr("Exit"));
mTrayIcon->setContextMenu(menu);
mTrayIcon->show();
connect(menu, &QMenu::triggered, this, &WuakeWindow::onAction);
connect(mTrayIcon, &QSystemTrayIcon::activated, this, &WuakeWindow::onTrayActive);
mTabWidget = new WuakeTabWidget(this);
connect(mTabWidget, &WuakeTabWidget::focusLost, this, &WuakeWindow::hide);
QVBoxLayout* layoutRoot = new QVBoxLayout(this);
layoutRoot->setContentsMargins(0, 0, 0, 0);
layoutRoot->addWidget(mTabWidget);
initHotkeys();
initAnim();
mTabWidget->newTab();
}
WuakeWindow::~WuakeWindow()
{
}
void WuakeWindow::show()
{
if (mIsShowing) return;
mPrevActiveWindow = ::GetActiveWindow();
if (!mPrevActiveWindow) {
mPrevActiveWindow = ::GetForegroundWindow();
}
qDebug() << "Active Window:" << mPrevActiveWindow;
QDialog::show();
mShowAnim.start();
}
void WuakeWindow::hide()
{
if (!mIsShowing) return;
if (mShowAnim.state() == QAbstractAnimation::Running ||
mHideAnim.state() == QAbstractAnimation::Running) {
return;
}
mHideAnim.start();
}
void WuakeWindow::onShow()
{
activateWindow();
mIsShowing = true;
}
void WuakeWindow::onHide()
{
mIsShowing = false;
if (mPrevActiveWindow) {
::SetForegroundWindow((HWND)mPrevActiveWindow);
::SetActiveWindow((HWND)mPrevActiveWindow);
}
}
void WuakeWindow::initAnim()
{
updateAnimRect();
mShowAnim.setTargetObject(this);
mShowAnim.setPropertyName("pos");
mShowAnim.setDuration(170);
mShowAnim.setEasingCurve(QEasingCurve::Linear);
connect(&mShowAnim, &QPropertyAnimation::finished, this, &WuakeWindow::onShow);
mHideAnim.setTargetObject(this);
mHideAnim.setPropertyName("pos");
mHideAnim.setDuration(170);
mHideAnim.setEasingCurve(QEasingCurve::Linear);
connect(&mHideAnim, &QPropertyAnimation::finished, this, &WuakeWindow::onHide);
}
void WuakeWindow::updateAnimRect()
{
QScreen *screen = QGuiApplication::screenAt(QCursor::pos());
if (nullptr == screen) {
qWarning() << "Cannot find screen!";
return;
}
int w = width();
int h = height();
int x = (screen->size().width() - w)/2;
int y = 0;
move(x, y);
QPoint posStart(x, y - h);
QPoint posEnd(x, y);
mShowAnim.setStartValue(posStart);
mShowAnim.setEndValue(posEnd);
mHideAnim.setStartValue(posEnd);
mHideAnim.setEndValue(posStart);
}
void WuakeWindow::initHotkeys()
{
mHotkeys.insert(HOTKEY_SHOW_HIDE, "F12");
mHotkeys.insert(HOTKEY_NEW_TAB, "Ctrl+Shift+N");
mHotkeys.insert(HOTKEY_SWITCH_LEFT_TAB, "Shift+Left");
mHotkeys.insert(HOTKEY_SWITCH_RIGHT_TAB, "Shift+Right");
mHotkeys.insert(HOTKEY_MOVE_ACTIVE_TAB_LEFT, "Ctrl+Shift+Left");
mHotkeys.insert(HOTKEY_MOVE_ACTIVE_TAB_RIGHT, "Ctrl+Shift+Right");
auto it = mHotkeys.constBegin();
for (; it != mHotkeys.constEnd(); ++it) {
QHotkey* hotkey = new QHotkey(QKeySequence(it.value()), true, this);
if (hotkey->isRegistered()) {
connect(hotkey, &QHotkey::activated, this, &WuakeWindow::onHotkey);
} else {
QMessageBox::warning(this, tr("Warn"), tr("Register global hotkey") + QString(" [%1] ").arg(it.value()) + tr("failed!"));
}
}
}
void WuakeWindow::onHotkey()
{
QHotkey* hotKey = qobject_cast<QHotkey*>(sender());
QString hotkeyStr = hotKey->shortcut().toString();
HotKeyCode keyCode = mHotkeys.key(hotkeyStr, HOTKEY_NONE);
if (HOTKEY_SHOW_HIDE == keyCode) {
if (mIsShowing) {
hide();
} else {
show();
}
return;
}
if (isHidden()) return;
switch (keyCode) {
case HOTKEY_NEW_TAB:
mTabWidget->newTab();
break;
case HOTKEY_SWITCH_LEFT_TAB:
mTabWidget->switchToLeft();
break;
case HOTKEY_SWITCH_RIGHT_TAB:
mTabWidget->switchToRight();
break;
case HOTKEY_MOVE_ACTIVE_TAB_LEFT:
mTabWidget->moveToLeft();
break;
case HOTKEY_MOVE_ACTIVE_TAB_RIGHT:
mTabWidget->moveToRight();
break;
//disable warning in gcc
case HOTKEY_NONE:
case HOTKEY_SHOW_HIDE:
break;
}
}
void WuakeWindow::onAction(QAction *action)
{
if (action == mExitAct) {
mTabWidget->closeAll();
}
}
void WuakeWindow::onTrayActive(QSystemTrayIcon::ActivationReason reason)
{
if (QSystemTrayIcon::Trigger == reason) {
if (isHidden()) {
show();
} else {
hide();
}
}
}
void WuakeWindow::resizeEvent(QResizeEvent *event)
{
Q_UNUSED(event);
if (mShowAnim.state() == QAbstractAnimation::Running ||
mHideAnim.state() == QAbstractAnimation::Running) {
return;
}
updateAnimRect();
}