Go to file
Tor Arne Vestbø 5c05eb3ea0 macOS: Don't clam to support action messages from non-Qt menu items
As described in 3bedeb837e, the way
menu items on macOS are typically set up they have an action, e.g.
copy:, but no target, and the system then takes care of finding the
right target at runtime, starting with the first responder, walking
the responder chain, and then moving on to other NSWindows, before
ending up in the NSApplication and its delegate.

As we (still) don't have a mechanism in Qt to forward generic
actions, such as the cut/copy/paste, or selectAll, so we rely on
mapping the actions back to QCocoaNSMenuItem that we can trace
back to a QPlatformMenuItem that we in turn emit activated() for.

Normally this works fine, but in the case where the Qt app is embedded
in a native UI, which has its own menu items with cut/copy/paste,
we'll get callbacks into QNSView for actions triggered by a generic
NSMenuItem.

In that case, we need to bail out, but we want to do so in a way
that lets AppKit continue to walk the responder chain. This is
possible by implementing supplementalTargetForAction:sender:,
where we have access to the sender. If sender doesn't match
the expected QCocoaNSMenuItem we let AppKit find a better match
up the chain.

Since the target we return needs to ultimately respond to the
selectors and/or forward them, we can't point the target back
to ourselves, nor can we point it to the application delegate
directly, as the menu items need to be validated in the context
of the view, so a new per-view QNSViewMenuHelper class has been
added to take the role of forwarding the menu actions.

The logic for forwarding the resulting actions to the application
delegate has been simplified and hardened a bit as well.

A possible scenario with this new approach is that the Qt app
has a line edit focused, and the user tries to activate the
menu item for Paste, but the item is grayed out because we
can not support the action. This is of course confusing for
the user, but less so than having an active menu item that
then doesn't do anything when activated.

Another scenario is that a responder later in the chain does
respond to the paste action, and the menu item will end up
pasting into something that is not the first responder.
This might also be confusing for the user, but it's generally
recommended that implementers of actions like paste only
allow the action if the view is the first responder, and
this is something views have to deal with anyways, so it
doesn't change anything that we're now bailing out earlier
in not accepting the paste.

One benefit of allowing AppKit to find a better target for the
action is that if no target is found, and the user presses the
key equivalent of the disabled menu item, the key event will
be delivered as a normal keyDown to our QNSView, which we do
forward, allowing the Qt app to respond to the action, even
though the action came from a generic menu item. With our old
approach this would not happen, as we would claim to support
the action for our QNSView, but then drop it on the floor when
AppKit tried to deliver it to us.

Change-Id: I609db42df6a107a49e287f435e8808812c83d43e
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
2023-05-04 01:03:52 +02:00
.github/workflows Repair github action workflow 2021-11-10 18:57:33 +01:00
bin Add simple project generation based on existing source files 2023-02-08 12:59:19 +01:00
cmake Introduce QT_ALLOW_SYMLINK_IN_PATHS flag 2023-05-03 19:44:45 +02:00
coin COIN: Add initial set of Debian packaging instructions 2023-04-19 17:11:34 +03:00
config.tests Rid of 'special case' markers 2023-04-13 18:30:58 +02:00
dist Add Qt 6.0.0 changes file 2020-11-16 10:02:08 +02:00
doc Add reference location for positioning examples 2023-05-02 14:20:08 +00:00
examples savegame ex.: revamp the way we (de)serialize JSON 2023-05-03 21:36:14 +02:00
lib Purge all fonts 2015-08-18 19:59:14 +00:00
libexec Pass native path in qt-internal-configure-tests 2023-04-17 23:32:34 +02:00
LICENSES Clarify license of SHA-1 algorithm 2023-04-26 16:36:18 +02:00
mkspecs qmake: Fix infinite make loop if RESOURCES contains nonexistent .qrc 2023-04-17 23:32:34 +02:00
qmake QMake: fix build with clang-cl 2023-05-03 07:48:08 +00:00
src macOS: Don't clam to support action messages from non-Qt menu items 2023-05-04 01:03:52 +02:00
tests Suppress a redundant GCC warning 2023-05-04 02:03:51 +03:00
util Pack languageCodeList tighter 2023-03-15 19:48:30 +01:00
.cmake.conf Centralize copyright information for Qt 2023-03-20 13:03:44 +01:00
.gitattributes Give batch files CRLF line endings 2020-11-04 15:02:29 +00:00
.gitignore git: ignore ._* files 2023-03-28 10:44:57 +02:00
.lgtm.yml Skip LGTM analysis for the bootstrap library and tools 2020-07-16 01:04:34 +02:00
.tag Update the git-archive export options 2012-09-07 15:39:31 +02:00
CMakeLists.txt Introduce QT_ALLOW_SYMLINK_IN_PATHS flag 2023-05-03 19:44:45 +02:00
conanfile.py Conan: Do not force 'qt_host_path' usage in cross-build context 2022-06-22 18:18:42 +03:00
config_help.txt SQL plugins: add mimer to the help text 2023-04-18 05:32:34 +08:00
configure Retrieve an old -redo behavior 2023-02-07 18:51:58 +00:00
configure.bat Retrieve an old -redo behavior 2023-02-07 18:51:58 +00:00
configure.cmake Rid of 'special case' markers 2023-04-13 18:30:58 +02:00
dependencies.yaml Re-add dependencies.yaml now that qt5.git wip/qt6 builds fine 2019-09-18 13:19:31 +02:00
qt_cmdline.cmake Rid of 'special case' markers 2023-04-13 18:30:58 +02:00
sync.profile Fix capitalization error in auto-generated qdbusmacros.h include 2023-04-26 13:47:43 +00:00