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
qxcomponent.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 <render/qxcomponent.h>
5
6#include <array>
7#include <cstdint>
8#include <qohosutils.h>
9
11
12namespace
13{
14
15const std::string xComponentPrefixForMainWindow = "__nnMainWindow_";
16const std::string xComponentPrefixForSubWindow = "__nnSubWindow_";
17const std::string xComponentPrefixForFloatWindow = "__nnFloatWindow_";
18const std::string xComponentPrefixForRenderXComponent = "RenderXComponent_";
19
20bool startsWith(const std::string &str, const std::string &prefix)
21{
22 return str.compare(0, prefix.size(), prefix) == 0;
23}
24
26{
27 static const std::pair<std::string, QXComponentId::RecognizedType> prefixToTypeMapping[] = {
28 {xComponentPrefixForMainWindow, QXComponentId::RecognizedType::NativeNodeMainWindow},
29 {xComponentPrefixForSubWindow, QXComponentId::RecognizedType::NativeNodeSubWindow},
30 {xComponentPrefixForFloatWindow, QXComponentId::RecognizedType::NativeNodeFloatWindow},
31 {xComponentPrefixForRenderXComponent, QXComponentId::RecognizedType::RenderXComponent},
32 };
33
34 for (const auto &prefixTypePair: prefixToTypeMapping) {
35 if (startsWith(idValue, prefixTypePair.first))
36 return prefixTypePair.second;
37 }
38
39 return {};
40}
41
42}
43
44bool QXComponentId::operator==(const QXComponentId &other) const
45{
46 return m_id == other.m_id;
47}
48
49bool QXComponentId::operator!=(const QXComponentId &other) const
50{
51 return m_id != other.m_id;
52}
53
54bool QXComponentId::operator<(const QXComponentId &other) const
55{
56 return m_id < other.m_id;
57}
58
60{
61 return QXComponentId(xComponentPrefixForMainWindow + qAbilityInstanceId);
62}
63
64QXComponentId QXComponentId::createForNativeNodeSubWindow(QtOhos::InternalWindowId windowId)
65{
66 return QXComponentId(xComponentPrefixForSubWindow + windowId.toStdString());
67}
68
69QXComponentId QXComponentId::createForRenderXComponent(QtOhos::InternalWindowId windowId)
70{
71 return QXComponentId(xComponentPrefixForRenderXComponent + windowId.toStdString());
72}
73
74QXComponentId QXComponentId::createForNativeNodeFloatWindow(QtOhos::InternalWindowId windowId)
75{
76 return QXComponentId(xComponentPrefixForFloatWindow + windowId.toStdString());
77}
78
79QXComponentId::QXComponentId(std::string id)
80 : m_id(std::move(id))
81 , m_optRecognizedType(tryMapXComponentIdValueToRecognizedType(m_id))
82{
83}
84
85std::string QXComponentId::stringId() const
86{
87 return m_id;
88}
89
90std::optional<QXComponentId> QXComponentId::tryCreateFromXComponent(::OH_NativeXComponent *xComponent)
91{
92 constexpr auto requiredBufferSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
93
94 if (xComponent == nullptr)
95 qOhosReportFatalErrorAndAbort("OH_NativeXComponent was null");
96
97 std::uint64_t xComponentIdLength = requiredBufferSize;
98 std::array<char, requiredBufferSize> xComponentIdData = {};
99 auto errorCode = ::OH_NativeXComponent_GetXComponentId(
100 xComponent,
101 xComponentIdData.data(),
102 &xComponentIdLength);
103 if (errorCode != ::OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
104 qOhosReportFatalErrorAndAbort(
105 "OH_NativeXComponent_GetXComponentId failed with error: %d", errorCode);
106 }
107
108 return QXComponentId(
109 std::string(
110 xComponentIdData.data(),
111 xComponentIdLength));
112}
113
115{
116 return QNapi::String::New(env, m_id);
117}
118
120{
121 return m_optRecognizedType;
122}
123
124QT_END_NAMESPACE
static QXComponentId createForNativeNodeMainWindow(const std::string &qAbilityInstanceId)
std::string stringId() const
QNapi::Value toNapiValue(napi_env env) const
bool operator!=(const QXComponentId &other) const
bool operator==(const QXComponentId &other) const
bool operator<(const QXComponentId &other) const
std::optional< RecognizedType > recognizedType() const
Combined button and popup list for selecting options.
const std::string xComponentPrefixForFloatWindow
bool startsWith(const std::string &str, const std::string &prefix)
const std::string xComponentPrefixForMainWindow
const std::string xComponentPrefixForRenderXComponent
const std::string xComponentPrefixForSubWindow
std::optional< QXComponentId::RecognizedType > tryMapXComponentIdValueToRecognizedType(const std::string &idValue)