Android: Resize QWindow when its QtView is resized

If the Android View is resized, the QWindow instantiated by it
should be resized accordingly.

Task-number: QTBUG-122626
Change-Id: I7bfbca149f927718d1e28cdabfa8759afbd06039
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
(cherry picked from commit 7942f7eedf4a8d7fac82737ea490f3c443e82149)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tinja Paavoseppä 2024-02-23 14:41:30 +02:00 committed by Qt Cherry-pick Bot
parent 589529e728
commit dc5894b865
3 changed files with 26 additions and 1 deletions

View File

@ -40,6 +40,7 @@ abstract class QtView extends QtLayout {
abstract protected void createWindow(long parentWindowRef);
private static native void setWindowVisible(long windowReference, boolean visible);
private static native void resizeWindow(long windowReference, int width, int height);
/**
* Create QtView for embedding a QWindow. Instantiating a QtView will load the Qt libraries
@ -59,6 +60,20 @@ abstract class QtView extends QtLayout {
QtEmbeddedLoader loader = new QtEmbeddedLoader(context);
m_delegate = QtEmbeddedDelegateFactory.create((Activity)context);
loader.setMainLibraryName(appLibName);
addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
if (m_windowReference != 0L) {
final int oldWidth = oldRight - oldLeft;
final int oldHeight = oldBottom - oldTop;
final int newWidth = right - left;
final int newHeight = bottom - top;
if (oldWidth != newWidth || oldHeight != newHeight)
resizeWindow(m_windowReference, right - left, bottom - top);
}
}
});
loader.loadQtLibraries();
// Start Native Qt application
m_delegate.startNativeApplication(loader.getApplicationParameters(),

View File

@ -45,6 +45,13 @@ namespace QtAndroidWindowEmbedding {
});
}
void resizeWindow(JNIEnv *, jclass, jlong windowRef, jint width, jint height)
{
QWindow *window = reinterpret_cast<QWindow*>(windowRef);
window->setWidth(width);
window->setHeight(height);
}
bool registerNatives(QJniEnvironment& env) {
using namespace QtJniTypes;
bool success = env.registerNativeMethods(Traits<QtEmbeddedDelegate>::className(),
@ -52,7 +59,8 @@ namespace 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(setWindowVisible, QtAndroidWindowEmbedding),
Q_JNI_NATIVE_SCOPED_METHOD(resizeWindow, QtAndroidWindowEmbedding)});
return success;
}

View File

@ -31,6 +31,8 @@ namespace QtAndroidWindowEmbedding
Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE(deleteWindow)
void setWindowVisible(JNIEnv *, jclass, jlong window, jboolean visible);
Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE(setWindowVisible)
void resizeWindow(JNIEnv *, jclass, jlong windowRef, jint width, jint height);
Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE(resizeWindow)
};
QT_END_NAMESPACE