From 48849d4c483a1e4374b54f2382571125029b0d7d Mon Sep 17 00:00:00 2001 From: Mihail Milev Date: Fri, 29 Sep 2023 19:37:36 +0000 Subject: [PATCH] keyboard_layout: Add shortcut to toggle last-used keyboard layout This is a companion to https://invent.kde.org/plasma/plasma-desktop/-/commit/c01b583e535fe995f8dd84a5189263bee86341bf that enables the above functionality on Wayland. For Xorg, the functionality is implemented in plasma-desktop in the above commit. --- src/keyboard_layout.cpp | 21 +++++++++++++++++++++ src/keyboard_layout.h | 3 +++ 2 files changed, 24 insertions(+) diff --git a/src/keyboard_layout.cpp b/src/keyboard_layout.cpp index 429b5f4a6b..4bba404365 100644 --- a/src/keyboard_layout.cpp +++ b/src/keyboard_layout.cpp @@ -49,6 +49,16 @@ void KeyboardLayout::init() connect(switchKeyboardAction, &QAction::triggered, this, &KeyboardLayout::switchToNextLayout); + QAction *switchLastUsedKeyboardAction = new QAction(this); + switchLastUsedKeyboardAction->setObjectName(QStringLiteral("Switch to Last-Used Keyboard Layout")); + switchLastUsedKeyboardAction->setProperty("componentName", QStringLiteral("KDE Keyboard Layout Switcher")); + switchLastUsedKeyboardAction->setProperty("componentDisplayName", i18n("Keyboard Layout Switcher")); + const QKeySequence sequenceLastUsed = QKeySequence(Qt::META | Qt::ALT | Qt::Key_L); + KGlobalAccel::self()->setDefaultShortcut(switchLastUsedKeyboardAction, QList({sequenceLastUsed})); + KGlobalAccel::self()->setShortcut(switchLastUsedKeyboardAction, QList({sequenceLastUsed})); + + connect(switchLastUsedKeyboardAction, &QAction::triggered, this, &KeyboardLayout::switchToLastUsedLayout); + QDBusConnection::sessionBus().connect(QString(), QStringLiteral("/Layouts"), QStringLiteral("org.kde.keyboard"), @@ -99,6 +109,16 @@ void KeyboardLayout::switchToLayout(xkb_layout_index_t index) checkLayoutChange(previousLayout); } +void KeyboardLayout::switchToLastUsedLayout() +{ + const quint32 count = m_xkb->numberOfLayouts(); + if (!m_lastUsedLayout.has_value() || *m_lastUsedLayout >= count) { + switchToPreviousLayout(); + } else { + switchToLayout(*m_lastUsedLayout); + } +} + void KeyboardLayout::reconfigure() { if (m_configGroup.isValid()) { @@ -154,6 +174,7 @@ void KeyboardLayout::checkLayoutChange(uint previousLayout) // We need OSD if current layout deviates from any of these const uint currentLayout = m_xkb->currentLayout(); if (m_layout != currentLayout || previousLayout != currentLayout) { + m_lastUsedLayout = std::optional{previousLayout}; m_layout = currentLayout; notifyLayoutChange(); Q_EMIT layoutChanged(currentLayout); diff --git a/src/keyboard_layout.h b/src/keyboard_layout.h index 5e5c548ecc..801ea0e9ee 100644 --- a/src/keyboard_layout.h +++ b/src/keyboard_layout.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -43,6 +44,7 @@ public: void checkLayoutChange(uint previousLayout); void switchToNextLayout(); void switchToPreviousLayout(); + void switchToLastUsedLayout(); void resetLayout(); Q_SIGNALS: @@ -63,6 +65,7 @@ private: QVector m_layoutShortcuts; KeyboardLayoutDBusInterface *m_dbusInterface = nullptr; std::unique_ptr m_policy; + std::optional m_lastUsedLayout; }; class KeyboardLayoutDBusInterface : public QObject