keyboard_layout: Add shortcut to toggle last-used keyboard layout

This is a companion to
c01b583e53
that enables the above functionality on Wayland. For Xorg, the
functionality is implemented in plasma-desktop in the above commit.
master
Mihail Milev 12 months ago committed by Nate Graham
parent a16506c665
commit 48849d4c48

@ -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<QKeySequence>({sequenceLastUsed}));
KGlobalAccel::self()->setShortcut(switchLastUsedKeyboardAction, QList<QKeySequence>({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<uint>{previousLayout};
m_layout = currentLayout;
notifyLayoutChange();
Q_EMIT layoutChanged(currentLayout);

@ -12,6 +12,7 @@
#include <QObject>
#include <QVector>
#include <memory>
#include <optional>
#include <KConfigGroup>
#include <KSharedConfig>
@ -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<QAction *> m_layoutShortcuts;
KeyboardLayoutDBusInterface *m_dbusInterface = nullptr;
std::unique_ptr<KeyboardLayoutSwitching::Policy> m_policy;
std::optional<uint> m_lastUsedLayout;
};
class KeyboardLayoutDBusInterface : public QObject

Loading…
Cancel
Save