From 287f548d4c7cc594ffecc9c050dc5ec9fdaa6d37 Mon Sep 17 00:00:00 2001 From: Dmitry Shachnev Date: Wed, 2 Nov 2016 10:51:39 +0300 Subject: Make shortcuts work for platform menu bars MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a platform menu bar is used, the QMenuBar is hidden, so shortcuts for QActions attached only to it do not work. Extend the macOS-specific code to treat such menubars as visible to other platforms, to make the shortcuts work. The exception is made for internal QMenuBar shortcuts, which are forwarded to the platform menu. A follow-up change will add support for this to QDBusPlatformMenu. The updateGeometries() method is called for platform menu bars too to make sure the internal shortcuts are registered even if the global menu is in use. Add two cases to the tst_QMenuBar::activatedCount() test to test both native and non-native menu bars when possible (it now passes with native menu bars too). Change-Id: I2d7128512719ac199cd3f8f7ba28333d04d84ed4 Reviewed-by: Tor Arne Vestbø --- src/widgets/kernel/qshortcut.cpp | 8 +++++--- src/widgets/widgets/qmenubar.cpp | 13 +++++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/widgets/kernel/qshortcut.cpp b/src/widgets/kernel/qshortcut.cpp index 6eec5ff7e8..be5788274e 100644 --- a/src/widgets/kernel/qshortcut.cpp +++ b/src/widgets/kernel/qshortcut.cpp @@ -141,9 +141,11 @@ bool qWidgetShortcutContextMatcher(QObject *object, Qt::ShortcutContext context) static bool correctWidgetContext(Qt::ShortcutContext context, QWidget *w, QWidget *active_window) { bool visible = w->isVisible(); -#if defined(Q_OS_DARWIN) && !defined(QT_NO_MENUBAR) - if (!qApp->testAttribute(Qt::AA_DontUseNativeMenuBar) && qobject_cast(w)) - visible = true; +#ifndef QT_NO_MENUBAR + if (QMenuBar *menuBar = qobject_cast(w)) { + if (menuBar->isNativeMenuBar()) + visible = true; + } #endif if (!visible || !w->isEnabled()) diff --git a/src/widgets/widgets/qmenubar.cpp b/src/widgets/widgets/qmenubar.cpp index 63fe09f77e..2588f41468 100644 --- a/src/widgets/widgets/qmenubar.cpp +++ b/src/widgets/widgets/qmenubar.cpp @@ -1271,10 +1271,12 @@ void QMenuBar::actionEvent(QActionEvent *e) } else if(e->type() == QEvent::ActionRemoved) { e->action()->disconnect(this); } - if (isVisible()) { + // updateGeometries() is also needed for native menu bars because + // it updates shortcutIndexMap + if (isVisible() || isNativeMenuBar()) d->updateGeometries(); + if (isVisible()) update(); - } } /*! @@ -1686,6 +1688,13 @@ void QMenuBarPrivate::_q_internalShortcutActivated(int id) { Q_Q(QMenuBar); QAction *act = actions.at(id); + if (act && act->menu()) { + if (QPlatformMenu *platformMenu = act->menu()->platformMenu()) { + platformMenu->showPopup(q->windowHandle(), actionRects.at(id), Q_NULLPTR); + return; + } + } + setCurrentAction(act, true, true); if (act && !act->menu()) { activateAction(act, QAction::Trigger); -- cgit v1.2.3