From 1c8953e52074e7b377c27fe167f4e00729bb2ad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Tue, 7 Jul 2020 17:13:45 +0200 Subject: macOS: Move AA_MacDontSwapCtrlAndMeta logic out of QPlatformTheme Change-Id: Ia36f60587d98902406de7de8acdc3c4521cfd05a Reviewed-by: Volker Hilsheimer --- src/plugins/platforms/cocoa/qcocoatheme.h | 4 ++++ src/plugins/platforms/cocoa/qcocoatheme.mm | 37 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) (limited to 'src/plugins/platforms/cocoa') diff --git a/src/plugins/platforms/cocoa/qcocoatheme.h b/src/plugins/platforms/cocoa/qcocoatheme.h index 50e56ef1bf..f6276e3d1a 100644 --- a/src/plugins/platforms/cocoa/qcocoatheme.h +++ b/src/plugins/platforms/cocoa/qcocoatheme.h @@ -80,6 +80,10 @@ public: void handleSystemThemeChange(); +#ifndef QT_NO_SHORTCUT + QList keyBindings(QKeySequence::StandardKey key) const override; +#endif + private: mutable QPalette *m_systemPalette; QMacNotificationObserver m_systemColorObserver; diff --git a/src/plugins/platforms/cocoa/qcocoatheme.mm b/src/plugins/platforms/cocoa/qcocoatheme.mm index 13e1b9da2b..674a8da60a 100644 --- a/src/plugins/platforms/cocoa/qcocoatheme.mm +++ b/src/plugins/platforms/cocoa/qcocoatheme.mm @@ -561,4 +561,41 @@ QPlatformMenuBar *QCocoaTheme::createPlatformMenuBar() const return new QCocoaMenuBar(); } +#ifndef QT_NO_SHORTCUT +QList QCocoaTheme::keyBindings(QKeySequence::StandardKey key) const +{ + // The default key bindings in QPlatformTheme all hard-coded to use the Ctrl + // modifier, to match other platforms. In the normal case, when translating + // those to key sequences, we'll end up with Qt::ControlModifier+X, which is + // then matched against incoming key events that have been mapped from the + // command key to Qt::ControlModifier, and we'll get a match. If, however, + // the AA_MacDontSwapCtrlAndMeta application attribute is set, we need to + // fix the resulting key sequence so that it will match against unmapped + // key events that contain Qt::MetaModifier. + auto bindings = QPlatformTheme::keyBindings(key); + + if (qApp->testAttribute(Qt::AA_MacDontSwapCtrlAndMeta)) { + static auto swapCtrlMeta = [](int keySequence) { + auto originalKeySequence = keySequence; + keySequence &= ~(Qt::ControlModifier | Qt::MetaModifier); + if (originalKeySequence & Qt::ControlModifier) + keySequence |= Qt::MetaModifier; + if (originalKeySequence & Qt::MetaModifier) + keySequence |= Qt::ControlModifier; + return keySequence; + }; + + QList swappedBindings; + for (auto binding : bindings) { + Q_ASSERT(binding.count() == 1); + swappedBindings.append(QKeySequence(swapCtrlMeta(binding[0]))); + } + + bindings = swappedBindings; + } + + return bindings; +} +#endif + QT_END_NAMESPACE -- cgit v1.2.3