From 3021b1ecb7e1ec6e75e851c44f8ded9b8552235b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Sun, 28 Jun 2020 16:04:51 +0200 Subject: [PATCH] Simplify QCocoaKeyMapper by always returning a valid keymap QCocoaKeyMapper::updateKeyMap would always end up creating a valid key map, so the logic in CocoaKeyMapper::possibleKeys for dealing with missing keymaps was not needed, and was likely copied from one of the other key map implementations. Since we know that we have a key map we might as well return it after possibly updating it. Change-Id: If83974f4ddedae8b1acefbadef48da3ee326eadd Reviewed-by: Volker Hilsheimer --- src/plugins/platforms/cocoa/qcocoakeymapper.h | 6 ++--- .../platforms/cocoa/qcocoakeymapper.mm | 27 +++++++++---------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/plugins/platforms/cocoa/qcocoakeymapper.h b/src/plugins/platforms/cocoa/qcocoakeymapper.h index bb39717b79..b2ad36712b 100644 --- a/src/plugins/platforms/cocoa/qcocoakeymapper.h +++ b/src/plugins/platforms/cocoa/qcocoakeymapper.h @@ -85,7 +85,7 @@ public: private: bool updateKeyboard(); void deleteLayouts(); - void updateKeyMap(unsigned short macVirtualKey, QChar unicodeKey); + KeyboardLayoutItem *keyMapForKey(unsigned short macVirtualKey, QChar unicodeKey) const; void clearMappings(); QCFType m_currentInputSource = nullptr; @@ -93,8 +93,8 @@ private: enum { NullMode, UnicodeMode, OtherMode } m_keyboardMode = NullMode; const UCKeyboardLayout *m_keyboardLayoutFormat = nullptr; KeyboardLayoutKind m_keyboardKind = kKLKCHRuchrKind; - UInt32 m_deadKeyState = 0; // Maintains dead key state beween calls to UCKeyTranslate - KeyboardLayoutItem *m_keyLayout[256]; + mutable UInt32 m_deadKeyState = 0; // Maintains dead key state beween calls to UCKeyTranslate + mutable KeyboardLayoutItem *m_keyLayout[256]; }; QT_END_NAMESPACE diff --git a/src/plugins/platforms/cocoa/qcocoakeymapper.mm b/src/plugins/platforms/cocoa/qcocoakeymapper.mm index a0004f6f8f..c326c1ca84 100644 --- a/src/plugins/platforms/cocoa/qcocoakeymapper.mm +++ b/src/plugins/platforms/cocoa/qcocoakeymapper.mm @@ -363,15 +363,16 @@ static constexpr Qt::KeyboardModifiers modifierCombinations[] = { }; /* - Updates the key map for the given \macVirtualKey by applying - all possible modifier combinations. + Returns a key map for the given \macVirtualKey based on all + possible modifier combinations. */ -void QCocoaKeyMapper::updateKeyMap(unsigned short macVirtualKey, QChar unicodeKey) +KeyboardLayoutItem *QCocoaKeyMapper::keyMapForKey(unsigned short macVirtualKey, QChar unicodeKey) const { - updateKeyboard(); + const_cast(this)->updateKeyboard(); - if (m_keyLayout[macVirtualKey]) - return; + Q_ASSERT(macVirtualKey < 256); + if (auto *existingKeyMap = m_keyLayout[macVirtualKey]) + return existingKeyMap; qCDebug(lcQpaKeyMapper, "Updating key map for virtual key = 0x%02x!", (uint)macVirtualKey); @@ -402,27 +403,25 @@ void QCocoaKeyMapper::updateKeyMap(unsigned short macVirtualKey, QChar unicodeKe qCDebug(lcQpaKeyMapper, " [%d] (%d,0x%02x,'%c')", i, qtkey, qtkey, qtkey); } + + return m_keyLayout[macVirtualKey]; } QList QCocoaKeyMapper::possibleKeys(const QKeyEvent *event) const { QList ret; - auto virtualKey = event->nativeVirtualKey(); - const_cast(this)->updateKeyMap(virtualKey, QChar(event->key())); - KeyboardLayoutItem *keyLayout = m_keyLayout[virtualKey]; + auto *keyMap = keyMapForKey(event->nativeVirtualKey(), QChar(event->key())); + Q_ASSERT(keyMap); - if (!keyLayout) // Key is not in any keyboard layout (e.g. eisu-key on Japanese keyboard) - return ret; - - int baseKey = keyLayout->qtKey[Qt::NoModifier]; + int baseKey = keyMap->qtKey[Qt::NoModifier]; auto eventModifiers = event->modifiers(); // The base key is always valid ret << int(baseKey + eventModifiers); for (int i = 1; i < 8; ++i) { - int keyAfterApplyingModifiers = keyLayout->qtKey[i]; + int keyAfterApplyingModifiers = keyMap->qtKey[i]; if (!keyAfterApplyingModifiers) continue; if (keyAfterApplyingModifiers == baseKey)