Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

draft: move the touch-hold gestures into KisShortcutsMatcher

Dmitry Kazakovae81b7596791
diff --git a/libs/ui/CMakeLists.txt b/libs/ui/CMakeLists.txt
index c674ab9595..d158b36681 100644
--- a/libs/ui/CMakeLists.txt
+++ b/libs/ui/CMakeLists.txt
@@ -369,6 +369,7 @@ set(kritaui_LIB_SRCS
input/kis_zoom_and_rotate_action.cpp
input/KisCanvasOnlyAction.cpp
input/KisTouchGestureAction.cpp
+ input/KisTouchHoldEventsPostponer.cpp
input/KisInputProfileMigrator.cpp
operations/kis_operation.cpp
diff --git a/libs/ui/input/KisTouchHoldEventsPostponer.cpp b/libs/ui/input/KisTouchHoldEventsPostponer.cpp
new file mode 100644
index 0000000000..e27a897cb5
--- /dev/null
+++ b/libs/ui/input/KisTouchHoldEventsPostponer.cpp
@@ -0,0 +1,106 @@
+/*
+ * SPDX-FileCopyrightText: 2026 Dmitry Kazakov <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "KisTouchHoldEventsPostponer.h"
+
+#include <QTimer>
+
+#include <kis_assert.h>
+#include <input/kis_touch_shortcut.h>
+
+#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
+#include <KoPointerEvent.h>
+#else
+#include <QTouchEvent>
+#endif
+
+
+KisTouchHoldEventsPostponer::KisTouchHoldEventsPostponer(qreal maxHoldDistance, int holdTimeout)
+ : m_maxHoldDistance(maxHoldDistance)
+{
+ connect(&m_timer, &QTimer::timeout, this, &KisTouchHoldEventsPostponer::slotHoldCompletionTimeout);
+
+ m_timer.setSingleShot(true);
+ m_timer.setTimerType(Qt::CoarseTimer);
+ m_timer.setInterval(holdTimeout);
+ m_timer.start();
+}
+
+KisTouchHoldEventsPostponer::~KisTouchHoldEventsPostponer()
+{
+ qDeleteAll(m_postponedEvents);
+}
+
+KisTouchHoldEventsPostponer::State KisTouchHoldEventsPostponer::state() const
+{
+ return m_state;
+}
+
+void KisTouchHoldEventsPostponer::pushThrough(QTouchEvent *event)
+{
+ KIS_SAFE_ASSERT_RECOVER_RETURN(m_state == WaitingForHold);
+
+ if (qualifyForPostponing(event)) {
+ enqueueEvent(event);
+ } else {
+ // enqueue the event to make sure it is delivered via
+ // the "deliver all the postponed events" mechanism
+ enqueueEvent(event);
+ cancelHoldWait();
+ }
+}
+
+void KisTouchHoldEventsPostponer::cancelHoldWait()
+{
+ m_state = HoldCancelled;
+ m_timer.stop();
+}
+
+QVector<QTouchEvent*> KisTouchHoldEventsPostponer::postponedEvents()
+{
+ KIS_SAFE_ASSERT_RECOVER_NOOP(m_state != WaitingForHold);
+ return m_postponedEvents;
+}
+
+void KisTouchHoldEventsPostponer::slotHoldCompletionTimeout()
+{
+ // it might happen that the timer event has been emitted before
+ // a cancellation touch event was delivered, then the timer event
+ // may come "late"
+ if (m_state != WaitingForHold) {
+ // the caller should have processed all the postponed events in this case
+ KIS_SAFE_ASSERT_RECOVER_NOOP(m_postponedEvents.isEmpty());
+ return;
+ }
+
+ m_state = HoldCompleted;
+ Q_EMIT sigHoldCompleted();
+}
+
+bool KisTouchHoldEventsPostponer::qualifyForPostponing(QTouchEvent *event)
+{
+ const int numPressedPoints = KisTouchShortcut::countTouchPoints(event, KisTouchShortcut::pressedOnlyTouchStates());
+ if (numPressedPoints > 1)
+ return false;
+
+ const qreal pressDistance = KisTouchShortcut::touchDragDistance(event, KisTouchShortcut::pressedOnlyTouchStates());
+ if (pressDistance > m_maxHoldDistance)
+ return false;
+
+ return true;
+}
+
+void KisTouchHoldEventsPostponer::enqueueEvent(QTouchEvent *event)
+{
+#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
+ // TODO: change QScopedPointer -> std::unique_ptr
+ QScopedPointer<QEvent> clonedTouchEvent;
+ KoPointerEvent::copyQtPointerEvent(event, clonedTouchEvent);
+ m_postponedEvents.append(static_cast<QTouchEvent *>(clonedTouchEvent.take()));
+#else
+ m_postponedEvents.append(static_cast<QTouchEvent *>(event->clone()));
+#endif
+}
diff --git a/libs/ui/input/KisTouchHoldEventsPostponer.h b/libs/ui/input/KisTouchHoldEventsPostponer.h
new file mode 100644
index 0000000000..4b1b7ec164
--- /dev/null
+++ b/libs/ui/input/KisTouchHoldEventsPostponer.h
@@ -0,0 +1,128 @@
+/*
+ * SPDX-FileCopyrightText: 2026 Dmitry Kazakov <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef KISTOUCHHOLDEVENTSPOSTPONER_H
+#define KISTOUCHHOLDEVENTSPOSTPONER_H
+
+#include <QTimer>
+#include <kritaui_export.h>
+
+class QTouchEvent;
+
+/**
+ * The object to store and manage postponed events while waiting for
+ * a touch-hold shortcut to trigger. Currently, it supports single-
+ * finger touch actions only.
+ *
+ * Usage:
+ *
+ * \code{.cpp}
+ *
+ * // in the class
+ * std::optional<KisTouchHoldEventsPostponer> postponer;
+ *
+ * // on action start
+ * postponer.emplace(20, 1000);
+ *
+ * // on any touch event (including the first)
+ * if (postponer) {
+ * postponer->pushThrough(event);
+ * if (postponer->state() == KisTouchHoldEventsPostponer::HoldCancelled) {
+ * Q_FOREACH (QTouchEvent *event, postponer->postponedEvents()) {
+ * // ... deliver the event ...
+ * }
+ * postponer.reset();
+ * }
+ * }
+ *
+ * // on sigHoldCompleted()
+ * QTouchEvent *tiggeringEvent = postponer->postponedEvents().front();
+ * // ... start operation using the very first as the rule ...
+ *
+ * \endcode
+ */
+class KRITAUI_EXPORT KisTouchHoldEventsPostponer : public QObject
+{
+ Q_OBJECT
+public:
+ enum State {
+ WaitingForHold, /// the postponer is still accumulating the events
+ HoldCompleted, /// the hold action has been detected, the accumulated events should be dropped and the hold action should be started
+ HoldCancelled /// hold action has been cancelled, the accumulated events should be fired to the consumers
+ };
+public:
+ /**
+ * Createa touch-hold postponer with \p maxHoldDistance used
+ * as a threshold for hold/drag and \p holdTimeout as a timeout
+ * to consider the hold operation completed
+ */
+ KisTouchHoldEventsPostponer(qreal maxHoldDistance, int holdTimeout);
+ KisTouchHoldEventsPostponer(const KisTouchHoldEventsPostponer &rhs) = delete;
+ ~KisTouchHoldEventsPostponer();
+
+ /**
+ * \return the current state of the postponer
+ */
+ State state() const;
+
+ /**
+ * Push a new event through the postponer. The method should be called in
+ * `WaitingForHold` state only. In all other states the postponer is not
+ * considered as active anymore.
+ *
+ * If \p event does not qualify as an event for touch-hold, then the state
+ * of the postponer will be switched into `HoldCancelled`, which should be
+ * handled by the called on exiting pushThrough().
+ *
+ * Note: the \p event that caused the state transition into `HoldCancelled`
+ * state will still be added to the queue, so that it could be managed
+ * uniformely with the rest of the postponed events.
+ */
+ void pushThrough(QTouchEvent *event);
+
+ /**
+ * Cancel waiting for a touch hold. The state will transition into `HoldCancelled`
+ */
+ void cancelHoldWait();
+
+ /**
+ * \return the clones of all the postponed events, potentially including the one
+ * that caused the cancellation of the hold-wait. The postponer **owns** all the
+ * postponed events.
+ */
+ QVector<QTouchEvent*> postponedEvents();
+
+Q_SIGNALS:
+ /**
+ * Emitted when the postponed has detected a touch-and-hold action
+ */
+ void sigHoldCompleted();
+
+private Q_SLOTS:
+ void slotHoldCompletionTimeout();
+
+private:
+ /**
+ * \return if \p event can be considered as a touch-hold event
+ *
+ * It is currently hardcoded to support only single-finger touch-holds.
+ */
+ bool qualifyForPostponing(QTouchEvent *event);
+
+ /**
+ * Make a clone of the event and add to the queue. Can be removed after
+ * we stop supporting Qt5.
+ */
+ void enqueueEvent(QTouchEvent *event);
+
+private:
+ QTimer m_timer;
+ State m_state { WaitingForHold };
+ QVector<QTouchEvent*> m_postponedEvents;
+ qreal m_maxHoldDistance {20.0} ;
+};
+
+#endif // KISTOUCHHOLDEVENTSPOSTPONER_H
diff --git a/libs/ui/input/kis_input_manager.cpp b/libs/ui/input/kis_input_manager.cpp
index 506f21b474..30229410a6 100644
--- a/libs/ui/input/kis_input_manager.cpp
+++ b/libs/ui/input/kis_input_manager.cpp
@@ -129,17 +129,6 @@ void KisInputManager::slotConfigChanged()
#endif
}
-void KisInputManager::slotTouchHoldTriggered()
-{
- d->cancelTouchHoldTimer();
- d->clearBufferedTouchEvents();
- KIS_SAFE_ASSERT_RECOVER_RETURN(d->originatingTouchBeginEvent);
- if (d->matcher.touchHoldBeginEvent(static_cast<QTouchEvent *>(d->originatingTouchBeginEvent.data()))) {
- d->touchHasBlockedPressEvents = true;
- }
-}
-
-
void KisInputManager::toggleTabletLogger()
{
KisTabletDebugger::instance()->toggleDebugging();
@@ -716,26 +705,8 @@ bool KisInputManager::eventFilterImpl(QEvent * event)
d->lastPointCount = touchEvent->touchPoints().size();
d->startingPos = touchEvent->touchPoints().at(0).pos();
d->previousPos = d->startingPos;
- // we don't want to lose this event
-#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
- KoPointerEvent::copyQtPointerEvent(touchEvent, d->originatingTouchBeginEvent);
-#else
- d->originatingTouchBeginEvent.reset(touchEvent->clone());
-#endif
- // In the face of a touch and hold shortcut being present, we need
- // to disambiguate whether this is a touch being held or the user
- // is doing something else. For that purpose, we start buffering
- // received touch events until we can actually make a decision.
- d->clearBufferedTouchEvents();
- if (d->matcher.hasTouchHoldShortcut() && touchEvent->touchPoints().length() == 1) {
- d->bufferTouchEvent(touchEvent);
- d->restartTouchHoldTimer();
- retval = true;
- } else {
- d->cancelTouchHoldTimer();
- retval = handleTouchBegin(touchEvent);
- }
+ retval = d->matcher.touchBeginEvent(touchEvent);
d->resetCompressor();
event->accept();
@@ -765,16 +736,13 @@ bool KisInputManager::eventFilterImpl(QEvent * event)
if (count < 2 && eventPointCount > count) {
d->touchHasBlockedPressEvents = false;
- d->cancelTouchHoldTimer();
retval = d->matcher.touchEndEvent(touchEvent);
} else {
#endif
- // Touch hold shortcuts need to buffer events, see TouchBegin.
- if (touchHoldBufferUpdate(touchEvent)) {
- retval = true; // Event was buffered.
- } else {
- retval = handleTouchUpdate(touchEvent);
- }
+ KisAbstractInputAction::setInputManager(this);
+ retval = d->matcher.touchUpdateEvent(touchEvent);
+ d->touchHasBlockedPressEvents = retval;
+
#ifdef Q_OS_MACOS
}
#endif
@@ -785,9 +753,6 @@ bool KisInputManager::eventFilterImpl(QEvent * event)
case QEvent::TouchEnd:
{
- d->cancelTouchHoldTimer();
- d->flushBufferedTouchEvents();
-
if (d->popupWasActive) {
event->setAccepted(true);
return true;
@@ -817,13 +782,6 @@ bool KisInputManager::eventFilterImpl(QEvent * event)
bool ignoreCancel = false;
#endif
- d->cancelTouchHoldTimer();
- if (ignoreCancel) {
- d->flushBufferedTouchEvents();
- } else {
- d->clearBufferedTouchEvents();
- }
-
if (d->popupWasActive) {
event->setAccepted(true);
return true;
@@ -904,33 +862,6 @@ void KisInputManager::endTouch()
d->touchHasBlockedPressEvents = false;
}
-bool KisInputManager::touchHoldBufferUpdate(QTouchEvent *touchEvent)
-{
- if (d->isPendingTouchHold()) {
- if (touchEvent->touchPoints().length() == 1 && d->isWithinTouchHoldSlopRange(touchEvent->touchPoints().at(0).pos())) {
- d->bufferTouchEvent(touchEvent);
- return true;
- } else {
- d->cancelTouchHoldTimer();
- d->flushBufferedTouchEvents();
- }
- }
- return false;
-}
-
-bool KisInputManager::handleTouchBegin(QTouchEvent *touchEvent)
-{
- return d->matcher.touchBeginEvent(touchEvent);
-}
-
-bool KisInputManager::handleTouchUpdate(QTouchEvent *touchEvent)
-{
- KisAbstractInputAction::setInputManager(this);
- bool retval = d->matcher.touchUpdateEvent(touchEvent);
- d->touchHasBlockedPressEvents = retval;
- return retval;
-}
-
void KisInputManager::slotCompressedMoveEvent()
{
if (d->compressedMoveEvent) {
diff --git a/libs/ui/input/kis_input_manager.h b/libs/ui/input/kis_input_manager.h
index 89990cace5..38de484526 100644
--- a/libs/ui/input/kis_input_manager.h
+++ b/libs/ui/input/kis_input_manager.h
@@ -102,16 +102,10 @@ private Q_SLOTS:
void slotCompressedMoveEvent();
void deregisterPopupWidget();
void slotConfigChanged();
- void slotTouchHoldTriggered();
private:
bool startTouch(bool &retval);
void endTouch();
- bool touchHoldBufferUpdate(QTouchEvent *touchEvent);
-
- // Handlers for events that may have been buffered for a touch hold.
- bool handleTouchBegin(QTouchEvent *touchEvent);
- bool handleTouchUpdate(QTouchEvent *touchEvent);
bool eventFilterImpl(QEvent * event);
template <class Event>
diff --git a/libs/ui/input/kis_input_manager_p.cpp b/libs/ui/input/kis_input_manager_p.cpp
index 0762f6f108..3b551b61eb 100644
--- a/libs/ui/input/kis_input_manager_p.cpp
+++ b/libs/ui/input/kis_input_manager_p.cpp
@@ -204,7 +204,6 @@ KisInputManager::Private::Private(KisInputManager *qq)
KisSignalCompressor::ADDITIVE_INTERVAL)
, priorityEventFilterSeqNo(0)
, popupWidget(nullptr)
- , touchHoldTimer(new QTimer(qq))
, canvasSwitcher(this, qq)
{
KisConfig cfg(true);
@@ -242,11 +241,6 @@ KisInputManager::Private::Private(KisInputManager *qq)
if (qEnvironmentVariableIsSet("KRITA_FIX_UNBALANCED_KEY_EVENTS")) {
useUnbalancedKeyPressEventWorkaround = qEnvironmentVariableIntValue("KRITA_FIX_UNBALANCED_KEY_EVENTS");
}
-
- touchHoldTimer->setTimerType(Qt::CoarseTimer);
- touchHoldTimer->setSingleShot(true);
- touchHoldTimer->setInterval(TOUCH_HOLD_DELAY_MS);
- connect(touchHoldTimer, &QTimer::timeout, qq, &KisInputManager::slotTouchHoldTriggered);
}
static const int InputWidgetsThreshold = 2000;
@@ -722,66 +716,6 @@ void KisInputManager::Private::stopBlockingTouch()
eventEater.stopBlockingTouch();
}
-void KisInputManager::Private::restartTouchHoldTimer()
-{
- touchHoldTimer->start();
-}
-
-void KisInputManager::Private::cancelTouchHoldTimer()
-{
- touchHoldTimer->stop();
-}
-
-bool KisInputManager::Private::isPendingTouchHold() const
-{
- return touchHoldTimer->isActive();
-}
-
-bool KisInputManager::Private::isWithinTouchHoldSlopRange(const QPointF &currentPos) const
-{
- QPointF d = startingPos - currentPos;
- qreal distanceSquared = (d.x() * d.x()) + (d.y() * d.y());
- return distanceSquared <= KisShortcutMatcher::TOUCH_SLOP_SQUARED;
-}
-
-void KisInputManager::Private::bufferTouchEvent(QTouchEvent *touchEvent)
-{
-#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
- QScopedPointer<QEvent> dst;
- KoPointerEvent::copyQtPointerEvent(touchEvent, dst);
- bufferedTouchEvents.append(static_cast<QTouchEvent *>(dst.take()));
-#else
- bufferedTouchEvents.append(touchEvent->clone());
-#endif
-}
-
-void KisInputManager::Private::flushBufferedTouchEvents()
-{
- for (QTouchEvent *touchEvent : bufferedTouchEvents) {
- switch (touchEvent->type()) {
- case QEvent::TouchBegin:
- q->handleTouchBegin(touchEvent);
- break;
- case QEvent::TouchUpdate:
- q->handleTouchUpdate(touchEvent);
- break;
- default:
- qWarning("Unhandled buffered touch event type %d", int(touchEvent->type()));
- break;
- }
- delete touchEvent;
- }
- bufferedTouchEvents.clear();
-}
-
-void KisInputManager::Private::clearBufferedTouchEvents()
-{
- for (QTouchEvent *event : bufferedTouchEvents) {
- delete event;
- }
- bufferedTouchEvents.clear();
-}
-
bool KisInputManager::Private::handleCompressedTabletEvent(QEvent *event)
{
bool retval = false;
diff --git a/libs/ui/input/kis_input_manager_p.h b/libs/ui/input/kis_input_manager_p.h
index f7a9073cbc..bc829c5ae8 100644
--- a/libs/ui/input/kis_input_manager_p.h
+++ b/libs/ui/input/kis_input_manager_p.h
@@ -29,8 +29,6 @@ class KisToolInvocationAction;
class KisInputManager::Private
{
public:
- static constexpr int TOUCH_HOLD_DELAY_MS = 400;
-
Private(KisInputManager *qq);
void addStrokeShortcut(KisAbstractInputAction* action, int index, const QList< Qt::Key >& modifiers, Qt::MouseButtons buttons);
void addKeyShortcut(KisAbstractInputAction* action, int index,const QList<Qt::Key> &keys);
@@ -73,14 +71,12 @@ public:
QPointF startingPos;
QPointF previousPos;
- QScopedPointer<QEvent> originatingTouchBeginEvent;
bool useUnbalancedKeyPressEventWorkaround = false;
bool shouldSynchronizeOnNextKeyPress = false;
KisPopupWidgetInterface *popupWidget;
- QTimer *touchHoldTimer;
QVector<QTouchEvent *> bufferedTouchEvents;
void blockMouseEvents();
@@ -90,13 +86,6 @@ public:
void resetCompressor();
void startBlockingTouch();
void stopBlockingTouch();
- void restartTouchHoldTimer();
- void cancelTouchHoldTimer();
- bool isPendingTouchHold() const;
- bool isWithinTouchHoldSlopRange(const QPointF &currentPos) const;
- void bufferTouchEvent(QTouchEvent *event);
- void flushBufferedTouchEvents();
- void clearBufferedTouchEvents();
template <class Event, bool useBlocking>
void debugEvent(QEvent *event)
diff --git a/libs/ui/input/kis_shortcut_matcher.cpp b/libs/ui/input/kis_shortcut_matcher.cpp
index 062966ca90..98a8274a21 100644
--- a/libs/ui/input/kis_shortcut_matcher.cpp
+++ b/libs/ui/input/kis_shortcut_matcher.cpp
@@ -17,6 +17,7 @@
#include "kis_native_gesture_shortcut.h"
#include "kis_config.h"
#include "kis_extended_modifiers_mapper.h"
+#include "KisTouchHoldEventsPostponer.h"
#include <KoPointerEvent.h>
//#define DEBUG_MATCHER
@@ -82,6 +83,7 @@ public:
KisTouchShortcut *touchShortcut;
KisNativeGestureShortcut *nativeGestureShortcut;
QList<QTouchEvent::TouchPoint> lastTouchPoints;
+ std::optional<KisTouchHoldEventsPostponer> touchHoldEventPostponer;
int maxTouchPoints{0};
int matchingIteration{0};
@@ -409,10 +411,80 @@ bool KisShortcutMatcher::touchBeginEvent( QTouchEvent* event )
m_d->bestCandidateForTapTouchEvent.reset(event->clone());
#endif
+ // TODO: also check if the first point actually qualifies
+ if (hasTouchHoldShortcut()) {
+ //static constexpr int TOUCH_HOLD_DELAY_MS = 400;
+ static constexpr int TOUCH_HOLD_DELAY_MS = 180;
+ m_d->touchHoldEventPostponer.emplace(TOUCH_SLOP, TOUCH_HOLD_DELAY_MS);
+ QObject::connect(&m_d->touchHoldEventPostponer.value(),
+ &KisTouchHoldEventsPostponer::sigHoldCompleted,
+ [this]() {
+ slotTouchHoldCompleted();
+ });
+ m_d->touchHoldEventPostponer->pushThrough(event);
+ if (m_d->touchHoldEventPostponer->state() == KisTouchHoldEventsPostponer::HoldCancelled) {
+ m_d->touchHoldEventPostponer.reset();
+ }
+ }
+
return !notifier.isInRecursion();
}
bool KisShortcutMatcher::touchUpdateEvent(QTouchEvent *event)
+{
+ if (m_d->touchHoldEventPostponer) {
+ m_d->touchHoldEventPostponer->pushThrough(event);
+ if (m_d->touchHoldEventPostponer->state() == KisTouchHoldEventsPostponer::HoldCancelled) {
+ // process all the postponed **update** events,
+ // the very first begin event is only used in
+ // the hold-completion operation
+ Q_FOREACH (QTouchEvent *event, m_d->touchHoldEventPostponer->postponedEvents()) {
+ if (event->type() == QEvent::TouchUpdate) {
+ (void) touchUpdateEventImpl(event);
+ }
+ }
+ m_d->touchHoldEventPostponer.reset();
+ } else {
+ // noop, we are waiting for hold
+ }
+ } else {
+ // TODO: don't enter for hold-drag events
+ return touchUpdateEventImpl(event);
+ }
+
+ return true;
+}
+
+void KisShortcutMatcher::slotTouchHoldCompleted()
+{
+ KIS_SAFE_ASSERT_RECOVER_RETURN(m_d->touchHoldEventPostponer);
+ KIS_SAFE_ASSERT_RECOVER_RETURN(m_d->touchHoldEventPostponer->state() == KisTouchHoldEventsPostponer::HoldCompleted);
+
+ // Can happen when multiple shortcut sources interact
+ KIS_SAFE_ASSERT_RECOVER_RETURN(!m_d->runningShortcut);
+
+ // take the very first begin event to start the action
+ QTouchEvent *event = m_d->touchHoldEventPostponer->postponedEvents().front();
+ KIS_SAFE_ASSERT_RECOVER_RETURN(event->type() == QEvent::TouchBegin);
+
+ m_d->isTouchHeld = true; // Must be set first, used in tryRunTouchShortcut.
+ if (tryRunTouchShortcut(event, KisTouchShortcut::pressedOnlyTouchStates(), TouchShortcutMode::Hold)) {
+ DEBUG_ACTION("touch-hold shortcut found");
+ } else {
+ KIS_SAFE_ASSERT_RECOVER(0 && "should not happen")
+ {
+ // Shouldn't really happen, since KisInputManager checks whether a touch
+ // hold shortcut exists beforehand. We'll just handle this though.
+ DEBUG_ACTION("touch-hold shortcut not found");
+ m_d->isTouchHeld = false;
+ }
+ }
+
+ // remove the postponer to continue processing events in-place
+ m_d->touchHoldEventPostponer.reset();
+}
+
+bool KisShortcutMatcher::touchUpdateEventImpl(QTouchEvent *event)
{
DEBUG_TOUCH_ACTION("entered", event)
@@ -464,6 +536,7 @@ bool KisShortcutMatcher::touchUpdateEvent(QTouchEvent *event)
if (m_d->isTouchDragDetected) {
if (m_d->touchShortcut
+ // TODO: use a different check without threshold! split into match() and matchBegin()
&& !m_d->touchShortcut->matchDragType(event, KisTouchShortcut::pressedOnlyTouchStates())) {
DEBUG_TOUCH_ACTION("ending", event)
// we should end the event as an event with more touchpoints was received
@@ -502,6 +575,21 @@ bool KisShortcutMatcher::touchEndEvent(QTouchEvent *event)
{
Private::RecursionNotifier notifier(this);
+ // flush all the touch-hold postiponed events if they were present
+ if (m_d->touchHoldEventPostponer) {
+ KIS_SAFE_ASSERT_RECOVER_NOOP(m_d->touchHoldEventPostponer->state() == KisTouchHoldEventsPostponer::WaitingForHold);
+ m_d->touchHoldEventPostponer->cancelHoldWait();
+ // process all the postponed **update** events,
+ // the very first begin event is only used in
+ // the hold-completion operation
+ Q_FOREACH (QTouchEvent *event, m_d->touchHoldEventPostponer->postponedEvents()) {
+ if (event->type() == QEvent::TouchUpdate) {
+ (void) touchUpdateEventImpl(event);
+ }
+ }
+ m_d->touchHoldEventPostponer.reset();
+ }
+
m_d->maxTouchPoints = 0;
m_d->isTouchHeld = false;
@@ -531,6 +619,13 @@ void KisShortcutMatcher::touchCancelEvent(QTouchEvent *event)
{
Private::RecursionNotifier notifier(this);
+ // discard all the postponed touch-hold events
+ if (m_d->touchHoldEventPostponer) {
+ KIS_SAFE_ASSERT_RECOVER_NOOP(m_d->touchHoldEventPostponer->state() == KisTouchHoldEventsPostponer::WaitingForHold);
+ m_d->touchHoldEventPostponer->cancelHoldWait();
+ m_d->touchHoldEventPostponer.reset();
+ }
+
m_d->maxTouchPoints = 0;
m_d->isTouchHeld = false;
@@ -570,28 +665,6 @@ void KisShortcutMatcher::touchCancelEvent(QTouchEvent *event)
}
}
-bool KisShortcutMatcher::touchHoldBeginEvent(QTouchEvent *event)
-{
- DEBUG_TOUCH_ACTION("hold", event)
-
- // Can happen when multiple shortcut sources interact.
- if (m_d->runningShortcut) {
- return false;
- }
-
- m_d->isTouchHeld = true; // Must be set first, used in tryRunTouchShortcut.
- if (tryRunTouchShortcut(event, KisTouchShortcut::pressedOnlyTouchStates(), TouchShortcutMode::Hold)) {
- DEBUG_ACTION("touch shortcut found");
- return true;
- } else {
- // Shouldn't really happen, since KisInputManager checks whether a touch
- // hold shortcut exists beforehand. We'll just handle this though.
- DEBUG_ACTION("touch shortcut not found");
- m_d->isTouchHeld = false;
- return touchBeginEvent(event);
- }
-}
-
bool KisShortcutMatcher::nativeGestureBeginEvent(QNativeGestureEvent *event)
{
Q_UNUSED(event);
diff --git a/libs/ui/input/kis_shortcut_matcher.h b/libs/ui/input/kis_shortcut_matcher.h
index a742952cf3..093283457b 100644
--- a/libs/ui/input/kis_shortcut_matcher.h
+++ b/libs/ui/input/kis_shortcut_matcher.h
@@ -187,13 +187,6 @@ public:
*/
void touchCancelEvent(QTouchEvent *event);
- /**
- * Effectively a long-press touch event, generated by KisInputManager
- * when the user touches and holds a spot with one finger. Replaces
- * touchBeginEvent, but other events keep streaming in as normal.
- */
- bool touchHoldBeginEvent(QTouchEvent *event);
-
bool nativeGestureBeginEvent(QNativeGestureEvent *event);
bool nativeGestureEvent(QNativeGestureEvent *event);
bool nativeGestureEndEvent(QNativeGestureEvent *event);
@@ -312,6 +305,9 @@ private:
void forceEndRunningShortcut(const QPointF &localPos);
void forceDeactivateAllActions();
+ bool touchUpdateEventImpl(QTouchEvent *event);
+ void slotTouchHoldCompleted();
+
void setMaxTouchPointEvent(QTouchEvent *event);
bool tryFireTapTouchShortcut(QTouchEvent *event, Qt::TouchPointStates allowedStates);
KisTouchShortcut *matchTouchShortcut(QTouchEvent *event, Qt::TouchPointStates allowedStates, TouchShortcutMode mode);
diff --git a/libs/ui/tests/kis_input_manager_test.cpp b/libs/ui/tests/kis_input_manager_test.cpp
index 4de38cda8b..4f71e5ed9c 100644
--- a/libs/ui/tests/kis_input_manager_test.cpp
+++ b/libs/ui/tests/kis_input_manager_test.cpp
@@ -21,6 +21,8 @@
#include "input/kis_abstract_input_action.h"
#include "input/kis_shortcut_matcher.h"
+#include "input/KisTouchHoldEventsPostponer.h"
+
#include <kis_debug.h>
#include <kis_config.h>
@@ -781,6 +783,44 @@ protected:
}
};
+struct TouchSlowDrag50ms : TouchSequenceGeneratorBase
+{
+ using TouchSequenceGeneratorBase::TouchSequenceGeneratorBase;
+
+protected:
+ void updatePointsSet(int eventIndex, QList<TouchPoint> &touchPoints) override
+ {
+ if (eventIndex == 0) {
+ for (int i = 0; i < m_fingerCount; i++) {
+ appendNewPoint(touchPoints, eventIndex);
+ }
+ }
+
+ if (eventIndex > 0) {
+ QTest::qWait(50);
+ }
+ }
+};
+
+struct TouchHoldFirstFourUpdateEventsFor50ms : TouchSequenceGeneratorBase
+{
+ using TouchSequenceGeneratorBase::TouchSequenceGeneratorBase;
+
+protected:
+ void updatePointsSet(int eventIndex, QList<TouchPoint> &touchPoints) override
+ {
+ if (eventIndex == 0) {
+ for (int i = 0; i < m_fingerCount; i++) {
+ appendNewPoint(touchPoints, eventIndex);
+ }
+ }
+
+ if (eventIndex > 0 && eventIndex < 5) {
+ QTest::qWait(50);
+ }
+ }
+};
+
TouchSequenceGeneratorBase* createTouchSequenceGenerator(const QString sequenceName, int fingerCount, int maxEventIndex, const QPointF pointOffset)
{
if (sequenceName == "touchCleanDrag") {
@@ -795,6 +835,10 @@ TouchSequenceGeneratorBase* createTouchSequenceGenerator(const QString sequenceN
return new TouchDragThreeThenTwo(fingerCount, maxEventIndex, pointOffset);
} else if (sequenceName == "touchDragThreeThenTwoThenThree") {
return new TouchDragThreeThenTwoThenThree(fingerCount, maxEventIndex, pointOffset);
+ } else if (sequenceName == "touchSlowDrag50ms") {
+ return new TouchSlowDrag50ms(fingerCount, maxEventIndex, pointOffset);
+ } else if (sequenceName == "touchHoldFirstFourUpdateEventsFor50ms") {
+ return new TouchHoldFirstFourUpdateEventsFor50ms(fingerCount, maxEventIndex, pointOffset);
} else {
qFatal("Unknown touch sequence name");
}
@@ -815,6 +859,7 @@ void KisInputManagerTest::testTouchMoves_data()
QTest::addColumn<int>("eventsCount");
QTest::addColumn<QPointF>("pointOffset");
QTest::addColumn<bool>("enableTouchPainting");
+ QTest::addColumn<bool>("enableHoldShortcuts");
QTest::addColumn<QList<int>>("triggeredTouchShortcuts");
QTest::addColumn<QList<int>>("triggeredPaintShortcuts");
QTest::addColumn<int>("actionEventsCount"); // `-1` to ignore this test
@@ -830,50 +875,69 @@ void KisInputManagerTest::testTouchMoves_data()
const int ignoreActionEventCount = -1;
const int ignorePaintEventCount = -1;
- QTest::addRow("clean-1p-drag") << "touchCleanDrag" << 1 << 100 << dragOffset << touchPaintingOff << QList<int>{15} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- QTest::addRow("clean-2p-drag") << "touchCleanDrag" << 2 << 100 << dragOffset << touchPaintingOff << QList<int>{16} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- QTest::addRow("clean-3p-drag") << "touchCleanDrag" << 3 << 100 << dragOffset << touchPaintingOff << QList<int>{17} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ for (bool enableHoldShortcuts : {false, true}) {
+ // clang-format off
+
+ const char *holdPrefix = enableHoldShortcuts ? "hold_enabled-" : "";
- QTest::addRow("clean-1p-drag-paint") << "touchCleanDrag" << 1 << 100 << dragOffset << touchPaintingOn << QList<int>{} << QList<int>{30} << ignoreActionEventCount << ignorePaintEventCount;
- QTest::addRow("clean-2p-drag-paint") << "touchCleanDrag" << 2 << 100 << dragOffset << touchPaintingOn << QList<int>{16} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- QTest::addRow("clean-3p-drag-paint") << "touchCleanDrag" << 3 << 100 << dragOffset << touchPaintingOn << QList<int>{17} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ QTest::addRow("%sclean-1p-drag", holdPrefix) << "touchCleanDrag" << 1 << 100 << dragOffset << touchPaintingOff << enableHoldShortcuts << QList<int>{15} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ QTest::addRow("%sclean-2p-drag", holdPrefix) << "touchCleanDrag" << 2 << 100 << dragOffset << touchPaintingOff << enableHoldShortcuts << QList<int>{16} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ QTest::addRow("%sclean-3p-drag", holdPrefix) << "touchCleanDrag" << 3 << 100 << dragOffset << touchPaintingOff << enableHoldShortcuts << QList<int>{17} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- // painting skips only the first two events
- QTest::addRow("clean-1p-drag-paint-delayed-start") << "touchCleanDrag" << 1 << 100 << QPointF(1.0, 0) << touchPaintingOn << QList<int>{} << QList<int>{30} << ignoreActionEventCount << 98;
- // action has higher threshold, so it skips the first 17 events
- QTest::addRow("clean-1p-drag-delayed-start") << "touchCleanDrag" << 1 << 100 << QPointF(1.0, 0) << touchPaintingOff << QList<int>{15} << QList<int>{} << 83 << ignorePaintEventCount;
+ QTest::addRow("%sclean-1p-drag-paint", holdPrefix) << "touchCleanDrag" << 1 << 100 << dragOffset << touchPaintingOn << enableHoldShortcuts << QList<int>{} << QList<int>{30} << ignoreActionEventCount << ignorePaintEventCount;
+ QTest::addRow("%sclean-2p-drag-paint", holdPrefix) << "touchCleanDrag" << 2 << 100 << dragOffset << touchPaintingOn << enableHoldShortcuts << QList<int>{16} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ QTest::addRow("%sclean-3p-drag-paint", holdPrefix) << "touchCleanDrag" << 3 << 100 << dragOffset << touchPaintingOn << enableHoldShortcuts << QList<int>{17} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- QTest::addRow("clean-1p-tap") << "touchCleanDrag" << 1 << 10 << tapOffset << touchPaintingOff << QList<int>{20} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- QTest::addRow("clean-2p-tap") << "touchCleanDrag" << 2 << 10 << tapOffset << touchPaintingOff << QList<int>{21} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- QTest::addRow("clean-3p-tap") << "touchCleanDrag" << 3 << 10 << tapOffset << touchPaintingOff << QList<int>{22} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ // painting skips only the first two events
+ QTest::addRow("%sclean-1p-drag-paint-delayed-start", holdPrefix) << "touchCleanDrag" << 1 << 100 << QPointF(1.0, 0) << touchPaintingOn << enableHoldShortcuts << QList<int>{} << QList<int>{30} << ignoreActionEventCount << 98;
+ // action has higher threshold, so it skips the first 17 events
+ QTest::addRow("%sclean-1p-drag-delayed-start", holdPrefix) << "touchCleanDrag" << 1 << 100 << QPointF(1.0, 0) << touchPaintingOff << enableHoldShortcuts << QList<int>{15} << QList<int>{} << 83 << ignorePaintEventCount;
- // touch painting stroke has a lower drag threshold, so we should use smaller offset value for it
- QTest::addRow("clean-1p-tap-paint") << "touchCleanDrag" << 1 << 10 << smallTapOffset << touchPaintingOn << QList<int>{} << QList<int>{31} << ignoreActionEventCount << ignorePaintEventCount;
- QTest::addRow("clean-2p-tap-paint") << "touchCleanDrag" << 2 << 10 << tapOffset << touchPaintingOn << QList<int>{21} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- QTest::addRow("clean-3p-tap-paint") << "touchCleanDrag" << 3 << 10 << tapOffset << touchPaintingOn << QList<int>{22} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ QTest::addRow("%sclean-1p-tap", holdPrefix) << "touchCleanDrag" << 1 << 10 << tapOffset << touchPaintingOff << enableHoldShortcuts << QList<int>{20} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ QTest::addRow("%sclean-2p-tap", holdPrefix) << "touchCleanDrag" << 2 << 10 << tapOffset << touchPaintingOff << enableHoldShortcuts << QList<int>{21} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ QTest::addRow("%sclean-3p-tap", holdPrefix) << "touchCleanDrag" << 3 << 10 << tapOffset << touchPaintingOff << enableHoldShortcuts << QList<int>{22} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- // 16px is the threshold for distinguishing taps and drags (touch-begin + 16 steps + touch-end)
- QTest::addRow("clean-1p-tap-upper-bound") << "touchCleanDrag" << 1 << 18 << QPointF(1, 0) << touchPaintingOff << QList<int>{20} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- QTest::addRow("clean-1p-drag-lower-bound") << "touchCleanDrag" << 1 << 19 << QPointF(1, 0) << touchPaintingOff << QList<int>{15} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ // touch painting stroke has a lower drag threshold, so we should use smaller offset value for it
+ QTest::addRow("%sclean-1p-tap-paint", holdPrefix) << "touchCleanDrag" << 1 << 10 << smallTapOffset << touchPaintingOn << enableHoldShortcuts << QList<int>{} << QList<int>{31} << ignoreActionEventCount << ignorePaintEventCount;
+ QTest::addRow("%sclean-2p-tap-paint", holdPrefix) << "touchCleanDrag" << 2 << 10 << tapOffset << touchPaintingOn << enableHoldShortcuts << QList<int>{21} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ QTest::addRow("%sclean-3p-tap-paint", holdPrefix) << "touchCleanDrag" << 3 << 10 << tapOffset << touchPaintingOn << enableHoldShortcuts << QList<int>{22} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- QTest::addRow("dirty-start-2p-drag") << "touchDragDirtyStart" << 2 << 100 << dirtyDragOffset << touchPaintingOff << QList<int>{16} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- QTest::addRow("dirty-start-3p-drag") << "touchDragDirtyStart" << 3 << 100 << dirtyDragOffset << touchPaintingOff << QList<int>{17} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ // 16px is the threshold for distinguishing taps and drags (touch-begin + 16 steps + touch-end)
+ QTest::addRow("%sclean-1p-tap-upper-bound", holdPrefix) << "touchCleanDrag" << 1 << 18 << QPointF(1, 0) << touchPaintingOff << enableHoldShortcuts << QList<int>{20} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ QTest::addRow("%sclean-1p-drag-lower-bound", holdPrefix) << "touchCleanDrag" << 1 << 19 << QPointF(1, 0) << touchPaintingOff << enableHoldShortcuts << QList<int>{15} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- QTest::addRow("dirty-end-2p-drag") << "touchDragDirtyEnd" << 2 << 100 << dirtyDragOffset << touchPaintingOff << QList<int>{16} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- QTest::addRow("dirty-end-3p-drag") << "touchDragDirtyEnd" << 3 << 100 << dirtyDragOffset << touchPaintingOff << QList<int>{17} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ QTest::addRow("%sdirty-start-2p-drag", holdPrefix) << "touchDragDirtyStart" << 2 << 100 << dirtyDragOffset << touchPaintingOff << enableHoldShortcuts << QList<int>{16} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ QTest::addRow("%sdirty-start-3p-drag", holdPrefix) << "touchDragDirtyStart" << 3 << 100 << dirtyDragOffset << touchPaintingOff << enableHoldShortcuts << QList<int>{17} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- QTest::addRow("dirty-end-2p-tap") << "touchDragDirtyEnd" << 2 << 10 << tapOffset << touchPaintingOff << QList<int>{21} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- QTest::addRow("dirty-end-3p-tap") << "touchDragDirtyEnd" << 3 << 10 << tapOffset << touchPaintingOff << QList<int>{22} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ QTest::addRow("%sdirty-end-2p-drag", holdPrefix) << "touchDragDirtyEnd" << 2 << 100 << dirtyDragOffset << touchPaintingOff << enableHoldShortcuts << QList<int>{16} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ QTest::addRow("%sdirty-end-3p-drag", holdPrefix) << "touchDragDirtyEnd" << 3 << 100 << dirtyDragOffset << touchPaintingOff << enableHoldShortcuts << QList<int>{17} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- // the current implementation is expected to switch to a "more-fingers-rich" shortcut immetiately
- QTest::addRow("clean-2p-then-3p-drag") << "touchDragTwoThenThree" << 2 << 100 << dragOffset << touchPaintingOff << QList<int>{16, 17} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ QTest::addRow("%sdirty-end-2p-tap", holdPrefix) << "touchDragDirtyEnd" << 2 << 10 << tapOffset << touchPaintingOff << enableHoldShortcuts << QList<int>{21} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ QTest::addRow("%sdirty-end-3p-tap", holdPrefix) << "touchDragDirtyEnd" << 3 << 10 << tapOffset << touchPaintingOff << enableHoldShortcuts << QList<int>{22} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- // the current implementation is expected to stay on the "finger-richest" shortcut and,
- // not to switch to a "finger-poorer" shortcut
- QTest::addRow("clean-3p-then-2p-drag") << "touchDragThreeThenTwo" << 3 << 100 << dragOffset << touchPaintingOff << QList<int>{17} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ // the current implementation is expected to switch to a "more-fingers-rich" shortcut immetiately
+ QTest::addRow("%sclean-2p-then-3p-drag", holdPrefix) << "touchDragTwoThenThree" << 2 << 100 << dragOffset << touchPaintingOff << enableHoldShortcuts << QList<int>{16, 17} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
- // the 3-point gesture should be run twice during the same run
- QTest::addRow("clean-3p-then-2p-then-3p-drag") << "touchDragThreeThenTwoThenThree" << 3 << 100 << dragOffset << touchPaintingOff << QList<int>{17, 17} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+ // the current implementation is expected to stay on the "finger-richest" shortcut and,
+ // not to switch to a "finger-poorer" shortcut
+ QTest::addRow("%sclean-3p-then-2p-drag", holdPrefix) << "touchDragThreeThenTwo" << 3 << 100 << dragOffset << touchPaintingOff << enableHoldShortcuts << QList<int>{17} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+
+ // the 3-point gesture should be run twice during the same run
+ QTest::addRow("%sclean-3p-then-2p-then-3p-drag", holdPrefix) << "touchDragThreeThenTwoThenThree" << 3 << 100 << dragOffset << touchPaintingOff << enableHoldShortcuts << QList<int>{17, 17} << QList<int>{} << ignoreActionEventCount << ignorePaintEventCount;
+
+ // the amount of paint events should **not** depend on whether we have hold enabled or not
+ QTest::addRow("%sclean-1p-drag-paint-very-short", holdPrefix) << "touchCleanDrag" << 1 << 15 << QPointF(1.0, 0) << touchPaintingOn << enableHoldShortcuts << QList<int>{} << QList<int>{30} << ignoreActionEventCount << 13;
+
+ // clang-format on
+ }
+
+ // clang-format off
+
+ const bool holdShortcutsOn = true;
+
+ QTest::addRow("hold-drag-1p") << "touchHoldFirstFourUpdateEventsFor50ms" << 1 << 100 << QPointF(1.0, 0) << touchPaintingOn << holdShortcutsOn << QList<int>{40} << QList<int>{} << 97 << ignorePaintEventCount;
+
+ // clang-format on
}
void KisInputManagerTest::testTouchMoves()
@@ -908,6 +972,8 @@ void KisInputManagerTest::testTouchMoves()
m.addShortcut(
createTouchShortcut(a.get(), 22, KisShortcutConfiguration::ThreeFingerTap));
+ m.addShortcut(
+ createTouchShortcut(a.get(), 22, KisShortcutConfiguration::ThreeFingerTap));
{
// Touch painting shortcuts
@@ -926,11 +992,18 @@ void KisInputManagerTest::testTouchMoves()
QFETCH(int, eventsCount);
QFETCH(QPointF, pointOffset);
QFETCH(bool, enableTouchPainting);
+ QFETCH(bool, enableHoldShortcuts);
QFETCH(QList<int>, triggeredTouchShortcuts);
QFETCH(QList<int>, triggeredPaintShortcuts);
QFETCH(int, actionEventsCount);
QFETCH(int, paintEventsCount);
+ if (enableHoldShortcuts) {
+ m.addShortcut(
+ createTouchShortcut(a.get(), 40, KisShortcutConfiguration::OneFingerHold));
+ }
+ QCOMPARE(m.hasTouchHoldShortcut(), enableHoldShortcuts);
+
KisConfig(false).setTouchPainting(enableTouchPainting ? KisConfig::TOUCH_PAINTING_ENABLED
: KisConfig::TOUCH_PAINTING_DISABLED);
@@ -990,6 +1063,8 @@ void KisInputManagerTest::testTouchMoves()
QCOMPARE(paintAction->m_endedIndexes, triggeredPaintShortcuts);
QCOMPARE(paintAction->m_ended, !paintAction->m_begunIndexes.isEmpty());
+ qDebug() << ppVar(a->m_inputEventCount) << ppVar(paintAction->m_inputEventCount);
+
if (actionEventsCount >= 0) {
QCOMPARE(a->m_inputEventCount, actionEventsCount);
}
@@ -999,6 +1074,124 @@ void KisInputManagerTest::testTouchMoves()
}
}
+void KisInputManagerTest::testTouchHoldPostponer_data()
+{
+ QTest::addColumn<QString>("sequenceName");
+ QTest::addColumn<int>("fingerCount");
+ QTest::addColumn<int>("eventsCount");
+ QTest::addColumn<QPointF>("pointOffset");
+ QTest::addColumn<int>("holdTimeout");
+ QTest::addColumn<int>("cancelOnIndex");
+ QTest::addColumn<int>("completeOnIndex");
+
+ const int noCancel = -1;
+ const int noComplete = -1;
+
+ const QPointF dragOffset(1,0);
+ const int holdTimeout = 170 /* ms */; // the timeout should happen on the fourth event after the first (with a 30 ms margin)
+
+ QTest::addRow("clean-1p-drag") << "touchCleanDrag" << 1 << 100 << dragOffset << holdTimeout << 21 << noComplete;
+ QTest::addRow("clean-2p-drag") << "touchCleanDrag" << 2 << 100 << dragOffset << holdTimeout << 0 << noComplete;
+ QTest::addRow("dirty-2p-drag") << "touchDragDirtyStart" << 2 << 100 << dragOffset << holdTimeout << 2 << noComplete;
+
+ QTest::addRow("clean-1p-slow-drag") << "touchSlowDrag50ms" << 1 << 100 << dragOffset << holdTimeout << noCancel << 4;
+}
+
+void KisInputManagerTest::testTouchHoldPostponer()
+{
+ QFETCH(QString, sequenceName);
+ QFETCH(int, fingerCount);
+ QFETCH(int, eventsCount);
+ QFETCH(QPointF, pointOffset);
+ QFETCH(int, holdTimeout);
+ QFETCH(int, cancelOnIndex);
+ QFETCH(int, completeOnIndex);
+
+ std::unique_ptr<TouchSequenceGeneratorBase> strokeGenerator(
+ createTouchSequenceGenerator(sequenceName, fingerCount, eventsCount - 1, pointOffset));
+
+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
+ QTouchDevice device;
+ auto calcTouchPointStates = [] (const QList<TouchSequenceGeneratorBase::TouchPoint> &touchPoints) -> Qt::TouchPointStates {
+ Qt::TouchPointStates result;
+
+ for (auto it = touchPoints.begin(); it != touchPoints.end(); ++it) {
+ result |= it->state();
+ }
+
+ return result;
+ };
+#else
+ QPointingDevice device;
+#endif
+
+ QList<TouchSequenceGeneratorBase::TouchPoint> touchPoints;
+
+ KisTouchHoldEventsPostponer postponer(20.0, holdTimeout);
+
+ bool holdHasBeenCompleted = false;
+ connect(&postponer, &KisTouchHoldEventsPostponer::sigHoldCompleted, this, [&] () {
+ holdHasBeenCompleted = true;
+ });
+
+ for (int eventIndex = 0; eventIndex < eventsCount; eventIndex++) {
+ std::unique_ptr<QTouchEvent> event;
+
+ // qWait() will happen here (if it does at all)
+ strokeGenerator->updatePoints(eventIndex, touchPoints);
+
+ // if the timer event has arrived, then verify consistency and exit
+ if (postponer.state() == KisTouchHoldEventsPostponer::HoldCompleted || holdHasBeenCompleted) {
+ QVERIFY(holdHasBeenCompleted);
+ QCOMPARE(postponer.state(), KisTouchHoldEventsPostponer::HoldCompleted);
+ QCOMPARE(completeOnIndex, eventIndex);
+
+ // the last event has not yet been pushed, so no "+1"
+ QCOMPARE(postponer.postponedEvents().size(), eventIndex);
+
+ break;
+ }
+
+ if (eventIndex == 0) {
+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
+ event.reset(new QTouchEvent(QEvent::TouchBegin, &device, Qt::NoModifier, calcTouchPointStates(touchPoints), touchPoints));
+#else
+ event.reset(new QTouchEvent(QEvent::TouchBegin, &device, Qt::NoModifier, touchPoints));
+#endif
+ } else if (eventIndex > 0 && eventIndex < eventsCount - 1) {
+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
+ event.reset(new QTouchEvent(QEvent::TouchUpdate, &device, Qt::NoModifier, calcTouchPointStates(touchPoints), touchPoints));
+#else
+ event.reset(new QTouchEvent(QEvent::TouchUpdate, &device, Qt::NoModifier, touchPoints));
+#endif
+ } else if (eventIndex == eventsCount - 1) {
+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
+ event.reset(new QTouchEvent(QEvent::TouchEnd, &device, Qt::NoModifier, calcTouchPointStates(touchPoints), touchPoints));
+#else
+ event.reset(new QTouchEvent(QEvent::TouchEnd, &device, Qt::NoModifier, touchPoints));
+#endif
+ } else {
+ qFatal("invalid step value");
+ }
+
+ postponer.pushThrough(event.get());
+
+ if (cancelOnIndex >= 0 && cancelOnIndex == eventIndex) {
+ QCOMPARE(postponer.state(), KisTouchHoldEventsPostponer::HoldCancelled);
+
+ // the last event must also have been pushed into the postponed events queue
+ // to be handled uniformly with the rest of the events
+ QCOMPARE(postponer.postponedEvents().size(), eventIndex + 1);
+
+ break;
+ } else {
+ QCOMPARE(postponer.state(), KisTouchHoldEventsPostponer::WaitingForHold);
+ }
+ }
+
+
+}
+
#include "../input/wintab/kis_incremental_average.h"
void KisInputManagerTest::testIncrementalAverage()
diff --git a/libs/ui/tests/kis_input_manager_test.h b/libs/ui/tests/kis_input_manager_test.h
index 8267cb78bc..efe6ec9e95 100644
--- a/libs/ui/tests/kis_input_manager_test.h
+++ b/libs/ui/tests/kis_input_manager_test.h
@@ -22,6 +22,9 @@ private Q_SLOTS:
void testTouchMoves_data();
void testTouchMoves();
+ void testTouchHoldPostponer_data();
+ void testTouchHoldPostponer();
+
void testIncrementalAverage();
};