Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qohosnativegestureshandler.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <QtCore/private/qohoslogger_p.h>
6#include <private/qhighdpiscaling_p.h>
7#include <qohosinputmethodeventhandler.h>
8#include <qohosjsmain.h>
9#include <qohosplatformintegration.h>
10#include <qohosutils.h>
11#include <render/qohosbatchingrequestshandler.h>
12#include <utility>
13#include <vector>
14
16
17namespace {
18
19constexpr auto gestureEventMinAgeForDrop = std::chrono::milliseconds(20);
20
21class QOhosNativeGesturesHandler final : public QEnableSharedFromThis<QOhosNativeGesturesHandler>
22{
23public:
25 QtOhos::QThreadSafeRef<QWindow> qWindowRef,
26 std::function<void(QOhosNativeGestureEvent &)> qtThreadEventTransformer);
27
29
30private:
31 void processGestureEventsInQtThread(std::vector<QOhosNativeGestureEvent> &&batch);
32
33 QtOhos::QThreadSafeRef<QWindow> m_qWindowRef;
34 std::function<void(QOhosNativeGestureEvent &)> m_qtThreadEventTransformer;
35 QOhosConsumer<QOhosNativeGestureEvent> m_gestureEventsHandler;
36};
37
38QOhosNativeGesturesHandler::QOhosNativeGesturesHandler(
39 QtOhos::QThreadSafeRef<QWindow> qWindowRef,
40 std::function<void(QOhosNativeGestureEvent &)> qtThreadEventTransformer)
43{
44}
45
46void QOhosNativeGesturesHandler::processGestureEventInJsThread(
47 const QOhosNativeGestureEvent &event)
48{
49 if (!m_gestureEventsHandler) {
50 auto weakSelf = sharedFromThis().toWeakRef();
51 m_gestureEventsHandler = makeQtOhosSimpleBatchingQtRequestsHandler<QOhosNativeGestureEvent>(
52 m_qWindowRef.toQObjectThreadSafeRef(),
53 [weakSelf](std::vector<QOhosNativeGestureEvent> &&batch) {
54 auto sharedSelf = weakSelf.toStrongRef();
55 if (!sharedSelf.isNull())
56 sharedSelf->processGestureEventsInQtThread(std::move(batch));
57 });
58 }
59
60 m_gestureEventsHandler(event);
61}
62
63void QOhosNativeGesturesHandler::processGestureEventsInQtThread(std::vector<QOhosNativeGestureEvent> &&batch)
64{
65 auto *inputMethodEventHandler =
67
68 if (inputMethodEventHandler == nullptr) {
69 qOhosPrintfError("Cannot dispatch gesture event: inputMethodEventHandler is nullptr.");
70 return;
71 }
72
73 const auto now = std::chrono::steady_clock::now();
74
75 const auto mayDropGestureEvent = [now](const auto &event, const auto &nextEvent) {
76 return now - event.timestamp >= gestureEventMinAgeForDrop
77 && event.gestureType == nextEvent.gestureType
78 && event.deviceType == nextEvent.deviceType;
79 };
80
81 batch.erase(
82 QtOhos::removeMatchingWithLookahead(
83 batch.begin(), batch.end(), mayDropGestureEvent),
84 batch.end());
85
86 const auto *platformScreen = m_qWindowRef.data()->screen()->handle();
87
88 for (auto &event : batch) {
89 m_qtThreadEventTransformer(event);
90
91 auto screenBasedPosition = QHighDpi::fromNativePixels(
92 event.displayBasedPosition, platformScreen);
93 inputMethodEventHandler->onGestureEventFromNativeNode(
94 m_qWindowRef.data(), event.gestureTimestamp, event.value, event.localPosition,
95 screenBasedPosition, event.gestureType, event.deviceType);
96 }
97}
98
99}
100
102 QtOhos::QThreadSafeRef<QWindow> qWindowRef,
103 std::function<void(QOhosNativeGestureEvent &)> optQtThreadEventTransformer)
104{
105 auto gesturesHandler = QSharedPointer<QOhosNativeGesturesHandler>::create(
106 qWindowRef,
107 optQtThreadEventTransformer
108 ? std::move(optQtThreadEventTransformer)
109 : [](QOhosNativeGestureEvent &) {
110 });
111 return [gesturesHandler](const QOhosNativeGestureEvent &event) {
112 gesturesHandler->processGestureEventInJsThread(event);
113 };
114}
115
116QT_END_NAMESPACE
static QOhosPlatformIntegration * instance()
QOhosInputMethodEventHandler * inputMethodEventHandler() const
QOhosNativeGesturesHandler(QtOhos::QThreadSafeRef< QWindow > qWindowRef, std::function< void(QOhosNativeGestureEvent &)> qtThreadEventTransformer)
void processGestureEventInJsThread(const QOhosNativeGestureEvent &event)
Combined button and popup list for selecting options.
QOhosConsumer< const QOhosNativeGestureEvent & > makeQOhosNativeGesturesHandler(QtOhos::QThreadSafeRef< QWindow > qWindowRef, std::function< void(QOhosNativeGestureEvent &)> optQtThreadEventTransformer=nullptr)