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
qxcomponentregistry.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
4#include <qarkui/qxcomponentregistry.h>
5
6#include <QtCore/private/qohoslogger_p.h>
7#include <qohosutils.h>
8
10
11namespace QArkUi {
12
15{
17 auto it = m_xComponents.find(id);
18 if (it != m_xComponents.end()) {
19 result = it->second;
20 m_xComponents.erase(it);
21 }
22 return result;
23}
24
26{
27 static QXComponentRegistry registry;
28 return registry;
29}
30
31bool QXComponentRegistry::Init(napi_env env, napi_value exports)
32{
33 auto exportsObj = QNapi::checkedCast<QNapi::Object>(QNapi::Value{env, exports});
34 auto xComponentWrappedValue = QNapi::getOptionalPropOrEmpty<QNapi::Object>(
35 exportsObj,
36 OH_NATIVE_XCOMPONENT_OBJ);
37 if (xComponentWrappedValue.IsEmpty())
38 return false;
39
40 ::OH_NativeXComponent *xComponent = nullptr;
41 auto status = ::napi_unwrap(
42 env,
43 xComponentWrappedValue,
44 reinterpret_cast<void **>(&xComponent));
45 if (status != ::napi_ok) {
46 qOhosPrintfError("Failed to unwrap xcomponent with napi_status: %d", status);
47 return false;
48 }
49
50 auto optXComponentId = QXComponentId::tryCreateFromXComponent(xComponent);
51 if (!optXComponentId.hasValue()) {
52 qOhosPrintfError("Failed to retrieve id from xcomponent. Ignoring the component");
53 return false;
54 }
55
56 auto xComponentId = optXComponentId.value();
57
58 auto optXComponentIdType = xComponentId.recognizedType();
59 if (!optXComponentIdType.hasValue()) {
60 qOhosPrintfError(
61 "Ignoring xComponent due to unrecognized id value: %s",
62 xComponentId.stringId().c_str());
63 return false;
64 }
65
66 bool canRegister = false;
67 switch (optXComponentIdType.value()) {
69 canRegister = false;
70 break;
74 canRegister = true;
75 break;
76 }
77
78 if (!canRegister) {
79 qOhosPrintfDebug(
80 "Ignoring xComponent because its id type is not supported in registry. id: %s type: %d",
81 xComponentId.stringId().c_str(),
82 optXComponentIdType.value());
83 return false;
84 }
85
86 auto &registry = instance();
87
88 bool xComponentRegistered;
89 std::tie(std::ignore, xComponentRegistered) =
90 registry.m_xComponents.emplace(xComponentId, QXComponentNode(xComponent));
91
92 if (!xComponentRegistered) {
93 qOhosReportFatalErrorAndAbort(
94 "Error: Duplicate xComponent detected. Duplicate ID: %s",
95 xComponentId.stringId().c_str());
96 }
97
98 return true;
99}
100
101}
102
103QT_END_NAMESPACE
static QXComponentRegistry & instance()
QOhosOptional< QXComponentNode > tryTakeNodeByXComponentId(const QXComponentId &id)
std::enable_if_t< qohosplugincore_h_detail::isQOhosOptional< QOhosInvokeResult< Func, T > >, QOhosInvokeResult< Func, T > > andThen(Func &&func) const
Combined button and popup list for selecting options.
QXComponent< QXComponentType::Node > QXComponentNode
Definition qxcomponent.h:44