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
androidwindowembedding.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 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// Qt-Security score:significant reason:default
4
6
7#include <QtCore/qcoreapplication.h>
8#include <QtCore/qjnienvironment.h>
9#include <QtCore/qjniobject.h>
10#include <QtCore/qjnitypes.h>
11#include <QtGui/qwindow.h>
12
14
15Q_DECLARE_JNI_CLASS(QtView, "org/qtproject/qt/android/QtView");
16
18 void createRootWindow(JNIEnv *, jclass, QtJniTypes::View rootView,
19 jint x, jint y, jint width, jint height)
20 {
21 // QWindow should be constructed on the Qt thread rather than directly in the caller thread
22 // To avoid hitting checkReceiverThread assert in QCoreApplication::doNotify
23 QMetaObject::invokeMethod(qApp, [rootView, x, y, width, height] {
24 QWindow *parentWindow = QWindow::fromWinId(reinterpret_cast<WId>(rootView.object()));
25 if (!parentWindow)
26 return;
27 parentWindow->setGeometry(x, y, width, height);
28 rootView.callMethod<void>("createWindow", reinterpret_cast<jlong>(parentWindow));
29 });
30 }
31
32 void deleteWindow(JNIEnv *, jclass, jlong windowRef)
33 {
34 QWindow *window = reinterpret_cast<QWindow*>(windowRef);
35 if (window)
36 window->deleteLater();
37 }
38
39 void setWindowVisible(JNIEnv *, jclass, jlong windowRef, jboolean visible)
40 {
41 QMetaObject::invokeMethod(qApp, [windowRef, visible] {
42 QWindow *window = reinterpret_cast<QWindow*>(windowRef);
43 if (!window)
44 return;
45 if (visible) {
46 window->showNormal();
47 QWindow *parent = window->parent();
48 if (parent && !parent->isVisible())
49 parent->showNormal();
50 } else {
51 window->hide();
52 }
53 });
54 }
55
56 void resizeWindow(JNIEnv *, jclass, jlong windowRef, jint x, jint y, jint width, jint height)
57 {
58 QMetaObject::invokeMethod(qApp, [windowRef, x, y, width, height] {
59 QWindow *window = reinterpret_cast<QWindow*>(windowRef);
60 if (!window)
61 return;
62 QWindow *parent = window->parent();
63 if (parent)
64 parent->setGeometry(x, y, width, height);
65 window->setGeometry(0, 0, width, height);
66 });
67 }
68
69 bool registerNatives(QJniEnvironment& env) {
70 return env.registerNativeMethods(
71 QtJniTypes::Traits<QtJniTypes::QtView>::className(),
72 { Q_JNI_NATIVE_SCOPED_METHOD(createRootWindow, QtAndroidWindowEmbedding),
73 Q_JNI_NATIVE_SCOPED_METHOD(deleteWindow, QtAndroidWindowEmbedding),
74 Q_JNI_NATIVE_SCOPED_METHOD(setWindowVisible, QtAndroidWindowEmbedding),
75 Q_JNI_NATIVE_SCOPED_METHOD(resizeWindow, QtAndroidWindowEmbedding) });
76 }
77}
78
79QT_END_NAMESPACE
Combined button and popup list for selecting options.
bool registerNatives(QJniEnvironment &env)
void setWindowVisible(JNIEnv *, jclass, jlong windowRef, jboolean visible)
void createRootWindow(JNIEnv *, jclass, QtJniTypes::View rootView, jint x, jint y, jint width, jint height)
void resizeWindow(JNIEnv *, jclass, jlong windowRef, jint x, jint y, jint width, jint height)
void deleteWindow(JNIEnv *, jclass, jlong windowRef)