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
qohosforeignwindow.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 <qohosforeignwindow.h>
5
6#include <QtCore/private/qcore_ohos_p.h>
7#include <QtGui/private/qhighdpiscaling_p.h>
8#include <arkui/native_node.h>
9#include <arkui/native_type.h>
10#include <qarkui/qembeddedwindownode.h>
11#include <qohosplatformwindow.h>
12#include <qohosplatformscreen.h>
13#include <qohosutils.h>
14#include <qpa/qplatformscreen.h>
15#include <qpa/qwindowsysteminterface.h>
16#include <render/qohosview.h>
17
19
20QOhosForeignWindow::QOhosForeignWindow(QWindow *qWindow, WId windowId)
21 : QOhosPlatformWindow(qWindow)
22{
23 QOhosJsThreadGateway::runAndWait([&](QOhosJsState &) {
24 auto windowIdStruct = std::unique_ptr<QtOhos::WindowIdStruct>(
25 reinterpret_cast<QtOhos::WindowIdStruct *>(windowId));
26
27 if (windowIdStruct->content == nullptr)
28 qOhosReportFatalErrorAndAbort("Invalid WId data, content node is empty");
29
30 bool windowIdHasStackNode = windowIdStruct->stack != nullptr;
31 std::unique_ptr<QArkUi::Node> stackNode;
32 std::unique_ptr<QArkUi::Node> embeddedComponentNode =
33 QArkUi::Node::takeOwnershipOfExternalNode(windowIdStruct->content);
34
35 if (windowIdHasStackNode) {
36 stackNode = QArkUi::Node::takeOwnershipOfExternalNode(windowIdStruct->stack);
37 } else {
38 stackNode = QArkUi::Node::createOrFail(::ARKUI_NODE_STACK);
39 stackNode->setAttributeOrFail(::NODE_ACCESSIBILITY_ROLE, ::ARKUI_NODE_STACK);
40 stackNode->setAttributeOrFail(::NODE_STACK_ALIGN_CONTENT, ::ARKUI_ALIGNMENT_TOP_START);
41 stackNode->setAttributeOrFail(::NODE_Z_INDEX, QArkUi::QEmbeddedWindowNode::minimumNodeZIndexValue);
42 stackNode->addChildOrFail(*embeddedComponentNode);
43 windowIdStruct->stack = stackNode->handle();
44 }
45
46 *m_jsStateData = JsStateData {
47 .embeddedWindow = std::make_unique<QArkUi::QEmbeddedWindowNode>(
48 std::move(stackNode), std::move(embeddedComponentNode),
49 std::move(windowIdStruct)),
50 };
51 },
52 Q_FUNC_INFO);
53}
54
55void QOhosForeignWindow::initialize()
56{
57}
58
59bool QOhosForeignWindow::isForeignWindow() const
60{
61 return true;
62};
63
64void QOhosForeignWindow::setGeometry(const QRect &unscaledGeometry)
65{
66 auto scaledGeometry = QHighDpi::fromNative(QRectF(unscaledGeometry),
67 static_cast<QOhosPlatformScreen *>(screen())->pixelScalingCoefficient());
68
69 QOhosJsThreadGateway::runAndWait([&](QOhosJsState &) {
70 m_jsStateData->embeddedWindow->setSize(scaledGeometry.size());
71 m_jsStateData->embeddedWindow->setPosition(scaledGeometry.topLeft());
72 },
73 Q_FUNC_INFO);
74
75 setWindowGeometryFromOhos(unscaledGeometry);
76}
77
78WId QOhosForeignWindow::winId() const
79{
80 return QOhosJsThreadGateway::eval([this](QOhosJsState &) {
81 return reinterpret_cast<WId>(m_jsStateData->embeddedWindow->qtWindowId());
82 },
83 Q_FUNC_INFO);
84}
85
86void QOhosForeignWindow::setParent(const QPlatformWindow *window)
87{
88 if (window != nullptr && window->isForeignWindow())
89 qOhosReportFatalErrorAndAbort("Reparenting to foreign windows is not supported");
90
91 if (window == nullptr) {
92 QOhosJsThreadGateway::runAndWait([&](QOhosJsState &) {
93 m_jsStateData->embeddedWindow->detachFromParentIfPresent();
94 },
95 Q_FUNC_INFO);
96 return;
97 }
98
99 const auto *ohosPlatformWindow = static_cast<const QOhosPlatformWindow *>(window);
100 auto *view = ohosPlatformWindow->ownedViewOrNull();
101
102 if (view == nullptr)
103 qOhosReportFatalErrorAndAbort("view was null, but should not be");
104
105 view->addForeignWindowChild(this);
106}
107
108void QOhosForeignWindow::setVisible(bool visible)
109{
110 QOhosJsThreadGateway::runAndWait([&](QOhosJsState &) {
111 m_jsStateData->embeddedWindow->setNodeVisibility(visible);
112 },
113 Q_FUNC_INFO);
114
115 setExposedFromOhos(visible);
116}
117
118QMargins QOhosForeignWindow::frameMargins() const
119{
120 return QMargins{};
121}
122
124{
125 return *m_jsStateData->embeddedWindow;
126}
127
128QT_END_NAMESPACE
QArkUi::QEmbeddedWindowNode & embeddedWindowNodeInJsThread()
void initialize() override
Called as part of QWindow::create(), after constructing the window.
void setVisible(bool visible) override
Reimplemented in subclasses to show the surface if visible is true, and hide it if visible is false.
WId winId() const override
Reimplement in subclasses to return a handle to the native window.
void setGeometry(const QRect &unscaledGeometry) override
This function is called by Qt whenever a window is moved or resized using the QWindow API.
void setParent(const QPlatformWindow *window) override
This function is called to enable native child window in QPA.
bool isForeignWindow() const override
QMargins frameMargins() const override
void setExposedFromOhos(bool exposed)
\inmodule QtGui
Definition qwindow.h:64
Combined button and popup list for selecting options.