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 QtOhos::QThreadSafeRef<QWindow> m_qWindowRef;
56};
57
58QOhosArkUiNativeGesturesHandler::QOhosArkUiNativeGesturesHandler(
59 QtOhos::QThreadSafeRef<QWindow> qWindowRef)
62{
63}
64
65void QOhosArkUiNativeGesturesHandler::handleNativeGestureEvent(
66 const QArkUi::NativeGestureInfo &nativeGestureInfo)
67{
68 switch (nativeGestureInfo.type) {
69 case QArkUi::NativeGestureInfo::GestureType::Pinch:
70 handlePinchGestureEvent(nativeGestureInfo.event);
71 break;
72 case QArkUi::NativeGestureInfo::GestureType::Rotation:
73 handleRotationGestureEvent(nativeGestureInfo.event);
74 break;
75 }
76}
77
78void QOhosArkUiNativeGesturesHandler::handleRotationGestureEvent(
79 const ::ArkUI_GestureEvent *gestureEvent)
80{
81 const auto timestamp = std::chrono::steady_clock::now();
82 const auto actionType = ::OH_ArkUI_GestureEvent_GetActionType(gestureEvent);
83 const auto qtGestureType = getQtGestureType(actionType, Qt::RotateNativeGesture);
84 const auto delta = ::OH_ArkUI_RotationGesture_GetAngle(gestureEvent);
85
86 const auto *inputEvent = ::OH_ArkUI_GestureEvent_GetRawInputEvent(gestureEvent);
87 const auto gestureTimestamp = ::OH_ArkUI_UIInputEvent_GetEventTime(inputEvent);
88 const auto localPosition = QArkUi::getPointerEventLocalPosition(inputEvent);
89 const auto globalPosition = QArkUi::getPointerEventGlobalPosition(inputEvent);
90 const auto deviceType = QArkUi::getTouchDeviceType(inputEvent);
91
93 .timestamp = timestamp,
94 .gestureTimestamp = gestureTimestamp,
95 .value = delta,
96 .localPosition = localPosition,
97 .globalPosition = globalPosition,
98 .gestureType = qtGestureType,
99 .deviceType = deviceType,
100 };
101
102 m_baseGesturesHandler(newEvent);
103}
104
105void QOhosArkUiNativeGesturesHandler::handlePinchGestureEvent(const ::ArkUI_GestureEvent *gestureEvent)
106{
107 const auto timestamp = std::chrono::steady_clock::now();
108 const auto actionType = ::OH_ArkUI_GestureEvent_GetActionType(gestureEvent);
109 const auto qtGestureType = getQtGestureType(actionType, Qt::ZoomNativeGesture);
110
111 const auto totalScale = ::OH_ArkUI_PinchGesture_GetScale(gestureEvent);
112 const auto localX = ::OH_ArkUI_PinchGesture_GetCenterX(gestureEvent);
113 const auto localY = ::OH_ArkUI_PinchGesture_GetCenterY(gestureEvent);
114 const QPointF localPosition { localX, localY };
115
116 const auto *inputEvent = ::OH_ArkUI_GestureEvent_GetRawInputEvent(gestureEvent);
117
118 const auto toolType = ::OH_ArkUI_UIInputEvent_GetToolType(inputEvent);
119 if (toolType == UI_INPUT_EVENT_TOOL_TYPE_MOUSE || toolType == UI_INPUT_EVENT_TOOL_TYPE_TOUCHPAD)
120 return;
121
122 const auto gestureTimestamp = ::OH_ArkUI_UIInputEvent_GetEventTime(inputEvent);
123 const auto globalPosition = QArkUi::getPointerEventGlobalPosition(inputEvent);
124 const auto deviceType = QArkUi::getTouchDeviceType(inputEvent);
125
126 const auto scaleFactor =
127 qtGestureType == Qt::BeginNativeGesture
128 ? totalScale
129 : totalScale / m_lastTotalScale;
130 m_lastTotalScale = totalScale;
131
132 QOhosNativeGestureEvent newEvent {
133 .timestamp = timestamp,
134 .gestureTimestamp = gestureTimestamp,
135 .value = scaleFactor,
136 .localPosition = localPosition,
137 .globalPosition = globalPosition,
138 .gestureType = qtGestureType,
139 .deviceType = deviceType,
140 };
141
142 m_baseGesturesHandler(newEvent);
143}
144
145}
146
148 QtOhos::QThreadSafeRef<QWindow> qWindowRef)
149{
150 return [gesturesHandler = QSharedPointer<QOhosArkUiNativeGesturesHandler>::create(qWindowRef)](
151 const QArkUi::NativeGestureInfo &nativeGestureInfo) {
152 gesturesHandler->handleNativeGestureEvent(nativeGestureInfo);
153 };
154}
155
156QT_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)