Skip to content

Commit

Permalink
Merge pull request #721 from jensenr30/navigation-hotkeys
Browse files Browse the repository at this point in the history
Add Hotkeys for Navigating CommitList
  • Loading branch information
Murmele authored Mar 28, 2024
2 parents 063099a + 611052d commit ec9980f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/ui/CommitList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "git/Signature.h"
#include "git/TagRef.h"
#include "git/Tree.h"
#include "ui/HotkeyManager.h"
#include <QAbstractListModel>
#include <QApplication>
#include <QMenu>
Expand Down Expand Up @@ -1168,6 +1169,12 @@ class SelectionModel : public QItemSelectionModel {

} // namespace

static Hotkey selectCommitDownHotKey = HotkeyManager::registerHotkey(
"j", "commitList/selectCommitDown", "CommitList/Select Next Commit Down");

static Hotkey selectCommitUpHotKey = HotkeyManager::registerHotkey(
"k", "commitList/selectCommitUp", "CommitList/Select Next Commit Up");

CommitList::CommitList(Index *index, QWidget *parent)
: QListView(parent), mIndex(index) {
Theme *theme = Application::theme();
Expand Down Expand Up @@ -1223,6 +1230,15 @@ CommitList::CommitList(Index *index, QWidget *parent)
connect(this, &CommitList::entered,
[this](const QModelIndex &index) { update(index); });

QShortcut *shortcut = new QShortcut(this);
selectCommitDownHotKey.use(shortcut);
connect(shortcut, &QShortcut::activated, [this] { selectCommitRelative(1); });

shortcut = new QShortcut(this);
selectCommitUpHotKey.use(shortcut);
connect(shortcut, &QShortcut::activated,
[this] { selectCommitRelative(-1); });

#ifdef Q_OS_MAC
QFont font = this->font();
font.setPointSize(13);
Expand Down Expand Up @@ -1345,6 +1361,19 @@ void CommitList::selectFirstCommit(bool spontaneous) {
}
}

void CommitList::selectCommitRelative(int offset) {
QModelIndexList indices = selectionModel()->selectedIndexes();
QModelIndex index = indices[0];
if (!index.isValid()) {
return;
}
QModelIndex new_index = model()->index(index.row() + offset, index.column());
if (!new_index.isValid()) {
return;
}
selectIndexes(QItemSelection(new_index, new_index), QString(), true);
}

bool CommitList::selectRange(const QString &range, const QString &file,
bool spontaneous) {
// Try to select the "status" index.
Expand Down
1 change: 1 addition & 0 deletions src/ui/CommitList.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class CommitList : public QListView {
void selectReference(const git::Reference &ref);
void resetSelection(bool spontaneous = false);
void selectFirstCommit(bool spontaneous = false);
void selectCommitRelative(int offset);
bool selectRange(const QString &range, const QString &file = QString(),
bool spontaneous = false);
void suppressResetWalker(bool suppress);
Expand Down
25 changes: 25 additions & 0 deletions src/ui/DiffView/DiffView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "CommentWidget.h"
#include "ui/DiffTreeModel.h"
#include "ui/DoubleTreeWidget.h"
#include "ui/HotkeyManager.h"
#include "git/Tree.h"
#include <QScrollBar>
#include <QPushButton>
Expand Down Expand Up @@ -47,6 +48,12 @@ bool copy(const QString &source, const QDir &targetDir) {

} // namespace

static Hotkey moveHalfPageDownHotKey = HotkeyManager::registerHotkey(
"d", "diffView/moveHalfPageDownHotKey", "DiffView/Move Half Page Down");

static Hotkey moveHalfPageUpHotKey = HotkeyManager::registerHotkey(
"u", "diffView/moveHalfPageUpHotKey", "DiffView/Move Half Page Up");

DiffView::DiffView(const git::Repository &repo, QWidget *parent)
: QScrollArea(parent), mParent(parent) {
setStyleSheet(DiffViewStyle::kStyleSheet);
Expand Down Expand Up @@ -84,6 +91,14 @@ DiffView::DiffView(const git::Repository &repo, QWidget *parent)
fetchMore();
});
}

QShortcut *shortcut = new QShortcut(this);
moveHalfPageDownHotKey.use(shortcut);
connect(shortcut, &QShortcut::activated, [this] { moveHalfPageDown(); });

shortcut = new QShortcut(this);
moveHalfPageUpHotKey.use(shortcut);
connect(shortcut, &QShortcut::activated, [this] { moveHalfPageUp(); });
}

DiffView::~DiffView() {}
Expand Down Expand Up @@ -513,3 +528,13 @@ void DiffView::indexChanged(const QStringList &paths) {
// }
// }
}

void DiffView::moveHalfPageDown() { moveRelative(height() / 2); }

void DiffView::moveHalfPageUp() { moveRelative(-height() / 2); }

void DiffView::moveRelative(int pixelsDown) {
int oldPosition = verticalScrollBar()->sliderPosition();
int newPosition = oldPosition + pixelsDown;
verticalScrollBar()->setSliderPosition(newPosition);
}
3 changes: 3 additions & 0 deletions src/ui/DiffView/DiffView.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ class DiffView : public QScrollArea, public EditorProvider {
void diffTreeModelDataChanged(const QModelIndex &topLeft,
const QModelIndex &bottomRight,
const QVector<int> &roles);
void moveHalfPageDown();
void moveHalfPageUp();
void moveRelative(int pixelsDown);

signals:
void diagnosticAdded(TextEditor::DiagnosticKind kind);
Expand Down

0 comments on commit ec9980f

Please sign in to comment.