summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/android/androidwindowembedding.cpp
blob: 230776f5713746bd8ef07947a3df915b77c695ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "androidwindowembedding.h"

#include <QtCore/qcoreapplication.h>
#include <QtCore/qjnienvironment.h>
#include <QtCore/qjniobject.h>
#include <QtCore/qjnitypes.h>
#include <QtGui/qwindow.h>

QT_BEGIN_NAMESPACE

Q_DECLARE_JNI_CLASS(QtView, "org/qtproject/qt/android/QtView");
Q_DECLARE_JNI_CLASS(QtEmbeddedDelegate, "org/qtproject/qt/android/QtEmbeddedDelegate");

namespace QtAndroidWindowEmbedding {
    void createRootWindow(JNIEnv *, jclass, QtJniTypes::View rootView,
                          jint x, jint y, jint width, jint height)
    {
        // QWindow should be constructed on the Qt thread rather than directly in the caller thread
        // To avoid hitting checkReceiverThread assert in QCoreApplication::doNotify
        QMetaObject::invokeMethod(qApp, [rootView, x, y, width, height] {
            QWindow *parentWindow = QWindow::fromWinId(reinterpret_cast<WId>(rootView.object()));
            parentWindow->setGeometry(x, y, width, height);
            rootView.callMethod<void>("createWindow", reinterpret_cast<jlong>(parentWindow));
        });
    }

    void deleteWindow(JNIEnv *, jclass, jlong windowRef)
    {
        QWindow *window = reinterpret_cast<QWindow*>(windowRef);
        window->deleteLater();
    }

    void setWindowVisible(JNIEnv *, jclass, jlong windowRef, jboolean visible)
    {
        QMetaObject::invokeMethod(qApp, [windowRef, visible] {
            QWindow *window = reinterpret_cast<QWindow*>(windowRef);
            if (visible) {
                window->showNormal();
                if (!window->parent()->isVisible())
                    window->parent()->showNormal();
            } else {
                window->hide();
            }
        });
    }

    void resizeWindow(JNIEnv *, jclass, jlong windowRef, jint x, jint y, jint width, jint height)
    {
        QMetaObject::invokeMethod(qApp, [windowRef, x, y, width, height] {
            QWindow *window = reinterpret_cast<QWindow*>(windowRef);
            QWindow *parent = window->parent();
            if (parent)
                parent->setGeometry(x, y, width, height);
            window->setGeometry(0, 0, width, height);
        });
    }

    bool registerNatives(QJniEnvironment& env) {
        using namespace QtJniTypes;
        bool success = env.registerNativeMethods(Traits<QtEmbeddedDelegate>::className(),
                            {Q_JNI_NATIVE_SCOPED_METHOD(createRootWindow, QtAndroidWindowEmbedding),
                             Q_JNI_NATIVE_SCOPED_METHOD(deleteWindow, QtAndroidWindowEmbedding)});

        success &= env.registerNativeMethods(Traits<QtView>::className(),
                            {Q_JNI_NATIVE_SCOPED_METHOD(setWindowVisible, QtAndroidWindowEmbedding),
                             Q_JNI_NATIVE_SCOPED_METHOD(resizeWindow, QtAndroidWindowEmbedding)});
        return success;

    }
}

QT_END_NAMESPACE