diff --git a/src/ui/DiffView/DiffView.cpp b/src/ui/DiffView/DiffView.cpp index 089a11f7a..c164b9980 100644 --- a/src/ui/DiffView/DiffView.cpp +++ b/src/ui/DiffView/DiffView.cpp @@ -374,12 +374,37 @@ bool DiffView::canFetchMore() { mFiles.size() < mDiffTreeModel->fileCount(dtw->selectedIndex()); } +class Guard { +public: + Guard(bool &variable, bool initValue) + : m_variable(variable), m_initValue(initValue) { + m_variable = m_initValue; + // qDebug() << "Guarding variable" << m_variable; + } + ~Guard() { + m_variable = !m_initValue; + // qDebug() << "Releasing variable" << m_variable; + } + +private: + bool &m_variable; + bool m_initValue; +}; + /*! * \brief DiffView::fetchMore * Fetch maxNewFiles more patches * use a while loop with canFetchMore() to get all */ void DiffView::fetchMore(int fetchWidgets) { + if (mFetchMoreInProgress) { + mCancelFetchMore = true; + // We cannot wait here because then processEvents() from below will never + // return + return; + } + auto g = Guard(mFetchMoreInProgress, true); + QVBoxLayout *layout = static_cast(widget()->layout()); // Add widgets. @@ -403,6 +428,9 @@ void DiffView::fetchMore(int fetchWidgets) { // Load hunk(s) and update scrollbar QApplication::processEvents(); + if (mCancelFetchMore) { + return; + } // Running the eventloop may trigger a view refresh if (mFiles.isEmpty()) diff --git a/src/ui/DiffView/DiffView.h b/src/ui/DiffView/DiffView.h index e72a8b18a..ed9cc33d3 100644 --- a/src/ui/DiffView/DiffView.h +++ b/src/ui/DiffView/DiffView.h @@ -141,6 +141,9 @@ class DiffView : public QScrollArea, public EditorProvider { DiffTreeModel *mDiffTreeModel{nullptr}; QWidget *mParent{nullptr}; QVBoxLayout *mFileWidgetLayout{nullptr}; + + bool mFetchMoreInProgress{false}; + bool mCancelFetchMore{false}; }; #endif