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
qohosarkuinativegestureshandler.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
6#include <QtCore/private/qohoslogger_p.h>
7#include <QtCore/qglobal.h>
8#include <QtCore/qpoint.h>
9#include <QtGui/qwindow.h>
10#include <arkui/native_gesture.h>
11#include <arkui/native_interface.h>
12#include <arkui/native_type.h>
13#include <arkui/ui_input_event.h>
14#include <chrono>
15#include <qarkui/qembeddedwindownode.h>
16#include <qarkui/input.h>
17#include <qohosplatformintegration.h>
18#include <qohosutils.h>
19#include <qpa/qwindowsysteminterface.h>
20#include <render/qohosnativegestureshandler.h>
21
23
24namespace {
25
26QPointF getLocalPosition(const ::ArkUI_UIInputEvent *inputEvent)
27{
28 const auto localX = ::OH_ArkUI_PointerEvent_GetX(inputEvent);
29 const auto localY = ::OH_ArkUI_PointerEvent_GetY(inputEvent);
30 return { localX, localY };
31}
32
33QPointF getDisplayBasedPosition(const ::ArkUI_UIInputEvent *inputEvent)
34{
35 return QPointF(
36 ::OH_ArkUI_PointerEvent_GetDisplayX(inputEvent),
37 ::OH_ArkUI_PointerEvent_GetDisplayY(inputEvent));
38}
39
41 ::ArkUI_GestureEventActionType ohEventActionType, Qt::NativeGestureType defaultGestureType)
42{
43 switch (ohEventActionType) {
44 case ::GESTURE_EVENT_ACTION_ACCEPT:
45 return Qt::BeginNativeGesture;
46 case ::GESTURE_EVENT_ACTION_END:
47 return Qt::EndNativeGesture;
48 case ::GESTURE_EVENT_ACTION_CANCEL:
49 return Qt::EndNativeGesture;
50 case ::GESTURE_EVENT_ACTION_UPDATE:
51 return defaultGestureType;
52 }
53 return defaultGestureType;
54}
55
56class QOhosArkUiNativeGesturesHandler final : public QEnableSharedFromThis<QOhosArkUiNativeGesturesHandler>
57{
58public:
59 QOhosArkUiNativeGesturesHandler(QtOhos::QThreadSafeRef<QWindow> qWindowRef);
60
61 void handleNativeGestureEvent(const QArkUi::NativeGestureInfo &nativeGestureInfo);
62
63private:
64 void handleRotationGestureEvent(const ::ArkUI_GestureEvent *gestureEvent);
65 void handlePinchGestureEvent(const ::ArkUI_GestureEvent *gestureEvent);
66
67 QOhosConsumer<const QOhosNativeGestureEvent &> m_baseGesturesHandler;
68 qreal m_lastTotalScale{1.0};
69 QtOhos::QThreadSafeRef<QWindow> m_qWindowRef;
70};
71
72QOhosArkUiNativeGesturesHandler::QOhosArkUiNativeGesturesHandler(
73 QtOhos::QThreadSafeRef<QWindow> qWindowRef)
76{
77}
78
79void QOhosArkUiNativeGesturesHandler::handleNativeGestureEvent(
80 const QArkUi::NativeGestureInfo &nativeGestureInfo)
81{
82 switch (nativeGestureInfo.type) {
83 case QArkUi::NativeGestureInfo::GestureType::Pinch:
84 handlePinchGestureEvent(nativeGestureInfo.event);
85 break;
86 case QArkUi::NativeGestureInfo::GestureType::Rotation:
87 handleRotationGestureEvent(nativeGestureInfo.event);
88 break;
89 }
90}
91
92void QOhosArkUiNativeGesturesHandler::handleRotationGestureEvent(
93 const ::ArkUI_GestureEvent *gestureEvent)
94{
95 const auto timestamp = std::chrono::steady_clock::now();
96 const auto actionType = ::OH_ArkUI_GestureEvent_GetActionType(gestureEvent);
97 const auto qtGestureType = getQtGestureType(actionType, Qt::RotateNativeGesture);
98 const auto delta = ::OH_ArkUI_RotationGesture_GetAngle(gestureEvent);
99
100 const auto *inputEvent = ::OH_ArkUI_GestureEvent_GetRawInputEvent(gestureEvent);
101 const auto gestureTimestamp = ::OH_ArkUI_UIInputEvent_GetEventTime(inputEvent);
102 const auto localPosition = getLocalPosition(inputEvent);
103 const auto displayBasedPosition = getDisplayBasedPosition(inputEvent);
104 const auto deviceType = QArkUi::getTouchDeviceType(inputEvent);
105
106 QOhosNativeGestureEvent newEvent {
107 .timestamp = timestamp,
108 .gestureTimestamp = gestureTimestamp,
109 .value = delta,
110 .localPosition = localPosition,
111 .displayBasedPosition = displayBasedPosition,
112 .gestureType = qtGestureType,
113 .deviceType = deviceType,
114 };
115
116 m_baseGesturesHandler(newEvent);
117}
118
119void QOhosArkUiNativeGesturesHandler::handlePinchGestureEvent(const ::ArkUI_GestureEvent *gestureEvent)
120{
121 const auto timestamp = std::chrono::steady_clock::now();
122 const auto actionType = ::OH_ArkUI_GestureEvent_GetActionType(gestureEvent);
123 const auto qtGestureType = getQtGestureType(actionType, Qt::ZoomNativeGesture);
124
125 const auto totalScale = ::OH_ArkUI_PinchGesture_GetScale(gestureEvent);
126 const auto localX = ::OH_ArkUI_PinchGesture_GetCenterX(gestureEvent);
127 const auto localY = ::OH_ArkUI_PinchGesture_GetCenterY(gestureEvent);
128 const QPointF localPosition { localX, localY };
129
130 const auto *inputEvent = ::OH_ArkUI_GestureEvent_GetRawInputEvent(gestureEvent);
131
132 const auto toolType = ::OH_ArkUI_UIInputEvent_GetToolType(inputEvent);
133 if (toolType == UI_INPUT_EVENT_TOOL_TYPE_MOUSE || toolType == UI_INPUT_EVENT_TOOL_TYPE_TOUCHPAD)
134 return;
135
136 const auto gestureTimestamp = ::OH_ArkUI_UIInputEvent_GetEventTime(inputEvent);
137 const auto displayBasedPosition = getDisplayBasedPosition(inputEvent);
138 const auto deviceType = QArkUi::getTouchDeviceType(inputEvent);
139
140 const auto scaleFactor =
141 qtGestureType == Qt::BeginNativeGesture
142 ? totalScale
143 : totalScale / m_lastTotalScale;
144 m_lastTotalScale = totalScale;
145
146 QOhosNativeGestureEvent newEvent {
147 .timestamp = timestamp,
148 .gestureTimestamp = gestureTimestamp,
149 .value = scaleFactor,
150 .localPosition = localPosition,
151 .displayBasedPosition = displayBasedPosition,
152 .gestureType = qtGestureType,
153 .deviceType = deviceType,
154 };
155
156 m_baseGesturesHandler(newEvent);
157}
158
159}
160
162 QtOhos::QThreadSafeRef<QWindow> qWindowRef)
163{
164 return [gesturesHandler = QSharedPointer<QOhosArkUiNativeGesturesHandler>::create(qWindowRef)](
165 const QArkUi::NativeGestureInfo &nativeGestureInfo) {
166 gesturesHandler->handleNativeGestureEvent(nativeGestureInfo);
167 };
168}
169
170QT_END_NAMESPACE
QOhosArkUiNativeGesturesHandler(QtOhos::QThreadSafeRef< QWindow > qWindowRef)
void handleNativeGestureEvent(const QArkUi::NativeGestureInfo &nativeGestureInfo)
Combined button and popup list for selecting options.
Qt::NativeGestureType getQtGestureType(::ArkUI_GestureEventActionType ohEventActionType, Qt::NativeGestureType defaultGestureType)
QPointF getLocalPosition(const ::ArkUI_UIInputEvent *inputEvent)
QPointF getDisplayBasedPosition(const ::ArkUI_UIInputEvent *inputEvent)
QOhosConsumer< const QArkUi::NativeGestureInfo & > makeQOhosArkUiNativeGesturesHandler(QtOhos::QThreadSafeRef< QWindow > qWindowRef)