Skip to content

Commit

Permalink
Navigate more quickly when holding arrow keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jdpurcell committed Feb 26, 2024
1 parent 241f873 commit a87096a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/actionmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <QPushButton>
#include <QMimeDatabase>
#include <QFileIconProvider>
#include <QKeyEvent>

ActionManager::ActionManager(QObject *parent) : QObject(parent)
{
Expand Down Expand Up @@ -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<QAction*> ActionManager::getAllInstancesOfAction(const QString &key) const
{
QList<QAction*> listOfActions = getAllClonesOfAction(key);
Expand Down
2 changes: 2 additions & 0 deletions src/actionmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class ActionManager : public QObject

QAction *getAction(const QString &key) const;

bool wouldTriggerAction(const QKeyEvent *event, const QString &key) const;

QList<QAction*> getAllInstancesOfAction(const QString &key) const;

QList<QAction*> getAllClonesOfAction(const QString &key) const;
Expand Down
43 changes: 43 additions & 0 deletions src/qvgraphicsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QKeyEvent*>(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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions src/qvgraphicsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ private slots:
Qt::MouseButton pressedMouseButton {Qt::MouseButton::NoButton};
Qt::KeyboardModifiers mousePressModifiers {Qt::KeyboardModifier::NoModifier};
QPoint lastMousePos;

std::optional<GoToFileMode> turboNavMode;
QElapsedTimer lastTurboNav;
const int turboNavInterval {50};
};
Q_DECLARE_METATYPE(QVGraphicsView::SwipeData)
#endif // QVGRAPHICSVIEW_H

0 comments on commit a87096a

Please sign in to comment.