Commit 4730be08 authored by James Pike's avatar James Pike

Support for workspace.clientList() in declarative script

Summary:
The version provided is only compatible with QtScript so it became
necessary to split WorkspaceWrapper into a base class and two child
classes, one for QtScript and one for QmlScript.

BUG: 340125
FIXED-IN: 5.8.4
REVIEW: D3185
parent 254690e8
......@@ -492,7 +492,7 @@ bool ScriptedEffect::init(const QString &effectName, const QString &pathToScript
m_engine->globalObject().setProperty(QStringLiteral("effects"), effectsObject, QScriptValue::Undeletable);
m_engine->globalObject().setProperty(QStringLiteral("Effect"), m_engine->newQMetaObject(&ScriptedEffect::staticMetaObject));
#ifndef KWIN_UNIT_TEST
m_engine->globalObject().setProperty(QStringLiteral("KWin"), m_engine->newQMetaObject(&WorkspaceWrapper::staticMetaObject));
m_engine->globalObject().setProperty(QStringLiteral("KWin"), m_engine->newQMetaObject(&QtScriptWorkspaceWrapper::staticMetaObject));
#endif
m_engine->globalObject().setProperty(QStringLiteral("QEasingCurve"), m_engine->newQMetaObject(&QEasingCurve::staticMetaObject));
m_engine->globalObject().setProperty(QStringLiteral("effect"), m_engine->newQObject(this, QScriptEngine::QtOwnership, QScriptEngine::ExcludeDeleteLater), QScriptValue::Undeletable);
......
......@@ -46,6 +46,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QMenu>
#include <QQmlContext>
#include <QQmlEngine>
#include <QQmlExpression>
#include <QtScript/QScriptEngine>
#include <QtScript/QScriptValue>
#include <QtCore/QStandardPaths>
......@@ -293,7 +294,7 @@ void KWin::Script::installScriptFunctions(QScriptEngine* engine)
QScriptValue assertNotNullFunc = engine->newFunction(kwinAssertNotNull);
engine->globalObject().setProperty(QStringLiteral("assertNotNull"), assertNotNullFunc);
// global properties
engine->globalObject().setProperty(QStringLiteral("KWin"), engine->newQMetaObject(&WorkspaceWrapper::staticMetaObject));
engine->globalObject().setProperty(QStringLiteral("KWin"), engine->newQMetaObject(&QtScriptWorkspaceWrapper::staticMetaObject));
QScriptValue workspace = engine->newQObject(Scripting::self()->workspaceWrapper(), QScriptEngine::QtOwnership,
QScriptEngine::ExcludeSuperClassContents | QScriptEngine::ExcludeDeleteLater);
engine->globalObject().setProperty(QStringLiteral("workspace"), workspace, QScriptValue::Undeletable);
......@@ -527,7 +528,7 @@ void KWin::ScriptUnloaderAgent::scriptUnload(qint64 id)
KWin::DeclarativeScript::DeclarativeScript(int id, QString scriptName, QString pluginName, QObject* parent)
: AbstractScript(id, scriptName, pluginName, parent)
, m_context(new QQmlContext(Scripting::self()->qmlEngine(), this))
, m_context(new QQmlContext(Scripting::self()->declarativeScriptSharedContext(), this))
, m_component(new QQmlComponent(Scripting::self()->qmlEngine(), this))
{
m_context->setContextProperty(QStringLiteral("KWin"), new JSEngineGlobalMethodsWrapper(this));
......@@ -622,7 +623,8 @@ KWin::Scripting::Scripting(QObject *parent)
: QObject(parent)
, m_scriptsLock(new QMutex(QMutex::Recursive))
, m_qmlEngine(new QQmlEngine(this))
, m_workspaceWrapper(new WorkspaceWrapper(this))
, m_declarativeScriptSharedContext(new QQmlContext(m_qmlEngine, this))
, m_workspaceWrapper(new QtScriptWorkspaceWrapper(this))
{
init();
QDBusConnection::sessionBus().registerObject(QStringLiteral("/Scripting"), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportScriptableInvokables);
......@@ -647,6 +649,11 @@ void KWin::Scripting::init()
m_qmlEngine->rootContext()->setContextProperty(QStringLiteral("workspace"), m_workspaceWrapper);
m_qmlEngine->rootContext()->setContextProperty(QStringLiteral("options"), options);
m_declarativeScriptSharedContext->setContextProperty(QStringLiteral("workspace"), new DeclarativeScriptWorkspaceWrapper(this));
// QQmlListProperty interfaces only work via properties, rebind them as functions here
QQmlExpression expr(m_declarativeScriptSharedContext, nullptr, "workspace.clientList = function() { return workspace.clients }");
expr.evaluate();
}
void KWin::Scripting::start()
......
......@@ -51,7 +51,7 @@ namespace KWin
class AbstractClient;
class Client;
class ScriptUnloaderAgent;
class WorkspaceWrapper;
class QtScriptWorkspaceWrapper;
class KWIN_EXPORT AbstractScript : public QObject
{
......@@ -362,7 +362,9 @@ public:
QQmlEngine *qmlEngine() const;
QQmlEngine *qmlEngine();
WorkspaceWrapper *workspaceWrapper() const;
QQmlContext *declarativeScriptSharedContext() const;
QQmlContext *declarativeScriptSharedContext();
QtScriptWorkspaceWrapper *workspaceWrapper() const;
AbstractScript *findScript(const QString &pluginName) const;
......@@ -381,7 +383,8 @@ private:
LoadScriptList queryScriptsToLoad();
static Scripting *s_self;
QQmlEngine *m_qmlEngine;
WorkspaceWrapper *m_workspaceWrapper;
QQmlContext *m_declarativeScriptSharedContext;
QtScriptWorkspaceWrapper *m_workspaceWrapper;
};
inline
......@@ -397,7 +400,19 @@ QQmlEngine *Scripting::qmlEngine()
}
inline
WorkspaceWrapper *Scripting::workspaceWrapper() const
QQmlContext *Scripting::declarativeScriptSharedContext() const
{
return m_declarativeScriptSharedContext;
}
inline
QQmlContext *Scripting::declarativeScriptSharedContext()
{
return m_declarativeScriptSharedContext;
}
inline
QtScriptWorkspaceWrapper *Scripting::workspaceWrapper() const
{
return m_workspaceWrapper;
}
......
......@@ -90,12 +90,12 @@ void WorkspaceWrapper::setNumberOfDesktops(int count)
VirtualDesktopManager::self()->setCount(count);
}
#define GETTER( rettype, getterName ) \
rettype WorkspaceWrapper::getterName( ) const { \
#define GETTER( klass, rettype, getterName ) \
rettype klass::getterName( ) const { \
return Workspace::self()->getterName(); \
}
GETTER(KWin::AbstractClient*, activeClient)
GETTER(QList< KWin::Client* >, clientList)
GETTER(WorkspaceWrapper, KWin::AbstractClient*, activeClient)
GETTER(QtScriptWorkspaceWrapper, QList< KWin::Client* >, clientList)
#undef GETTER
......@@ -324,4 +324,26 @@ QSize WorkspaceWrapper::virtualScreenSize() const
return screens()->size();
}
QtScriptWorkspaceWrapper::QtScriptWorkspaceWrapper(QObject* parent)
: WorkspaceWrapper(parent) {}
QQmlListProperty<KWin::Client> DeclarativeScriptWorkspaceWrapper::clients()
{
return QQmlListProperty<KWin::Client>(this, 0, &DeclarativeScriptWorkspaceWrapper::countClientList, &DeclarativeScriptWorkspaceWrapper::atClientList);
}
int DeclarativeScriptWorkspaceWrapper::countClientList(QQmlListProperty<KWin::Client> *clients)
{
return Workspace::self()->clientList().size();
}
KWin::Client *DeclarativeScriptWorkspaceWrapper::atClientList(QQmlListProperty<KWin::Client> *clients, int index)
{
return Workspace::self()->clientList().at(index);
}
DeclarativeScriptWorkspaceWrapper::DeclarativeScriptWorkspaceWrapper(QObject* parent)
: WorkspaceWrapper(parent) {}
} // KWin
......@@ -26,6 +26,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QSize>
#include <QStringList>
#include <QRect>
#include <QtQml/QQmlListProperty>
#include <kwinglobals.h>
namespace KWin
......@@ -198,7 +199,10 @@ public:
ElectricNone
};
protected:
explicit WorkspaceWrapper(QObject* parent = nullptr);
public:
#define GETTERSETTERDEF( rettype, getter, setter ) \
rettype getter() const; \
void setter( rettype val );
......@@ -222,10 +226,6 @@ void setter( rettype val );
QSize virtualScreenSize() const;
QRect virtualScreenGeometry() const;
/**
* List of Clients currently managed by KWin.
**/
Q_INVOKABLE QList< KWin::Client* > clientList() const;
/**
* Returns the geometry a Client can use with the specified option.
* This method should be preferred over other methods providing screen sizes as the
......@@ -349,6 +349,31 @@ private Q_SLOTS:
void setupClientConnections(KWin::Client* client);
};
class QtScriptWorkspaceWrapper : public WorkspaceWrapper
{
Q_OBJECT
public:
/**
* List of Clients currently managed by KWin.
**/
Q_INVOKABLE QList< KWin::Client* > clientList() const;
explicit QtScriptWorkspaceWrapper(QObject* parent = nullptr);
};
class DeclarativeScriptWorkspaceWrapper : public WorkspaceWrapper
{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<KWin::Client> clients READ clients)
public:
QQmlListProperty<KWin::Client> clients();
static int countClientList(QQmlListProperty<KWin::Client> *clients);
static KWin::Client *atClientList(QQmlListProperty<KWin::Client> *clients, int index);
explicit DeclarativeScriptWorkspaceWrapper(QObject* parent = nullptr);
};
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment