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