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 <qohosinputmethodeventhandler.h>
7#include <qohosjsmain.h>
8#include <qohosplatformintegration.h>
9#include <qohosutils.h>
10#include <render/qohosbatchingrequestshandler.h>
11#include <utility>
12#include <vector>
13
15
16namespace {
17
18constexpr auto gestureEventMinAgeForDrop = std::chrono::milliseconds(20);
19
20class QOhosNativeGesturesHandler final : public QEnableSharedFromThis<QOhosNativeGesturesHandler>
21{
22public:
24 QtOhos::QThreadSafeRef<QWindow> qWindowRef,
25 std::function<void(QOhosNativeGestureEvent &)> qtThreadEventTransformer);
26
28
29private:
30 void processGestureEventsInQtThread(std::vector<QOhosNativeGestureEvent> &&batch);
31
32 QtOhos::QThreadSafeRef<QWindow> m_qWindowRef;
33 std::function<void(QOhosNativeGestureEvent &)> m_qtThreadEventTransformer;
34 QOhosConsumer<QOhosNativeGestureEvent> m_gestureEventsHandler;
35};
36
37QOhosNativeGesturesHandler::QOhosNativeGesturesHandler(
38 QtOhos::QThreadSafeRef<QWindow> qWindowRef,
39 std::function<void(QOhosNativeGestureEvent &)> qtThreadEventTransformer)
42{
43}
44
45void QOhosNativeGesturesHandler::processGestureEventInJsThread(
46 const QOhosNativeGestureEvent &event)
47{
48 if (!m_gestureEventsHandler) {
49 auto weakSelf = sharedFromThis().toWeakRef();
50 m_gestureEventsHandler = makeQtOhosSimpleBatchingQtRequestsHandler<QOhosNativeGestureEvent>(
51 m_qWindowRef.toQObjectThreadSafeRef(),
52 [weakSelf](std::vector<QOhosNativeGestureEvent> &&batch) {
53 auto sharedSelf = weakSelf.toStrongRef();
54 if (!sharedSelf.isNull())
55 sharedSelf->processGestureEventsInQtThread(std::move(batch));
56 });
57 }
58
59 m_gestureEventsHandler(event);
60}
61
62void QOhosNativeGesturesHandler::processGestureEventsInQtThread(std::vector<QOhosNativeGestureEvent> &&batch)
63{
64 auto *inputMethodEventHandler =
66
67 if (inputMethodEventHandler == nullptr) {
68 qOhosPrintfError("Cannot dispatch gesture event: inputMethodEventHandler is nullptr.");
69 return;
70 }
71
72 const auto now = std::chrono::steady_clock::now();
73
74 const auto mayDropGestureEvent = [now](const auto &event, const auto &nextEvent) {
75 return now - event.timestamp >= gestureEventMinAgeForDrop
76 && event.gestureType == nextEvent.gestureType
77 && event.deviceType == nextEvent.deviceType;
78 };
79
80 batch.erase(
81 QtOhos::removeMatchingWithLookahead(
82 batch.begin(), batch.end(), mayDropGestureEvent),
83 batch.end());
84
85 for (auto &event : batch) {
86 m_qtThreadEventTransformer(event);
87
88 inputMethodEventHandler->onGestureEventFromNativeNode(
89 QOhosGestureEvent {
90 .targetWindow = m_qWindowRef.data(),
91 .timestamp = event.gestureTimestamp,
92 .value = event.value,
93 .localPosition = event.localPosition,
94 .globalPosition = event.globalPosition,
95 .gestureType = event.gestureType,
96 .deviceType = event.deviceType,
97 });
98 }
99}
100
101}
102
104 QtOhos::QThreadSafeRef<QWindow> qWindowRef,
105 std::function<void(QOhosNativeGestureEvent &)> optQtThreadEventTransformer)
106{
107 auto gesturesHandler = QSharedPointer<QOhosNativeGesturesHandler>::create(
108 qWindowRef,
109 optQtThreadEventTransformer
110 ? std::move(optQtThreadEventTransformer)
111 : [](QOhosNativeGestureEvent &) {
112 });
113 return [gesturesHandler](const QOhosNativeGestureEvent &event) {
114 gesturesHandler->processGestureEventInJsThread(event);
115 };
116}
117
118QT_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)