scripting: Add gesture handlers

master
Vlad Zahorodnii 2 years ago
parent 264b054c5c
commit 793a0e72bf

@ -153,6 +153,7 @@ target_sources(kwin PRIVATE
screenedge.cpp screenedge.cpp
scripting/dbuscall.cpp scripting/dbuscall.cpp
scripting/desktopbackgrounditem.cpp scripting/desktopbackgrounditem.cpp
scripting/gesturehandler.cpp
scripting/screenedgehandler.cpp scripting/screenedgehandler.cpp
scripting/scriptedeffect.cpp scripting/scriptedeffect.cpp
scripting/scripting.cpp scripting/scripting.cpp

@ -0,0 +1,178 @@
/*
SPDX-FileCopyrightText: 2023 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "gesturehandler.h"
#include "effects.h"
#include <QAction>
#include <functional>
namespace KWin
{
SwipeGestureHandler::SwipeGestureHandler(QObject *parent)
: QObject(parent)
{
}
void SwipeGestureHandler::classBegin()
{
}
void SwipeGestureHandler::componentComplete()
{
m_action = new QAction(this);
connect(m_action, &QAction::triggered, this, &SwipeGestureHandler::activated);
std::function<void (EffectsHandler *, SwipeDirection dir, uint fingerCount, QAction *onUp, std::function<void(qreal)> progressCallback)> registrator;
switch (m_deviceType) {
case Device::Touchpad:
registrator = std::mem_fn(&EffectsHandler::registerTouchpadSwipeShortcut);
break;
case Device::Touchscreen:
registrator = std::mem_fn(&EffectsHandler::registerTouchscreenSwipeShortcut);
break;
}
registrator(effects, SwipeDirection(m_direction), m_fingerCount, m_action, [this](qreal progress) {
setProgress(progress);
});
}
SwipeGestureHandler::Direction SwipeGestureHandler::direction() const
{
return m_direction;
}
void SwipeGestureHandler::setDirection(Direction direction)
{
if (m_direction != direction) {
m_direction = direction;
Q_EMIT directionChanged();
}
}
int SwipeGestureHandler::fingerCount() const
{
return m_fingerCount;
}
void SwipeGestureHandler::setFingerCount(int fingerCount)
{
if (m_fingerCount != fingerCount) {
m_fingerCount = fingerCount;
Q_EMIT fingerCountChanged();
}
}
qreal SwipeGestureHandler::progress() const
{
return m_progress;
}
void SwipeGestureHandler::setProgress(qreal progress)
{
if (m_progress != progress) {
m_progress = progress;
Q_EMIT progressChanged();
}
}
SwipeGestureHandler::Device SwipeGestureHandler::deviceType() const
{
return m_deviceType;
}
void SwipeGestureHandler::setDeviceType(Device device)
{
if (m_deviceType != device) {
m_deviceType = device;
Q_EMIT deviceTypeChanged();
}
}
PinchGestureHandler::PinchGestureHandler(QObject *parent)
: QObject(parent)
{
}
void PinchGestureHandler::classBegin()
{
}
void PinchGestureHandler::componentComplete()
{
m_action = new QAction(this);
connect(m_action, &QAction::triggered, this, &PinchGestureHandler::activated);
std::function<void (EffectsHandler *, PinchDirection dir, uint fingerCount, QAction *onUp, std::function<void(qreal)> progressCallback)> registrator;
switch (m_deviceType) {
case Device::Touchpad:
registrator = std::mem_fn(&EffectsHandler::registerTouchpadPinchShortcut);
break;
}
registrator(effects, PinchDirection(m_direction), m_fingerCount, m_action, [this](qreal progress) {
setProgress(progress);
});
}
PinchGestureHandler::Direction PinchGestureHandler::direction() const
{
return m_direction;
}
void PinchGestureHandler::setDirection(Direction direction)
{
if (m_direction != direction) {
m_direction = direction;
Q_EMIT directionChanged();
}
}
int PinchGestureHandler::fingerCount() const
{
return m_fingerCount;
}
void PinchGestureHandler::setFingerCount(int fingerCount)
{
if (m_fingerCount != fingerCount) {
m_fingerCount = fingerCount;
Q_EMIT fingerCountChanged();
}
}
qreal PinchGestureHandler::progress() const
{
return m_progress;
}
void PinchGestureHandler::setProgress(qreal progress)
{
if (m_progress != progress) {
m_progress = progress;
Q_EMIT progressChanged();
}
}
PinchGestureHandler::Device PinchGestureHandler::deviceType() const
{
return m_deviceType;
}
void PinchGestureHandler::setDeviceType(Device device)
{
if (m_deviceType != device) {
m_deviceType = device;
Q_EMIT deviceTypeChanged();
}
}
} // namespace KWin
#include "moc_gesturehandler.cpp"

@ -0,0 +1,155 @@
/*
SPDX-FileCopyrightText: 2023 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <kwinglobals.h>
#include <QQmlParserStatus>
class QAction;
namespace KWin
{
/**
* The SwipeGestureHandler type provides a way to handle global swipe gestures.
*
* Example usage:
* @code
* SwipeGestureHandler {
* direction: SwipeGestureHandler.Direction.Up
* fingerCount: 3
* onActivated: console.log("activated")
* }
* @endcode
*/
class SwipeGestureHandler : public QObject, public QQmlParserStatus
{
Q_OBJECT
Q_INTERFACES(QQmlParserStatus)
Q_PROPERTY(Direction direction READ direction WRITE setDirection NOTIFY directionChanged)
Q_PROPERTY(int fingerCount READ fingerCount WRITE setFingerCount NOTIFY fingerCountChanged)
Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged)
Q_PROPERTY(Device deviceType READ deviceType WRITE setDeviceType NOTIFY deviceTypeChanged)
public:
explicit SwipeGestureHandler(QObject *parent = nullptr);
// Matches SwipeDirection.
enum class Direction {
Invalid,
Down,
Left,
Up,
Right,
};
Q_ENUM(Direction)
enum class Device {
Touchpad,
Touchscreen,
};
Q_ENUM(Device)
void classBegin() override;
void componentComplete() override;
Direction direction() const;
void setDirection(Direction direction);
int fingerCount() const;
void setFingerCount(int fingerCount);
qreal progress() const;
void setProgress(qreal progress);
Device deviceType() const;
void setDeviceType(Device device);
Q_SIGNALS:
void activated();
void progressChanged();
void directionChanged();
void fingerCountChanged();
void deviceTypeChanged();
private:
QAction *m_action = nullptr;
Direction m_direction = Direction::Invalid;
Device m_deviceType = Device::Touchpad;
qreal m_progress = 0;
int m_fingerCount = 3;
};
/**
* The PinchGestureHandler type provides a way to handle global pinch gestures.
*
* Example usage:
* @code
* PinchGestureHandler {
* direction: PinchGestureHandler.Direction.Contracting
* fingerCount: 3
* onActivated: console.log("activated")
* }
* @endcode
*/
class PinchGestureHandler : public QObject, public QQmlParserStatus
{
Q_OBJECT
Q_INTERFACES(QQmlParserStatus)
Q_PROPERTY(Direction direction READ direction WRITE setDirection NOTIFY directionChanged)
Q_PROPERTY(int fingerCount READ fingerCount WRITE setFingerCount NOTIFY fingerCountChanged)
Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged)
public:
explicit PinchGestureHandler(QObject *parent = nullptr);
// Matches PinchDirection.
enum class Direction {
Expanding,
Contracting,
};
Q_ENUM(Direction)
enum class Device {
Touchpad,
};
Q_ENUM(Device)
void classBegin() override;
void componentComplete() override;
Direction direction() const;
void setDirection(Direction direction);
int fingerCount() const;
void setFingerCount(int fingerCount);
qreal progress() const;
void setProgress(qreal progress);
Device deviceType() const;
void setDeviceType(Device device);
Q_SIGNALS:
void activated();
void progressChanged();
void directionChanged();
void fingerCountChanged();
void deviceTypeChanged();
private:
QAction *m_action = nullptr;
Direction m_direction = Direction::Contracting;
Device m_deviceType = Device::Touchpad;
qreal m_progress = 0;
int m_fingerCount = 3;
};
} // namespace KWin

@ -13,6 +13,7 @@
// own // own
#include "dbuscall.h" #include "dbuscall.h"
#include "desktopbackgrounditem.h" #include "desktopbackgrounditem.h"
#include "gesturehandler.h"
#include "libkwineffects/kwinquickeffect.h" #include "libkwineffects/kwinquickeffect.h"
#include "screenedgehandler.h" #include "screenedgehandler.h"
#include "scripting_logging.h" #include "scripting_logging.h"
@ -645,6 +646,8 @@ void KWin::Scripting::init()
qmlRegisterType<DBusCall>("org.kde.kwin", 3, 0, "DBusCall"); qmlRegisterType<DBusCall>("org.kde.kwin", 3, 0, "DBusCall");
qmlRegisterType<ScreenEdgeHandler>("org.kde.kwin", 3, 0, "ScreenEdgeHandler"); qmlRegisterType<ScreenEdgeHandler>("org.kde.kwin", 3, 0, "ScreenEdgeHandler");
qmlRegisterType<ShortcutHandler>("org.kde.kwin", 3, 0, "ShortcutHandler"); qmlRegisterType<ShortcutHandler>("org.kde.kwin", 3, 0, "ShortcutHandler");
qmlRegisterType<SwipeGestureHandler>("org.kde.kwin", 3, 0, "SwipeGestureHandler");
qmlRegisterType<PinchGestureHandler>("org.kde.kwin", 3, 0, "PinchGestureHandler");
qmlRegisterType<WindowModel>("org.kde.kwin", 3, 0, "WindowModel"); qmlRegisterType<WindowModel>("org.kde.kwin", 3, 0, "WindowModel");
qmlRegisterType<WindowFilterModel>("org.kde.kwin", 3, 0, "WindowFilterModel"); qmlRegisterType<WindowFilterModel>("org.kde.kwin", 3, 0, "WindowFilterModel");
qmlRegisterType<VirtualDesktopModel>("org.kde.kwin", 3, 0, "VirtualDesktopModel"); qmlRegisterType<VirtualDesktopModel>("org.kde.kwin", 3, 0, "VirtualDesktopModel");

Loading…
Cancel
Save