From 7e182735e29d7432bfab2962f29465396a3e76cf Mon Sep 17 00:00:00 2001 From: Ryan Jensen Date: Thu, 21 Mar 2024 00:01:02 -0500 Subject: [PATCH] Add selectCommitRelative() --- src/ui/CommitList.cpp | 34 ++++++++++++++++++++++------------ src/ui/CommitList.h | 1 + 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/ui/CommitList.cpp b/src/ui/CommitList.cpp index 7106daa0..8050d166 100644 --- a/src/ui/CommitList.cpp +++ b/src/ui/CommitList.cpp @@ -1169,11 +1169,11 @@ public: } // namespace -static Hotkey prevCommitHotKey = HotkeyManager::registerHotkey( - "J", "commitList/prevCommit", "CommitList/PrevCommit"); +static Hotkey selectCommitDownHotKey = HotkeyManager::registerHotkey( + "j", "commitList/selectCommitDown", "CommitList/Select Next Commit Down"); -static Hotkey nextCommitHotKey = HotkeyManager::registerHotkey( - "K", "commitList/nextCommit", "CommitList/NextCommit"); +static Hotkey selectCommitUpHotKey = HotkeyManager::registerHotkey( + "k", "commitList/selectCommitUp", "CommitList/Select Next Commit Up"); CommitList::CommitList(Index *index, QWidget *parent) : QListView(parent), mIndex(index) { @@ -1231,16 +1231,13 @@ CommitList::CommitList(Index *index, QWidget *parent) [this](const QModelIndex &index) { update(index); }); QShortcut *shortcut = new QShortcut(this); - prevCommitHotKey.use(shortcut); - connect(shortcut, &QShortcut::activated, [this] { - printf("prevCommit\n"); - }); + selectCommitDownHotKey.use(shortcut); + connect(shortcut, &QShortcut::activated, [this] { selectCommitRelative(1); }); shortcut = new QShortcut(this); - nextCommitHotKey.use(shortcut); - connect(shortcut, &QShortcut::activated, [this] { - printf("nextCommit\n"); - }); + selectCommitUpHotKey.use(shortcut); + connect(shortcut, &QShortcut::activated, + [this] { selectCommitRelative(-1); }); #ifdef Q_OS_MAC QFont font = this->font(); @@ -1364,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. diff --git a/src/ui/CommitList.h b/src/ui/CommitList.h index fd0ffd16..ac236f6a 100644 --- a/src/ui/CommitList.h +++ b/src/ui/CommitList.h @@ -47,6 +47,7 @@ public: 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);