From 0c4c529d6884c101cfe7aee05557f18dc86802e0 Mon Sep 17 00:00:00 2001 From: James Pike Date: Sun, 30 Oct 2016 01:00:22 +0100 Subject: [PATCH] Support for KWin.registerShortcut() in declarative script Summary: registerShortcut is available to javascript KWin scripts but was not available to those written in declarative script. BUG: 340225 FIXED-IN: 5.8.4 REVIEW: 129250 --- scripting/scripting.cpp | 22 ++++++++++++++++++++++ scripting/scripting.h | 2 ++ 2 files changed, 24 insertions(+) diff --git a/scripting/scripting.cpp b/scripting/scripting.cpp index 7ce6d59fd7..3e3ab5691e 100644 --- a/scripting/scripting.cpp +++ b/scripting/scripting.cpp @@ -587,6 +587,28 @@ void KWin::JSEngineGlobalMethodsWrapper::registerWindow(QQuickWindow *window) }); } +bool KWin::JSEngineGlobalMethodsWrapper::registerShortcut(const QString &name, const QString &text, const QKeySequence& keys, QJSValue function) +{ + if (!function.isCallable()) { + qCDebug(KWIN_SCRIPTING) << "Fourth and final argument must be a javascript function"; + return false; + } + + QAction *a = new QAction(this); + a->setObjectName(name); + a->setText(text); + const QKeySequence shortcut = QKeySequence(keys); + KGlobalAccel::self()->setShortcut(a, QList{shortcut}); + KWin::input()->registerShortcut(shortcut, a); + + connect(a, &QAction::triggered, this, [=]() mutable { + QJSValueList arguments; + arguments << Scripting::self()->qmlEngine()->toScriptValue(a); + function.call(arguments); + }); + return true; +} + KWin::Scripting *KWin::Scripting::s_self = nullptr; KWin::Scripting *KWin::Scripting::create(QObject *parent) diff --git a/scripting/scripting.h b/scripting/scripting.h index 07f5e7346c..d2682e22e0 100644 --- a/scripting/scripting.h +++ b/scripting/scripting.h @@ -28,6 +28,7 @@ along with this program. If not, see . #include #include #include +#include class QQmlComponent; class QQmlContext; @@ -318,6 +319,7 @@ public: public Q_SLOTS: QVariant readConfig(const QString &key, QVariant defaultValue = QVariant()); void registerWindow(QQuickWindow *window); + bool registerShortcut(const QString &name, const QString &text, const QKeySequence& keys, QJSValue function); private: DeclarativeScript *m_script;