aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/platform/android/qandroidviewsignalmanager_p.h
blob: 2b9ea710b4804b4d67fc64395cb445477f6e4eb7 (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
76
77
78
79
80
81
82
83
84
85
// Copyright (C) 2024 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

#ifndef QANDROIDVIEWSIGNALMANAGER_P_H
#define QANDROIDVIEWSIGNALMANAGER_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtCore/qhash.h>
#include <QtCore/qjnitypes.h>
#include <QtCore/qjniobject.h>
#include <QtCore/qmap.h>
#include <QtCore/qobject.h>
#include <QtCore/qmutex.h>
#include <QtQuick/qquickview.h>

QT_BEGIN_NAMESPACE

class QAndroidViewSignalManager : public QObject
{
    using connection_key_t = int;

public:
    explicit QAndroidViewSignalManager(QQuickView *view, QObject *parent = nullptr);

    int qt_metacall(QMetaObject::Call call, int methodId, void **args) override;

    void removeConnection(connection_key_t signalIdx);
    bool addConnection(const QString &signalName,
                      const QJniArray<jclass> &argTypes,
                      const QJniObject &listener,
                      int id);

private:
    /*
        This will store the necessary information to call the listener
        when the signal is emitted, including the Java function name and
        signature so that we can call it quickly without recalculating those.
    */
    struct ConnectionInfo
    {
        QMetaObject::Connection connection;
        QJniObject listenerObject;
        QString qmlSignalName;

        QList<QMetaType::Type> qmlArgumentTypes;
        bool isPropertySignal;
        std::optional<int> qmlPropertyIndex; // Only filled if isPropertySignal
        int connectionId;
    };

    struct QueuedConnectionInfo
    {
        connection_key_t id;
        QString signalName;
        QJniArray<jclass> argTypes;
        QJniObject listener;
    };

    bool hasConnection(connection_key_t key) const;
    connection_key_t createNewSignalKey() const;
    void onViewStatusChanged(QQuickView::Status status);
    bool queueConnection(const QString &signalName,
                         const QJniArray<jclass> &argTypes,
                         const QJniObject &listener,
                         int id);

    QMap<connection_key_t, ConnectionInfo> m_connections;
    QQuickView *m_view;
    QVector<QueuedConnectionInfo> m_queuedConnections;
    QMutex m_queueMutex;
};

QT_END_NAMESPACE

#endif // QANDROIDVIEWSIGNALMANAGER_P_H