gesture: use the global direction enums

This commit is contained in:
Xaver Hugl 2023-01-25 17:18:06 +01:00 committed by Vlad Zahorodnii
parent a9334cb9c6
commit 1746af4b02
6 changed files with 87 additions and 104 deletions

View file

@ -171,6 +171,7 @@ add_executable(testGestures ${testGestures_SRCS})
target_link_libraries(testGestures
Qt::Test
Qt::Widgets
)
add_test(NAME kwin-testGestures COMMAND testGestures)

View file

@ -15,6 +15,9 @@
using namespace KWin;
Q_DECLARE_METATYPE(SwipeDirection);
Q_DECLARE_METATYPE(PinchDirection);
class GestureTest : public QObject
{
Q_OBJECT
@ -168,44 +171,44 @@ void GestureTest::testPinchMaxFinger()
void GestureTest::testSwipeDirection_data()
{
QTest::addColumn<KWin::SwipeGesture::Direction>("swipe_direction");
QTest::addColumn<SwipeDirection>("swipe_direction");
QTest::newRow("Up") << KWin::SwipeGesture::Direction::Up;
QTest::newRow("Left") << KWin::SwipeGesture::Direction::Left;
QTest::newRow("Right") << KWin::SwipeGesture::Direction::Right;
QTest::newRow("Down") << KWin::SwipeGesture::Direction::Down;
QTest::newRow("Up") << SwipeDirection::Up;
QTest::newRow("Left") << SwipeDirection::Left;
QTest::newRow("Right") << SwipeDirection::Right;
QTest::newRow("Down") << SwipeDirection::Down;
}
void GestureTest::testSwipeDirection()
{
SwipeGesture gesture;
QCOMPARE(gesture.direction(), SwipeGesture::Direction::Down);
QFETCH(KWin::SwipeGesture::Direction, swipe_direction);
QCOMPARE(gesture.direction(), SwipeDirection::Down);
QFETCH(SwipeDirection, swipe_direction);
gesture.setDirection(swipe_direction);
QCOMPARE(gesture.direction(), swipe_direction);
// back to down
gesture.setDirection(SwipeGesture::Direction::Down);
QCOMPARE(gesture.direction(), SwipeGesture::Direction::Down);
gesture.setDirection(SwipeDirection::Down);
QCOMPARE(gesture.direction(), SwipeDirection::Down);
}
void GestureTest::testPinchDirection_data()
{
QTest::addColumn<KWin::PinchGesture::Direction>("pinch_direction");
QTest::addColumn<PinchDirection>("pinch_direction");
QTest::newRow("Contracting") << KWin::PinchGesture::Direction::Contracting;
QTest::newRow("Expanding") << KWin::PinchGesture::Direction::Expanding;
QTest::newRow("Contracting") << PinchDirection::Contracting;
QTest::newRow("Expanding") << PinchDirection::Expanding;
}
void GestureTest::testPinchDirection()
{
PinchGesture gesture;
QCOMPARE(gesture.direction(), PinchGesture::Direction::Expanding);
QFETCH(KWin::PinchGesture::Direction, pinch_direction);
QCOMPARE(gesture.direction(), PinchDirection::Expanding);
QFETCH(PinchDirection, pinch_direction);
gesture.setDirection(pinch_direction);
QCOMPARE(gesture.direction(), pinch_direction);
// back to down
gesture.setDirection(PinchGesture::Direction::Expanding);
QCOMPARE(gesture.direction(), PinchGesture::Direction::Expanding);
gesture.setDirection(PinchDirection::Expanding);
QCOMPARE(gesture.direction(), PinchDirection::Expanding);
}
void GestureTest::testMinimumX_data()
@ -327,24 +330,24 @@ void GestureTest::testSetMinimumDelta()
void GestureTest::testMinimumDeltaReached_data()
{
QTest::addColumn<KWin::SwipeGesture::Direction>("direction");
QTest::addColumn<SwipeDirection>("direction");
QTest::addColumn<QPointF>("minimumDelta");
QTest::addColumn<QPointF>("delta");
QTest::addColumn<bool>("reached");
QTest::addColumn<qreal>("progress");
QTest::newRow("Up (more)") << KWin::SwipeGesture::Direction::Up << QPointF(0, -30) << QPointF(0, -40) << true << 1.0;
QTest::newRow("Up (exact)") << KWin::SwipeGesture::Direction::Up << QPointF(0, -30) << QPointF(0, -30) << true << 1.0;
QTest::newRow("Up (less)") << KWin::SwipeGesture::Direction::Up << QPointF(0, -30) << QPointF(0, -29) << false << 29.0 / 30.0;
QTest::newRow("Left (more)") << KWin::SwipeGesture::Direction::Left << QPointF(-30, -30) << QPointF(-40, 20) << true << 1.0;
QTest::newRow("Left (exact)") << KWin::SwipeGesture::Direction::Left << QPointF(-30, -40) << QPointF(-30, 0) << true << 1.0;
QTest::newRow("Left (less)") << KWin::SwipeGesture::Direction::Left << QPointF(-30, -30) << QPointF(-29, 0) << false << 29.0 / 30.0;
QTest::newRow("Right (more)") << KWin::SwipeGesture::Direction::Right << QPointF(30, -30) << QPointF(40, 20) << true << 1.0;
QTest::newRow("Right (exact)") << KWin::SwipeGesture::Direction::Right << QPointF(30, -40) << QPointF(30, 0) << true << 1.0;
QTest::newRow("Right (less)") << KWin::SwipeGesture::Direction::Right << QPointF(30, -30) << QPointF(29, 0) << false << 29.0 / 30.0;
QTest::newRow("Down (more)") << KWin::SwipeGesture::Direction::Down << QPointF(0, 30) << QPointF(0, 40) << true << 1.0;
QTest::newRow("Down (exact)") << KWin::SwipeGesture::Direction::Down << QPointF(0, 30) << QPointF(0, 30) << true << 1.0;
QTest::newRow("Down (less)") << KWin::SwipeGesture::Direction::Down << QPointF(0, 30) << QPointF(0, 29) << false << 29.0 / 30.0;
QTest::newRow("Up (more)") << SwipeDirection::Up << QPointF(0, -30) << QPointF(0, -40) << true << 1.0;
QTest::newRow("Up (exact)") << SwipeDirection::Up << QPointF(0, -30) << QPointF(0, -30) << true << 1.0;
QTest::newRow("Up (less)") << SwipeDirection::Up << QPointF(0, -30) << QPointF(0, -29) << false << 29.0 / 30.0;
QTest::newRow("Left (more)") << SwipeDirection::Left << QPointF(-30, -30) << QPointF(-40, 20) << true << 1.0;
QTest::newRow("Left (exact)") << SwipeDirection::Left << QPointF(-30, -40) << QPointF(-30, 0) << true << 1.0;
QTest::newRow("Left (less)") << SwipeDirection::Left << QPointF(-30, -30) << QPointF(-29, 0) << false << 29.0 / 30.0;
QTest::newRow("Right (more)") << SwipeDirection::Right << QPointF(30, -30) << QPointF(40, 20) << true << 1.0;
QTest::newRow("Right (exact)") << SwipeDirection::Right << QPointF(30, -40) << QPointF(30, 0) << true << 1.0;
QTest::newRow("Right (less)") << SwipeDirection::Right << QPointF(30, -30) << QPointF(29, 0) << false << 29.0 / 30.0;
QTest::newRow("Down (more)") << SwipeDirection::Down << QPointF(0, 30) << QPointF(0, 40) << true << 1.0;
QTest::newRow("Down (exact)") << SwipeDirection::Down << QPointF(0, 30) << QPointF(0, 30) << true << 1.0;
QTest::newRow("Down (less)") << SwipeDirection::Down << QPointF(0, 30) << QPointF(0, 29) << false << 29.0 / 30.0;
}
void GestureTest::testMinimumDeltaReached()
@ -353,7 +356,7 @@ void GestureTest::testMinimumDeltaReached()
// swipe gesture
SwipeGesture gesture;
QFETCH(SwipeGesture::Direction, direction);
QFETCH(SwipeDirection, direction);
gesture.setDirection(direction);
QFETCH(QPointF, minimumDelta);
gesture.setMinimumDelta(minimumDelta);
@ -392,7 +395,7 @@ void GestureTest::testMinimumScaleDelta()
{
// pinch gesture
PinchGesture gesture;
gesture.setDirection(PinchGesture::Direction::Contracting);
gesture.setDirection(PinchDirection::Contracting);
gesture.setMinimumScaleDelta(.5);
gesture.setMinimumFingerCount(3);
gesture.setMaximumFingerCount(4);
@ -470,19 +473,19 @@ void GestureTest::testDeleteSwipeCancels()
void GestureTest::testSwipeCancel_data()
{
QTest::addColumn<KWin::SwipeGesture::Direction>("direction");
QTest::addColumn<SwipeDirection>("direction");
QTest::newRow("Up") << KWin::SwipeGesture::Direction::Up;
QTest::newRow("Left") << KWin::SwipeGesture::Direction::Left;
QTest::newRow("Right") << KWin::SwipeGesture::Direction::Right;
QTest::newRow("Down") << KWin::SwipeGesture::Direction::Down;
QTest::newRow("Up") << SwipeDirection::Up;
QTest::newRow("Left") << SwipeDirection::Left;
QTest::newRow("Right") << SwipeDirection::Right;
QTest::newRow("Down") << SwipeDirection::Down;
}
void GestureTest::testSwipeCancel()
{
GestureRecognizer recognizer;
std::unique_ptr<SwipeGesture> gesture(new SwipeGesture);
QFETCH(SwipeGesture::Direction, direction);
QFETCH(SwipeDirection, direction);
gesture->setDirection(direction);
QSignalSpy startedSpy(gesture.get(), &SwipeGesture::started);
QSignalSpy cancelledSpy(gesture.get(), &SwipeGesture::cancelled);
@ -499,20 +502,20 @@ void GestureTest::testSwipeCancel()
void GestureTest::testSwipeUpdateTrigger_data()
{
QTest::addColumn<KWin::SwipeGesture::Direction>("direction");
QTest::addColumn<SwipeDirection>("direction");
QTest::addColumn<QPointF>("delta");
QTest::newRow("Up") << KWin::SwipeGesture::Direction::Up << QPointF(2, -3);
QTest::newRow("Left") << KWin::SwipeGesture::Direction::Left << QPointF(-3, 1);
QTest::newRow("Right") << KWin::SwipeGesture::Direction::Right << QPointF(20, -19);
QTest::newRow("Down") << KWin::SwipeGesture::Direction::Down << QPointF(0, 50);
QTest::newRow("Up") << SwipeDirection::Up << QPointF(2, -3);
QTest::newRow("Left") << SwipeDirection::Left << QPointF(-3, 1);
QTest::newRow("Right") << SwipeDirection::Right << QPointF(20, -19);
QTest::newRow("Down") << SwipeDirection::Down << QPointF(0, 50);
}
void GestureTest::testSwipeUpdateTrigger()
{
GestureRecognizer recognizer;
SwipeGesture gesture;
QFETCH(SwipeGesture::Direction, direction);
QFETCH(SwipeDirection, direction);
gesture.setDirection(direction);
QSignalSpy triggeredSpy(&gesture, &SwipeGesture::triggered);
@ -591,11 +594,11 @@ void GestureTest::testNotEmitCallbacksBeforeDirectionDecided()
SwipeGesture right;
PinchGesture expand;
PinchGesture contract;
up.setDirection(SwipeGesture::Direction::Up);
down.setDirection(SwipeGesture::Direction::Down);
right.setDirection(SwipeGesture::Direction::Right);
expand.setDirection(PinchGesture::Direction::Expanding);
contract.setDirection(PinchGesture::Direction::Contracting);
up.setDirection(SwipeDirection::Up);
down.setDirection(SwipeDirection::Down);
right.setDirection(SwipeDirection::Right);
expand.setDirection(PinchDirection::Expanding);
contract.setDirection(PinchDirection::Contracting);
recognizer.registerSwipeGesture(&up);
recognizer.registerSwipeGesture(&down);
recognizer.registerSwipeGesture(&right);

View file

@ -48,11 +48,11 @@ qreal SwipeGesture::deltaToProgress(const QPointF &delta) const
}
switch (m_direction) {
case Direction::Up:
case Direction::Down:
case SwipeDirection::Up:
case SwipeDirection::Down:
return std::min(std::abs(delta.y()) / std::abs(m_minimumDelta.y()), 1.0);
case Direction::Left:
case Direction::Right:
case SwipeDirection::Left:
case SwipeDirection::Right:
return std::min(std::abs(delta.x()) / std::abs(m_minimumDelta.x()), 1.0);
default:
Q_UNREACHABLE();
@ -173,18 +173,20 @@ int GestureRecognizer::startSwipeGesture(uint fingerCount, const QPointF &startP
// Only add gestures who's direction aligns with current swipe axis
switch (gesture->direction()) {
case SwipeGesture::Direction::Up:
case SwipeGesture::Direction::Down:
case SwipeDirection::Up:
case SwipeDirection::Down:
if (m_currentSwipeAxis == Axis::Horizontal) {
continue;
}
break;
case SwipeGesture::Direction::Left:
case SwipeGesture::Direction::Right:
case SwipeDirection::Left:
case SwipeDirection::Right:
if (m_currentSwipeAxis == Axis::Vertical) {
continue;
}
break;
case SwipeDirection::Invalid:
Q_UNREACHABLE();
}
m_activeSwipeGestures << gesture;
@ -198,17 +200,17 @@ void GestureRecognizer::updateSwipeGesture(const QPointF &delta)
{
m_currentDelta += delta;
SwipeGesture::Direction direction; // Overall direction
SwipeDirection direction; // Overall direction
Axis swipeAxis;
// Pick an axis for gestures so horizontal ones don't change to vertical ones without lifting fingers
if (m_currentSwipeAxis == Axis::None) {
if (std::abs(m_currentDelta.x()) >= std::abs(m_currentDelta.y())) {
swipeAxis = Axis::Horizontal;
direction = m_currentDelta.x() < 0 ? SwipeGesture::Direction::Left : SwipeGesture::Direction::Right;
direction = m_currentDelta.x() < 0 ? SwipeDirection::Left : SwipeDirection::Right;
} else {
swipeAxis = Axis::Vertical;
direction = m_currentDelta.y() < 0 ? SwipeGesture::Direction::Up : SwipeGesture::Direction::Down;
direction = m_currentDelta.y() < 0 ? SwipeDirection::Up : SwipeDirection::Down;
}
if (std::abs(m_currentDelta.x()) >= 5 || std::abs(m_currentDelta.y()) >= 5) {
// only lock in a direction if the delta is big enough
@ -222,10 +224,10 @@ void GestureRecognizer::updateSwipeGesture(const QPointF &delta)
// Find the current swipe direction
switch (swipeAxis) {
case Axis::Vertical:
direction = m_currentDelta.y() < 0 ? SwipeGesture::Direction::Up : SwipeGesture::Direction::Down;
direction = m_currentDelta.y() < 0 ? SwipeDirection::Up : SwipeDirection::Down;
break;
case Axis::Horizontal:
direction = m_currentDelta.x() < 0 ? SwipeGesture::Direction::Left : SwipeGesture::Direction::Right;
direction = m_currentDelta.x() < 0 ? SwipeDirection::Left : SwipeDirection::Right;
break;
default:
Q_UNREACHABLE();
@ -332,11 +334,11 @@ void GestureRecognizer::updatePinchGesture(qreal scale, qreal angleDelta, const
m_currentScale = scale;
// Determine the direction of the swipe
PinchGesture::Direction direction;
PinchDirection direction;
if (scale < 1) {
direction = PinchGesture::Direction::Contracting;
direction = PinchDirection::Contracting;
} else {
direction = PinchGesture::Direction::Expanding;
direction = PinchDirection::Expanding;
}
// Eliminate wrong gestures (takes two iterations)
@ -418,12 +420,12 @@ uint SwipeGesture::maximumFingerCount() const
return m_maximumFingerCount;
}
SwipeGesture::Direction SwipeGesture::direction() const
SwipeDirection SwipeGesture::direction() const
{
return m_direction;
}
void SwipeGesture::setDirection(Direction direction)
void SwipeGesture::setDirection(SwipeDirection direction)
{
m_direction = direction;
}
@ -540,12 +542,12 @@ uint PinchGesture::maximumFingerCount() const
return m_maximumFingerCount;
}
PinchGesture::Direction PinchGesture::direction() const
PinchDirection PinchGesture::direction() const
{
return m_direction;
}
void PinchGesture::setDirection(Direction direction)
void PinchGesture::setDirection(PinchDirection direction)
{
m_direction = direction;
}

View file

@ -8,6 +8,7 @@
*/
#pragma once
#include "kwinglobals.h"
#include <kwin_export.h>
#include <QMap>
@ -53,13 +54,6 @@ class SwipeGesture : public Gesture
{
Q_OBJECT
public:
enum class Direction {
Down,
Left,
Up,
Right,
};
explicit SwipeGesture(QObject *parent = nullptr);
~SwipeGesture() override;
@ -71,8 +65,8 @@ public:
void setMaximumFingerCount(uint count);
uint maximumFingerCount() const;
Direction direction() const;
void setDirection(Direction direction);
SwipeDirection direction() const;
void setDirection(SwipeDirection direction);
void setMinimumX(int x);
int minimumX() const;
@ -113,7 +107,7 @@ private:
uint m_minimumFingerCount = 0;
bool m_maximumFingerCountRelevant = false;
uint m_maximumFingerCount = 0;
Direction m_direction = Direction::Down;
SwipeDirection m_direction = SwipeDirection::Down;
bool m_minimumXRelevant = false;
int m_minimumX = 0;
bool m_minimumYRelevant = false;
@ -130,11 +124,6 @@ class PinchGesture : public Gesture
{
Q_OBJECT
public:
enum class Direction {
Expanding,
Contracting,
};
explicit PinchGesture(QObject *parent = nullptr);
~PinchGesture() override;
@ -146,8 +135,8 @@ public:
void setMaximumFingerCount(uint count);
uint maximumFingerCount() const;
Direction direction() const;
void setDirection(Direction direction);
PinchDirection direction() const;
void setDirection(PinchDirection direction);
qreal minimumScaleDelta() const;
@ -173,7 +162,7 @@ private:
uint m_minimumFingerCount = 0;
bool m_maximumFingerCountRelevant = false;
uint m_maximumFingerCount = 0;
Direction m_direction = Direction::Expanding;
PinchDirection m_direction = PinchDirection::Expanding;
bool m_minimumScaleDeltaRelevant = false;
qreal m_minimumScaleDelta = DEFAULT_UNIT_SCALE_DELTA;
};
@ -227,6 +216,3 @@ private:
};
}
Q_DECLARE_METATYPE(KWin::SwipeGesture::Direction)
Q_DECLARE_METATYPE(KWin::PinchGesture::Direction)

View file

@ -30,18 +30,9 @@ GlobalShortcut::GlobalShortcut(Shortcut &&sc, QAction *action)
: m_shortcut(sc)
, m_action(action)
{
static const QMap<SwipeDirection, SwipeGesture::Direction> swipeDirs = {
{SwipeDirection::Up, SwipeGesture::Direction::Up},
{SwipeDirection::Down, SwipeGesture::Direction::Down},
{SwipeDirection::Left, SwipeGesture::Direction::Left},
{SwipeDirection::Right, SwipeGesture::Direction::Right},
};
static const QMap<PinchDirection, PinchGesture::Direction> pinchDirs = {
{PinchDirection::Expanding, PinchGesture::Direction::Expanding},
{PinchDirection::Contracting, PinchGesture::Direction::Contracting}};
if (auto swipeGesture = std::get_if<RealtimeFeedbackSwipeShortcut>(&sc)) {
m_swipeGesture = std::make_unique<SwipeGesture>();
m_swipeGesture->setDirection(swipeDirs[swipeGesture->direction]);
m_swipeGesture->setDirection(swipeGesture->direction);
m_swipeGesture->setMinimumDelta(QPointF(200, 200));
m_swipeGesture->setMaximumFingerCount(swipeGesture->fingerCount);
m_swipeGesture->setMinimumFingerCount(swipeGesture->fingerCount);
@ -52,7 +43,7 @@ GlobalShortcut::GlobalShortcut(Shortcut &&sc, QAction *action)
}
} else if (auto pinchGesture = std::get_if<RealtimeFeedbackPinchShortcut>(&sc)) {
m_pinchGesture = std::make_unique<PinchGesture>();
m_pinchGesture->setDirection(pinchDirs[pinchGesture->direction]);
m_pinchGesture->setDirection(pinchGesture->direction);
m_pinchGesture->setMaximumFingerCount(pinchGesture->fingerCount);
m_pinchGesture->setMinimumFingerCount(pinchGesture->fingerCount);
QObject::connect(m_pinchGesture.get(), &PinchGesture::triggered, m_action, &QAction::trigger, Qt::QueuedConnection);

View file

@ -709,16 +709,16 @@ void Edge::setBorder(ElectricBorder border)
m_border = border;
switch (m_border) {
case ElectricTop:
m_gesture->setDirection(SwipeGesture::Direction::Down);
m_gesture->setDirection(SwipeDirection::Down);
break;
case ElectricRight:
m_gesture->setDirection(SwipeGesture::Direction::Left);
m_gesture->setDirection(SwipeDirection::Left);
break;
case ElectricBottom:
m_gesture->setDirection(SwipeGesture::Direction::Up);
m_gesture->setDirection(SwipeDirection::Up);
break;
case ElectricLeft:
m_gesture->setDirection(SwipeGesture::Direction::Right);
m_gesture->setDirection(SwipeDirection::Right);
break;
default:
break;