| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | #include <effect/effect.h> |
| 3 | #include <effect/effecthandler.h> |
| 4 | #include <input.h> |
| 5 | #include <input_event.h> |
| 6 | #include <input_event_spy.h> |
| 7 | #include <scene/item.h> |
| 8 | #include <scene/workspacescene.h> |
| 9 | |
| 10 | #include "settings.h" |
| 11 | #include <QPointer> |
| 12 | #include <QVariantAnimation> |
| 13 | |
| 14 | namespace KWin { |
| 15 | |
| 16 | class ClickTweenEffect final : public Effect, public InputEventSpy |
| 17 | { |
| 18 | Q_OBJECT |
| 19 | public: |
| 20 | ClickTweenEffect() |
| 21 | : m_cursor(effects->scene()->cursorItem()) |
| 22 | , m_original(m_cursor->transform()) |
| 23 | { |
| 24 | reconfigure(ReconfigureAll); |
| 25 | connect(&m_animation, &QVariantAnimation::valueChanged, this, [this](const QVariant &value) { |
| 26 | m_scale = value.toReal(); |
| 27 | if (m_cursor) { |
| 28 | // CursorItem's origin is the hotspot, not the image corner. |
| 29 | m_cursor->setTransform(QTransform::fromScale(m_scale, m_scale) * m_original); |
| 30 | } |
| 31 | }); |
| 32 | connect(effects, &EffectsHandler::screenLockingChanged, this, [this](bool) { reset(); }); |
| 33 | input()->installInputEventSpy(this); |
| 34 | } |
| 35 | |
| 36 | ~ClickTweenEffect() override { reset(); } |
| 37 | |
| 38 | void reconfigure(ReconfigureFlags) override |
| 39 | { |
| 40 | m_settings.read(KConfigGroup(effects->config(), QStringLiteral("Effect-clicktween"))); |
| 41 | } |
| 42 | |
| 43 | bool isActive() const override { return m_scale != 1.0; } |
| 44 | |
| 45 | void pointerButton(PointerButtonEvent *event) override |
| 46 | { |
| 47 | updateButtons(event->buttons); |
| 48 | } |
| 49 | |
| 50 | void pointerMotion(PointerMotionEvent *event) override |
| 51 | { |
| 52 | // Reconcile state after device removal or other changes between clicks. |
| 53 | updateButtons(event->buttons); |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | void updateButtons(Qt::MouseButtons buttons) |
| 58 | { |
| 59 | if (effects->isScreenLocked()) { |
| 60 | reset(); |
| 61 | return; |
| 62 | } |
| 63 | const bool pressed = bool(buttons & (Qt::LeftButton | Qt::RightButton)); |
| 64 | if (pressed == m_pressed) { |
| 65 | return; |
| 66 | } |
| 67 | m_pressed = pressed; |
| 68 | m_animation.stop(); |
| 69 | m_animation.setStartValue(m_scale); |
| 70 | m_animation.setEndValue(pressed ? 0.8 : 1.0); |
| 71 | m_animation.setDuration(pressed ? m_settings.clickTime : m_settings.releaseTime); |
| 72 | m_animation.setEasingCurve(ClickTween::curveType(pressed ? m_settings.clickEasing : m_settings.releaseEasing)); |
| 73 | m_animation.start(); |
| 74 | } |
| 75 | |
| 76 | void reset() |
| 77 | { |
| 78 | m_animation.stop(); |
| 79 | m_pressed = false; |
| 80 | m_scale = 1.0; |
| 81 | if (m_cursor) { |
| 82 | m_cursor->setTransform(m_original); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | QPointer<Item> m_cursor; |
| 87 | QTransform m_original; |
| 88 | QVariantAnimation m_animation; |
| 89 | ClickTween::Settings m_settings; |
| 90 | qreal m_scale = 1.0; |
| 91 | bool m_pressed = false; |
| 92 | }; |
| 93 | |
| 94 | KWIN_EFFECT_FACTORY_SUPPORTED(ClickTweenEffect, "metadata.json", |
| 95 | return effects->scene() && effects->scene()->cursorItem();) |
| 96 | |
| 97 | } // namespace KWin |
| 98 | |
| 99 | #include "clicktween.moc" |