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
27 ::ArkUI_GestureEventActionType ohEventActionType, Qt::NativeGestureType defaultGestureType)
28{
29 switch (ohEventActionType) {
30 case ::GESTURE_EVENT_ACTION_ACCEPT:
31 return Qt::BeginNativeGesture;
32 case ::GESTURE_EVENT_ACTION_END:
33 return Qt::EndNativeGesture;
34 case ::GESTURE_EVENT_ACTION_CANCEL:
35 return Qt::EndNativeGesture;
36 case ::GESTURE_EVENT_ACTION_UPDATE:
37 return defaultGestureType;
38 }
39 return defaultGestureType;
40}
41
42class QOhosArkUiNativeGesturesHandler final : public QEnableSharedFromThis<QOhosArkUiNativeGesturesHandler>
43{
44public:
45 QOhosArkUiNativeGesturesHandler(QtOhos::QThreadSafeRef<QWindow> qWindowRef);
46
47 void handleNativeGestureEvent(const QArkUi::NativeGestureInfo &nativeGestureInfo);
48
49private:
50 void handleRotationGestureEvent(const ::ArkUI_GestureEvent *gestureEvent);
51 void handlePinchGestureEvent(const ::ArkUI_GestureEvent *gestureEvent);
52
53 QOhosConsumer<const QOhosNativeGestureEvent &> m_baseGesturesHandler;
54 qreal m_lastTotalScale{1.0};
55 qreal m_lastTotalAngle{0.0};
56 QtOhos::QThreadSafeRef<QWindow> m_qWindowRef;
57};
58
59QOhosArkUiNativeGesturesHandler::QOhosArkUiNativeGesturesHandler(
60 QtOhos::QThreadSafeRef<QWindow> qWindowRef)
63{
64}
65
66void QOhosArkUiNativeGesturesHandler::handleNativeGestureEvent(
67 const QArkUi::NativeGestureInfo &nativeGestureInfo)
68{
69 switch (nativeGestureInfo.type) {
70 case QArkUi::NativeGestureInfo::GestureType::Pinch:
71 handlePinchGestureEvent(nativeGestureInfo.event);
72 break;
73 case QArkUi::NativeGestureInfo::GestureType::Rotation:
74 handleRotationGestureEvent(nativeGestureInfo.event);
75 break;
76 }
77}
78
79void QOhosArkUiNativeGesturesHandler::handleRotationGestureEvent(
80 const ::ArkUI_GestureEvent *gestureEvent)
81{
82 const auto timestamp = std::chrono::steady_clock::now();
83 const auto actionType = ::OH_ArkUI_GestureEvent_GetActionType(gestureEvent);
84 const auto qtGestureType = getQtGestureType(actionType, Qt::RotateNativeGesture);
85
86 const auto *inputEvent = ::OH_ArkUI_GestureEvent_GetRawInputEvent(gestureEvent);
87 const auto toolType = ::OH_ArkUI_UIInputEvent_GetToolType(inputEvent);
88 if (toolType != UI_INPUT_EVENT_TOOL_TYPE_TOUCHPAD)
89 return;
90
91 const auto totalAngle = ::OH_ArkUI_RotationGesture_GetAngle(gestureEvent);
92 const auto delta =
93 qtGestureType == Qt::BeginNativeGesture
94 ? 0.0
95 : totalAngle - m_lastTotalAngle;
96 m_lastTotalAngle = totalAngle;
97
98 const auto gestureTimestamp = ::OH_ArkUI_UIInputEvent_GetEventTime(inputEvent);
99 const auto localPosition = QArkUi::getPointerEventLocalPosition(inputEvent);
100 const auto globalPosition = QArkUi::getPointerEventGlobalPosition(inputEvent);
101 const auto deviceType = QArkUi::getPointingDeviceType(inputEvent);
102
103 QOhosNativeGestureEvent newEvent {
104 .timestamp = timestamp,
105 .gestureTimestamp = gestureTimestamp,
106 .value = delta,
107 .localPosition = localPosition,
108 .globalPosition = globalPosition,
109 .gestureType = qtGestureType,
110 .deviceType = deviceType,
111 };
112
113 m_baseGesturesHandler(newEvent);
114}
115
116void QOhosArkUiNativeGesturesHandler::handlePinchGestureEvent(const ::ArkUI_GestureEvent *gestureEvent)
117{
118 const auto timestamp = std::chrono::steady_clock::now();
119 const auto actionType = ::OH_ArkUI_GestureEvent_GetActionType(gestureEvent);
120 const auto qtGestureType = getQtGestureType(actionType, Qt::ZoomNativeGesture);
121
122 const auto totalScale = ::OH_ArkUI_PinchGesture_GetScale(gestureEvent);
123 const auto *inputEvent = ::OH_ArkUI_GestureEvent_GetRawInputEvent(gestureEvent);
124
125 const QPointF localPosition = QArkUi::getPointerEventLocalPosition(inputEvent);
126 const auto toolType = ::OH_ArkUI_UIInputEvent_GetToolType(inputEvent);
127 if (toolType != UI_INPUT_EVENT_TOOL_TYPE_TOUCHPAD)
128 return;
129
130 const auto gestureTimestamp = ::OH_ArkUI_UIInputEvent_GetEventTime(inputEvent);
131 const auto globalPosition = QArkUi::getPointerEventGlobalPosition(inputEvent);
132 const auto deviceType = QArkUi::getPointingDeviceType(inputEvent);
133
134 const auto scaleDelta =
135 (qtGestureType == Qt::BeginNativeGesture)
136 ? 0.0
137 : (totalScale / m_lastTotalScale) - 1.0;
138 m_lastTotalScale = totalScale;
139
140 QOhosNativeGestureEvent newEvent {
141 .timestamp = timestamp,
142 .gestureTimestamp = gestureTimestamp,
143 .value = scaleDelta,
144 .localPosition = localPosition,
145 .globalPosition = globalPosition,
146 .gestureType = qtGestureType,
147 .deviceType = deviceType,
148 };
149
150 m_baseGesturesHandler(newEvent);
151}
152
153}
154
156 QtOhos::QThreadSafeRef<QWindow> qWindowRef)
157{
158 return [gesturesHandler = QSharedPointer<QOhosArkUiNativeGesturesHandler>::create(qWindowRef)](
159 const QArkUi::NativeGestureInfo &nativeGestureInfo) {
160 gesturesHandler->handleNativeGestureEvent(nativeGestureInfo);
161 };
162}
163
164QT_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)
QOhosConsumer< const QArkUi::NativeGestureInfo & > makeQOhosArkUiNativeGesturesHandler(QtOhos::QThreadSafeRef< QWindow > qWindowRef)