diff options
author | Petri Virkkunen <[email protected]> | 2024-03-31 15:37:31 +0300 |
---|---|---|
committer | Petri Virkkunen <[email protected]> | 2024-05-21 16:15:13 +0300 |
commit | 9319576a594e2ac8f06aa67e137fff96d77d6671 (patch) | |
tree | 9244530af7cf018b1a1a302581260e1502d40f0e /src | |
parent | 2322c71cc7576ddfcb5d0897dc67798786980285 (diff) |
Android: Implement context menu backend interface
Implements interface for context menu in QtActivityDelegate and
QtEmbeddedDelegate.
Task-number: QTBUG-118874
Change-Id: I799ad1aca4beb789b87830b720abf0963ca09274
Reviewed-by: Tinja Paavoseppä <[email protected]>
Diffstat (limited to 'src')
5 files changed, 80 insertions, 20 deletions
diff --git a/src/android/jar/CMakeLists.txt b/src/android/jar/CMakeLists.txt index c9731786a6b..89561f5366b 100644 --- a/src/android/jar/CMakeLists.txt +++ b/src/android/jar/CMakeLists.txt @@ -43,6 +43,7 @@ set(java_sources src/org/qtproject/qt/android/BackendRegister.java src/org/qtproject/qt/android/QtWindowInterface.java src/org/qtproject/qt/android/QtAccessibilityInterface.java + src/org/qtproject/qt/android/QtMenuInterface.java ) qt_internal_add_jar(Qt${QtBase_VERSION_MAJOR}Android diff --git a/src/android/jar/src/org/qtproject/qt/android/QtActivityDelegate.java b/src/android/jar/src/org/qtproject/qt/android/QtActivityDelegate.java index fac42bc221c..4a8e82dfe04 100644 --- a/src/android/jar/src/org/qtproject/qt/android/QtActivityDelegate.java +++ b/src/android/jar/src/org/qtproject/qt/android/QtActivityDelegate.java @@ -31,8 +31,8 @@ import android.widget.PopupMenu; import java.util.HashMap; -class QtActivityDelegate - extends QtActivityDelegateBase implements QtWindowInterface, QtAccessibilityInterface +class QtActivityDelegate extends QtActivityDelegateBase + implements QtWindowInterface, QtAccessibilityInterface, QtMenuInterface { private static final String QtTAG = "QtActivityDelegate"; @@ -61,6 +61,8 @@ class QtActivityDelegate (QtWindowInterface)QtActivityDelegate.this); BackendRegister.registerBackend(QtAccessibilityInterface.class, (QtAccessibilityInterface)QtActivityDelegate.this); + BackendRegister.registerBackend(QtMenuInterface.class, + (QtMenuInterface)QtActivityDelegate.this); } } @@ -70,6 +72,7 @@ class QtActivityDelegate m_backendsRegistered = false; BackendRegister.unregisterBackend(QtWindowInterface.class); BackendRegister.unregisterBackend(QtAccessibilityInterface.class); + BackendRegister.unregisterBackend(QtMenuInterface.class); } } @@ -290,27 +293,25 @@ class QtActivityDelegate }); } - @UsedFromNativeCode + // QtMenuInterface implementation begin + @Override public void resetOptionsMenu() { QtNative.runAction(() -> m_activity.invalidateOptionsMenu()); } - @UsedFromNativeCode + @Override public void openOptionsMenu() { QtNative.runAction(() -> m_activity.openOptionsMenu()); } - private boolean m_contextMenuVisible = false; - - public void onCreatePopupMenu(Menu menu) + @Override + public void closeContextMenu() { - QtNative.fillContextMenu(menu); - m_contextMenuVisible = true; + QtNative.runAction(() -> m_activity.closeContextMenu()); } - @UsedFromNativeCode @Override public void openContextMenu(final int x, final int y, final int w, final int h) { @@ -330,11 +331,14 @@ class QtActivityDelegate popup.show(); }, 100); } + // QtMenuInterface implementation end - @UsedFromNativeCode - public void closeContextMenu() + private boolean m_contextMenuVisible = false; + + public void onCreatePopupMenu(Menu menu) { - QtNative.runAction(() -> m_activity.closeContextMenu()); + QtNative.fillContextMenu(menu); + m_contextMenuVisible = true; } @Override diff --git a/src/android/jar/src/org/qtproject/qt/android/QtEmbeddedDelegate.java b/src/android/jar/src/org/qtproject/qt/android/QtEmbeddedDelegate.java index 20d34180007..ef2226d840b 100644 --- a/src/android/jar/src/org/qtproject/qt/android/QtEmbeddedDelegate.java +++ b/src/android/jar/src/org/qtproject/qt/android/QtEmbeddedDelegate.java @@ -15,15 +15,19 @@ import android.os.Handler; import android.os.Looper; import android.util.DisplayMetrics; import android.util.Log; +import android.view.Menu; import android.view.View; import android.view.ViewGroup; +import android.widget.PopupMenu; import java.util.ArrayList; import java.util.HashMap; class QtEmbeddedDelegate extends QtActivityDelegateBase - implements QtNative.AppStateDetailsListener, QtEmbeddedViewInterface, QtWindowInterface + implements QtNative.AppStateDetailsListener, QtEmbeddedViewInterface, QtWindowInterface, + QtMenuInterface { + private static final String QtTAG = "QtEmbeddedDelegate"; // TODO simplistic implementation with one QtView, expand to support multiple views QTBUG-117649 private QtView m_view; private QtNative.ApplicationStateDetails m_stateDetails; @@ -93,9 +97,11 @@ class QtEmbeddedDelegate extends QtActivityDelegateBase if (details.isStarted && !m_backendsRegistered) { m_backendsRegistered = true; BackendRegister.registerBackend(QtWindowInterface.class, (QtWindowInterface)this); + BackendRegister.registerBackend(QtMenuInterface.class, (QtMenuInterface)this); } else if (!details.isStarted && m_backendsRegistered) { m_backendsRegistered = false; BackendRegister.unregisterBackend(QtWindowInterface.class); + BackendRegister.unregisterBackend(QtMenuInterface.class); } } } @@ -175,4 +181,36 @@ class QtEmbeddedDelegate extends QtActivityDelegateBase m_windowLoaded = true; } } + + // QtMenuInterface implementation begin + @Override + public void resetOptionsMenu() { QtNative.runAction(() -> m_activity.invalidateOptionsMenu()); } + + @Override + public void openOptionsMenu() { QtNative.runAction(() -> m_activity.openOptionsMenu()); } + + @Override + public void closeContextMenu() { QtNative.runAction(() -> m_activity.closeContextMenu()); } + + @Override + public void openContextMenu(final int x, final int y, final int w, final int h) + { + QtLayout layout = getQtLayout(); + layout.postDelayed(() -> { + final QtEditText focusedEditText = m_inputDelegate.getCurrentQtEditText(); + if (focusedEditText == null) { + Log.w(QtTAG, "No focused view when trying to open context menu"); + return; + } + layout.setLayoutParams(focusedEditText, new QtLayout.LayoutParams(w, h, x, y), false); + PopupMenu popup = new PopupMenu(m_activity, focusedEditText); + QtNative.fillContextMenu(popup.getMenu()); + popup.setOnMenuItemClickListener(menuItem -> + m_activity.onContextItemSelected(menuItem)); + popup.setOnDismissListener(popupMenu -> + m_activity.onContextMenuClosed(popupMenu.getMenu())); + popup.show(); + }, 100); + } + // QtMenuInterface implementation end } diff --git a/src/android/jar/src/org/qtproject/qt/android/QtMenuInterface.java b/src/android/jar/src/org/qtproject/qt/android/QtMenuInterface.java new file mode 100644 index 00000000000..556b9a57b94 --- /dev/null +++ b/src/android/jar/src/org/qtproject/qt/android/QtMenuInterface.java @@ -0,0 +1,11 @@ +// 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 +package org.qtproject.qt.android; + +@UsedFromNativeCode +interface QtMenuInterface { + void resetOptionsMenu(); + void openOptionsMenu(); + void closeContextMenu(); + void openContextMenu(final int x, final int y, final int w, final int h); +} diff --git a/src/plugins/platforms/android/androidjnimenu.cpp b/src/plugins/platforms/android/androidjnimenu.cpp index 8bf37d1af21..5f8a0b92e92 100644 --- a/src/plugins/platforms/android/androidjnimenu.cpp +++ b/src/plugins/platforms/android/androidjnimenu.cpp @@ -20,6 +20,8 @@ QT_BEGIN_NAMESPACE using namespace QtAndroid; +Q_DECLARE_JNI_CLASS(QtMenuInterface, "org/qtproject/qt/android/QtMenuInterface"); + namespace QtAndroidMenu { static QList<QAndroidPlatformMenu *> pendingContextMenus; @@ -44,12 +46,14 @@ namespace QtAndroidMenu void resetMenuBar() { - qtActivityDelegate().callMethod<void>("resetOptionsMenu"); + AndroidBackendRegister *reg = QtAndroid::backendRegister(); + reg->callInterface<QtJniTypes::QtMenuInterface, void>("resetOptionsMenu"); } void openOptionsMenu() { - qtActivityDelegate().callMethod<void>("openOptionsMenu"); + AndroidBackendRegister *reg = QtAndroid::backendRegister(); + reg->callInterface<QtJniTypes::QtMenuInterface, void>("openOptionsMenu"); } void showContextMenu(QAndroidPlatformMenu *menu, const QRect &anchorRect) @@ -59,16 +63,18 @@ namespace QtAndroidMenu pendingContextMenus.append(visibleMenu); visibleMenu = menu; menu->aboutToShow(); - qtActivityDelegate().callMethod<void>("openContextMenu", - anchorRect.x(), anchorRect.y(), - anchorRect.width(), anchorRect.height()); + AndroidBackendRegister *reg = QtAndroid::backendRegister(); + reg->callInterface<QtJniTypes::QtMenuInterface, void>("openContextMenu", anchorRect.x(), + anchorRect.y(), anchorRect.width(), + anchorRect.height()); } void hideContextMenu(QAndroidPlatformMenu *menu) { QMutexLocker lock(&visibleMenuMutex); if (visibleMenu == menu) { - qtActivityDelegate().callMethod<void>("closeContextMenu"); + AndroidBackendRegister *reg = QtAndroid::backendRegister(); + reg->callInterface<QtJniTypes::QtMenuInterface, void>("closeContextMenu"); pendingContextMenus.clear(); } else { pendingContextMenus.removeOne(menu); |