diff options
author | Gabriel de Dietrich <gabriel.dedietrich@qt.io> | 2018-04-18 19:04:13 -0700 |
---|---|---|
committer | Gabriel de Dietrich <gabriel.dedietrich@qt.io> | 2018-05-23 18:09:28 +0000 |
commit | 595c1ae9e7be14162acdfcbc573f31ef6661e718 (patch) | |
tree | 437422a3cfd7d0499510f5b375e030ee6ae4e2d2 /tests/manual/cocoa | |
parent | bd74b624d53ce076a8b6a90a66da0a19da1ec17c (diff) |
Cocoa Menus: Add support for menu items in window-less apps
Since we moved the menu items validation and target/action to
QNSView (thus relying on the responder chain), we need to take
care of case when the applications that doesn't have any window
open. By adding similar methods to QCocoaApplicationDelegate,
the last responder, we ensure the menu items will be validated
and will trigger properly. This is particularly necessary for
dock menu items, which live separately from any top-level widget.
Dock menu added to Menurama which won't quit when its last window
is closed. This way we can test that dock menu items will trigger
in the absence of any window.
Change-Id: I56d864eb9da1f8dd5adb2a3b6c3dd5304c723117
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
Diffstat (limited to 'tests/manual/cocoa')
-rw-r--r-- | tests/manual/cocoa/menurama/main.cpp | 37 | ||||
-rw-r--r-- | tests/manual/cocoa/menurama/mainwindow.ui | 16 |
2 files changed, 50 insertions, 3 deletions
diff --git a/tests/manual/cocoa/menurama/main.cpp b/tests/manual/cocoa/menurama/main.cpp index 00594b6d1f..e39c01ea4d 100644 --- a/tests/manual/cocoa/menurama/main.cpp +++ b/tests/manual/cocoa/menurama/main.cpp @@ -29,9 +29,46 @@ #include "mainwindow.h" #include "menuramaapplication.h" +#include <QtWidgets/QAction> +#include <QtWidgets/QMenu> + int main(int argc, char *argv[]) { MenuramaApplication a(argc, argv); + a.setQuitOnLastWindowClosed(false); + + auto *dockMenu = new QMenu(); + dockMenu->setAsDockMenu(); + dockMenu->addAction(QLatin1String("New Window"), [=] { + auto *w = new MainWindow; + w->setAttribute(Qt::WA_DeleteOnClose, true); + w->show(); + }); + auto *disabledAction = dockMenu->addAction(QLatin1String("Disabled Item"), [=] { + qDebug() << "Should not happen!"; + Q_UNREACHABLE(); + }); + disabledAction->setEnabled(false); + dockMenu->addAction(QLatin1String("Last Item Before Separator"), [=] { + qDebug() << "Last Item triggered"; + }); + auto *hiddenAction = dockMenu->addAction(QLatin1String("Invisible Item (FIXME rdar:39615815)"), [=] { + qDebug() << "Should not happen!"; + Q_UNREACHABLE(); + }); + hiddenAction->setVisible(false); + dockMenu->addSeparator(); + auto *toolsMenu = dockMenu->addMenu(QLatin1String("Menurama Tools")); + toolsMenu->addAction(QLatin1String("Hammer"), [=] { + qDebug() << "Bang! Bang!"; + }); + toolsMenu->addAction(QLatin1String("Wrench"), [=] { + qDebug() << "Clang! Clang!"; + }); + toolsMenu->addAction(QLatin1String("Screwdriver"), [=] { + qDebug() << "Squeak! Squeak!"; + }); + MainWindow w; w.show(); diff --git a/tests/manual/cocoa/menurama/mainwindow.ui b/tests/manual/cocoa/menurama/mainwindow.ui index 4fb3e3420e..2c6ac52f58 100644 --- a/tests/manual/cocoa/menurama/mainwindow.ui +++ b/tests/manual/cocoa/menurama/mainwindow.ui @@ -7,7 +7,7 @@ <x>0</x> <y>0</y> <width>486</width> - <height>288</height> + <height>376</height> </rect> </property> <property name="windowTitle"> @@ -58,7 +58,7 @@ Click on "Dynamic Stuff" then move left and right to other menus. Disa </item> <item> <layout class="QHBoxLayout" name="horizontalLayout"> - <item> + <item alignment="Qt::AlignTop"> <widget class="QPushButton" name="addManyButton"> <property name="sizePolicy"> <sizepolicy hsizetype="Maximum" vsizetype="Fixed"> @@ -71,7 +71,7 @@ Click on "Dynamic Stuff" then move left and right to other menus. Disa </property> </widget> </item> - <item> + <item alignment="Qt::AlignTop"> <widget class="QLabel" name="label_2"> <property name="text"> <string>Adding hundreds of items should not block the UI for noticeable periods of time. Odd numbered items should be disabled, those with 2nd LSB on should be hidden.</string> @@ -83,6 +83,16 @@ Click on "Dynamic Stuff" then move left and right to other menus. Disa </item> </layout> </item> + <item alignment="Qt::AlignTop"> + <widget class="QLabel" name="label_3"> + <property name="text"> + <string>Check out the dock menu. You can close this window to verify that its actions will trigger in the absence of any window. And you can add more windows from there too.</string> + </property> + <property name="wordWrap"> + <bool>true</bool> + </property> + </widget> + </item> </layout> </widget> <widget class="QMenuBar" name="menuBar"> |