-
Notifications
You must be signed in to change notification settings - Fork 6
/
globalmotions.cpp
230 lines (200 loc) · 6.51 KB
/
globalmotions.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
229
230
/*
KWin - the KDE window manager
This file is part of the KDE project.
SPDX-FileCopyrightText: 2020 guoxiang yang <yangguoxiang@jingos.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "globalmotions.h"
#include "globalmotions.h"
#include <QMouseEvent>
#include <QDateTime>
namespace KWin {
const quint32 GESTURE_INTERVAL = 500;
MouseMotion::MouseMotion(QObject *parent)
: Gesture(parent)
{
}
void MouseMotion::setStartGeometry(const QRect &geometry)
{
setMinimumX(geometry.x());
setMinimumY(geometry.y());
setMaximumX(geometry.x() + geometry.width());
setMaximumY(geometry.y() + geometry.height());
Q_ASSERT(m_maximumX >= m_minimumX);
Q_ASSERT(m_maximumY >= m_minimumY);
}
qreal MouseMotion::minimumDeltaReachedProgress(const QSizeF &delta) const
{
if (!m_minimumDeltaRelevant || m_minimumDelta.isNull()) {
return 1.0;
}
switch (m_direction) {
case Direction::Up:
case Direction::Down:
return std::min(std::abs(delta.height()) / std::abs(m_minimumDelta.height()), 1.0);
case Direction::Left:
case Direction::Right:
return std::min(std::abs(delta.width()) / std::abs(m_minimumDelta.width()), 1.0);
default:
Q_UNREACHABLE();
}
}
bool MouseMotion::minimumDeltaReached(const QSizeF &delta) const
{
return minimumDeltaReachedProgress(delta) >= 1.0 ;
}
MouseSwipeMotionRecognizer::MouseSwipeMotionRecognizer(QObject *parent)
: QObject(parent)
{
}
MouseSwipeMotionRecognizer::~MouseSwipeMotionRecognizer()
{
}
void MouseSwipeMotionRecognizer::registerMotion(MouseMotion *motion)
{
Q_ASSERT(!m_motions.contains(motion));
auto connection = connect(motion, &QObject::destroyed, this, std::bind(&MouseSwipeMotionRecognizer::unregisterMotion, this, motion));
m_destroyConnections.insert(motion, connection);
m_motions << motion;
}
void MouseSwipeMotionRecognizer::unregisterMotion(MouseMotion *motion)
{
auto it = m_destroyConnections.find(motion);
if (it != m_destroyConnections.end()) {
disconnect(it.value());
m_destroyConnections.erase(it);
}
m_motions.removeAll(motion);
if (m_activeSwipeMotions.removeOne(motion)) {
emit motion->cancelled(1, 0);
}
}
int MouseSwipeMotionRecognizer::startSwipeMotion(const QPointF &startPos, qint64 time)
{
int count = 0;
// TODO: verify that no gesture is running
for (MouseMotion *motion : qAsConst(m_motions)) {
if (!motion) {
continue;
}
if (motion->minimumXIsRelevant()) {
if (motion->minimumX() > startPos.x()) {
continue;
}
}
if (motion->maximumXIsRelevant()) {
if (motion->maximumX() < startPos.x()) {
continue;
}
}
if (motion->minimumYIsRelevant()) {
if (motion->minimumY() > startPos.y()) {
continue;
}
}
if (motion->maximumYIsRelevant()) {
if (motion->maximumY() < startPos.y()) {
continue;
}
}
// direction doesn't matter yet
if (!m_activeSwipeMotions.contains(motion)) {
m_activeSwipeMotions << motion;
}
count++;
m_lastPos = m_endPos = m_startPos = startPos;
m_lastEndUpdateTime = m_lastUpdateTime = time;
emit motion->started(time);
}
return count;
}
void MouseSwipeMotionRecognizer::updateSwipeMotion(const QPointF &pos, qint64 time)
{
if (m_lastUpdateTime <= 0 || (time - m_lastUpdateTime) < GESTURE_INTERVAL || m_activeSwipeMotions.size() < 0) {
return;
}
QSizeF delta(pos.x() - m_lastPos.x(), pos.y() - m_lastPos.y());
if (std::abs(delta.width()) > std::abs(delta.height())) {
m_lastAverageSeed = delta.width() / qreal(time - m_lastUpdateTime);
} else {
m_lastAverageSeed = delta.height() / qreal(time - m_lastUpdateTime);
}
m_lastUpdateTime = m_lastEndUpdateTime;
m_lastEndUpdateTime = time;
m_lastPos = m_endPos;
m_endPos = pos;
}
void MouseSwipeMotionRecognizer::cancelSwipeMotion(qint64 time)
{
for (auto g : qAsConst(m_activeSwipeMotions)) {
emit g->cancelled(time - m_lastUpdateTime, 0);
}
m_lastAverageSeed = 0.0f;
m_lastUpdateTime = 0;
m_lastUpdateTime = 0;
m_lastEndUpdateTime = 0;
m_activeSwipeMotions.clear();
}
void MouseSwipeMotionRecognizer::endSwipeMotion(const QPointF &pos, qint64 time)
{
if (time - m_lastUpdateTime < GESTURE_INTERVAL || 0.0f == m_lastAverageSeed) {
QSizeF lastDelta(pos.x() - m_lastPos.x(), pos.y() - m_lastPos.y());
if (std::abs(lastDelta.width()) > std::abs(lastDelta.height())) {
m_lastAverageSeed = lastDelta.width() / qreal(time - m_lastUpdateTime);
} else {
m_lastAverageSeed = lastDelta.height() / qreal(time - m_lastUpdateTime);
}
}
QSizeF delta(pos.x() - m_startPos.x(), pos.y() - m_startPos.y());
for (auto g : qAsConst(m_activeSwipeMotions)) {
if (static_cast<MouseMotion*>(g)->minimumDeltaReached(delta)) {
emit g->motionTriggered(time - m_lastUpdateTime, m_lastAverageSeed);
} else {
emit g->cancelled(time - m_lastUpdateTime, 0);
}
}
m_activeSwipeMotions.clear();
m_lastAverageSeed = 0.0f;
m_lastUpdateTime = 0;
m_lastUpdateTime = 0;
m_lastEndUpdateTime = 0;
}
MouseSwipeMotionMgr* MouseSwipeMotionMgr::_self = nullptr;
MouseSwipeMotionMgr::MouseSwipeMotionMgr(QObject *parent)
: QObject(parent)
, m_recognizer(new MouseSwipeMotionRecognizer(this))
{
}
MouseSwipeMotionMgr::~MouseSwipeMotionMgr()
{
_self = nullptr;
}
void MouseSwipeMotionMgr::onPointEvent(QMouseEvent *event)
{
switch (event->type()) {
case QEvent::MouseButtonPress:
onPressed(QPointF(event->x(), event->y()), QDateTime::currentMSecsSinceEpoch());
break;
case QEvent::MouseButtonRelease:
onRelease(QPointF(event->x(), event->y()), QDateTime::currentMSecsSinceEpoch());
break;
case QEvent::MouseMove:
onMotion(QPointF(event->x(), event->y()), QDateTime::currentMSecsSinceEpoch());
break;
default:
break;
}
}
void MouseSwipeMotionMgr::onPressed(const QPointF &startPos, qint64 time)
{
m_recognizer->startSwipeMotion(startPos, time);
}
void MouseSwipeMotionMgr::onMotion(const QPointF &startPos, qint64 time)
{
m_recognizer->updateSwipeMotion(startPos, time);
}
void MouseSwipeMotionMgr::onRelease(const QPointF &startPos, qint64 time)
{
m_recognizer->endSwipeMotion(startPos, time);
}
}