From f4e41430e5bd6f1f1834b52cffa90902620046cd Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Thu, 2 Apr 2020 12:09:47 +0200 Subject: [PATCH] Improve PowerOff/PowerDown behaviour Summary: Trigger PowerDown after pressing for 1s instead of having to wait for release to decide, feels more natural. Also don't operate the modifiers, it's done later by KGlobalAccel. Reviewers: #kwin, #plasma:_mobile, bshah Reviewed By: #plasma:_mobile, bshah Subscribers: ngraham, kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D28490 --- input.cpp | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/input.cpp b/input.cpp index e2c5f09c65..c0879252fe 100644 --- a/input.cpp +++ b/input.cpp @@ -736,6 +736,15 @@ private: class GlobalShortcutFilter : public InputEventFilter { public: + GlobalShortcutFilter() { + m_powerDown = new QTimer; + m_powerDown->setSingleShot(true); + m_powerDown->setInterval(1000); + } + ~GlobalShortcutFilter() { + delete m_powerDown; + } + bool pointerEvent(QMouseEvent *event, quint32 nativeButton) override { Q_UNUSED(nativeButton); if (event->type() == QEvent::MouseButtonPress) { @@ -763,13 +772,19 @@ public: } bool keyEvent(QKeyEvent *event) override { if (event->key() == Qt::Key_PowerOff) { - if (event->type() == QEvent::KeyPress) { - m_powerOffPress = event->timestamp(); + const auto modifiers = static_cast(event)->modifiersRelevantForGlobalShortcuts(); + if (event->type() == QEvent::KeyPress && !event->isAutoRepeat()) { + QObject::connect(m_powerDown, &QTimer::timeout, input()->shortcuts(), [this, modifiers] { + QObject::disconnect(m_powerDown, &QTimer::timeout, input()->shortcuts(), nullptr); + m_powerDown->stop(); + input()->shortcuts()->processKey(modifiers, Qt::Key_PowerDown); + }); + m_powerDown->start(); + return true; } else if (event->type() == QEvent::KeyRelease) { - const uint duration = (event->timestamp() - m_powerOffPress); - const Qt::Key key = duration > 1000 ? Qt::Key_PowerDown : Qt::Key_PowerOff; - const auto shortcuts = static_cast(event)->modifiersRelevantForGlobalShortcuts(); - return input()->shortcuts()->processKey(shortcuts, key | (event->key() & ~Qt::KeyboardModifierMask)); + const bool ret = !m_powerDown->isActive() || input()->shortcuts()->processKey(modifiers, event->key()); + m_powerDown->stop(); + return ret; } } else if (event->type() == QEvent::KeyPress) { return input()->shortcuts()->processKey(static_cast(event)->modifiersRelevantForGlobalShortcuts(), event->key()); @@ -798,7 +813,7 @@ public: } private: - uint m_powerOffPress; + QTimer* m_powerDown = nullptr; };