From 059f66af9a01596556da750da6be51aed32b3c40 Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Mon, 24 Jun 2024 09:52:36 +0300 Subject: [PATCH] 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. --- src/globalshortcuts.cpp | 15 +++++++++ src/globalshortcuts.h | 3 ++ src/scripting/gesturehandler.cpp | 53 +++++++++++++++++++------------- src/scripting/gesturehandler.h | 13 +++++--- 4 files changed, 58 insertions(+), 26 deletions(-) diff --git a/src/globalshortcuts.cpp b/src/globalshortcuts.cpp index 1aad983b46..2a65ae65f9 100644 --- a/src/globalshortcuts.cpp +++ b/src/globalshortcuts.cpp @@ -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 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 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 progressCallback) { GlobalShortcut shortcut{RealtimeFeedbackSwipeShortcut{DeviceType::Touchscreen, direction, progressCallback, fingerCount}, action}; diff --git a/src/globalshortcuts.h b/src/globalshortcuts.h index db3db30b7c..9fdea26473 100644 --- a/src/globalshortcuts.h +++ b/src/globalshortcuts.h @@ -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 progressCallback = {}); + void registerTouchpadSwipe(SwipeGesture *swipeGesture); void registerTouchpadPinch(PinchDirection direction, uint32_t fingerCount, QAction *action, std::function progressCallback = {}); + void registerTouchpadPinch(PinchGesture *pinchGesture); void registerTouchscreenSwipe(SwipeDirection direction, uint32_t fingerCount, QAction *action, std::function progressCallback = {}); + void registerTouchscreenSwipe(SwipeGesture *swipeGesture); void forceRegisterTouchscreenSwipe(SwipeDirection direction, uint32_t fingerCount, QAction *action, std::function progressCallback = {}); /** diff --git a/src/scripting/gesturehandler.cpp b/src/scripting/gesturehandler.cpp index 22a4596f31..6dc018f3c0 100644 --- a/src/scripting/gesturehandler.cpp +++ b/src/scripting/gesturehandler.cpp @@ -5,11 +5,9 @@ */ #include "gesturehandler.h" -#include "effect/effecthandler.h" - -#include - -#include +#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(); + 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 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(); + 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 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 diff --git a/src/scripting/gesturehandler.h b/src/scripting/gesturehandler.h index dd551340b5..2a84d0e163 100644 --- a/src/scripting/gesturehandler.h +++ b/src/scripting/gesturehandler.h @@ -6,8 +6,6 @@ #pragma once -#include "effect/globals.h" - #include 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 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 m_gesture; Direction m_direction = Direction::Contracting; Device m_deviceType = Device::Touchpad; qreal m_progress = 0;