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