From a87096a6d6093f3c59362a35971ab7a1735161fa Mon Sep 17 00:00:00 2001 From: "J.D. Purcell" Date: Sun, 25 Feb 2024 20:30:14 -0500 Subject: [PATCH] Navigate more quickly when holding arrow keys --- src/actionmanager.cpp | 6 ++++++ src/actionmanager.h | 2 ++ src/qvgraphicsview.cpp | 43 ++++++++++++++++++++++++++++++++++++++++++ src/qvgraphicsview.h | 4 ++++ 4 files changed, 55 insertions(+) diff --git a/src/actionmanager.cpp b/src/actionmanager.cpp index 584836de..6efc0c04 100644 --- a/src/actionmanager.cpp +++ b/src/actionmanager.cpp @@ -8,6 +8,7 @@ #include #include #include +#include ActionManager::ActionManager(QObject *parent) : QObject(parent) { @@ -77,6 +78,11 @@ QAction *ActionManager::getAction(const QString &key) const return nullptr; } +bool ActionManager::wouldTriggerAction(const QKeyEvent *event, const QString &key) const +{ + return getAction(key)->shortcuts().contains(QKeySequence((event->modifiers() | event->key()) & ~(Qt::KeypadModifier | Qt::GroupSwitchModifier))); +} + QList ActionManager::getAllInstancesOfAction(const QString &key) const { QList listOfActions = getAllClonesOfAction(key); diff --git a/src/actionmanager.h b/src/actionmanager.h index 385218d2..103d1c6d 100644 --- a/src/actionmanager.h +++ b/src/actionmanager.h @@ -80,6 +80,8 @@ class ActionManager : public QObject QAction *getAction(const QString &key) const; + bool wouldTriggerAction(const QKeyEvent *event, const QString &key) const; + QList getAllInstancesOfAction(const QString &key) const; QList getAllClonesOfAction(const QString &key) const; diff --git a/src/qvgraphicsview.cpp b/src/qvgraphicsview.cpp index 11fcd97c..79cdaa3b 100644 --- a/src/qvgraphicsview.cpp +++ b/src/qvgraphicsview.cpp @@ -217,6 +217,23 @@ bool QVGraphicsView::event(QEvent *event) return true; } } + else if (event->type() == QEvent::ShortcutOverride || event->type() == QEvent::KeyRelease) + { + const QKeyEvent *keyEvent = static_cast(event); + const auto &actionManager = qvApp->getActionManager(); + if (actionManager.wouldTriggerAction(keyEvent, "previousfile") || actionManager.wouldTriggerAction(keyEvent, "nextfile")) + { + if (event->type() == QEvent::ShortcutOverride) + { + event->accept(); + return true; + } + else if (event->type() == QEvent::KeyRelease) + { + turboNavMode = {}; + } + } + } return QGraphicsView::event(event); } @@ -255,6 +272,21 @@ void QVGraphicsView::wheelEvent(QWheelEvent *event) void QVGraphicsView::keyPressEvent(QKeyEvent *event) { + const auto &actionManager = qvApp->getActionManager(); + const bool navPrev = actionManager.wouldTriggerAction(event, "previousfile"); + const bool navNext = actionManager.wouldTriggerAction(event, "nextfile"); + if (navPrev || navNext) + { + if (turboNavMode.has_value()) + return; + const GoToFileMode navMode = navPrev ? GoToFileMode::previous : GoToFileMode::next; + if (event->isAutoRepeat()) + turboNavMode = navMode; + lastTurboNav.start(); + goToFile(navMode); + return; + } + // The base class has logic to scroll in response to certain key presses, but we'll // handle that ourselves here instead to ensure any bounds constraints are enforced. const int scrollXSmallSteps = event->key() == Qt::Key_Left ? -1 : event->key() == Qt::Key_Right ? 1 : 0; @@ -443,6 +475,17 @@ void QVGraphicsView::postLoad() emit fileChanged(loadIsFromSessionRestore); loadIsFromSessionRestore = false; + + if (turboNavMode.has_value()) + { + const qint64 delay = qMax(turboNavInterval - lastTurboNav.elapsed(), 0LL); + QTimer::singleShot(delay, this, [this]() { + if (!turboNavMode.has_value()) + return; + lastTurboNav.start(); + goToFile(turboNavMode.value()); + }); + } } void QVGraphicsView::zoomIn() diff --git a/src/qvgraphicsview.h b/src/qvgraphicsview.h index af01d9a5..c326daea 100644 --- a/src/qvgraphicsview.h +++ b/src/qvgraphicsview.h @@ -209,6 +209,10 @@ private slots: Qt::MouseButton pressedMouseButton {Qt::MouseButton::NoButton}; Qt::KeyboardModifiers mousePressModifiers {Qt::KeyboardModifier::NoModifier}; QPoint lastMousePos; + + std::optional turboNavMode; + QElapsedTimer lastTurboNav; + const int turboNavInterval {50}; }; Q_DECLARE_METATYPE(QVGraphicsView::SwipeData) #endif // QVGRAPHICSVIEW_H