From 611052da32a8c8471d900116414acd7642a1ec65 Mon Sep 17 00:00:00 2001 From: Ryan Jensen Date: Sun, 24 Mar 2024 21:54:54 -0500 Subject: [PATCH] Add hotkeys D and U move DiffView down/up by half page --- src/ui/DiffView/DiffView.cpp | 25 +++++++++++++++++++++++++ src/ui/DiffView/DiffView.h | 3 +++ 2 files changed, 28 insertions(+) diff --git a/src/ui/DiffView/DiffView.cpp b/src/ui/DiffView/DiffView.cpp index 4a4c2911..0418afcd 100644 --- a/src/ui/DiffView/DiffView.cpp +++ b/src/ui/DiffView/DiffView.cpp @@ -15,6 +15,7 @@ #include "CommentWidget.h" #include "ui/DiffTreeModel.h" #include "ui/DoubleTreeWidget.h" +#include "ui/HotkeyManager.h" #include "git/Tree.h" #include #include @@ -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); @@ -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() {} @@ -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); +} diff --git a/src/ui/DiffView/DiffView.h b/src/ui/DiffView/DiffView.h index 6ece8107..e72a8b18 100644 --- a/src/ui/DiffView/DiffView.h +++ b/src/ui/DiffView/DiffView.h @@ -103,6 +103,9 @@ public: void diffTreeModelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles); + void moveHalfPageDown(); + void moveHalfPageUp(); + void moveRelative(int pixelsDown); signals: void diagnosticAdded(TextEditor::DiagnosticKind kind);