diff --git a/src/ui/DiffView/DiffView.cpp b/src/ui/DiffView/DiffView.cpp index 089a11f7a..6f65a3534 100644 --- a/src/ui/DiffView/DiffView.cpp +++ b/src/ui/DiffView/DiffView.cpp @@ -20,6 +20,7 @@ #include #include #include +#include namespace { @@ -198,11 +199,13 @@ void DiffView::setDiff(const git::Diff &diff) { fetchMore(); })); - mConnections.append( - connect(scrollBar, &QScrollBar::rangeChanged, [this](int min, int max) { + mConnections.append(connect( + scrollBar, &QScrollBar::rangeChanged, this, + [this](int min, int max) { if (max - min < this->widget()->height() / 2 && canFetchMore()) fetchMore(); - })); + }, + Qt::QueuedConnection)); // Request comments for this diff. if (Repository *remoteRepo = view->remoteRepo()) { @@ -380,6 +383,13 @@ bool DiffView::canFetchMore() { * use a while loop with canFetchMore() to get all */ void DiffView::fetchMore(int fetchWidgets) { + // Back out early if we're reentrant or lazy loading isn't triggered + if (mFetching || + (verticalScrollBar()->maximum() - verticalScrollBar()->value() > + height() / 2)) + return; + QScopedValueRollback rollback(mFetching, true); + QVBoxLayout *layout = static_cast(widget()->layout()); // Add widgets. @@ -395,23 +405,12 @@ void DiffView::fetchMore(int fetchWidgets) { bool fetchFiles = true; if (!mFiles.isEmpty()) { FileWidget *lastFile = mFiles.last(); - while (lastFile->canFetchMore() && - ((verticalScrollBar()->maximum() - verticalScrollBar()->value() < - height() / 2) || - fetchAll)) { - addedWidgets += lastFile->fetchMore(fetchAll ? -1 : 1); - - // Load hunk(s) and update scrollbar - QApplication::processEvents(); - - // Running the eventloop may trigger a view refresh - if (mFiles.isEmpty()) - return; + if (lastFile->canFetchMore()) { + addedWidgets += lastFile->fetchMore(fetchAll ? -1 : fetchWidgets); } // Stop loading files - if (verticalScrollBar()->maximum() - verticalScrollBar()->value() > - height() / 2) + if (fetchAll == false && addedWidgets >= fetchWidgets) fetchFiles = false; } diff --git a/src/ui/DiffView/DiffView.h b/src/ui/DiffView/DiffView.h index e72a8b18a..262340123 100644 --- a/src/ui/DiffView/DiffView.h +++ b/src/ui/DiffView/DiffView.h @@ -138,6 +138,7 @@ class DiffView : public QScrollArea, public EditorProvider { Account::CommitComments mComments; bool mEnabled{true}; + bool mFetching{false}; DiffTreeModel *mDiffTreeModel{nullptr}; QWidget *mParent{nullptr}; QVBoxLayout *mFileWidgetLayout{nullptr};