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