4#include <qarkui/qqtembeddedwindownode.h>
6#include <arkui/native_node.h>
7#include <arkui/native_type.h>
8#include <qarkui/qarkuiutils.h>
9#include <qohosjsmain.h>
10#include <qohosutils.h>
17class XComponentCallbackDispatcher
20 static XComponentCallbackDispatcher &instance();
22 XComponentCallbackDispatcher(
const XComponentCallbackDispatcher &) =
delete;
23 XComponentCallbackDispatcher(XComponentCallbackDispatcher &&) =
delete;
24 XComponentCallbackDispatcher &operator=(
const XComponentCallbackDispatcher &) =
delete;
25 XComponentCallbackDispatcher &operator=(XComponentCallbackDispatcher &&) =
delete;
27 std::shared_ptr<
void> registerCallbackReceiver(
28 ::OH_NativeXComponent *xComponent,
32 XComponentCallbackDispatcher() =
default;
34 static void handleSurfaceCreated(::OH_NativeXComponent *xComponent,
void *window);
35 static void handleSurfaceChanged(::OH_NativeXComponent *xComponent,
void *window);
36 static void handleSurfaceDestroyed(::OH_NativeXComponent *xComponent,
void *window);
37 static void handleSurfaceTouchEvent(::OH_NativeXComponent *xComponent,
void *window);
38 static void handleMouseEvent(::OH_NativeXComponent *xComponent,
void *window);
39 static void handleHoverEvent(::OH_NativeXComponent *xComponent,
bool hover);
40 static void handleKeyEvent(::OH_NativeXComponent *xComponent,
void *window);
43 std::map<::OH_NativeXComponent *, QXComponentCallbackReceiver *> m_receivers;
46XComponentCallbackDispatcher &XComponentCallbackDispatcher::instance()
48 static XComponentCallbackDispatcher dispatcher;
52std::shared_ptr<
void> XComponentCallbackDispatcher::registerCallbackReceiver(
55 qOhosPrintfDebug(
"Registering callbacks for xcomponent %p", xComponent);
57 if (m_receivers.find(xComponent) != m_receivers.end()) {
58 qOhosReportFatalErrorAndAbort(
59 "%s: XComponent %p callbacks were already registered", Q_FUNC_INFO, xComponent);
62 m_receivers[xComponent] = receiver;
64 static ::OH_NativeXComponent_Callback callbacks = {
65 .OnSurfaceCreated = &XComponentCallbackDispatcher::handleSurfaceCreated,
66 .OnSurfaceChanged = &XComponentCallbackDispatcher::handleSurfaceChanged,
67 .OnSurfaceDestroyed = &XComponentCallbackDispatcher::handleSurfaceDestroyed,
68 .DispatchTouchEvent = &XComponentCallbackDispatcher::handleSurfaceTouchEvent,
71 static ::OH_NativeXComponent_MouseEvent_Callback mouseEventCallbacks = {
72 .DispatchMouseEvent = &XComponentCallbackDispatcher::handleMouseEvent,
73 .DispatchHoverEvent = &XComponentCallbackDispatcher::handleHoverEvent,
76 auto lifecycleAndTouchCallbackRegisterStatus = ::OH_NativeXComponent_RegisterCallback(
77 xComponent, &callbacks);
78 if (lifecycleAndTouchCallbackRegisterStatus != ::OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
79 qOhosReportFatalErrorAndAbort(
80 "OH_NativeXComponent_RegisterCallback failed with error: %d",
81 lifecycleAndTouchCallbackRegisterStatus);
85 auto mouseRegisterStatus = ::OH_NativeXComponent_RegisterMouseEventCallback(
87 &mouseEventCallbacks);
88 if (mouseRegisterStatus != ::OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
89 qOhosReportFatalErrorAndAbort(
90 "OH_NativeXComponent_RegisterMouseEventCallback failed with error: %d",
96 auto keyRegisterStatus = ::OH_NativeXComponent_RegisterKeyEventCallback(
97 xComponent, &XComponentCallbackDispatcher::handleKeyEvent);
98 if (keyRegisterStatus != ::OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
99 qOhosReportFatalErrorAndAbort(
100 "OH_NativeXComponent_RegisterKeyEventCallback failed with error: %d",
105 return QtOhos::makeDestroyNotifier([
this, xComponent](){
106 qOhosPrintfDebug(
"Unregistering callbacks for xcomponent %p", xComponent);
107 m_receivers.erase(xComponent);
108 ::OH_NativeXComponent_RegisterCallback(xComponent,
nullptr);
109 ::OH_NativeXComponent_RegisterMouseEventCallback(xComponent,
nullptr);
110 ::OH_NativeXComponent_RegisterKeyEventCallback(xComponent,
nullptr);
116 auto &dispatcher = XComponentCallbackDispatcher::instance();
117 auto it = dispatcher.m_receivers.find(xComponent);
118 if (it == dispatcher.m_receivers.end()) {
119 qOhosPrintfDebug(
"Error: xComponent %p does not have any assigned receiver", xComponent);
125void XComponentCallbackDispatcher::handleSurfaceTouchEvent(::OH_NativeXComponent *xComponent,
void *window)
127 auto *receiver = findCallbackReceiverOrNull(xComponent);
128 if (receiver !=
nullptr) {
129 receiver->onInputEvent(
131 reinterpret_cast<::OHNativeWindow *>(window));
135void XComponentCallbackDispatcher::handleHoverEvent(::OH_NativeXComponent *xComponent,
bool isHover)
137 auto *receiver = findCallbackReceiverOrNull(xComponent);
138 if (receiver !=
nullptr)
139 receiver->onHoverEvent(isHover);
143void XComponentCallbackDispatcher::handleKeyEvent(::OH_NativeXComponent *xComponent,
void *window)
145 auto *receiver = findCallbackReceiverOrNull(xComponent);
146 if (receiver !=
nullptr) {
147 receiver->onInputEvent(
149 reinterpret_cast<::OHNativeWindow *>(window));
153void XComponentCallbackDispatcher::handleMouseEvent(::OH_NativeXComponent *xComponent,
void *window)
155 auto *receiver = findCallbackReceiverOrNull(xComponent);
156 if (receiver !=
nullptr) {
157 receiver->onInputEvent(
159 reinterpret_cast<::OHNativeWindow *>(window));
163void XComponentCallbackDispatcher::handleSurfaceCreated(::OH_NativeXComponent *xComponent,
void *window)
165 qOhosPrintfDebug(
"%s: xc: %p window: %p", Q_FUNC_INFO, xComponent, window);
166 auto *receiver = findCallbackReceiverOrNull(xComponent);
167 if (receiver !=
nullptr) {
168 receiver->onSurfaceEvent(
170 reinterpret_cast<::OHNativeWindow*>(window));
174void XComponentCallbackDispatcher::handleSurfaceChanged(::OH_NativeXComponent *xComponent,
void *window)
176 qOhosPrintfDebug(
"%s: xc: %p window: %p", Q_FUNC_INFO, xComponent, window);
177 auto *receiver = findCallbackReceiverOrNull(xComponent);
178 if (receiver !=
nullptr) {
179 receiver->onSurfaceEvent(
181 reinterpret_cast<::OHNativeWindow*>(window));
185void XComponentCallbackDispatcher::handleSurfaceDestroyed(::OH_NativeXComponent *xComponent,
void *window)
187 qOhosPrintfDebug(
"%s: xc: %p window: %p", Q_FUNC_INFO, xComponent, window);
188 auto *receiver = findCallbackReceiverOrNull(xComponent);
189 if (receiver !=
nullptr) {
190 receiver->onSurfaceEvent(
192 reinterpret_cast<::OHNativeWindow*>(window));
202 constexpr std::uint32_t transparentArgb8888 = 0;
204 auto stackNode = Node::createOrFail(::ARKUI_NODE_STACK);
205 stackNode->setAttributeOrFail(::NODE_STACK_ALIGN_CONTENT, ::ARKUI_ALIGNMENT_TOP_START);
206 stackNode->setAttributeOrFail(::NODE_Z_INDEX, createInfo
.zIndex);
207 stackNode->setAttributeOrFail(::NODE_CLIP,
true);
208 stackNode->setAttributeOrFail(::NODE_BACKGROUND_COLOR, transparentArgb8888);
209 stackNode->setAttributeOrFail(
211 std::make_tuple(::ARKUI_BLEND_MODE_SRC_OVER, ::BLEND_APPLY_TYPE_FAST));
212 stackNode->setLengthMetricUnitOrFail(::ARKUI_LENGTH_METRIC_UNIT_PX);
214 ::ArkUI_NodeAttributeType widthAttribute;
215 ::ArkUI_NodeAttributeType heightAttribute;
219 widthAttribute = ::NODE_WIDTH;
220 heightAttribute = ::NODE_HEIGHT;
223 widthAttribute = ::NODE_WIDTH_PERCENT;
224 heightAttribute = ::NODE_HEIGHT_PERCENT;
228 stackNode->setAttributeOrFail(
230 static_cast<
float>(createInfo.size.width()));
231 stackNode->setAttributeOrFail(
233 static_cast<
float>(createInfo.size.height()));
234 stackNode->setAttributeOrFail(
236 toFloatArray(createInfo.offset));
238 auto xComponentNode = Node::createOrFail(::ARKUI_NODE_XCOMPONENT);
239 xComponentNode->setAttributeOrFail(::NODE_XCOMPONENT_TYPE, createInfo.xComponentType);
240 xComponentNode->setAttributeOrFail(::NODE_FOCUS_ON_TOUCH, createInfo
.focusOnTouch);
241 xComponentNode->setAttributeOrFail(
242 ::NODE_BACKGROUND_COLOR,
243 createInfo.backgroundColor.has_value()
244 ? createInfo.backgroundColor.value().rgba()
245 : transparentArgb8888);
246 xComponentNode->setAttributeOrFail(
247 ::NODE_XCOMPONENT_ID,
248 createInfo.xComponentId.stringId());
249 xComponentNode->setAttributeOrFail(
251 std::make_tuple(::ARKUI_BLEND_MODE_SRC_OVER, ::BLEND_APPLY_TYPE_FAST));
252 xComponentNode->setAttributeOrFail(::NODE_FOCUSABLE, createInfo
.focusable);
253 xComponentNode->setAttributeOrFail(::NODE_RENDER_FIT, createInfo.renderFit);
254 xComponentNode->setAttributeOrFail(::NODE_Z_INDEX, createInfo
.zIndex);
255 xComponentNode->setLengthMetricUnitOrFail(::ARKUI_LENGTH_METRIC_UNIT_PX);
257 stackNode->addChildOrFail(*xComponentNode);
260 .nodeType = ::ArkUI_NodeType::ARKUI_NODE_XCOMPONENT,
261 .content = xComponentNode->handle(),
262 .stack = stackNode->handle(),
264 .nodePrivate =
nullptr,
267 auto node =
std::shared_ptr<QQtEmbeddedWindowNode>(
268 new QQtEmbeddedWindowNode(
std::move(stackNode),
std::move(xComponentNode),
std::move(windowId)));
269 if (createInfo.optParent.has_value())
270 node->setParentOrReparent(createInfo.optParent.value());
272 node->setNodeVisibility(
false);
279 auto *xComponent = ::OH_NativeXComponent_GetNativeXComponent(contentNode().handle());
280 if (xComponent ==
nullptr)
281 qOhosReportFatalErrorAndAbort(
"::OH_NativeXComponent_GetNativeXComponent failed");
286 std::unique_ptr<QXComponentCallbackReceiver> callbackReceiver)
288 m_callbackReceiver = std::move(callbackReceiver);
293 ::OHNativeWindow *nativeWindow)
295 qOhosPrintfDebug(
"%s: node: %p eventType: %d surface: %p", Q_FUNC_INFO,
this, eventType, nativeWindow);
297 case SurfaceEventType::SurfaceCreated:
298 case SurfaceEventType::SurfaceChanged:
299 m_nativeWindow = nativeWindow;
301 case SurfaceEventType::SurfaceDestroyed:
302 m_nativeWindow =
nullptr;
306 if (m_callbackReceiver)
307 m_callbackReceiver->onSurfaceEvent(eventType, nativeWindow);
312 ::OHNativeWindow *nativeWindow)
314 if (m_callbackReceiver)
315 m_callbackReceiver->onInputEvent(inputEventType, nativeWindow);
320 if (m_callbackReceiver)
321 m_callbackReceiver->onHoverEvent(isHover);
324QQtEmbeddedWindowNode::QQtEmbeddedWindowNode(
325 std::unique_ptr<Node> stackNode,
326 std::unique_ptr<Node> xComponentNode,
327 std::unique_ptr<QtOhos::WindowIdStruct> windowId)
329 , m_xComponentCallbackDispatcherToken(
330 XComponentCallbackDispatcher::instance().registerCallbackReceiver(
331 renderXComponent().handle(),
this))
339 m_optAreaChangedReceiver = std::move(areaChangeReceiver);
341 stackNode().setEventHandler(
342 ::ArkUI_NodeEventType::NODE_EVENT_ON_AREA_CHANGE,
343 [
this](::ArkUI_NodeEvent *) {
344 m_optAreaChangedReceiver(nodeAreaInfo());
350 m_optFocusedChangedReceiver = std::move(focusChangedReceiver);
352 stackNode().setEventHandler(
353 ::ArkUI_NodeEventType::NODE_ON_FOCUS,
354 [
this](::ArkUI_NodeEvent *) {
355 m_optFocusedChangedReceiver(
true);
358 stackNode().setEventHandler(
359 ::ArkUI_NodeEventType::NODE_ON_BLUR,
360 [
this](::ArkUI_NodeEvent *) {
361 m_optFocusedChangedReceiver(
false);
367 m_optVisibilityChangedReceiver = std::move(visibilityChangedReceiver);
369 stackNode().setEventHandler(
370 ::ArkUI_NodeEventType::NODE_EVENT_ON_APPEAR,
371 [
this](::ArkUI_NodeEvent *) {
372 m_optVisibilityChangedReceiver(
true);
375 stackNode().setEventHandler(
376 ::ArkUI_NodeEventType::NODE_EVENT_ON_DISAPPEAR,
377 [
this](::ArkUI_NodeEvent *) {
378 m_optVisibilityChangedReceiver(
false);
383 QOhosConsumer<
const ::ArkUI_UIInputEvent *> touchInterceptReceiver)
385 m_optTouchInterceptReceiver = std::move(touchInterceptReceiver);
386 stackNode().setEventHandler(
387 ::ArkUI_NodeEventType::NODE_ON_TOUCH_INTERCEPT,
388 [
this](::ArkUI_NodeEvent *nodeEvent) {
389 auto *uiInputEvent = ::OH_ArkUI_NodeEvent_GetInputEvent(nodeEvent);
390 if (uiInputEvent ==
nullptr)
392 m_optTouchInterceptReceiver(uiInputEvent);
398 ::ArkUI_IntOffset screenPositionPx;
399 callArkUiOrFailOnErrorResult(
400 Q_OHOS_NAMED_FUNC(::OH_ArkUI_NodeUtils_GetLayoutPositionInScreen),
401 stackNode().handle(), &screenPositionPx);
403 ::ArkUI_IntSize screenSizePx;
404 callArkUiOrFailOnErrorResult(
405 Q_OHOS_NAMED_FUNC(::OH_ArkUI_NodeUtils_GetLayoutSize),
406 stackNode().handle(), &screenSizePx);
409 QPoint(screenPositionPx.x, screenPositionPx.y),
410 QSize(screenSizePx.width, screenSizePx.height));
415 return QArkUi::Node::tryfindChild(
416 stackNode().handle(),
417 [&](::ArkUI_NodeHandle nodeHandle) {
418 return !QArkUi::Node::isQtManagedNode(nodeHandle);
424 return NodeAreaInfo {
425 .screenGeometryPixels = nodeScreenGeometryPixels(),
426 .windowRelativeOffsetPixels = windowRelativeOffsetPixels(),
427 .parentRelativeOffsetPixels = parentRelativeOffsetPixels(),
428 .globalRelativeOffsetPixels = globalRelativeOffsetPixels(),
434 ::ArkUI_IntOffset windowPositionPx;
435 callArkUiOrFailOnErrorResult(
436 Q_OHOS_NAMED_FUNC(::OH_ArkUI_NodeUtils_GetLayoutPositionInWindow),
437 stackNode().handle(), &windowPositionPx);
438 return QPoint(windowPositionPx.x, windowPositionPx.y);
443 ::ArkUI_IntOffset parentPositionPx;
444 callArkUiOrFailOnErrorResult(
445 Q_OHOS_NAMED_FUNC(::OH_ArkUI_NodeUtils_GetLayoutPosition),
446 stackNode().handle(), &parentPositionPx);
447 return QPoint(parentPositionPx.x, parentPositionPx.y);
452 return QArkUi::Node::nodeGlobalPosition(stackNode().handle());
void setTouchInterceptReceiver(QOhosConsumer< const ::ArkUI_UIInputEvent * > touchInterceptReceiver)
void onHoverEvent(bool isHover) override
QPoint parentRelativeOffsetPixels() const
QPoint globalRelativeOffsetPixels() const
void setFocusedChangeReceiver(QOhosConsumer< bool > focus)
bool hasNonQtManagedChildren() const
~QQtEmbeddedWindowNode() override
void onSurfaceEvent(SurfaceEventType surfaceEventType, ::OHNativeWindow *nativeWindow) override
NodeAreaInfo nodeAreaInfo() const
static std::shared_ptr< QQtEmbeddedWindowNode > createOrFail(const CreateInfo &createInfo)
QRect nodeScreenGeometryPixels() const
void onInputEvent(InputEventType inputEventType, ::OHNativeWindow *nativeWindow) override
QPoint windowRelativeOffsetPixels() const
void setCallbackReceiver(std::unique_ptr< QXComponentCallbackReceiver > callbackReceiver)
QXComponentRender renderXComponent() const
void setAreaChangeReceiver(QOhosConsumer< NodeAreaInfo > areaChangeReceiver)
void setVisibilityChangeReceiver(QOhosConsumer< bool > visibilityChangedReceiver)
virtual ~QXComponentCallbackReceiver()
Combined button and popup list for selecting options.
bool isNativeNodeApiMouseEventsEnabled()
bool isNativeNodeApiKeyEventsEnabled()
QXComponent< QXComponentType::Render > QXComponentRender