export retarget to scripts

This commit is contained in:
Thomas Lübking 2016-02-03 19:10:00 +01:00
parent 1faa8aa039
commit 070b85bbcf
2 changed files with 77 additions and 35 deletions

View file

@ -308,53 +308,27 @@ QScriptValue kwinEffectSet(QScriptContext *context, QScriptEngine *engine)
return engine->newVariant(animIds); return engine->newVariant(animIds);
} }
QScriptValue kwinEffectCancel(QScriptContext *context, QScriptEngine *engine) QList<quint64> animations(const QVariant &v, bool *ok)
{ {
ScriptedEffect *effect = qobject_cast<ScriptedEffect*>(context->callee().data().toQObject());
if (context->argumentCount() != 1) {
context->throwError(QScriptContext::SyntaxError, QStringLiteral("Exactly one argument expected"));
return engine->undefinedValue();
}
QVariant v = context->argument(0).toVariant();
QList<quint64> animIds; QList<quint64> animIds;
bool ok = false; *ok = false;
if (v.isValid()) { if (v.isValid()) {
quint64 animId = v.toULongLong(&ok); quint64 animId = v.toULongLong(ok);
if (ok) if (*ok)
animIds << animId; animIds << animId;
} }
if (!ok) { // may still be a variantlist of variants being quint64 if (!*ok) { // may still be a variantlist of variants being quint64
QList<QVariant> list = v.toList(); QList<QVariant> list = v.toList();
if (!list.isEmpty()) { if (!list.isEmpty()) {
foreach (const QVariant &vv, list) { foreach (const QVariant &vv, list) {
quint64 animId = vv.toULongLong(&ok); quint64 animId = vv.toULongLong(ok);
if (ok) if (*ok)
animIds << animId; animIds << animId;
} }
ok = !animIds.isEmpty(); *ok = !animIds.isEmpty();
} }
} }
if (!ok) { return animIds;
context->throwError(QScriptContext::TypeError, QStringLiteral("Argument needs to be one or several quint64"));
return engine->undefinedValue();
}
foreach (const quint64 &animId, animIds) {
ok |= engine->newVariant(effect->cancel(animId)).toBool();
}
return engine->newVariant(ok);
}
QScriptValue effectWindowToScriptValue(QScriptEngine *eng, const KEffectWindowRef &window)
{
return eng->newQObject(window, QScriptEngine::QtOwnership,
QScriptEngine::ExcludeChildObjects | QScriptEngine::ExcludeDeleteLater | QScriptEngine::PreferExistingWrapperObject);
}
void effectWindowFromScriptValue(const QScriptValue &value, EffectWindow* &window)
{
window = qobject_cast<EffectWindow*>(value.toQObject());
} }
QScriptValue fpx2ToScriptValue(QScriptEngine *eng, const KWin::FPx2 &fpx2) QScriptValue fpx2ToScriptValue(QScriptEngine *eng, const KWin::FPx2 &fpx2)
@ -387,6 +361,63 @@ void fpx2FromScriptValue(const QScriptValue &value, KWin::FPx2 &fpx2)
} }
} }
QScriptValue kwinEffectRetarget(QScriptContext *context, QScriptEngine *engine)
{
ScriptedEffect *effect = qobject_cast<ScriptedEffect*>(context->callee().data().toQObject());
if (context->argumentCount() < 2 || context->argumentCount() > 3) {
context->throwError(QScriptContext::SyntaxError, QStringLiteral("2 or 3 arguments expected"));
return engine->undefinedValue();
}
QVariant v = context->argument(0).toVariant();
bool ok = false;
QList<quint64> animIds = animations(v, &ok);
if (!ok) {
context->throwError(QScriptContext::TypeError, QStringLiteral("Argument needs to be one or several quint64"));
return engine->undefinedValue();
}
FPx2 target;
fpx2FromScriptValue(context->argument(1), target);
const int remainingTime = context->argumentCount() == 2 ? context->argument(1).toVariant().toInt() : -1;
foreach (const quint64 &animId, animIds) {
ok |= engine->newVariant(effect->retarget(animId, target, remainingTime)).toBool();
}
return engine->newVariant(ok);
}
QScriptValue kwinEffectCancel(QScriptContext *context, QScriptEngine *engine)
{
ScriptedEffect *effect = qobject_cast<ScriptedEffect*>(context->callee().data().toQObject());
if (context->argumentCount() != 1) {
context->throwError(QScriptContext::SyntaxError, QStringLiteral("Exactly one argument expected"));
return engine->undefinedValue();
}
QVariant v = context->argument(0).toVariant();
bool ok = false;
QList<quint64> animIds = animations(v, &ok);
if (!ok) {
context->throwError(QScriptContext::TypeError, QStringLiteral("Argument needs to be one or several quint64"));
return engine->undefinedValue();
}
foreach (const quint64 &animId, animIds) {
ok |= engine->newVariant(effect->cancel(animId)).toBool();
}
return engine->newVariant(ok);
}
QScriptValue effectWindowToScriptValue(QScriptEngine *eng, const KEffectWindowRef &window)
{
return eng->newQObject(window, QScriptEngine::QtOwnership,
QScriptEngine::ExcludeChildObjects | QScriptEngine::ExcludeDeleteLater | QScriptEngine::PreferExistingWrapperObject);
}
void effectWindowFromScriptValue(const QScriptValue &value, EffectWindow* &window)
{
window = qobject_cast<EffectWindow*>(value.toQObject());
}
ScriptedEffect *ScriptedEffect::create(const KPluginMetaData &effect) ScriptedEffect *ScriptedEffect::create(const KPluginMetaData &effect)
{ {
const QString name = effect.pluginId(); const QString name = effect.pluginId();
@ -486,6 +517,11 @@ bool ScriptedEffect::init(const QString &effectName, const QString &pathToScript
setFunc.setData(m_engine->newQObject(this)); setFunc.setData(m_engine->newQObject(this));
m_engine->globalObject().setProperty(QStringLiteral("set"), setFunc); m_engine->globalObject().setProperty(QStringLiteral("set"), setFunc);
// retarget
QScriptValue retargetFunc = m_engine->newFunction(kwinEffectRetarget);
retargetFunc.setData(m_engine->newQObject(this));
m_engine->globalObject().setProperty(QStringLiteral("retarget"), retargetFunc);
// cancel... // cancel...
QScriptValue cancelFunc = m_engine->newFunction(kwinEffectCancel); QScriptValue cancelFunc = m_engine->newFunction(kwinEffectCancel);
cancelFunc.setData(m_engine->newQObject(this)); cancelFunc.setData(m_engine->newQObject(this));
@ -541,6 +577,11 @@ quint64 ScriptedEffect::set(KWin::EffectWindow* w, KWin::AnimationEffect::Attrib
return AnimationEffect::set(w, a, metaData, ms, to, qec, delay, from); return AnimationEffect::set(w, a, metaData, ms, to, qec, delay, from);
} }
bool ScriptedEffect::retarget(quint64 animationId, KWin::FPx2 newTarget, int newRemainingTime)
{
return AnimationEffect::retarget(animationId, newTarget, newRemainingTime);
}
bool ScriptedEffect::isGrabbed(EffectWindow* w, ScriptedEffect::DataRole grabRole) bool ScriptedEffect::isGrabbed(EffectWindow* w, ScriptedEffect::DataRole grabRole)
{ {
void *e = w->data(static_cast<KWin::DataRole>(grabRole)).value<void*>(); void *e = w->data(static_cast<KWin::DataRole>(grabRole)).value<void*>();

View file

@ -93,6 +93,7 @@ public:
public Q_SLOTS: public Q_SLOTS:
quint64 animate(KWin::EffectWindow *w, Attribute a, int ms, KWin::FPx2 to, KWin::FPx2 from = KWin::FPx2(), uint metaData = 0, QEasingCurve::Type curve = QEasingCurve::Linear, int delay = 0); quint64 animate(KWin::EffectWindow *w, Attribute a, int ms, KWin::FPx2 to, KWin::FPx2 from = KWin::FPx2(), uint metaData = 0, QEasingCurve::Type curve = QEasingCurve::Linear, int delay = 0);
quint64 set(KWin::EffectWindow *w, Attribute a, int ms, KWin::FPx2 to, KWin::FPx2 from = KWin::FPx2(), uint metaData = 0, QEasingCurve::Type curve = QEasingCurve::Linear, int delay = 0); quint64 set(KWin::EffectWindow *w, Attribute a, int ms, KWin::FPx2 to, KWin::FPx2 from = KWin::FPx2(), uint metaData = 0, QEasingCurve::Type curve = QEasingCurve::Linear, int delay = 0);
bool retarget(quint64 animationId, KWin::FPx2 newTarget, int newRemainingTime = -1);
bool cancel(quint64 animationId) { return AnimationEffect::cancel(animationId); } bool cancel(quint64 animationId) { return AnimationEffect::cancel(animationId); }
virtual bool borderActivated(ElectricBorder border); virtual bool borderActivated(ElectricBorder border);