scripting: Port gesture handlers to native gesture apis

If a SwipeGesture or a PinchGesture is cancelled, the associated QAction
is going to be triggered. It is against the expectations of the gesture
handlers.

In order to address that, this ports the gesture handlers to the native
gesture apis, which expose whether the gesture has been started or cancelled
better.
This commit is contained in:
Vlad Zahorodnii 2024-06-24 09:52:36 +03:00
parent c958a75e2b
commit 059f66af9a
4 changed files with 58 additions and 26 deletions

View file

@ -148,16 +148,31 @@ void GlobalShortcutsManager::registerTouchpadSwipe(SwipeDirection direction, uin
add(GlobalShortcut(RealtimeFeedbackSwipeShortcut{DeviceType::Touchpad, direction, progressCallback, fingerCount}, action), DeviceType::Touchpad);
}
void GlobalShortcutsManager::registerTouchpadSwipe(SwipeGesture *swipeGesture)
{
m_touchpadGestureRecognizer->registerSwipeGesture(swipeGesture);
}
void GlobalShortcutsManager::registerTouchpadPinch(PinchDirection direction, uint32_t fingerCount, QAction *action, std::function<void(qreal)> progressCallback)
{
add(GlobalShortcut(RealtimeFeedbackPinchShortcut{direction, progressCallback, fingerCount}, action), DeviceType::Touchpad);
}
void GlobalShortcutsManager::registerTouchpadPinch(PinchGesture *pinchGesture)
{
m_touchpadGestureRecognizer->registerPinchGesture(pinchGesture);
}
void GlobalShortcutsManager::registerTouchscreenSwipe(SwipeDirection direction, uint32_t fingerCount, QAction *action, std::function<void(qreal)> progressCallback)
{
add(GlobalShortcut(RealtimeFeedbackSwipeShortcut{DeviceType::Touchscreen, direction, progressCallback, fingerCount}, action), DeviceType::Touchscreen);
}
void GlobalShortcutsManager::registerTouchscreenSwipe(SwipeGesture *swipeGesture)
{
m_touchscreenGestureRecognizer->registerSwipeGesture(swipeGesture);
}
void GlobalShortcutsManager::forceRegisterTouchscreenSwipe(SwipeDirection direction, uint32_t fingerCount, QAction *action, std::function<void(qreal)> progressCallback)
{
GlobalShortcut shortcut{RealtimeFeedbackSwipeShortcut{DeviceType::Touchscreen, direction, progressCallback, fingerCount}, action};

View file

@ -65,8 +65,11 @@ public:
void registerAxisShortcut(QAction *action, Qt::KeyboardModifiers modifiers, PointerAxisDirection axis);
void registerTouchpadSwipe(SwipeDirection direction, uint32_t fingerCount, QAction *action, std::function<void(qreal)> progressCallback = {});
void registerTouchpadSwipe(SwipeGesture *swipeGesture);
void registerTouchpadPinch(PinchDirection direction, uint32_t fingerCount, QAction *action, std::function<void(qreal)> progressCallback = {});
void registerTouchpadPinch(PinchGesture *pinchGesture);
void registerTouchscreenSwipe(SwipeDirection direction, uint32_t fingerCount, QAction *action, std::function<void(qreal)> progressCallback = {});
void registerTouchscreenSwipe(SwipeGesture *swipeGesture);
void forceRegisterTouchscreenSwipe(SwipeDirection direction, uint32_t fingerCount, QAction *action, std::function<void(qreal)> progressCallback = {});
/**

View file

@ -5,11 +5,9 @@
*/
#include "gesturehandler.h"
#include "effect/effecthandler.h"
#include <QAction>
#include <functional>
#include "gestures.h"
#include "globalshortcuts.h"
#include "input.h"
namespace KWin
{
@ -19,28 +17,34 @@ SwipeGestureHandler::SwipeGestureHandler(QObject *parent)
{
}
SwipeGestureHandler::~SwipeGestureHandler()
{
}
void SwipeGestureHandler::classBegin()
{
}
void SwipeGestureHandler::componentComplete()
{
m_action = new QAction(this);
connect(m_action, &QAction::triggered, this, &SwipeGestureHandler::activated);
m_gesture = std::make_unique<SwipeGesture>();
m_gesture->setDirection(SwipeDirection(m_direction));
m_gesture->setMinimumDelta(QPointF(200, 200));
m_gesture->setMaximumFingerCount(m_fingerCount);
m_gesture->setMinimumFingerCount(m_fingerCount);
connect(m_gesture.get(), &SwipeGesture::triggered, this, &SwipeGestureHandler::activated);
connect(m_gesture.get(), &SwipeGesture::cancelled, this, &SwipeGestureHandler::cancelled);
connect(m_gesture.get(), &SwipeGesture::progress, this, &SwipeGestureHandler::setProgress);
std::function<void (EffectsHandler *, SwipeDirection dir, uint fingerCount, QAction *onUp, std::function<void(qreal)> progressCallback)> registrator;
switch (m_deviceType) {
case Device::Touchpad:
registrator = std::mem_fn(&EffectsHandler::registerTouchpadSwipeShortcut);
input()->shortcuts()->registerTouchpadSwipe(m_gesture.get());
break;
case Device::Touchscreen:
registrator = std::mem_fn(&EffectsHandler::registerTouchscreenSwipeShortcut);
input()->shortcuts()->registerTouchscreenSwipe(m_gesture.get());
break;
}
registrator(effects, SwipeDirection(m_direction), m_fingerCount, m_action, [this](qreal progress) {
setProgress(progress);
});
}
SwipeGestureHandler::Direction SwipeGestureHandler::direction() const
@ -100,25 +104,30 @@ PinchGestureHandler::PinchGestureHandler(QObject *parent)
{
}
PinchGestureHandler::~PinchGestureHandler()
{
}
void PinchGestureHandler::classBegin()
{
}
void PinchGestureHandler::componentComplete()
{
m_action = new QAction(this);
connect(m_action, &QAction::triggered, this, &PinchGestureHandler::activated);
m_gesture = std::make_unique<PinchGesture>();
m_gesture->setDirection(PinchDirection(m_direction));
m_gesture->setMaximumFingerCount(m_fingerCount);
m_gesture->setMinimumFingerCount(m_fingerCount);
connect(m_gesture.get(), &PinchGesture::triggered, this, &PinchGestureHandler::activated);
connect(m_gesture.get(), &PinchGesture::cancelled, this, &PinchGestureHandler::cancelled);
connect(m_gesture.get(), &PinchGesture::progress, this, &PinchGestureHandler::setProgress);
std::function<void (EffectsHandler *, PinchDirection dir, uint fingerCount, QAction *onUp, std::function<void(qreal)> progressCallback)> registrator;
switch (m_deviceType) {
case Device::Touchpad:
registrator = std::mem_fn(&EffectsHandler::registerTouchpadPinchShortcut);
input()->shortcuts()->registerTouchpadPinch(m_gesture.get());
break;
}
registrator(effects, PinchDirection(m_direction), m_fingerCount, m_action, [this](qreal progress) {
setProgress(progress);
});
}
PinchGestureHandler::Direction PinchGestureHandler::direction() const

View file

@ -6,8 +6,6 @@
#pragma once
#include "effect/globals.h"
#include <QQmlParserStatus>
class QAction;
@ -15,6 +13,9 @@ class QAction;
namespace KWin
{
class PinchGesture;
class SwipeGesture;
/**
* The SwipeGestureHandler type provides a way to handle global swipe gestures.
*
@ -39,6 +40,7 @@ class SwipeGestureHandler : public QObject, public QQmlParserStatus
public:
explicit SwipeGestureHandler(QObject *parent = nullptr);
~SwipeGestureHandler() override;
// Matches SwipeDirection.
enum class Direction {
@ -73,13 +75,14 @@ public:
Q_SIGNALS:
void activated();
void cancelled();
void progressChanged();
void directionChanged();
void fingerCountChanged();
void deviceTypeChanged();
private:
QAction *m_action = nullptr;
std::unique_ptr<SwipeGesture> m_gesture;
Direction m_direction = Direction::Invalid;
Device m_deviceType = Device::Touchpad;
qreal m_progress = 0;
@ -109,6 +112,7 @@ class PinchGestureHandler : public QObject, public QQmlParserStatus
public:
explicit PinchGestureHandler(QObject *parent = nullptr);
~PinchGestureHandler() override;
// Matches PinchDirection.
enum class Direction {
@ -139,13 +143,14 @@ public:
Q_SIGNALS:
void activated();
void cancelled();
void progressChanged();
void directionChanged();
void fingerCountChanged();
void deviceTypeChanged();
private:
QAction *m_action = nullptr;
std::unique_ptr<PinchGesture> m_gesture;
Direction m_direction = Direction::Contracting;
Device m_deviceType = Device::Touchpad;
qreal m_progress = 0;