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++

Fix touchpad-generated hover events on MacOS

Dmitry Kazakov4292cb675c9c
diff --git a/libs/ui/input/kis_input_manager.cpp b/libs/ui/input/kis_input_manager.cpp
index 7a2c50e54a..fe308df26f 100644
--- a/libs/ui/input/kis_input_manager.cpp
+++ b/libs/ui/input/kis_input_manager.cpp
@@ -682,11 +682,34 @@ bool KisInputManager::eventFilterImpl(QEvent * event)
}
if (count < 2 && eventPointCount > count) {
- retval = d->matcher.touchEndEvent(touchEvent);
+ d->matcher.touchEndEvent(touchEvent);
+ retval = true;
} else {
#endif
KisAbstractInputAction::setInputManager(this);
- retval = d->matcher.touchUpdateEvent(touchEvent);
+ d->matcher.touchUpdateEvent(touchEvent);
+ /**
+ * We don't try to pass unhandled events to the tool proxy
+ * (via toolProxy->forwardHoverEvent(event)) because of
+ * the following reasons:
+ *
+ * On Windows and Linux touch evetns are generated for
+ * touch screens only. And touch screens have no "hover"
+ * state.
+ *
+ * On MacOS touch events are also generated for the Apple's
+ * trackpad. Though, for single-finger movements, these touch
+ * events are usually unbalanced, i.e. they don't have a
+ * corresponding TouchBegin event.
+ *
+ * We could theoretically pass these events to the tool on
+ * MacOS only, but it seems like the system-generated mouse
+ * events are more precise and granular. That is why we just
+ * allow these mouse events in EventEater when we detect they
+ * are coming from a touchpad/trackpad outside of
+ * TouchBegin/TouchEnd guards.
+ */
+ retval = true;
#ifdef Q_OS_MACOS
}
diff --git a/libs/ui/input/kis_input_manager_p.cpp b/libs/ui/input/kis_input_manager_p.cpp
index 8daae5c34f..72f5186b8e 100644
--- a/libs/ui/input/kis_input_manager_p.cpp
+++ b/libs/ui/input/kis_input_manager_p.cpp
@@ -164,9 +164,43 @@ bool KisInputManager::Private::EventEater::eventFilter(QObject* target, QEvent*
{
// handle `touchIsActive` state transitions
- if (event->type() == QEvent::TouchBegin ||
- event->type() == QEvent::TouchUpdate) {
+ bool assumeTouchStarted = event->type() == QEvent::TouchBegin;
+ if (!assumeTouchStarted && event->type() == QEvent::TouchUpdate) {
+ QTouchEvent *touchEvent = static_cast<QTouchEvent*>(event);
+
+#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
+ using DeviceType = QInputDevice::DeviceType;
+#else
+ using DeviceType = QTouchDevice::DeviceType;
+#endif
+
+ if (touchEvent->device()->type() == DeviceType::TouchPad) {
+#ifdef Q_OS_MACOS
+ /**
+ * On MacOS TouchUpdate events arrive for normal touchpad
+ * movements. These TouchUpdate events have lower resolution
+ * and they are not wrapped into TouchBegin/TouchEnd pair.
+ * So we just allow synthesized events for them.
+ *
+ * For all other systems TouchUpdate events arrive for
+ * touch screens only, and only inside a valid gesture, so
+ * we should block mouse events for them.
+ *
+ * Also see a comment about hover events in the handler
+ * of QEvent::TouchUpdate in KisInputManager::eventFilter.
+ */
+
+ // noop, keep assumeTouchStarted false
+#else
+ dbgInputEater << "Received a TouchUpdate for a touchpad device on a non-MacOS system!";
+#endif
+ } else {
+ assumeTouchStarted = true;
+ }
+ }
+
+ if (assumeTouchStarted) {
if (!touchIsActive) {
touchIsActive = true;
debugEaterStateTransition("touchIsActive"_L1, true, event->type());
diff --git a/libs/ui/input/kis_shortcut_matcher.cpp b/libs/ui/input/kis_shortcut_matcher.cpp
index dcd8b7e7db..878867f2a0 100644
--- a/libs/ui/input/kis_shortcut_matcher.cpp
+++ b/libs/ui/input/kis_shortcut_matcher.cpp
@@ -495,7 +495,7 @@ bool KisShortcutMatcher::touchUpdateEvent(QTouchEvent *event)
{
// the touch action has been overridden by some tablet action,
// consume and ignore it.
- if (!m_d->touchActionTracked) return true;
+ if (!m_d->touchActionTracked) return false;
if (m_d->touchHoldEventPostponer) {
m_d->touchHoldEventPostponer->pushThrough(event);