summaryrefslogtreecommitdiffstats
path: root/src/android/jar/src/org/qtproject/qt/android/QtWindow.java
blob: 4f27532c0d272b1234645d3a2f24d9cbd714071d (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// 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

package org.qtproject.qt.android;

import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.util.Log;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;

import java.util.HashMap;

public class QtWindow implements QtSurfaceInterface, QtLayout.QtTouchListener {
    private final static String TAG = "QtWindow";

    private View m_surfaceContainer;
    private QtLayout m_layout;
    private View m_nativeView;
    private HashMap<Integer, QtWindow> m_childWindows = new HashMap<Integer, QtWindow>();
    private QtWindow m_parentWindow;
    private int m_id;
    private GestureDetector m_gestureDetector;

    private static native void setSurface(int windowId, Surface surface);

    public QtWindow(Context context, QtWindow parentWindow)
    {
        m_id = View.generateViewId();
        m_layout = new QtLayout(context, this);
        setParent(parentWindow);
        QtNative.runAction(() -> {
            m_gestureDetector =
                new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
                    public void onLongPress(MotionEvent event) {
                        QtInputDelegate.longPress(getId(), (int) event.getX(), (int) event.getY());
                    }
                });
            m_gestureDetector.setIsLongpressEnabled(true);
        });
    }

    void setVisible(boolean visible) {
        QtNative.runAction(() -> {
            if (visible)
                m_layout.setVisibility(View.VISIBLE);
            else
                m_layout.setVisibility(View.INVISIBLE);
        });
    }

    public int getId()
    {
        return m_id;
    }

    public QtLayout getLayout()
    {
        return m_layout;
    }

    @Override
    public void onSurfaceChanged(Surface surface)
    {
        setSurface(getId(), surface);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        QtInputDelegate.sendTouchEvent(event, getId());
        m_gestureDetector.onTouchEvent(event);
        return true;
    }

    @Override
    public boolean onTrackballEvent(MotionEvent event)
    {
        QtInputDelegate.sendTrackballEvent(event, getId());
        return true;
    }

    @Override
    public boolean onGenericMotionEvent(MotionEvent event)
    {
        return QtInputDelegate.sendGenericMotionEvent(event, getId());
    }

    public void removeWindow()
    {
        if (m_parentWindow != null)
            m_parentWindow.removeChildWindow(getId());
    }

    public void createSurface(final boolean onTop,
                              final int x, final int y, final int w, final int h,
                              final int imageDepth, final boolean isOpaque,
                              final int surfaceContainerType) // TODO constant for type
    {
        QtNative.runAction(new Runnable() {
            @Override
            public void run() {
                if (m_surfaceContainer != null)
                    m_layout.removeView(m_surfaceContainer);

                m_layout.setLayoutParams(new QtLayout.LayoutParams(w, h, x, y));
                if (surfaceContainerType == 0) {
                    m_surfaceContainer = new QtSurface(m_layout.getContext(), QtWindow.this,
                                                       onTop, imageDepth);
                } else {
                    m_surfaceContainer = new QtTextureView(m_layout.getContext(), QtWindow.this, isOpaque);
                }
                 m_surfaceContainer.setLayoutParams(new QtLayout.LayoutParams(
                                                            ViewGroup.LayoutParams.MATCH_PARENT,
                                                            ViewGroup.LayoutParams.MATCH_PARENT));
                // The surface container of this window will be added as the first of the stack.
                // All other views are stacked based on the order they are created.
                m_layout.addView(m_surfaceContainer, 0);
            }
        });
    }

    public void destroySurface()
    {
        QtNative.runAction(new Runnable() {
            @Override
            public void run() {
                if (m_surfaceContainer != null) {
                    m_layout.removeView(m_surfaceContainer);
                    m_surfaceContainer = null;
                }
            }
        });
    }

    public void setGeometry(final int x, final int y, final int w, final int h)
    {
        QtNative.runAction(new Runnable() {
            @Override
            public void run() {
                if (m_layout.getContext() instanceof QtActivityBase)
                    m_layout.setLayoutParams(new QtLayout.LayoutParams(w, h, x, y));
            }
        });
    }

    public void addChildWindow(QtWindow window)
    {
        QtNative.runAction(new Runnable() {
            @Override
            public void run() {
                m_childWindows.put(window.getId(), window);
                m_layout.addView(window.getLayout(), m_layout.getChildCount());
            }
        });
    }

    public void removeChildWindow(int id)
    {
        QtNative.runAction(new Runnable() {
            @Override
            public void run() {
                if (m_childWindows.containsKey(id))
                    m_layout.removeView(m_childWindows.remove(id).getLayout());
            }
        });
    }

    public void setNativeView(final View view,
                              final int x, final int y, final int w, final int h)
    {
        QtNative.runAction(new Runnable() {
            @Override
            public void run() {
                if (m_nativeView != null)
                    m_layout.removeView(m_nativeView);

                m_nativeView = view;
                m_layout.setLayoutParams(new QtLayout.LayoutParams(w, h, x, y));
                m_nativeView.setLayoutParams(new QtLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                                       ViewGroup.LayoutParams.MATCH_PARENT));
                m_layout.addView(m_nativeView);
            }
        });
    }

    public void bringChildToFront(int id)
    {
        QtNative.runAction(()-> {
            QtWindow window = m_childWindows.get(id);
            if (window != null) {
                if (m_layout.getChildCount() > 0)
                    m_layout.moveChild(window.getLayout(), m_layout.getChildCount() - 1);
            }
        });
    }

    public void bringChildToBack(int id) {
        QtNative.runAction(new Runnable() {
            @Override
            public void run() {
                QtWindow window = m_childWindows.get(id);
                if (window != null) {
                    m_layout.moveChild(window.getLayout(), 0);
                }
            }
        });
    }

    public void removeNativeView()
    {
        QtNative.runAction(new Runnable() {
            @Override
            public void run() {
                if (m_nativeView != null) {
                    m_layout.removeView(m_nativeView);
                    m_nativeView = null;
                }
            }
        });
    }

    void setParent(QtWindow parentWindow)
    {
        if (m_parentWindow == parentWindow)
            return;

        if (m_parentWindow != null)
            m_parentWindow.removeChildWindow(getId());

        m_parentWindow = parentWindow;
        if (m_parentWindow != null)
            m_parentWindow.addChildWindow(this);
    }

    QtWindow parent()
    {
        return m_parentWindow;
    }
}